OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (C) 2006, 2007 Rob Buis | 2 * Copyright (C) 2006, 2007 Rob Buis |
3 * Copyright (C) 2008 Apple, Inc. All rights reserved. | 3 * Copyright (C) 2008 Apple, Inc. All rights reserved. |
4 * | 4 * |
5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
9 * | 9 * |
10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
(...skipping 17 matching lines...) Expand all Loading... | |
28 #include "core/dom/Element.h" | 28 #include "core/dom/Element.h" |
29 #include "core/dom/ScriptableDocumentParser.h" | 29 #include "core/dom/ScriptableDocumentParser.h" |
30 #include "core/dom/StyleEngine.h" | 30 #include "core/dom/StyleEngine.h" |
31 #include "core/html/HTMLStyleElement.h" | 31 #include "core/html/HTMLStyleElement.h" |
32 #include "core/frame/ContentSecurityPolicy.h" | 32 #include "core/frame/ContentSecurityPolicy.h" |
33 #include "wtf/text/StringBuilder.h" | 33 #include "wtf/text/StringBuilder.h" |
34 #include "wtf/text/TextPosition.h" | 34 #include "wtf/text/TextPosition.h" |
35 | 35 |
36 namespace WebCore { | 36 namespace WebCore { |
37 | 37 |
38 static const unsigned cacheableTextContentSizeLimit = 256; | |
dglazkov
2013/10/18 16:56:19
How did you arrive at this number?
tasak
2013/10/21 12:27:37
I have no strong reason. So I removed this magic n
| |
39 | |
38 static bool isCSS(Element* element, const AtomicString& type) | 40 static bool isCSS(Element* element, const AtomicString& type) |
39 { | 41 { |
40 return type.isEmpty() || (element->isHTMLElement() ? equalIgnoringCase(type, "text/css") : (type == "text/css")); | 42 return type.isEmpty() || (element->isHTMLElement() ? equalIgnoringCase(type, "text/css") : (type == "text/css")); |
41 } | 43 } |
42 | 44 |
43 StyleElement::StyleElement(Document* document, bool createdByParser) | 45 StyleElement::StyleElement(Document* document, bool createdByParser) |
44 : m_createdByParser(createdByParser) | 46 : m_createdByParser(createdByParser) |
45 , m_loading(false) | 47 , m_loading(false) |
46 , m_startPosition(TextPosition::belowRangePosition()) | 48 , m_startPosition(TextPosition::belowRangePosition()) |
47 { | 49 { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
108 void StyleElement::process(Element* element) | 110 void StyleElement::process(Element* element) |
109 { | 111 { |
110 if (!element || !element->inDocument()) | 112 if (!element || !element->inDocument()) |
111 return; | 113 return; |
112 createSheet(element, element->textFromChildren()); | 114 createSheet(element, element->textFromChildren()); |
113 } | 115 } |
114 | 116 |
115 void StyleElement::clearSheet() | 117 void StyleElement::clearSheet() |
116 { | 118 { |
117 ASSERT(m_sheet); | 119 ASSERT(m_sheet); |
120 if (!m_sheetText.isEmpty() && m_sheet->ownerDocument() && m_sheet->ownerDocu ment()->isActive()) | |
dglazkov
2013/10/18 16:56:19
It might be a clearer (though more complex) factor
tasak
2013/10/21 12:27:37
I added a comments which describes the condition.
| |
121 m_sheet->ownerDocument()->styleEngine()->unregisterStyleSheetContents(m_ sheetText); | |
118 m_sheet.release()->clearOwnerNode(); | 122 m_sheet.release()->clearOwnerNode(); |
119 } | 123 } |
120 | 124 |
121 void StyleElement::createSheet(Element* e, const String& text) | 125 void StyleElement::createSheet(Element* e, const String& text) |
122 { | 126 { |
123 ASSERT(e); | 127 ASSERT(e); |
124 ASSERT(e->inDocument()); | 128 ASSERT(e->inDocument()); |
125 Document& document = e->document(); | 129 Document& document = e->document(); |
126 if (m_sheet) { | 130 if (m_sheet) { |
127 if (m_sheet->isLoading()) | 131 if (m_sheet->isLoading()) |
128 document.styleEngine()->removePendingSheet(e); | 132 document.styleEngine()->removePendingSheet(e); |
129 clearSheet(); | 133 clearSheet(); |
130 } | 134 } |
131 | 135 |
136 m_sheetText = isHTMLStyleElement(e) && text.length() < cacheableTextContentS izeLimit ? text : String(); | |
137 | |
132 // If type is empty or CSS, this is a CSS style sheet. | 138 // If type is empty or CSS, this is a CSS style sheet. |
133 const AtomicString& type = this->type(); | 139 const AtomicString& type = this->type(); |
134 bool passesContentSecurityPolicyChecks = document.contentSecurityPolicy()->a llowStyleNonce(e->fastGetAttribute(HTMLNames::nonceAttr)) || document.contentSec urityPolicy()->allowInlineStyle(e->document().url(), m_startPosition.m_line); | 140 bool passesContentSecurityPolicyChecks = document.contentSecurityPolicy()->a llowStyleNonce(e->fastGetAttribute(HTMLNames::nonceAttr)) || document.contentSec urityPolicy()->allowInlineStyle(e->document().url(), m_startPosition.m_line); |
135 if (isCSS(e, type) && passesContentSecurityPolicyChecks) { | 141 if (isCSS(e, type) && passesContentSecurityPolicyChecks) { |
136 RefPtr<MediaQuerySet> mediaQueries = MediaQuerySet::create(media()); | 142 RefPtr<MediaQuerySet> mediaQueries = MediaQuerySet::create(media()); |
137 | 143 |
138 MediaQueryEvaluator screenEval("screen", true); | 144 MediaQueryEvaluator screenEval("screen", true); |
139 MediaQueryEvaluator printEval("print", true); | 145 MediaQueryEvaluator printEval("print", true); |
140 if (screenEval.eval(mediaQueries.get()) || printEval.eval(mediaQueries.g et())) { | 146 if (screenEval.eval(mediaQueries.get()) || printEval.eval(mediaQueries.g et())) { |
141 document.styleEngine()->addPendingSheet(); | 147 document.styleEngine()->addPendingSheet(); |
142 m_loading = true; | 148 m_loading = true; |
143 | 149 |
144 TextPosition startPosition = m_startPosition == TextPosition::belowR angePosition() ? TextPosition::minimumPosition() : m_startPosition; | 150 TextPosition startPosition = m_startPosition == TextPosition::belowR angePosition() ? TextPosition::minimumPosition() : m_startPosition; |
145 m_sheet = CSSStyleSheet::createInline(e, KURL(), startPosition, docu ment.inputEncoding()); | 151 if (StyleSheetContents* contents = document.styleEngine()->findStyle SheetContents(m_sheetText)) { |
146 m_sheet->setMediaQueries(mediaQueries.release()); | 152 // FIXME: should we use registerClient to avoid allocating a new StyleSheetContents? |
147 m_sheet->setTitle(e->title()); | 153 // In this case, StyleSheetContents::singleOwnerNode() doesn't w ork. So need to modify |
148 m_sheet->contents()->parseStringAtPosition(text, startPosition, m_cr eatedByParser); | 154 // codes depending on singleOwnerNode(). |
149 | 155 m_sheet = CSSStyleSheet::createInline(contents, e, startPosition ); |
156 m_sheet->setMediaQueries(mediaQueries.release()); | |
157 m_sheet->setTitle(e->title()); | |
158 } else { | |
159 m_sheet = CSSStyleSheet::createInline(e, KURL(), startPosition, document.inputEncoding()); | |
160 m_sheet->setMediaQueries(mediaQueries.release()); | |
161 m_sheet->setTitle(e->title()); | |
162 m_sheet->contents()->parseStringAtPosition(text, startPosition, m_createdByParser); | |
163 } | |
150 m_loading = false; | 164 m_loading = false; |
151 } | 165 } |
152 } | 166 } |
153 | 167 |
154 if (m_sheet) | 168 if (m_sheet) { |
155 m_sheet->contents()->checkLoaded(); | 169 m_sheet->contents()->checkLoaded(); |
170 if (!m_sheetText.isEmpty() && m_sheet->contents()->isCacheable()) | |
171 document.styleEngine()->registerStyleSheetContents(m_sheetText, m_sh eet->contents()); | |
172 } | |
156 } | 173 } |
157 | 174 |
158 bool StyleElement::isLoading() const | 175 bool StyleElement::isLoading() const |
159 { | 176 { |
160 if (m_loading) | 177 if (m_loading) |
161 return true; | 178 return true; |
162 return m_sheet ? m_sheet->isLoading() : false; | 179 return m_sheet ? m_sheet->isLoading() : false; |
163 } | 180 } |
164 | 181 |
165 bool StyleElement::sheetLoaded(Document& document) | 182 bool StyleElement::sheetLoaded(Document& document) |
166 { | 183 { |
167 if (isLoading()) | 184 if (isLoading()) |
168 return false; | 185 return false; |
169 | 186 |
170 document.styleEngine()->removePendingSheet(m_sheet->ownerNode()); | 187 document.styleEngine()->removePendingSheet(m_sheet->ownerNode()); |
171 return true; | 188 return true; |
172 } | 189 } |
173 | 190 |
174 void StyleElement::startLoadingDynamicSheet(Document& document) | 191 void StyleElement::startLoadingDynamicSheet(Document& document) |
175 { | 192 { |
176 document.styleEngine()->addPendingSheet(); | 193 document.styleEngine()->addPendingSheet(); |
177 } | 194 } |
178 | 195 |
179 } | 196 } |
OLD | NEW |