blob: ce50af6a458cb5a554cb2d3fef182aa289fc516b [file] [log] [blame]
weinigaeca6362007-05-31 05:21:11 +00001/*
eseidelb3f42132006-05-16 02:08:51 +00002 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
weinigaeca6362007-05-31 05:21:11 +00003 * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved.
eseidelb3f42132006-05-16 02:08:51 +00004 *
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
ddkilzerc8eccec2007-09-26 02:29:57 +000017 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
eseidelb3f42132006-05-16 02:08:51 +000019 */
weinigaeca6362007-05-31 05:21:11 +000020
eseidelb3f42132006-05-16 02:08:51 +000021#include "config.h"
22#include "CSSStyleSheet.h"
23
eseidel13a64222006-05-16 05:33:34 +000024#include "CSSImportRule.h"
eseideld24f9c42006-05-16 07:52:04 +000025#include "CSSNamespace.h"
weinige7918082007-07-18 19:56:40 +000026#include "CSSParser.h"
eseidel13a64222006-05-16 05:33:34 +000027#include "CSSRuleList.h"
eseidelb3f42132006-05-16 02:08:51 +000028#include "Document.h"
29#include "ExceptionCode.h"
30#include "Node.h"
ddkilzer@apple.come9a55042008-12-23 00:00:14 +000031#include "TextEncoding.h"
ddkilzer@apple.com633b5c32008-12-23 00:49:34 +000032#include <wtf/Deque.h>
eseidelb3f42132006-05-16 02:08:51 +000033
34namespace WebCore {
35
ape08cbe82006-09-30 21:14:44 +000036CSSStyleSheet::CSSStyleSheet(CSSStyleSheet* parentSheet, const String& href, const String& charset)
eseidelb3f42132006-05-16 02:08:51 +000037 : StyleSheet(parentSheet, href)
38 , m_doc(parentSheet ? parentSheet->doc() : 0)
eseidelb3f42132006-05-16 02:08:51 +000039 , m_namespaces(0)
ape08cbe82006-09-30 21:14:44 +000040 , m_charset(charset)
rwlbuis63ecac92006-11-11 15:49:45 +000041 , m_loadCompleted(false)
antti@apple.comc3221bd2008-11-20 10:47:22 +000042 , m_strictParsing(!parentSheet || parentSheet->useStrictParsing())
eseidelb3f42132006-05-16 02:08:51 +000043{
44}
45
ape08cbe82006-09-30 21:14:44 +000046CSSStyleSheet::CSSStyleSheet(Node *parentNode, const String& href, const String& charset)
eseidelb3f42132006-05-16 02:08:51 +000047 : StyleSheet(parentNode, href)
48 , m_doc(parentNode->document())
eseidelb3f42132006-05-16 02:08:51 +000049 , m_namespaces(0)
ape08cbe82006-09-30 21:14:44 +000050 , m_charset(charset)
rwlbuis63ecac92006-11-11 15:49:45 +000051 , m_loadCompleted(false)
antti@apple.comc3221bd2008-11-20 10:47:22 +000052 , m_strictParsing(false)
eseidelb3f42132006-05-16 02:08:51 +000053{
54}
55
ape08cbe82006-09-30 21:14:44 +000056CSSStyleSheet::CSSStyleSheet(CSSRule *ownerRule, const String& href, const String& charset)
eseidelb3f42132006-05-16 02:08:51 +000057 : StyleSheet(ownerRule, href)
eseidelb3f42132006-05-16 02:08:51 +000058 , m_namespaces(0)
ape08cbe82006-09-30 21:14:44 +000059 , m_charset(charset)
rwlbuis63ecac92006-11-11 15:49:45 +000060 , m_loadCompleted(false)
antti@apple.comc3221bd2008-11-20 10:47:22 +000061 , m_strictParsing(!ownerRule || ownerRule->useStrictParsing())
eseidelb3f42132006-05-16 02:08:51 +000062{
hyatt@apple.com95d18752008-09-25 20:15:44 +000063 CSSStyleSheet* parentSheet = ownerRule ? ownerRule->parentStyleSheet() : 0;
64 m_doc = parentSheet ? parentSheet->doc() : 0;
eseidelb3f42132006-05-16 02:08:51 +000065}
66
eseideld24f9c42006-05-16 07:52:04 +000067CSSStyleSheet::~CSSStyleSheet()
68{
69 delete m_namespaces;
70}
71
eseidelb3f42132006-05-16 02:08:51 +000072CSSRule *CSSStyleSheet::ownerRule() const
73{
eseidel13a64222006-05-16 05:33:34 +000074 return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0;
eseidelb3f42132006-05-16 02:08:51 +000075}
76
77unsigned 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
weinigaeca6362007-05-31 05:21:11 +0000102int CSSStyleSheet::addRule(const String& selector, const String& style, int index, ExceptionCode& ec)
eseidelb3f42132006-05-16 02:08:51 +0000103{
weinigaeca6362007-05-31 05:21:11 +0000104 insertRule(selector + " { " + style + " }", index, ec);
105
106 // As per Microsoft documentation, always return -1.
107 return -1;
eseidelb3f42132006-05-16 02:08:51 +0000108}
109
weinigaeca6362007-05-31 05:21:11 +0000110int CSSStyleSheet::addRule(const String& selector, const String& style, ExceptionCode& ec)
111{
112 return addRule(selector, style, length(), ec);
113}
114
115
andersca@apple.com3f7f4bc2008-06-05 00:31:18 +0000116PassRefPtr<CSSRuleList> CSSStyleSheet::cssRules(bool omitCharsetRules)
eseidelb3f42132006-05-16 02:08:51 +0000117{
andersca@apple.com3f7f4bc2008-06-05 00:31:18 +0000118 return CSSRuleList::create(this, omitCharsetRules);
eseidelb3f42132006-05-16 02:08:51 +0000119}
120
121void 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
133void 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.
eric@webkit.org2490c872008-06-07 10:34:39 +0000143 p->m_defaultNamespace = uri;
eseidelb3f42132006-05-16 02:08:51 +0000144}
145
146const 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
160bool 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
168bool 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
179void CSSStyleSheet::checkLoaded()
180{
181 if (isLoading())
182 return;
183 if (parent())
184 parent()->checkLoaded();
simon.fraser@apple.com63514e92009-03-13 06:07:10 +0000185
186 // Avoid |this| being deleted by scripts that run via HTMLTokenizer::executeScriptsWaitingForStylesheets().
187 // See <rdar://problem/6622300>.
188 RefPtr<CSSStyleSheet> protector(this);
darin@apple.com894e8132008-06-17 19:16:22 +0000189 m_loadCompleted = ownerNode() ? ownerNode()->sheetLoaded() : true;
eseidelb3f42132006-05-16 02:08:51 +0000190}
191
eseidelb3f42132006-05-16 02:08:51 +0000192void CSSStyleSheet::styleSheetChanged()
193{
antti19539e52007-09-17 20:42:14 +0000194 StyleBase* root = this;
195 while (StyleBase* parent = root->parent())
196 root = parent;
eric@webkit.orgd241f9f2009-03-25 22:04:43 +0000197 Document* documentToUpdate = root->isCSSStyleSheet() ? static_cast<CSSStyleSheet*>(root)->doc() : 0;
antti19539e52007-09-17 20:42:14 +0000198
eseidelb3f42132006-05-16 02:08:51 +0000199 /* 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 */
antti19539e52007-09-17 20:42:14 +0000203 if (documentToUpdate)
204 documentToUpdate->updateStyleSelector();
eseidelb3f42132006-05-16 02:08:51 +0000205}
206
ddkilzer@apple.come9a55042008-12-23 00:00:14 +0000207KURL CSSStyleSheet::completeURL(const String& url) const
ddkilzer@apple.com1f85c1e2008-11-26 19:44:05 +0000208{
ddkilzer@apple.come9a55042008-12-23 00:00:14 +0000209 // 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}
beidson@apple.com96bfd56fe2008-04-03 05:56:21 +0000217
ddkilzer@apple.come9a55042008-12-23 00:00:14 +0000218void CSSStyleSheet::addSubresourceStyleURLs(ListHashSet<KURL>& urls)
219{
ddkilzer@apple.com633b5c32008-12-23 00:49:34 +0000220 Deque<CSSStyleSheet*> styleSheetQueue;
221 styleSheetQueue.append(this);
ddkilzer@apple.come9a55042008-12-23 00:00:14 +0000222
ddkilzer@apple.com633b5c32008-12-23 00:49:34 +0000223 while (!styleSheetQueue.isEmpty()) {
224 CSSStyleSheet* styleSheet = styleSheetQueue.first();
225 styleSheetQueue.removeFirst();
beidson@apple.com96bfd56fe2008-04-03 05:56:21 +0000226
ddkilzer@apple.com1f85c1e2008-11-26 19:44:05 +0000227 RefPtr<CSSRuleList> ruleList = styleSheet->cssRules();
228
ddkilzer@apple.com1f85c1e2008-11-26 19:44:05 +0000229 for (unsigned i = 0; i < ruleList->length(); ++i) {
230 CSSRule* rule = ruleList->item(i);
ddkilzer@apple.come9a55042008-12-23 00:00:14 +0000231 if (rule->isImportRule()) {
232 if (CSSStyleSheet* ruleStyleSheet = static_cast<CSSImportRule*>(rule)->styleSheet())
ddkilzer@apple.com633b5c32008-12-23 00:49:34 +0000233 styleSheetQueue.append(ruleStyleSheet);
ddkilzer@apple.com1f85c1e2008-11-26 19:44:05 +0000234 }
ddkilzer@apple.come9a55042008-12-23 00:00:14 +0000235 rule->addSubresourceStyleURLs(urls);
ddkilzer@apple.com1f85c1e2008-11-26 19:44:05 +0000236 }
beidson@apple.com96bfd56fe2008-04-03 05:56:21 +0000237 }
238}
239
eseidelb3f42132006-05-16 02:08:51 +0000240}