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 "core/svg/SVGStyleElement.h"
37 #include "platform/TraceEvent.h"
38 #include "wtf/text/StringBuilder.h"
42 static bool isCSS(Element
* element
, const AtomicString
& type
)
44 return type
.isEmpty() || (element
->isHTMLElement() ? equalIgnoringCase(type
, "text/css") : (type
== "text/css"));
47 StyleElement::StyleElement(Document
* document
, bool createdByParser
)
48 : m_createdByParser(createdByParser
)
50 , m_registeredAsCandidate(false)
51 , m_startPosition(TextPosition::belowRangePosition())
53 if (createdByParser
&& document
&& document
->scriptableDocumentParser() && !document
->isInDocumentWrite())
54 m_startPosition
= document
->scriptableDocumentParser()->textPosition();
57 StyleElement::~StyleElement()
65 StyleElement::ProcessingResult
StyleElement::processStyleSheet(Document
& document
, Element
* element
)
67 TRACE_EVENT0("blink", "StyleElement::processStyleSheet");
69 ASSERT(element
->inDocument());
71 m_registeredAsCandidate
= true;
72 document
.styleEngine().addStyleSheetCandidateNode(element
, m_createdByParser
);
73 if (m_createdByParser
)
74 return ProcessingSuccessful
;
76 return process(element
);
79 void StyleElement::insertedInto(Element
* element
, ContainerNode
* insertionPoint
)
81 if (!insertionPoint
->inDocument() || !element
->isInShadowTree())
83 if (ShadowRoot
* scope
= element
->containingShadowRoot())
84 scope
->registerScopedHTMLStyleChild();
87 void StyleElement::removedFrom(Element
* element
, ContainerNode
* insertionPoint
)
89 if (!insertionPoint
->inDocument())
92 ShadowRoot
* shadowRoot
= element
->containingShadowRoot();
94 shadowRoot
= insertionPoint
->containingShadowRoot();
97 shadowRoot
->unregisterScopedHTMLStyleChild();
99 Document
& document
= element
->document();
100 if (m_registeredAsCandidate
) {
101 document
.styleEngine().removeStyleSheetCandidateNode(element
, shadowRoot
? *toTreeScope(shadowRoot
) : toTreeScope(document
));
102 m_registeredAsCandidate
= false;
105 RefPtrWillBeRawPtr
<StyleSheet
> removedSheet
= m_sheet
.get();
110 document
.removedStyleSheet(removedSheet
.get(), AnalyzedStyleUpdate
);
113 void StyleElement::clearDocumentData(Document
& document
, Element
* element
)
116 m_sheet
->clearOwnerNode();
118 if (m_registeredAsCandidate
) {
119 ASSERT(element
->inDocument());
120 document
.styleEngine().removeStyleSheetCandidateNode(element
, element
->treeScope());
121 m_registeredAsCandidate
= false;
125 StyleElement::ProcessingResult
StyleElement::childrenChanged(Element
* element
)
128 if (m_createdByParser
)
129 return ProcessingSuccessful
;
131 return process(element
);
134 StyleElement::ProcessingResult
StyleElement::finishParsingChildren(Element
* element
)
137 ProcessingResult result
= process(element
);
138 m_createdByParser
= false;
142 StyleElement::ProcessingResult
StyleElement::process(Element
* element
)
144 if (!element
|| !element
->inDocument())
145 return ProcessingSuccessful
;
146 return createSheet(element
, element
->textFromChildren());
149 void StyleElement::clearSheet(Element
* ownerElement
)
153 if (ownerElement
&& m_sheet
->isLoading())
154 ownerElement
->document().styleEngine().removePendingSheet(ownerElement
);
156 m_sheet
.release()->clearOwnerNode();
159 static bool shouldBypassMainWorldCSP(Element
* element
)
161 // Main world CSP is bypassed within an isolated world.
162 LocalFrame
* frame
= element
->document().frame();
163 if (frame
&& frame
->script().shouldBypassMainWorldCSP())
166 // Main world CSP is bypassed for style elements in user agent shadow DOM.
167 ShadowRoot
* root
= element
->containingShadowRoot();
168 if (root
&& root
->type() == ShadowRootType::UserAgent
)
174 StyleElement::ProcessingResult
StyleElement::createSheet(Element
* e
, const String
& text
)
177 ASSERT(e
->inDocument());
178 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
, text
);
186 // Clearing the current sheet may remove the cache entry so create the new sheet first
187 RefPtrWillBeRawPtr
<CSSStyleSheet
> newSheet
= nullptr;
189 // If type is empty or CSS, this is a CSS style sheet.
190 const AtomicString
& type
= this->type();
191 if (isCSS(e
, type
) && passesContentSecurityPolicyChecks
) {
192 RefPtrWillBeRawPtr
<MediaQuerySet
> mediaQueries
= MediaQuerySet::create(media());
194 MediaQueryEvaluator
screenEval("screen", true);
195 MediaQueryEvaluator
printEval("print", true);
196 if (screenEval
.eval(mediaQueries
.get()) || printEval
.eval(mediaQueries
.get())) {
198 TextPosition startPosition
= m_startPosition
== TextPosition::belowRangePosition() ? TextPosition::minimumPosition() : m_startPosition
;
199 newSheet
= document
.styleEngine().createSheet(e
, text
, startPosition
);
200 newSheet
->setMediaQueries(mediaQueries
.release());
208 m_sheet
= newSheet
.release();
210 m_sheet
->contents()->checkLoaded();
212 return passesContentSecurityPolicyChecks
? ProcessingSuccessful
: ProcessingFatalError
;
215 bool StyleElement::isLoading() const
219 return m_sheet
? m_sheet
->isLoading() : false;
222 bool StyleElement::sheetLoaded(Document
& document
)
227 document
.styleEngine().removePendingSheet(m_sheet
->ownerNode());
231 void StyleElement::startLoadingDynamicSheet(Document
& document
)
233 document
.styleEngine().addPendingSheet();
236 DEFINE_TRACE(StyleElement
)
238 visitor
->trace(m_sheet
);