0
|
1 |
/*
|
|
2 |
* (C) 1999-2003 Lars Knoll (knoll@kde.org)
|
|
3 |
* Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
|
|
4 |
*
|
|
5 |
* This library is free software; you can redistribute it and/or
|
|
6 |
* modify it under the terms of the GNU Library General Public
|
|
7 |
* License as published by the Free Software Foundation; either
|
|
8 |
* version 2 of the License, or (at your option) any later version.
|
|
9 |
*
|
|
10 |
* This library is distributed in the hope that it will be useful,
|
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
13 |
* Library General Public License for more details.
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU Library General Public License
|
|
16 |
* along with this library; see the file COPYING.LIB. If not, write to
|
|
17 |
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
18 |
* Boston, MA 02110-1301, USA.
|
|
19 |
*/
|
|
20 |
|
|
21 |
#include "config.h"
|
|
22 |
#include "CSSStyleSheet.h"
|
|
23 |
|
|
24 |
#include "CSSImportRule.h"
|
|
25 |
#include "CSSNamespace.h"
|
|
26 |
#include "CSSParser.h"
|
|
27 |
#include "CSSRuleList.h"
|
|
28 |
#include "Document.h"
|
|
29 |
#include "ExceptionCode.h"
|
|
30 |
#include "Node.h"
|
|
31 |
#include "TextEncoding.h"
|
|
32 |
#include <wtf/Deque.h>
|
|
33 |
|
|
34 |
namespace WebCore {
|
|
35 |
|
|
36 |
CSSStyleSheet::CSSStyleSheet(CSSStyleSheet* parentSheet, const String& href, const String& charset)
|
|
37 |
: StyleSheet(parentSheet, href)
|
|
38 |
, m_doc(parentSheet ? parentSheet->doc() : 0)
|
|
39 |
, m_namespaces(0)
|
|
40 |
, m_charset(charset)
|
|
41 |
, m_loadCompleted(false)
|
|
42 |
, m_strictParsing(!parentSheet || parentSheet->useStrictParsing())
|
|
43 |
, m_isUserStyleSheet(parentSheet ? parentSheet->isUserStyleSheet() : false)
|
|
44 |
{
|
|
45 |
}
|
|
46 |
|
|
47 |
CSSStyleSheet::CSSStyleSheet(Node* parentNode, const String& href, const String& charset)
|
|
48 |
: StyleSheet(parentNode, href)
|
|
49 |
, m_doc(parentNode->document())
|
|
50 |
, m_namespaces(0)
|
|
51 |
, m_charset(charset)
|
|
52 |
, m_loadCompleted(false)
|
|
53 |
, m_strictParsing(false)
|
|
54 |
, m_isUserStyleSheet(false)
|
|
55 |
{
|
|
56 |
}
|
|
57 |
|
|
58 |
CSSStyleSheet::CSSStyleSheet(CSSRule* ownerRule, const String& href, const String& charset)
|
|
59 |
: StyleSheet(ownerRule, href)
|
|
60 |
, m_namespaces(0)
|
|
61 |
, m_charset(charset)
|
|
62 |
, m_loadCompleted(false)
|
|
63 |
, m_strictParsing(!ownerRule || ownerRule->useStrictParsing())
|
|
64 |
{
|
|
65 |
CSSStyleSheet* parentSheet = ownerRule ? ownerRule->parentStyleSheet() : 0;
|
|
66 |
m_doc = parentSheet ? parentSheet->doc() : 0;
|
|
67 |
m_isUserStyleSheet = parentSheet ? parentSheet->isUserStyleSheet() : false;
|
|
68 |
}
|
|
69 |
|
|
70 |
CSSStyleSheet::~CSSStyleSheet()
|
|
71 |
{
|
|
72 |
delete m_namespaces;
|
|
73 |
}
|
|
74 |
|
|
75 |
CSSRule *CSSStyleSheet::ownerRule() const
|
|
76 |
{
|
|
77 |
return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0;
|
|
78 |
}
|
|
79 |
|
|
80 |
unsigned CSSStyleSheet::insertRule(const String& rule, unsigned index, ExceptionCode& ec)
|
|
81 |
{
|
|
82 |
ec = 0;
|
|
83 |
if (index > length()) {
|
|
84 |
ec = INDEX_SIZE_ERR;
|
|
85 |
return 0;
|
|
86 |
}
|
|
87 |
CSSParser p(useStrictParsing());
|
|
88 |
RefPtr<CSSRule> r = p.parseRule(this, rule);
|
|
89 |
|
|
90 |
if (!r) {
|
|
91 |
ec = SYNTAX_ERR;
|
|
92 |
return 0;
|
|
93 |
}
|
|
94 |
|
|
95 |
// ###
|
|
96 |
// HIERARCHY_REQUEST_ERR: Raised if the rule cannot be inserted at the specified index e.g. if an
|
|
97 |
//@import rule is inserted after a standard rule set or other at-rule.
|
|
98 |
insert(index, r.release());
|
|
99 |
|
|
100 |
styleSheetChanged();
|
|
101 |
|
|
102 |
return index;
|
|
103 |
}
|
|
104 |
|
|
105 |
int CSSStyleSheet::addRule(const String& selector, const String& style, int index, ExceptionCode& ec)
|
|
106 |
{
|
|
107 |
insertRule(selector + " { " + style + " }", index, ec);
|
|
108 |
|
|
109 |
// As per Microsoft documentation, always return -1.
|
|
110 |
return -1;
|
|
111 |
}
|
|
112 |
|
|
113 |
int CSSStyleSheet::addRule(const String& selector, const String& style, ExceptionCode& ec)
|
|
114 |
{
|
|
115 |
return addRule(selector, style, length(), ec);
|
|
116 |
}
|
|
117 |
|
|
118 |
|
|
119 |
PassRefPtr<CSSRuleList> CSSStyleSheet::cssRules(bool omitCharsetRules)
|
|
120 |
{
|
|
121 |
return CSSRuleList::create(this, omitCharsetRules);
|
|
122 |
}
|
|
123 |
|
|
124 |
void CSSStyleSheet::deleteRule(unsigned index, ExceptionCode& ec)
|
|
125 |
{
|
|
126 |
if (index >= length()) {
|
|
127 |
ec = INDEX_SIZE_ERR;
|
|
128 |
return;
|
|
129 |
}
|
|
130 |
|
|
131 |
ec = 0;
|
|
132 |
remove(index);
|
|
133 |
styleSheetChanged();
|
|
134 |
}
|
|
135 |
|
|
136 |
void CSSStyleSheet::addNamespace(CSSParser* p, const AtomicString& prefix, const AtomicString& uri)
|
|
137 |
{
|
|
138 |
if (uri.isEmpty())
|
|
139 |
return;
|
|
140 |
|
|
141 |
m_namespaces = new CSSNamespace(prefix, uri, m_namespaces);
|
|
142 |
|
|
143 |
if (prefix.isEmpty())
|
|
144 |
// Set the default namespace on the parser so that selectors that omit namespace info will
|
|
145 |
// be able to pick it up easily.
|
|
146 |
p->m_defaultNamespace = uri;
|
|
147 |
}
|
|
148 |
|
|
149 |
const AtomicString& CSSStyleSheet::determineNamespace(const AtomicString& prefix)
|
|
150 |
{
|
|
151 |
if (prefix.isEmpty())
|
|
152 |
return nullAtom; // No namespace. If an element/attribute has a namespace, we won't match it.
|
|
153 |
else if (prefix == starAtom)
|
|
154 |
return starAtom; // We'll match any namespace.
|
|
155 |
else if (m_namespaces) {
|
|
156 |
CSSNamespace* ns = m_namespaces->namespaceForPrefix(prefix);
|
|
157 |
if (ns)
|
|
158 |
return ns->uri();
|
|
159 |
}
|
|
160 |
return nullAtom; // Assume we wont match any namespaces.
|
|
161 |
}
|
|
162 |
|
|
163 |
bool CSSStyleSheet::parseString(const String &string, bool strict)
|
|
164 |
{
|
|
165 |
setStrictParsing(strict);
|
|
166 |
CSSParser p(strict);
|
|
167 |
p.parseSheet(this, string);
|
|
168 |
return true;
|
|
169 |
}
|
|
170 |
|
|
171 |
bool CSSStyleSheet::isLoading()
|
|
172 |
{
|
|
173 |
unsigned len = length();
|
|
174 |
for (unsigned i = 0; i < len; ++i) {
|
|
175 |
StyleBase* rule = item(i);
|
|
176 |
if (rule->isImportRule() && static_cast<CSSImportRule*>(rule)->isLoading())
|
|
177 |
return true;
|
|
178 |
}
|
|
179 |
return false;
|
|
180 |
}
|
|
181 |
|
|
182 |
void CSSStyleSheet::checkLoaded()
|
|
183 |
{
|
|
184 |
if (isLoading())
|
|
185 |
return;
|
|
186 |
if (parent())
|
|
187 |
parent()->checkLoaded();
|
|
188 |
|
|
189 |
// Avoid |this| being deleted by scripts that run via HTMLTokenizer::executeScriptsWaitingForStylesheets().
|
|
190 |
// See <rdar://problem/6622300>.
|
|
191 |
RefPtr<CSSStyleSheet> protector(this);
|
|
192 |
m_loadCompleted = ownerNode() ? ownerNode()->sheetLoaded() : true;
|
|
193 |
}
|
|
194 |
|
|
195 |
void CSSStyleSheet::styleSheetChanged()
|
|
196 |
{
|
|
197 |
StyleBase* root = this;
|
|
198 |
while (StyleBase* parent = root->parent())
|
|
199 |
root = parent;
|
|
200 |
Document* documentToUpdate = root->isCSSStyleSheet() ? static_cast<CSSStyleSheet*>(root)->doc() : 0;
|
|
201 |
|
|
202 |
/* FIXME: We don't need to do everything updateStyleSelector does,
|
|
203 |
* basically we just need to recreate the document's selector with the
|
|
204 |
* already existing style sheets.
|
|
205 |
*/
|
|
206 |
if (documentToUpdate)
|
|
207 |
documentToUpdate->updateStyleSelector();
|
|
208 |
}
|
|
209 |
|
|
210 |
KURL CSSStyleSheet::completeURL(const String& url) const
|
|
211 |
{
|
|
212 |
// Always return a null URL when passed a null string.
|
|
213 |
// FIXME: Should we change the KURL constructor to have this behavior?
|
|
214 |
// See also Document::completeURL(const String&)
|
|
215 |
if (url.isNull() || m_charset.isEmpty())
|
|
216 |
return StyleSheet::completeURL(url);
|
|
217 |
const TextEncoding encoding = TextEncoding(m_charset);
|
|
218 |
return KURL(baseURL(), url, encoding);
|
|
219 |
}
|
|
220 |
|
|
221 |
void CSSStyleSheet::addSubresourceStyleURLs(ListHashSet<KURL>& urls)
|
|
222 |
{
|
|
223 |
Deque<CSSStyleSheet*> styleSheetQueue;
|
|
224 |
styleSheetQueue.append(this);
|
|
225 |
|
|
226 |
while (!styleSheetQueue.isEmpty()) {
|
|
227 |
CSSStyleSheet* styleSheet = styleSheetQueue.first();
|
|
228 |
styleSheetQueue.removeFirst();
|
|
229 |
|
|
230 |
RefPtr<CSSRuleList> ruleList = styleSheet->cssRules();
|
|
231 |
|
|
232 |
for (unsigned i = 0; i < ruleList->length(); ++i) {
|
|
233 |
CSSRule* rule = ruleList->item(i);
|
|
234 |
if (rule->isImportRule()) {
|
|
235 |
if (CSSStyleSheet* ruleStyleSheet = static_cast<CSSImportRule*>(rule)->styleSheet())
|
|
236 |
styleSheetQueue.append(ruleStyleSheet);
|
|
237 |
}
|
|
238 |
rule->addSubresourceStyleURLs(urls);
|
|
239 |
}
|
|
240 |
}
|
|
241 |
}
|
|
242 |
|
|
243 |
}
|