weinig | aeca636 | 2007-05-31 05:21:11 +0000 | [diff] [blame] | 1 | /* |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 2 | * (C) 1999-2003 Lars Knoll (knoll@kde.org) |
weinig | aeca636 | 2007-05-31 05:21:11 +0000 | [diff] [blame] | 3 | * Copyright (C) 2004, 2006, 2007 Apple Inc. All rights reserved. |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 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 |
ddkilzer | c8eccec | 2007-09-26 02:29:57 +0000 | [diff] [blame] | 17 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 | * Boston, MA 02110-1301, USA. |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 19 | */ |
weinig | aeca636 | 2007-05-31 05:21:11 +0000 | [diff] [blame] | 20 | |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include "CSSStyleSheet.h" |
| 23 | |
eseidel | 13a6422 | 2006-05-16 05:33:34 +0000 | [diff] [blame] | 24 | #include "CSSImportRule.h" |
eseidel | d24f9c4 | 2006-05-16 07:52:04 +0000 | [diff] [blame] | 25 | #include "CSSNamespace.h" |
weinig | e791808 | 2007-07-18 19:56:40 +0000 | [diff] [blame] | 26 | #include "CSSParser.h" |
eseidel | 13a6422 | 2006-05-16 05:33:34 +0000 | [diff] [blame] | 27 | #include "CSSRuleList.h" |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 28 | #include "Document.h" |
| 29 | #include "ExceptionCode.h" |
| 30 | #include "Node.h" |
ddkilzer@apple.com | e9a5504 | 2008-12-23 00:00:14 +0000 | [diff] [blame] | 31 | #include "TextEncoding.h" |
ddkilzer@apple.com | 633b5c3 | 2008-12-23 00:49:34 +0000 | [diff] [blame] | 32 | #include <wtf/Deque.h> |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 33 | |
| 34 | namespace WebCore { |
| 35 | |
ap | e08cbe8 | 2006-09-30 21:14:44 +0000 | [diff] [blame] | 36 | CSSStyleSheet::CSSStyleSheet(CSSStyleSheet* parentSheet, const String& href, const String& charset) |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 37 | : StyleSheet(parentSheet, href) |
| 38 | , m_doc(parentSheet ? parentSheet->doc() : 0) |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 39 | , m_namespaces(0) |
ap | e08cbe8 | 2006-09-30 21:14:44 +0000 | [diff] [blame] | 40 | , m_charset(charset) |
rwlbuis | 63ecac9 | 2006-11-11 15:49:45 +0000 | [diff] [blame] | 41 | , m_loadCompleted(false) |
antti@apple.com | c3221bd | 2008-11-20 10:47:22 +0000 | [diff] [blame] | 42 | , m_strictParsing(!parentSheet || parentSheet->useStrictParsing()) |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 43 | { |
| 44 | } |
| 45 | |
ap | e08cbe8 | 2006-09-30 21:14:44 +0000 | [diff] [blame] | 46 | CSSStyleSheet::CSSStyleSheet(Node *parentNode, const String& href, const String& charset) |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 47 | : StyleSheet(parentNode, href) |
| 48 | , m_doc(parentNode->document()) |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 49 | , m_namespaces(0) |
ap | e08cbe8 | 2006-09-30 21:14:44 +0000 | [diff] [blame] | 50 | , m_charset(charset) |
rwlbuis | 63ecac9 | 2006-11-11 15:49:45 +0000 | [diff] [blame] | 51 | , m_loadCompleted(false) |
antti@apple.com | c3221bd | 2008-11-20 10:47:22 +0000 | [diff] [blame] | 52 | , m_strictParsing(false) |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 53 | { |
| 54 | } |
| 55 | |
ap | e08cbe8 | 2006-09-30 21:14:44 +0000 | [diff] [blame] | 56 | CSSStyleSheet::CSSStyleSheet(CSSRule *ownerRule, const String& href, const String& charset) |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 57 | : StyleSheet(ownerRule, href) |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 58 | , m_namespaces(0) |
ap | e08cbe8 | 2006-09-30 21:14:44 +0000 | [diff] [blame] | 59 | , m_charset(charset) |
rwlbuis | 63ecac9 | 2006-11-11 15:49:45 +0000 | [diff] [blame] | 60 | , m_loadCompleted(false) |
antti@apple.com | c3221bd | 2008-11-20 10:47:22 +0000 | [diff] [blame] | 61 | , m_strictParsing(!ownerRule || ownerRule->useStrictParsing()) |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 62 | { |
hyatt@apple.com | 95d1875 | 2008-09-25 20:15:44 +0000 | [diff] [blame] | 63 | CSSStyleSheet* parentSheet = ownerRule ? ownerRule->parentStyleSheet() : 0; |
| 64 | m_doc = parentSheet ? parentSheet->doc() : 0; |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 65 | } |
| 66 | |
eseidel | d24f9c4 | 2006-05-16 07:52:04 +0000 | [diff] [blame] | 67 | CSSStyleSheet::~CSSStyleSheet() |
| 68 | { |
| 69 | delete m_namespaces; |
| 70 | } |
| 71 | |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 72 | CSSRule *CSSStyleSheet::ownerRule() const |
| 73 | { |
eseidel | 13a6422 | 2006-05-16 05:33:34 +0000 | [diff] [blame] | 74 | return (parent() && parent()->isRule()) ? static_cast<CSSRule*>(parent()) : 0; |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 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 | |
weinig | aeca636 | 2007-05-31 05:21:11 +0000 | [diff] [blame] | 102 | int CSSStyleSheet::addRule(const String& selector, const String& style, int index, ExceptionCode& ec) |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 103 | { |
weinig | aeca636 | 2007-05-31 05:21:11 +0000 | [diff] [blame] | 104 | insertRule(selector + " { " + style + " }", index, ec); |
| 105 | |
| 106 | // As per Microsoft documentation, always return -1. |
| 107 | return -1; |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 108 | } |
| 109 | |
weinig | aeca636 | 2007-05-31 05:21:11 +0000 | [diff] [blame] | 110 | int CSSStyleSheet::addRule(const String& selector, const String& style, ExceptionCode& ec) |
| 111 | { |
| 112 | return addRule(selector, style, length(), ec); |
| 113 | } |
| 114 | |
| 115 | |
andersca@apple.com | 3f7f4bc | 2008-06-05 00:31:18 +0000 | [diff] [blame] | 116 | PassRefPtr<CSSRuleList> CSSStyleSheet::cssRules(bool omitCharsetRules) |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 117 | { |
andersca@apple.com | 3f7f4bc | 2008-06-05 00:31:18 +0000 | [diff] [blame] | 118 | return CSSRuleList::create(this, omitCharsetRules); |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 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 | |
| 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. |
eric@webkit.org | 2490c87 | 2008-06-07 10:34:39 +0000 | [diff] [blame] | 143 | p->m_defaultNamespace = uri; |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 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 | |
| 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 | |
| 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 | |
| 179 | void CSSStyleSheet::checkLoaded() |
| 180 | { |
| 181 | if (isLoading()) |
| 182 | return; |
| 183 | if (parent()) |
| 184 | parent()->checkLoaded(); |
simon.fraser@apple.com | 63514e9 | 2009-03-13 06:07:10 +0000 | [diff] [blame] | 185 | |
| 186 | // Avoid |this| being deleted by scripts that run via HTMLTokenizer::executeScriptsWaitingForStylesheets(). |
| 187 | // See <rdar://problem/6622300>. |
| 188 | RefPtr<CSSStyleSheet> protector(this); |
darin@apple.com | 894e813 | 2008-06-17 19:16:22 +0000 | [diff] [blame] | 189 | m_loadCompleted = ownerNode() ? ownerNode()->sheetLoaded() : true; |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 190 | } |
| 191 | |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 192 | void CSSStyleSheet::styleSheetChanged() |
| 193 | { |
antti | 19539e5 | 2007-09-17 20:42:14 +0000 | [diff] [blame] | 194 | StyleBase* root = this; |
| 195 | while (StyleBase* parent = root->parent()) |
| 196 | root = parent; |
eric@webkit.org | d241f9f | 2009-03-25 22:04:43 +0000 | [diff] [blame] | 197 | Document* documentToUpdate = root->isCSSStyleSheet() ? static_cast<CSSStyleSheet*>(root)->doc() : 0; |
antti | 19539e5 | 2007-09-17 20:42:14 +0000 | [diff] [blame] | 198 | |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 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 | */ |
antti | 19539e5 | 2007-09-17 20:42:14 +0000 | [diff] [blame] | 203 | if (documentToUpdate) |
| 204 | documentToUpdate->updateStyleSelector(); |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 205 | } |
| 206 | |
ddkilzer@apple.com | e9a5504 | 2008-12-23 00:00:14 +0000 | [diff] [blame] | 207 | KURL CSSStyleSheet::completeURL(const String& url) const |
ddkilzer@apple.com | 1f85c1e | 2008-11-26 19:44:05 +0000 | [diff] [blame] | 208 | { |
ddkilzer@apple.com | e9a5504 | 2008-12-23 00:00:14 +0000 | [diff] [blame] | 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 | } |
beidson@apple.com | 96bfd56fe | 2008-04-03 05:56:21 +0000 | [diff] [blame] | 217 | |
ddkilzer@apple.com | e9a5504 | 2008-12-23 00:00:14 +0000 | [diff] [blame] | 218 | void CSSStyleSheet::addSubresourceStyleURLs(ListHashSet<KURL>& urls) |
| 219 | { |
ddkilzer@apple.com | 633b5c3 | 2008-12-23 00:49:34 +0000 | [diff] [blame] | 220 | Deque<CSSStyleSheet*> styleSheetQueue; |
| 221 | styleSheetQueue.append(this); |
ddkilzer@apple.com | e9a5504 | 2008-12-23 00:00:14 +0000 | [diff] [blame] | 222 | |
ddkilzer@apple.com | 633b5c3 | 2008-12-23 00:49:34 +0000 | [diff] [blame] | 223 | while (!styleSheetQueue.isEmpty()) { |
| 224 | CSSStyleSheet* styleSheet = styleSheetQueue.first(); |
| 225 | styleSheetQueue.removeFirst(); |
beidson@apple.com | 96bfd56fe | 2008-04-03 05:56:21 +0000 | [diff] [blame] | 226 | |
ddkilzer@apple.com | 1f85c1e | 2008-11-26 19:44:05 +0000 | [diff] [blame] | 227 | RefPtr<CSSRuleList> ruleList = styleSheet->cssRules(); |
| 228 | |
ddkilzer@apple.com | 1f85c1e | 2008-11-26 19:44:05 +0000 | [diff] [blame] | 229 | for (unsigned i = 0; i < ruleList->length(); ++i) { |
| 230 | CSSRule* rule = ruleList->item(i); |
ddkilzer@apple.com | e9a5504 | 2008-12-23 00:00:14 +0000 | [diff] [blame] | 231 | if (rule->isImportRule()) { |
| 232 | if (CSSStyleSheet* ruleStyleSheet = static_cast<CSSImportRule*>(rule)->styleSheet()) |
ddkilzer@apple.com | 633b5c3 | 2008-12-23 00:49:34 +0000 | [diff] [blame] | 233 | styleSheetQueue.append(ruleStyleSheet); |
ddkilzer@apple.com | 1f85c1e | 2008-11-26 19:44:05 +0000 | [diff] [blame] | 234 | } |
ddkilzer@apple.com | e9a5504 | 2008-12-23 00:00:14 +0000 | [diff] [blame] | 235 | rule->addSubresourceStyleURLs(urls); |
ddkilzer@apple.com | 1f85c1e | 2008-11-26 19:44:05 +0000 | [diff] [blame] | 236 | } |
beidson@apple.com | 96bfd56fe | 2008-04-03 05:56:21 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
eseidel | b3f4213 | 2006-05-16 02:08:51 +0000 | [diff] [blame] | 240 | } |