2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
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.
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.
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.
22 #include "CSSStyleSheet.h"
24 #include "CSSImportRule.h"
25 #include "CSSNamespace.h"
26 #include "CSSParser.h"
27 #include "CSSRuleList.h"
29 #include "ExceptionCode.h"
31 #include "SecurityOrigin.h"
32 #include "TextEncoding.h"
33 #include <wtf/Deque.h>
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)
42 , m_loadCompleted(false)
43 , m_strictParsing(!parentSheet || parentSheet->useStrictParsing())
44 , m_isUserStyleSheet(parentSheet ? parentSheet->isUserStyleSheet() : false)
45 , m_hasSyntacticallyValidCSSHeader(true)
49 CSSStyleSheet::CSSStyleSheet(Node* parentNode, const String& href, const KURL& baseURL, const String& charset)
50 : StyleSheet(parentNode, href, baseURL)
51 , m_doc(parentNode->document())
54 , m_loadCompleted(false)
55 , m_strictParsing(false)
56 , m_isUserStyleSheet(false)
57 , m_hasSyntacticallyValidCSSHeader(true)
61 CSSStyleSheet::CSSStyleSheet(CSSRule* ownerRule, const String& href, const KURL& baseURL, const String& charset)
62 : StyleSheet(ownerRule, href, baseURL)
65 , m_loadCompleted(false)
66 , m_strictParsing(!ownerRule || ownerRule->useStrictParsing())
67 , m_hasSyntacticallyValidCSSHeader(true)
69 CSSStyleSheet* parentSheet = ownerRule ? ownerRule->parentStyleSheet() : 0;
70 m_doc = parentSheet ? parentSheet->doc() : 0;
71 m_isUserStyleSheet = parentSheet ? parentSheet->isUserStyleSheet() : false;
74 CSSStyleSheet::~CSSStyleSheet()
79 CSSRule *CSSStyleSheet::ownerRule() const
81 return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0;
84 unsigned CSSStyleSheet::insertRule(const String& rule, unsigned index, ExceptionCode& ec)
87 if (index > length()) {
91 CSSParser p(useStrictParsing());
92 RefPtr<CSSRule> r = p.parseRule(this, rule);
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.
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;
110 } else if (r->isCharsetRule()) {
111 // The @charset rule has to come first and there can be only one.
112 ec = HIERARCHY_REQUEST_ERR;
117 insert(index, r.release());
124 int CSSStyleSheet::addRule(const String& selector, const String& style, int index, ExceptionCode& ec)
126 insertRule(selector + " { " + style + " }", index, ec);
128 // As per Microsoft documentation, always return -1.
132 int CSSStyleSheet::addRule(const String& selector, const String& style, ExceptionCode& ec)
134 return addRule(selector, style, length(), ec);
137 PassRefPtr<CSSRuleList> CSSStyleSheet::cssRules(bool omitCharsetRules)
139 KURL url = finalURL();
140 if (!url.isEmpty() && doc() && !doc()->securityOrigin()->canRequest(url))
142 return CSSRuleList::create(this, omitCharsetRules);
145 void CSSStyleSheet::deleteRule(unsigned index, ExceptionCode& ec)
147 if (index >= length()) {
157 void CSSStyleSheet::addNamespace(CSSParser* p, const AtomicString& prefix, const AtomicString& uri)
162 m_namespaces = new CSSNamespace(prefix, uri, m_namespaces);
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;
170 const AtomicString& CSSStyleSheet::determineNamespace(const AtomicString& prefix)
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.
177 CSSNamespace* ns = m_namespaces->namespaceForPrefix(prefix);
181 return nullAtom; // Assume we wont match any namespaces.
184 bool CSSStyleSheet::parseString(const String &string, bool strict)
186 return parseStringAtLine(string, strict, 0);
189 bool CSSStyleSheet::parseStringAtLine(const String& string, bool strict, int startLineNumber)
191 setStrictParsing(strict);
193 p.parseSheet(this, string, startLineNumber);
197 bool CSSStyleSheet::isLoading()
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())
208 void CSSStyleSheet::checkLoaded()
213 parent()->checkLoaded();
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;
222 void CSSStyleSheet::styleSheetChanged()
224 StyleBase* root = this;
225 while (StyleBase* parent = root->parent())
227 Document* documentToUpdate = root->isCSSStyleSheet() ? static_cast<CSSStyleSheet*>(root)->doc() : 0;
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.
233 if (documentToUpdate)
234 documentToUpdate->updateStyleSelector();
237 KURL CSSStyleSheet::completeURL(const String& url) const
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);
248 void CSSStyleSheet::addSubresourceStyleURLs(ListHashSet<KURL>& urls)
250 Deque<CSSStyleSheet*> styleSheetQueue;
251 styleSheetQueue.append(this);
253 while (!styleSheetQueue.isEmpty()) {
254 CSSStyleSheet* styleSheet = styleSheetQueue.takeFirst();
256 for (unsigned i = 0; i < styleSheet->length(); ++i) {
257 StyleBase* styleBase = styleSheet->item(i);
258 if (!styleBase->isRule())
261 CSSRule* rule = static_cast<CSSRule*>(styleBase);
262 if (rule->isImportRule()) {
263 if (CSSStyleSheet* ruleStyleSheet = static_cast<CSSImportRule*>(rule)->styleSheet())
264 styleSheetQueue.append(ruleStyleSheet);
266 rule->addSubresourceStyleURLs(urls);