|
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 "SecurityOrigin.h" |
|
32 #include "TextEncoding.h" |
|
33 #include <wtf/Deque.h> |
|
34 |
|
35 namespace WebCore { |
|
36 |
|
37 CSSStyleSheet::CSSStyleSheet(CSSStyleSheet* parentSheet, const String& href, const KURL& baseURL, const String& charset) |
|
38 : StyleSheet(parentSheet, href, baseURL) |
|
39 , m_doc(parentSheet ? parentSheet->doc() : 0) |
|
40 , m_namespaces(0) |
|
41 , m_charset(charset) |
|
42 , m_loadCompleted(false) |
|
43 , m_strictParsing(!parentSheet || parentSheet->useStrictParsing()) |
|
44 , m_isUserStyleSheet(parentSheet ? parentSheet->isUserStyleSheet() : false) |
|
45 , m_hasSyntacticallyValidCSSHeader(true) |
|
46 { |
|
47 } |
|
48 |
|
49 CSSStyleSheet::CSSStyleSheet(Node* parentNode, const String& href, const KURL& baseURL, const String& charset) |
|
50 : StyleSheet(parentNode, href, baseURL) |
|
51 , m_doc(parentNode->document()) |
|
52 , m_namespaces(0) |
|
53 , m_charset(charset) |
|
54 , m_loadCompleted(false) |
|
55 , m_strictParsing(false) |
|
56 , m_isUserStyleSheet(false) |
|
57 , m_hasSyntacticallyValidCSSHeader(true) |
|
58 { |
|
59 } |
|
60 |
|
61 CSSStyleSheet::CSSStyleSheet(CSSRule* ownerRule, const String& href, const KURL& baseURL, const String& charset) |
|
62 : StyleSheet(ownerRule, href, baseURL) |
|
63 , m_namespaces(0) |
|
64 , m_charset(charset) |
|
65 , m_loadCompleted(false) |
|
66 , m_strictParsing(!ownerRule || ownerRule->useStrictParsing()) |
|
67 , m_hasSyntacticallyValidCSSHeader(true) |
|
68 { |
|
69 CSSStyleSheet* parentSheet = ownerRule ? ownerRule->parentStyleSheet() : 0; |
|
70 m_doc = parentSheet ? parentSheet->doc() : 0; |
|
71 m_isUserStyleSheet = parentSheet ? parentSheet->isUserStyleSheet() : false; |
|
72 } |
|
73 |
|
74 CSSStyleSheet::~CSSStyleSheet() |
|
75 { |
|
76 delete m_namespaces; |
|
77 } |
|
78 |
|
79 CSSRule *CSSStyleSheet::ownerRule() const |
|
80 { |
|
81 return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0; |
|
82 } |
|
83 |
|
84 unsigned CSSStyleSheet::insertRule(const String& rule, unsigned index, ExceptionCode& ec) |
|
85 { |
|
86 ec = 0; |
|
87 if (index > length()) { |
|
88 ec = INDEX_SIZE_ERR; |
|
89 return 0; |
|
90 } |
|
91 CSSParser p(useStrictParsing()); |
|
92 RefPtr<CSSRule> r = p.parseRule(this, rule); |
|
93 |
|
94 if (!r) { |
|
95 ec = SYNTAX_ERR; |
|
96 return 0; |
|
97 } |
|
98 |
|
99 // Throw a HIERARCHY_REQUEST_ERR exception if the rule cannot be inserted at the specified index. The best |
|
100 // example of this is an @import rule inserted after regular rules. |
|
101 if (index > 0) { |
|
102 if (r->isImportRule()) { |
|
103 // Check all the rules that come before this one to make sure they are only @charset and @import rules. |
|
104 for (unsigned i = 0; i < index; ++i) { |
|
105 if (!item(i)->isCharsetRule() && !item(i)->isImportRule()) { |
|
106 ec = HIERARCHY_REQUEST_ERR; |
|
107 return 0; |
|
108 } |
|
109 } |
|
110 } else if (r->isCharsetRule()) { |
|
111 // The @charset rule has to come first and there can be only one. |
|
112 ec = HIERARCHY_REQUEST_ERR; |
|
113 return 0; |
|
114 } |
|
115 } |
|
116 |
|
117 insert(index, r.release()); |
|
118 |
|
119 styleSheetChanged(); |
|
120 |
|
121 return index; |
|
122 } |
|
123 |
|
124 int CSSStyleSheet::addRule(const String& selector, const String& style, int index, ExceptionCode& ec) |
|
125 { |
|
126 insertRule(selector + " { " + style + " }", index, ec); |
|
127 |
|
128 // As per Microsoft documentation, always return -1. |
|
129 return -1; |
|
130 } |
|
131 |
|
132 int CSSStyleSheet::addRule(const String& selector, const String& style, ExceptionCode& ec) |
|
133 { |
|
134 return addRule(selector, style, length(), ec); |
|
135 } |
|
136 |
|
137 PassRefPtr<CSSRuleList> CSSStyleSheet::cssRules(bool omitCharsetRules) |
|
138 { |
|
139 KURL url = finalURL(); |
|
140 if (!url.isEmpty() && doc() && !doc()->securityOrigin()->canRequest(url)) |
|
141 return 0; |
|
142 return CSSRuleList::create(this, omitCharsetRules); |
|
143 } |
|
144 |
|
145 void CSSStyleSheet::deleteRule(unsigned index, ExceptionCode& ec) |
|
146 { |
|
147 if (index >= length()) { |
|
148 ec = INDEX_SIZE_ERR; |
|
149 return; |
|
150 } |
|
151 |
|
152 ec = 0; |
|
153 remove(index); |
|
154 styleSheetChanged(); |
|
155 } |
|
156 |
|
157 void CSSStyleSheet::addNamespace(CSSParser* p, const AtomicString& prefix, const AtomicString& uri) |
|
158 { |
|
159 if (uri.isNull()) |
|
160 return; |
|
161 |
|
162 m_namespaces = new CSSNamespace(prefix, uri, m_namespaces); |
|
163 |
|
164 if (prefix.isEmpty()) |
|
165 // Set the default namespace on the parser so that selectors that omit namespace info will |
|
166 // be able to pick it up easily. |
|
167 p->m_defaultNamespace = uri; |
|
168 } |
|
169 |
|
170 const AtomicString& CSSStyleSheet::determineNamespace(const AtomicString& prefix) |
|
171 { |
|
172 if (prefix.isNull()) |
|
173 return nullAtom; // No namespace. If an element/attribute has a namespace, we won't match it. |
|
174 if (prefix == starAtom) |
|
175 return starAtom; // We'll match any namespace. |
|
176 if (m_namespaces) { |
|
177 CSSNamespace* ns = m_namespaces->namespaceForPrefix(prefix); |
|
178 if (ns) |
|
179 return ns->uri(); |
|
180 } |
|
181 return nullAtom; // Assume we wont match any namespaces. |
|
182 } |
|
183 |
|
184 bool CSSStyleSheet::parseString(const String &string, bool strict) |
|
185 { |
|
186 return parseStringAtLine(string, strict, 0); |
|
187 } |
|
188 |
|
189 bool CSSStyleSheet::parseStringAtLine(const String& string, bool strict, int startLineNumber) |
|
190 { |
|
191 setStrictParsing(strict); |
|
192 CSSParser p(strict); |
|
193 p.parseSheet(this, string, startLineNumber); |
|
194 return true; |
|
195 } |
|
196 |
|
197 bool CSSStyleSheet::isLoading() |
|
198 { |
|
199 unsigned len = length(); |
|
200 for (unsigned i = 0; i < len; ++i) { |
|
201 StyleBase* rule = item(i); |
|
202 if (rule->isImportRule() && static_cast<CSSImportRule*>(rule)->isLoading()) |
|
203 return true; |
|
204 } |
|
205 return false; |
|
206 } |
|
207 |
|
208 void CSSStyleSheet::checkLoaded() |
|
209 { |
|
210 if (isLoading()) |
|
211 return; |
|
212 if (parent()) |
|
213 parent()->checkLoaded(); |
|
214 |
|
215 // Avoid |this| being deleted by scripts that run via |
|
216 // ScriptableDocumentParser::executeScriptsWaitingForStylesheets(). |
|
217 // See <rdar://problem/6622300>. |
|
218 RefPtr<CSSStyleSheet> protector(this); |
|
219 m_loadCompleted = ownerNode() ? ownerNode()->sheetLoaded() : true; |
|
220 } |
|
221 |
|
222 void CSSStyleSheet::styleSheetChanged() |
|
223 { |
|
224 StyleBase* root = this; |
|
225 while (StyleBase* parent = root->parent()) |
|
226 root = parent; |
|
227 Document* documentToUpdate = root->isCSSStyleSheet() ? static_cast<CSSStyleSheet*>(root)->doc() : 0; |
|
228 |
|
229 /* FIXME: We don't need to do everything updateStyleSelector does, |
|
230 * basically we just need to recreate the document's selector with the |
|
231 * already existing style sheets. |
|
232 */ |
|
233 if (documentToUpdate) |
|
234 documentToUpdate->updateStyleSelector(); |
|
235 } |
|
236 |
|
237 KURL CSSStyleSheet::completeURL(const String& url) const |
|
238 { |
|
239 // Always return a null URL when passed a null string. |
|
240 // FIXME: Should we change the KURL constructor to have this behavior? |
|
241 // See also Document::completeURL(const String&) |
|
242 if (url.isNull() || m_charset.isEmpty()) |
|
243 return StyleSheet::completeURL(url); |
|
244 const TextEncoding encoding = TextEncoding(m_charset); |
|
245 return KURL(baseURL(), url, encoding); |
|
246 } |
|
247 |
|
248 void CSSStyleSheet::addSubresourceStyleURLs(ListHashSet<KURL>& urls) |
|
249 { |
|
250 Deque<CSSStyleSheet*> styleSheetQueue; |
|
251 styleSheetQueue.append(this); |
|
252 |
|
253 while (!styleSheetQueue.isEmpty()) { |
|
254 CSSStyleSheet* styleSheet = styleSheetQueue.takeFirst(); |
|
255 |
|
256 for (unsigned i = 0; i < styleSheet->length(); ++i) { |
|
257 StyleBase* styleBase = styleSheet->item(i); |
|
258 if (!styleBase->isRule()) |
|
259 continue; |
|
260 |
|
261 CSSRule* rule = static_cast<CSSRule*>(styleBase); |
|
262 if (rule->isImportRule()) { |
|
263 if (CSSStyleSheet* ruleStyleSheet = static_cast<CSSImportRule*>(rule)->styleSheet()) |
|
264 styleSheetQueue.append(ruleStyleSheet); |
|
265 } |
|
266 rule->addSubresourceStyleURLs(urls); |
|
267 } |
|
268 } |
|
269 } |
|
270 |
|
271 } |