Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / core / dom / StyleElement.cpp
1 /*
2  * Copyright (C) 2006, 2007 Rob Buis
3  * Copyright (C) 2008 Apple, Inc. All rights reserved.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "config.h"
22 #include "core/dom/StyleElement.h"
23
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"
35
36 namespace WebCore {
37
38 static bool isCSS(Element* element, const AtomicString& type)
39 {
40     return type.isEmpty() || (element->isHTMLElement() ? equalIgnoringCase(type, "text/css") : (type == "text/css"));
41 }
42
43 StyleElement::StyleElement(Document* document, bool createdByParser)
44     : m_createdByParser(createdByParser)
45     , m_loading(false)
46     , m_registeredAsCandidate(false)
47     , m_startPosition(TextPosition::belowRangePosition())
48 {
49     if (createdByParser && document && document->scriptableDocumentParser() && !document->isInDocumentWrite())
50         m_startPosition = document->scriptableDocumentParser()->textPosition();
51 }
52
53 StyleElement::~StyleElement()
54 {
55     if (m_sheet)
56         clearSheet();
57 }
58
59 void StyleElement::processStyleSheet(Document& document, Element* element)
60 {
61     TRACE_EVENT0("webkit", "StyleElement::processStyleSheet");
62     ASSERT(element);
63     ASSERT(element->inDocument());
64
65     m_registeredAsCandidate = true;
66     document.styleEngine()->addStyleSheetCandidateNode(element, m_createdByParser);
67     if (m_createdByParser)
68         return;
69
70     process(element);
71 }
72
73 void StyleElement::removedFromDocument(Document& document, Element* element)
74 {
75     removedFromDocument(document, element, 0, document);
76 }
77
78 void StyleElement::removedFromDocument(Document& document, Element* element, ContainerNode* scopingNode, TreeScope& treeScope)
79 {
80     ASSERT(element);
81
82     if (m_registeredAsCandidate) {
83         document.styleEngine()->removeStyleSheetCandidateNode(element, scopingNode, treeScope);
84         m_registeredAsCandidate = false;
85     }
86
87     RefPtrWillBeRawPtr<StyleSheet> removedSheet = m_sheet.get();
88
89     if (m_sheet)
90         clearSheet(element);
91     if (removedSheet)
92         document.removedStyleSheet(removedSheet.get(), RecalcStyleDeferred, AnalyzedStyleUpdate);
93 }
94
95 void StyleElement::clearDocumentData(Document& document, Element* element)
96 {
97     if (m_sheet)
98         m_sheet->clearOwnerNode();
99
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);
104     }
105 }
106
107 void StyleElement::childrenChanged(Element* element)
108 {
109     ASSERT(element);
110     if (m_createdByParser)
111         return;
112
113     process(element);
114 }
115
116 void StyleElement::finishParsingChildren(Element* element)
117 {
118     ASSERT(element);
119     process(element);
120     m_createdByParser = false;
121 }
122
123 void StyleElement::process(Element* element)
124 {
125     if (!element || !element->inDocument())
126         return;
127     createSheet(element, element->textFromChildren());
128 }
129
130 void StyleElement::clearSheet(Element* ownerElement)
131 {
132     ASSERT(m_sheet);
133
134     if (ownerElement && m_sheet->isLoading())
135         ownerElement->document().styleEngine()->removePendingSheet(ownerElement);
136
137     m_sheet.release()->clearOwnerNode();
138 }
139
140 void StyleElement::createSheet(Element* e, const String& text)
141 {
142     ASSERT(e);
143     ASSERT(e->inDocument());
144     Document& document = e->document();
145     if (m_sheet)
146         clearSheet(e);
147
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());
153
154         MediaQueryEvaluator screenEval("screen", true);
155         MediaQueryEvaluator printEval("print", true);
156         if (screenEval.eval(mediaQueries.get()) || printEval.eval(mediaQueries.get())) {
157             m_loading = true;
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());
161             m_loading = false;
162         }
163     }
164
165     if (m_sheet)
166         m_sheet->contents()->checkLoaded();
167 }
168
169 bool StyleElement::isLoading() const
170 {
171     if (m_loading)
172         return true;
173     return m_sheet ? m_sheet->isLoading() : false;
174 }
175
176 bool StyleElement::sheetLoaded(Document& document)
177 {
178     if (isLoading())
179         return false;
180
181     document.styleEngine()->removePendingSheet(m_sheet->ownerNode());
182     return true;
183 }
184
185 void StyleElement::startLoadingDynamicSheet(Document& document)
186 {
187     document.styleEngine()->addPendingSheet();
188 }
189
190 }