Move parseFontFaceDescriptor to CSSPropertyParser.cpp
[chromium-blink-merge.git] / third_party / WebKit / Source / core / dom / StyleElement.cpp
blobd7303a3bb7e73c5d84cf13a8eb6a7d8feffbde95
1 /*
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.
21 #include "config.h"
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"
40 namespace blink {
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)
49 , m_loading(false)
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()
59 #if !ENABLE(OILPAN)
60 if (m_sheet)
61 clearSheet();
62 #endif
65 StyleElement::ProcessingResult StyleElement::processStyleSheet(Document& document, Element* element)
67 TRACE_EVENT0("blink", "StyleElement::processStyleSheet");
68 ASSERT(element);
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())
82 return;
83 if (ShadowRoot* scope = element->containingShadowRoot())
84 scope->registerScopedHTMLStyleChild();
87 void StyleElement::removedFrom(Element* element, ContainerNode* insertionPoint)
89 if (!insertionPoint->inDocument())
90 return;
92 ShadowRoot* shadowRoot = element->containingShadowRoot();
93 if (!shadowRoot)
94 shadowRoot = insertionPoint->containingShadowRoot();
96 if (shadowRoot)
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();
107 if (m_sheet)
108 clearSheet(element);
109 if (removedSheet)
110 document.removedStyleSheet(removedSheet.get(), AnalyzedStyleUpdate);
113 void StyleElement::clearDocumentData(Document& document, Element* element)
115 if (m_sheet)
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)
127 ASSERT(element);
128 if (m_createdByParser)
129 return ProcessingSuccessful;
131 return process(element);
134 StyleElement::ProcessingResult StyleElement::finishParsingChildren(Element* element)
136 ASSERT(element);
137 ProcessingResult result = process(element);
138 m_createdByParser = false;
139 return result;
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)
151 ASSERT(m_sheet);
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())
164 return true;
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)
169 return true;
171 return false;
174 StyleElement::ProcessingResult StyleElement::createSheet(Element* e, const String& text)
176 ASSERT(e);
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())) {
197 m_loading = true;
198 TextPosition startPosition = m_startPosition == TextPosition::belowRangePosition() ? TextPosition::minimumPosition() : m_startPosition;
199 newSheet = document.styleEngine().createSheet(e, text, startPosition);
200 newSheet->setMediaQueries(mediaQueries.release());
201 m_loading = false;
205 if (m_sheet)
206 clearSheet(e);
208 m_sheet = newSheet.release();
209 if (m_sheet)
210 m_sheet->contents()->checkLoaded();
212 return passesContentSecurityPolicyChecks ? ProcessingSuccessful : ProcessingFatalError;
215 bool StyleElement::isLoading() const
217 if (m_loading)
218 return true;
219 return m_sheet ? m_sheet->isLoading() : false;
222 bool StyleElement::sheetLoaded(Document& document)
224 if (isLoading())
225 return false;
227 document.styleEngine().removePendingSheet(m_sheet->ownerNode());
228 return true;
231 void StyleElement::startLoadingDynamicSheet(Document& document)
233 document.styleEngine().addPendingSheet();
236 DEFINE_TRACE(StyleElement)
238 visitor->trace(m_sheet);