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()
61 void StyleElement::processStyleSheet(Document& document, Element* element)
63 TRACE_EVENT0("webkit", "StyleElement::processStyleSheet");
65 ASSERT(element->inDocument());
67 m_registeredAsCandidate = true;
68 document.styleEngine()->addStyleSheetCandidateNode(element, m_createdByParser);
69 if (m_createdByParser)
75 void StyleElement::removedFromDocument(Document& document, Element* element)
77 removedFromDocument(document, element, 0, document);
80 void StyleElement::removedFromDocument(Document& document, Element* element, ContainerNode* scopingNode, TreeScope& treeScope)
84 if (m_registeredAsCandidate) {
85 document.styleEngine()->removeStyleSheetCandidateNode(element, scopingNode, treeScope);
86 m_registeredAsCandidate = false;
89 RefPtrWillBeRawPtr<StyleSheet> removedSheet = m_sheet.get();
94 document.removedStyleSheet(removedSheet.get(), RecalcStyleDeferred, AnalyzedStyleUpdate);
97 void StyleElement::clearDocumentData(Document& document, Element* element)
100 m_sheet->clearOwnerNode();
102 if (element->inDocument()) {
103 ContainerNode* scopingNode = isHTMLStyleElement(element) ? toHTMLStyleElement(element)->scopingNode() : 0;
104 TreeScope& treeScope = scopingNode ? scopingNode->treeScope() : element->treeScope();
105 document.styleEngine()->removeStyleSheetCandidateNode(element, scopingNode, treeScope);
109 void StyleElement::childrenChanged(Element* element)
112 if (m_createdByParser)
118 void StyleElement::finishParsingChildren(Element* element)
122 m_createdByParser = false;
125 void StyleElement::process(Element* element)
127 if (!element || !element->inDocument())
129 createSheet(element, element->textFromChildren());
132 void StyleElement::clearSheet(Element* ownerElement)
136 if (ownerElement && m_sheet->isLoading())
137 ownerElement->document().styleEngine()->removePendingSheet(ownerElement);
139 m_sheet.release()->clearOwnerNode();
142 void StyleElement::createSheet(Element* e, const String& text)
145 ASSERT(e->inDocument());
146 Document& document = e->document();
150 // If type is empty or CSS, this is a CSS style sheet.
151 const AtomicString& type = this->type();
152 bool passesContentSecurityPolicyChecks = document.contentSecurityPolicy()->allowStyleHash(text) || document.contentSecurityPolicy()->allowStyleNonce(e->fastGetAttribute(HTMLNames::nonceAttr)) || document.contentSecurityPolicy()->allowInlineStyle(e->document().url(), m_startPosition.m_line);
153 if (isCSS(e, type) && passesContentSecurityPolicyChecks) {
154 RefPtrWillBeRawPtr<MediaQuerySet> mediaQueries = MediaQuerySet::create(media());
156 MediaQueryEvaluator screenEval("screen", true);
157 MediaQueryEvaluator printEval("print", true);
158 if (screenEval.eval(mediaQueries.get()) || printEval.eval(mediaQueries.get())) {
160 TextPosition startPosition = m_startPosition == TextPosition::belowRangePosition() ? TextPosition::minimumPosition() : m_startPosition;
161 m_sheet = document.styleEngine()->createSheet(e, text, startPosition, m_createdByParser);
162 m_sheet->setMediaQueries(mediaQueries.release());
168 m_sheet->contents()->checkLoaded();
171 bool StyleElement::isLoading() const
175 return m_sheet ? m_sheet->isLoading() : false;
178 bool StyleElement::sheetLoaded(Document& document)
183 document.styleEngine()->removePendingSheet(m_sheet->ownerNode());
187 void StyleElement::startLoadingDynamicSheet(Document& document)
189 document.styleEngine()->addPendingSheet();
192 void StyleElement::trace(Visitor* visitor)
194 visitor->trace(m_sheet);