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