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 "core/css/MediaList.h"
25 #include "core/css/MediaQueryEvaluator.h"
26 #include "core/css/StyleSheetContents.h"
27 #include "core/dom/Document.h"
28 #include "core/dom/Element.h"
29 #include "core/dom/ScriptableDocumentParser.h"
30 #include "core/dom/StyleEngine.h"
31 #include "core/frame/csp/ContentSecurityPolicy.h"
32 #include "core/html/HTMLStyleElement.h"
33 #include "platform/TraceEvent.h"
34 #include "wtf/text/StringBuilder.h"
38 static bool isCSS(Element* element, const AtomicString& type)
40 return type.isEmpty() || (element->isHTMLElement() ? equalIgnoringCase(type, "text/css") : (type == "text/css"));
43 StyleElement::StyleElement(Document* document, bool createdByParser)
44 : m_createdByParser(createdByParser)
46 , m_registeredAsCandidate(false)
47 , m_startPosition(TextPosition::belowRangePosition())
49 if (createdByParser && document && document->scriptableDocumentParser() && !document->isInDocumentWrite())
50 m_startPosition = document->scriptableDocumentParser()->textPosition();
53 StyleElement::~StyleElement()
59 void StyleElement::processStyleSheet(Document& document, Element* element)
61 TRACE_EVENT0("webkit", "StyleElement::processStyleSheet");
63 ASSERT(element->inDocument());
65 m_registeredAsCandidate = true;
66 document.styleEngine()->addStyleSheetCandidateNode(element, m_createdByParser);
67 if (m_createdByParser)
73 void StyleElement::removedFromDocument(Document& document, Element* element)
75 removedFromDocument(document, element, 0, document);
78 void StyleElement::removedFromDocument(Document& document, Element* element, ContainerNode* scopingNode, TreeScope& treeScope)
82 if (m_registeredAsCandidate) {
83 document.styleEngine()->removeStyleSheetCandidateNode(element, scopingNode, treeScope);
84 m_registeredAsCandidate = false;
87 RefPtrWillBeRawPtr<StyleSheet> removedSheet = m_sheet.get();
92 document.removedStyleSheet(removedSheet.get(), RecalcStyleDeferred, AnalyzedStyleUpdate);
95 void StyleElement::clearDocumentData(Document& document, Element* element)
98 m_sheet->clearOwnerNode();
100 if (element->inDocument()) {
101 ContainerNode* scopingNode = isHTMLStyleElement(element) ? toHTMLStyleElement(element)->scopingNode() : 0;
102 TreeScope& treeScope = scopingNode ? scopingNode->treeScope() : element->treeScope();
103 document.styleEngine()->removeStyleSheetCandidateNode(element, scopingNode, treeScope);
107 void StyleElement::childrenChanged(Element* element)
110 if (m_createdByParser)
116 void StyleElement::finishParsingChildren(Element* element)
120 m_createdByParser = false;
123 void StyleElement::process(Element* element)
125 if (!element || !element->inDocument())
127 createSheet(element, element->textFromChildren());
130 void StyleElement::clearSheet(Element* ownerElement)
134 if (ownerElement && m_sheet->isLoading())
135 ownerElement->document().styleEngine()->removePendingSheet(ownerElement);
137 m_sheet.release()->clearOwnerNode();
140 void StyleElement::createSheet(Element* e, const String& text)
143 ASSERT(e->inDocument());
144 Document& document = e->document();
148 // If type is empty or CSS, this is a CSS style sheet.
149 const AtomicString& type = this->type();
150 bool passesContentSecurityPolicyChecks = document.contentSecurityPolicy()->allowStyleHash(text) || document.contentSecurityPolicy()->allowStyleNonce(e->fastGetAttribute(HTMLNames::nonceAttr)) || document.contentSecurityPolicy()->allowInlineStyle(e->document().url(), m_startPosition.m_line);
151 if (isCSS(e, type) && passesContentSecurityPolicyChecks) {
152 RefPtrWillBeRawPtr<MediaQuerySet> mediaQueries = MediaQuerySet::create(media());
154 MediaQueryEvaluator screenEval("screen", true);
155 MediaQueryEvaluator printEval("print", true);
156 if (screenEval.eval(mediaQueries.get()) || printEval.eval(mediaQueries.get())) {
158 TextPosition startPosition = m_startPosition == TextPosition::belowRangePosition() ? TextPosition::minimumPosition() : m_startPosition;
159 m_sheet = document.styleEngine()->createSheet(e, text, startPosition, m_createdByParser);
160 m_sheet->setMediaQueries(mediaQueries.release());
166 m_sheet->contents()->checkLoaded();
169 bool StyleElement::isLoading() const
173 return m_sheet ? m_sheet->isLoading() : false;
176 bool StyleElement::sheetLoaded(Document& document)
181 document.styleEngine()->removePendingSheet(m_sheet->ownerNode());
185 void StyleElement::startLoadingDynamicSheet(Document& document)
187 document.styleEngine()->addPendingSheet();