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 "bindings/core/v8/ExceptionState.h"
25 #include "bindings/core/v8/V8Binding.h"
26 #include "bindings/core/v8/V8PerIsolateData.h"
27 #include "core/HTMLNames.h"
28 #include "core/SVGNames.h"
29 #include "core/css/CSSCharsetRule.h"
30 #include "core/css/CSSImportRule.h"
31 #include "core/css/CSSRuleList.h"
32 #include "core/css/MediaList.h"
33 #include "core/css/StyleRule.h"
34 #include "core/css/StyleSheetContents.h"
35 #include "core/css/parser/CSSParser.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, 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 RefPtrWillBeRawPtr<StyleRuleBase> rule = CSSParser::parseRule(context, m_contents.get(), ruleString);
316 exceptionState.throwDOMException(SyntaxError, "Failed to parse the rule '" + ruleString + "'.");
319 RuleMutationScope mutationScope(this);
321 bool success = m_contents->wrapperInsertRule(rule, index);
323 exceptionState.throwDOMException(HierarchyRequestError, "Failed to insert the rule.");
326 if (!m_childRuleCSSOMWrappers.isEmpty())
327 m_childRuleCSSOMWrappers.insert(index, RefPtrWillBeMember<CSSRule>(nullptr));
332 unsigned CSSStyleSheet::insertRule(const String& rule, ExceptionState& exceptionState)
334 UseCounter::countDeprecation(callingExecutionContext(V8PerIsolateData::mainThreadIsolate()), UseCounter::CSSStyleSheetInsertRuleOptionalArg);
335 return insertRule(rule, 0, exceptionState);
338 void CSSStyleSheet::deleteRule(unsigned index, ExceptionState& exceptionState)
340 ASSERT(m_childRuleCSSOMWrappers.isEmpty() || m_childRuleCSSOMWrappers.size() == m_contents->ruleCount());
342 if (index >= length()) {
343 exceptionState.throwDOMException(IndexSizeError, "The index provided (" + String::number(index) + ") is larger than the maximum index (" + String::number(length() - 1) + ").");
346 RuleMutationScope mutationScope(this);
348 m_contents->wrapperDeleteRule(index);
350 if (!m_childRuleCSSOMWrappers.isEmpty()) {
351 if (m_childRuleCSSOMWrappers[index])
352 m_childRuleCSSOMWrappers[index]->setParentStyleSheet(0);
353 m_childRuleCSSOMWrappers.remove(index);
357 int CSSStyleSheet::addRule(const String& selector, const String& style, int index, ExceptionState& exceptionState)
360 text.append(selector);
361 text.appendLiteral(" { ");
363 if (!style.isEmpty())
366 insertRule(text.toString(), index, exceptionState);
368 // As per Microsoft documentation, always return -1.
372 int CSSStyleSheet::addRule(const String& selector, const String& style, ExceptionState& exceptionState)
374 return addRule(selector, style, length(), exceptionState);
378 PassRefPtrWillBeRawPtr<CSSRuleList> CSSStyleSheet::cssRules()
380 if (!canAccessRules())
382 if (!m_ruleListCSSOMWrapper)
383 m_ruleListCSSOMWrapper = StyleSheetCSSRuleList::create(this);
384 return m_ruleListCSSOMWrapper.get();
387 String CSSStyleSheet::href() const
389 return m_contents->originalURL();
392 KURL CSSStyleSheet::baseURL() const
394 return m_contents->baseURL();
397 bool CSSStyleSheet::isLoading() const
399 return m_contents->isLoading();
402 MediaList* CSSStyleSheet::media() const
407 if (!m_mediaCSSOMWrapper)
408 m_mediaCSSOMWrapper = MediaList::create(m_mediaQueries.get(), const_cast<CSSStyleSheet*>(this));
409 return m_mediaCSSOMWrapper.get();
412 CSSStyleSheet* CSSStyleSheet::parentStyleSheet() const
414 return m_ownerRule ? m_ownerRule->parentStyleSheet() : 0;
417 Document* CSSStyleSheet::ownerDocument() const
419 const CSSStyleSheet* root = this;
420 while (root->parentStyleSheet())
421 root = root->parentStyleSheet();
422 return root->ownerNode() ? &root->ownerNode()->document() : 0;
425 void CSSStyleSheet::clearChildRuleCSSOMWrappers()
427 m_childRuleCSSOMWrappers.clear();
430 bool CSSStyleSheet::sheetLoaded()
433 setLoadCompleted(m_ownerNode->sheetLoaded());
434 return m_loadCompleted;
437 void CSSStyleSheet::startLoadingDynamicSheet()
439 setLoadCompleted(false);
440 m_ownerNode->startLoadingDynamicSheet();
443 void CSSStyleSheet::setLoadCompleted(bool completed)
445 if (completed == m_loadCompleted)
448 m_loadCompleted = completed;
451 m_contents->clientLoadCompleted(this);
453 m_contents->clientLoadStarted(this);
456 void CSSStyleSheet::trace(Visitor* visitor)
458 visitor->trace(m_contents);
459 visitor->trace(m_mediaQueries);
460 visitor->trace(m_ownerNode);
461 visitor->trace(m_ownerRule);
462 visitor->trace(m_mediaCSSOMWrapper);
463 visitor->trace(m_childRuleCSSOMWrappers);
464 visitor->trace(m_ruleListCSSOMWrapper);
465 StyleSheet::trace(visitor);