2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2006, 2007, 2012 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 "core/css/CSSStyleSheet.h"
24 #include "HTMLNames.h"
26 #include "bindings/v8/ExceptionState.h"
27 #include "bindings/v8/V8Binding.h"
28 #include "bindings/v8/V8PerIsolateData.h"
29 #include "core/css/CSSCharsetRule.h"
30 #include "core/css/CSSImportRule.h"
31 #include "core/css/parser/BisonCSSParser.h"
32 #include "core/css/CSSRuleList.h"
33 #include "core/css/MediaList.h"
34 #include "core/css/StyleRule.h"
35 #include "core/css/StyleSheetContents.h"
36 #include "core/dom/Document.h"
37 #include "core/dom/ExceptionCode.h"
38 #include "core/dom/Node.h"
39 #include "core/frame/UseCounter.h"
40 #include "core/html/HTMLStyleElement.h"
41 #include "core/inspector/InspectorInstrumentation.h"
42 #include "core/svg/SVGStyleElement.h"
43 #include "platform/weborigin/SecurityOrigin.h"
44 #include "wtf/text/StringBuilder.h"
48 class StyleSheetCSSRuleList FINAL : public CSSRuleList {
50 static PassOwnPtrWillBeRawPtr<StyleSheetCSSRuleList> create(CSSStyleSheet* sheet)
52 return adoptPtrWillBeNoop(new StyleSheetCSSRuleList(sheet));
55 virtual void trace(Visitor* visitor) OVERRIDE
57 visitor->trace(m_styleSheet);
58 CSSRuleList::trace(visitor);
62 StyleSheetCSSRuleList(CSSStyleSheet* sheet) : m_styleSheet(sheet) { }
65 virtual void ref() OVERRIDE { m_styleSheet->ref(); }
66 virtual void deref() OVERRIDE { m_styleSheet->deref(); }
69 virtual unsigned length() const OVERRIDE { return m_styleSheet->length(); }
70 virtual CSSRule* item(unsigned index) const OVERRIDE { return m_styleSheet->item(index); }
72 virtual CSSStyleSheet* styleSheet() const OVERRIDE { return m_styleSheet; }
74 RawPtrWillBeMember<CSSStyleSheet> m_styleSheet;
78 static bool isAcceptableCSSStyleSheetParent(Node* parentNode)
80 // Only these nodes can be parents of StyleSheets, and they need to call
81 // clearOwnerNode() when moved out of document.
82 // Destruction of the style sheet counts as being "moved out of the
83 // document", but only in the non-oilpan version of blink. I.e. don't call
84 // clearOwnerNode() in the owner's destructor in oilpan.
86 || parentNode->isDocumentNode()
87 || isHTMLLinkElement(*parentNode)
88 || isHTMLStyleElement(*parentNode)
89 || isSVGStyleElement(*parentNode)
90 || parentNode->nodeType() == Node::PROCESSING_INSTRUCTION_NODE;
94 PassRefPtrWillBeRawPtr<CSSStyleSheet> CSSStyleSheet::create(PassRefPtrWillBeRawPtr<StyleSheetContents> sheet, CSSImportRule* ownerRule)
96 return adoptRefWillBeNoop(new CSSStyleSheet(sheet, ownerRule));
99 PassRefPtrWillBeRawPtr<CSSStyleSheet> CSSStyleSheet::create(PassRefPtrWillBeRawPtr<StyleSheetContents> sheet, Node* ownerNode)
101 return adoptRefWillBeNoop(new CSSStyleSheet(sheet, ownerNode, false, TextPosition::minimumPosition()));
104 PassRefPtrWillBeRawPtr<CSSStyleSheet> CSSStyleSheet::createInline(PassRefPtrWillBeRawPtr<StyleSheetContents> sheet, Node* ownerNode, const TextPosition& startPosition)
107 return adoptRefWillBeNoop(new CSSStyleSheet(sheet, ownerNode, true, startPosition));
110 PassRefPtrWillBeRawPtr<CSSStyleSheet> CSSStyleSheet::createInline(Node* ownerNode, const KURL& baseURL, const TextPosition& startPosition, const String& encoding)
112 CSSParserContext parserContext(ownerNode->document(), 0, baseURL, encoding);
113 RefPtrWillBeRawPtr<StyleSheetContents> sheet = StyleSheetContents::create(baseURL.string(), parserContext);
114 return adoptRefWillBeNoop(new CSSStyleSheet(sheet.release(), ownerNode, true, startPosition));
117 CSSStyleSheet::CSSStyleSheet(PassRefPtrWillBeRawPtr<StyleSheetContents> contents, CSSImportRule* ownerRule)
118 : m_contents(contents)
119 , m_isInlineStylesheet(false)
120 , m_isDisabled(false)
121 , m_ownerNode(nullptr)
122 , m_ownerRule(ownerRule)
123 , m_startPosition(TextPosition::minimumPosition())
124 , m_loadCompleted(false)
126 m_contents->registerClient(this);
129 CSSStyleSheet::CSSStyleSheet(PassRefPtrWillBeRawPtr<StyleSheetContents> contents, Node* ownerNode, bool isInlineStylesheet, const TextPosition& startPosition)
130 : m_contents(contents)
131 , m_isInlineStylesheet(isInlineStylesheet)
132 , m_isDisabled(false)
133 , m_ownerNode(ownerNode)
134 , m_ownerRule(nullptr)
135 , m_startPosition(startPosition)
136 , m_loadCompleted(false)
138 ASSERT(isAcceptableCSSStyleSheetParent(ownerNode));
139 m_contents->registerClient(this);
142 CSSStyleSheet::~CSSStyleSheet()
144 // With oilpan the parent style sheet pointer is strong and the sheet and
145 // its RuleCSSOMWrappers die together and we don't need to clear them here.
146 // Also with oilpan the StyleSheetContents client pointers are weak and
147 // therefore do not need to be cleared here.
149 // For style rules outside the document, .parentStyleSheet can become null even if the style rule
150 // is still observable from JavaScript. This matches the behavior of .parentNode for nodes, but
151 // it's not ideal because it makes the CSSOM's behavior depend on the timing of garbage collection.
152 for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
153 if (m_childRuleCSSOMWrappers[i])
154 m_childRuleCSSOMWrappers[i]->setParentStyleSheet(0);
157 if (m_mediaCSSOMWrapper)
158 m_mediaCSSOMWrapper->clearParentStyleSheet();
160 m_contents->unregisterClient(this);
164 void CSSStyleSheet::willMutateRules()
166 InspectorInstrumentation::willMutateRules(this);
168 // If we are the only client it is safe to mutate.
169 if (m_contents->clientSize() <= 1 && !m_contents->isInMemoryCache()) {
170 m_contents->clearRuleSet();
171 if (Document* document = ownerDocument())
172 m_contents->removeSheetFromCache(document);
173 m_contents->setMutable();
176 // Only cacheable stylesheets should have multiple clients.
177 ASSERT(m_contents->isCacheable());
180 m_contents->unregisterClient(this);
181 m_contents = m_contents->copy();
182 m_contents->registerClient(this);
184 m_contents->setMutable();
186 // Any existing CSSOM wrappers need to be connected to the copied child rules.
187 reattachChildRuleCSSOMWrappers();
190 void CSSStyleSheet::didMutateRules()
192 ASSERT(m_contents->isMutable());
193 ASSERT(m_contents->clientSize() <= 1);
195 InspectorInstrumentation::didMutateRules(this);
196 didMutate(PartialRuleUpdate);
199 void CSSStyleSheet::didMutate(StyleSheetUpdateType updateType)
201 Document* owner = ownerDocument();
205 // Need FullStyleUpdate when insertRule or deleteRule,
206 // because StyleSheetCollection::analyzeStyleSheetChange cannot detect partial rule update.
207 StyleResolverUpdateMode updateMode = updateType != PartialRuleUpdate ? AnalyzedStyleUpdate : FullStyleUpdate;
208 owner->modifiedStyleSheet(this, RecalcStyleDeferred, updateMode);
211 void CSSStyleSheet::reattachChildRuleCSSOMWrappers()
213 for (unsigned i = 0; i < m_childRuleCSSOMWrappers.size(); ++i) {
214 if (!m_childRuleCSSOMWrappers[i])
216 m_childRuleCSSOMWrappers[i]->reattach(m_contents->ruleAt(i));
220 void CSSStyleSheet::setDisabled(bool disabled)
222 if (disabled == m_isDisabled)
224 m_isDisabled = disabled;
229 void CSSStyleSheet::setMediaQueries(PassRefPtrWillBeRawPtr<MediaQuerySet> mediaQueries)
231 m_mediaQueries = mediaQueries;
232 if (m_mediaCSSOMWrapper && m_mediaQueries)
233 m_mediaCSSOMWrapper->reattach(m_mediaQueries.get());
235 // Add warning message to inspector whenever dpi/dpcm values are used for "screen" media.
236 reportMediaQueryWarningIfNeeded(ownerDocument(), m_mediaQueries.get());
239 unsigned CSSStyleSheet::length() const
241 return m_contents->ruleCount();
244 CSSRule* CSSStyleSheet::item(unsigned index)
246 unsigned ruleCount = length();
247 if (index >= ruleCount)
250 if (m_childRuleCSSOMWrappers.isEmpty())
251 m_childRuleCSSOMWrappers.grow(ruleCount);
252 ASSERT(m_childRuleCSSOMWrappers.size() == ruleCount);
254 RefPtrWillBeMember<CSSRule>& cssRule = m_childRuleCSSOMWrappers[index];
256 if (index == 0 && m_contents->hasCharsetRule()) {
257 ASSERT(!m_contents->ruleAt(0));
258 cssRule = CSSCharsetRule::create(this, m_contents->encodingFromCharsetRule());
260 cssRule = m_contents->ruleAt(index)->createCSSOMWrapper(this);
262 return cssRule.get();
265 void CSSStyleSheet::clearOwnerNode()
267 didMutate(EntireStyleSheetUpdate);
269 m_contents->unregisterClient(this);
270 m_ownerNode = nullptr;
273 bool CSSStyleSheet::canAccessRules() const
275 if (m_isInlineStylesheet)
277 KURL baseURL = m_contents->baseURL();
278 if (baseURL.isEmpty())
280 Document* document = ownerDocument();
283 if (document->securityOrigin()->canRequest(baseURL))
288 PassRefPtrWillBeRawPtr<CSSRuleList> CSSStyleSheet::rules()
290 if (!canAccessRules())
293 RefPtrWillBeRawPtr<StaticCSSRuleList> nonCharsetRules(StaticCSSRuleList::create());
294 unsigned ruleCount = length();
295 for (unsigned i = 0; i < ruleCount; ++i) {
296 CSSRule* rule = item(i);
297 if (rule->type() == CSSRule::CHARSET_RULE)
299 nonCharsetRules->rules().append(rule);
301 return nonCharsetRules.release();
304 unsigned CSSStyleSheet::insertRule(const String& ruleString, unsigned index, ExceptionState& exceptionState)
306 ASSERT(m_childRuleCSSOMWrappers.isEmpty() || m_childRuleCSSOMWrappers.size() == m_contents->ruleCount());
308 if (index > length()) {
309 exceptionState.throwDOMException(IndexSizeError, "The index provided (" + String::number(index) + ") is larger than the maximum index (" + String::number(length()) + ").");
312 CSSParserContext context(m_contents->parserContext(), UseCounter::getFrom(this));
313 BisonCSSParser p(context);
314 RefPtrWillBeRawPtr<StyleRuleBase> rule = p.parseRule(m_contents.get(), ruleString);
317 exceptionState.throwDOMException(SyntaxError, "Failed to parse the rule '" + ruleString + "'.");
320 RuleMutationScope mutationScope(this);
322 bool success = m_contents->wrapperInsertRule(rule, index);
324 exceptionState.throwDOMException(HierarchyRequestError, "Failed to insert the rule.");
327 if (!m_childRuleCSSOMWrappers.isEmpty())
328 m_childRuleCSSOMWrappers.insert(index, RefPtrWillBeMember<CSSRule>(nullptr));
333 unsigned CSSStyleSheet::insertRule(const String& rule, ExceptionState& exceptionState)
335 UseCounter::countDeprecation(callingExecutionContext(V8PerIsolateData::mainThreadIsolate()), UseCounter::CSSStyleSheetInsertRuleOptionalArg);
336 return insertRule(rule, 0, exceptionState);
339 void CSSStyleSheet::deleteRule(unsigned index, ExceptionState& exceptionState)
341 ASSERT(m_childRuleCSSOMWrappers.isEmpty() || m_childRuleCSSOMWrappers.size() == m_contents->ruleCount());
343 if (index >= length()) {
344 exceptionState.throwDOMException(IndexSizeError, "The index provided (" + String::number(index) + ") is larger than the maximum index (" + String::number(length() - 1) + ").");
347 RuleMutationScope mutationScope(this);
349 m_contents->wrapperDeleteRule(index);
351 if (!m_childRuleCSSOMWrappers.isEmpty()) {
352 if (m_childRuleCSSOMWrappers[index])
353 m_childRuleCSSOMWrappers[index]->setParentStyleSheet(0);
354 m_childRuleCSSOMWrappers.remove(index);
358 int CSSStyleSheet::addRule(const String& selector, const String& style, int index, ExceptionState& exceptionState)
361 text.append(selector);
362 text.appendLiteral(" { ");
364 if (!style.isEmpty())
367 insertRule(text.toString(), index, exceptionState);
369 // As per Microsoft documentation, always return -1.
373 int CSSStyleSheet::addRule(const String& selector, const String& style, ExceptionState& exceptionState)
375 return addRule(selector, style, length(), exceptionState);
379 PassRefPtrWillBeRawPtr<CSSRuleList> CSSStyleSheet::cssRules()
381 if (!canAccessRules())
383 if (!m_ruleListCSSOMWrapper)
384 m_ruleListCSSOMWrapper = StyleSheetCSSRuleList::create(this);
385 return m_ruleListCSSOMWrapper.get();
388 String CSSStyleSheet::href() const
390 return m_contents->originalURL();
393 KURL CSSStyleSheet::baseURL() const
395 return m_contents->baseURL();
398 bool CSSStyleSheet::isLoading() const
400 return m_contents->isLoading();
403 MediaList* CSSStyleSheet::media() const
408 if (!m_mediaCSSOMWrapper)
409 m_mediaCSSOMWrapper = MediaList::create(m_mediaQueries.get(), const_cast<CSSStyleSheet*>(this));
410 return m_mediaCSSOMWrapper.get();
413 CSSStyleSheet* CSSStyleSheet::parentStyleSheet() const
415 return m_ownerRule ? m_ownerRule->parentStyleSheet() : 0;
418 Document* CSSStyleSheet::ownerDocument() const
420 const CSSStyleSheet* root = this;
421 while (root->parentStyleSheet())
422 root = root->parentStyleSheet();
423 return root->ownerNode() ? &root->ownerNode()->document() : 0;
426 void CSSStyleSheet::clearChildRuleCSSOMWrappers()
428 m_childRuleCSSOMWrappers.clear();
431 bool CSSStyleSheet::sheetLoaded()
434 setLoadCompleted(m_ownerNode->sheetLoaded());
435 return m_loadCompleted;
438 void CSSStyleSheet::startLoadingDynamicSheet()
440 setLoadCompleted(false);
441 m_ownerNode->startLoadingDynamicSheet();
444 void CSSStyleSheet::setLoadCompleted(bool completed)
446 if (completed == m_loadCompleted)
449 m_loadCompleted = completed;
452 m_contents->clientLoadCompleted(this);
454 m_contents->clientLoadStarted(this);
457 void CSSStyleSheet::trace(Visitor* visitor)
459 visitor->trace(m_contents);
460 visitor->trace(m_mediaQueries);
461 visitor->trace(m_ownerNode);
462 visitor->trace(m_ownerRule);
463 visitor->trace(m_mediaCSSOMWrapper);
464 visitor->trace(m_childRuleCSSOMWrappers);
465 visitor->trace(m_ruleListCSSOMWrapper);
466 StyleSheet::trace(visitor);