2 * Copyright (C) 2006, 2007 Rob Buis
3 * Copyright (C) 2008 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/dom/StyleElement.h"
24 #include "bindings/core/v8/ScriptController.h"
25 #include "core/css/MediaList.h"
26 #include "core/css/MediaQueryEvaluator.h"
27 #include "core/css/StyleSheetContents.h"
28 #include "core/dom/Document.h"
29 #include "core/dom/Element.h"
30 #include "core/dom/ScriptableDocumentParser.h"
31 #include "core/dom/StyleEngine.h"
32 #include "core/dom/shadow/ShadowRoot.h"
33 #include "core/frame/LocalFrame.h"
34 #include "core/frame/csp/ContentSecurityPolicy.h"
35 #include "core/html/HTMLStyleElement.h"
36 #include "platform/TraceEvent.h"
37 #include "wtf/text/StringBuilder.h"
41 static bool isCSS(Element* element, const AtomicString& type)
43 return type.isEmpty() || (element->isHTMLElement() ? equalIgnoringCase(type, "text/css") : (type == "text/css"));
46 StyleElement::StyleElement(Document* document, bool createdByParser)
47 : m_createdByParser(createdByParser)
49 , m_registeredAsCandidate(false)
50 , m_startPosition(TextPosition::belowRangePosition())
52 if (createdByParser && document && document->scriptableDocumentParser() && !document->isInDocumentWrite())
53 m_startPosition = document->scriptableDocumentParser()->textPosition();
56 StyleElement::~StyleElement()
64 void StyleElement::processStyleSheet(Document& document, Element* element)
66 TRACE_EVENT0("blink", "StyleElement::processStyleSheet");
68 ASSERT(element->inDocument());
70 m_registeredAsCandidate = true;
71 document.styleEngine()->addStyleSheetCandidateNode(element, m_createdByParser);
72 if (m_createdByParser)
78 void StyleElement::insertedInto(Element* element, ContainerNode* insertionPoint)
80 if (!insertionPoint->inDocument() || !element->isInShadowTree())
82 if (ShadowRoot* scope = element->containingShadowRoot())
83 scope->registerScopedHTMLStyleChild();
86 void StyleElement::removedFrom(Element* element, ContainerNode* insertionPoint)
88 if (!insertionPoint->inDocument())
91 ShadowRoot* shadowRoot = element->containingShadowRoot();
93 shadowRoot = insertionPoint->containingShadowRoot();
96 shadowRoot->unregisterScopedHTMLStyleChild();
98 Document& document = element->document();
99 if (m_registeredAsCandidate) {
100 document.styleEngine()->removeStyleSheetCandidateNode(element, shadowRoot ? *toTreeScope(shadowRoot) : toTreeScope(document));
101 m_registeredAsCandidate = false;
104 RefPtrWillBeRawPtr<StyleSheet> removedSheet = m_sheet.get();
109 document.removedStyleSheet(removedSheet.get(), AnalyzedStyleUpdate);
112 void StyleElement::clearDocumentData(Document& document, Element* element)
115 m_sheet->clearOwnerNode();
117 if (element->inDocument()) {
118 // HTMLLinkElement in shadow tree is not supported.
119 TreeScope& treeScope = isHTMLStyleElement(element) || isSVGStyleElement(element) ? element->treeScope() : element->document();
120 document.styleEngine()->removeStyleSheetCandidateNode(element, treeScope);
124 void StyleElement::childrenChanged(Element* element)
127 if (m_createdByParser)
133 void StyleElement::finishParsingChildren(Element* element)
137 m_createdByParser = false;
140 void StyleElement::process(Element* element)
142 if (!element || !element->inDocument())
144 createSheet(element, element->textFromChildren());
147 void StyleElement::clearSheet(Element* ownerElement)
151 if (ownerElement && m_sheet->isLoading())
152 ownerElement->document().styleEngine()->removePendingSheet(ownerElement);
154 m_sheet.release()->clearOwnerNode();
157 static bool shouldBypassMainWorldCSP(Element* element)
159 // Main world CSP is bypassed within an isolated world.
160 LocalFrame* frame = element->document().frame();
161 if (frame && frame->script().shouldBypassMainWorldCSP())
164 // Main world CSP is bypassed for style elements in user agent shadow DOM.
165 ShadowRoot* root = element->containingShadowRoot();
166 if (root && root->type() == ShadowRoot::UserAgentShadowRoot)
172 void StyleElement::createSheet(Element* e, const String& text)
175 ASSERT(e->inDocument());
176 Document& document = e->document();
180 const ContentSecurityPolicy* csp = document.contentSecurityPolicy();
181 bool passesContentSecurityPolicyChecks = shouldBypassMainWorldCSP(e)
182 || csp->allowStyleWithHash(text)
183 || csp->allowStyleWithNonce(e->fastGetAttribute(HTMLNames::nonceAttr))
184 || csp->allowInlineStyle(e->document().url(), m_startPosition.m_line);
186 // If type is empty or CSS, this is a CSS style sheet.
187 const AtomicString& type = this->type();
188 if (isCSS(e, type) && passesContentSecurityPolicyChecks) {
189 RefPtrWillBeRawPtr<MediaQuerySet> mediaQueries = MediaQuerySet::create(media());
191 MediaQueryEvaluator screenEval("screen", true);
192 MediaQueryEvaluator printEval("print", true);
193 if (screenEval.eval(mediaQueries.get()) || printEval.eval(mediaQueries.get())) {
195 TextPosition startPosition = m_startPosition == TextPosition::belowRangePosition() ? TextPosition::minimumPosition() : m_startPosition;
196 m_sheet = document.styleEngine()->createSheet(e, text, startPosition, m_createdByParser);
197 m_sheet->setMediaQueries(mediaQueries.release());
203 m_sheet->contents()->checkLoaded();
206 bool StyleElement::isLoading() const
210 return m_sheet ? m_sheet->isLoading() : false;
213 bool StyleElement::sheetLoaded(Document& document)
218 document.styleEngine()->removePendingSheet(m_sheet->ownerNode());
222 void StyleElement::startLoadingDynamicSheet(Document& document)
224 document.styleEngine()->addPendingSheet();
227 void StyleElement::trace(Visitor* visitor)
229 visitor->trace(m_sheet);