Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(348)

Side by Side Diff: third_party/WebKit/Source/core/css/parser/CSSParserImpl.cpp

Issue 2315923002: Lazy Parse CSS (Closed)
Patch Set: Remove the closure and refactor logic into a separate file Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/css/parser/CSSParserImpl.h" 5 #include "core/css/parser/CSSParserImpl.h"
6 6
7 #include "core/css/CSSCustomIdentValue.h" 7 #include "core/css/CSSCustomIdentValue.h"
8 #include "core/css/CSSCustomPropertyDeclaration.h" 8 #include "core/css/CSSCustomPropertyDeclaration.h"
9 #include "core/css/CSSKeyframesRule.h" 9 #include "core/css/CSSKeyframesRule.h"
10 #include "core/css/CSSStyleSheet.h" 10 #include "core/css/CSSStyleSheet.h"
11 #include "core/css/StylePropertySet.h" 11 #include "core/css/StylePropertySet.h"
12 #include "core/css/StyleRuleImport.h" 12 #include "core/css/StyleRuleImport.h"
13 #include "core/css/StyleRuleKeyframe.h" 13 #include "core/css/StyleRuleKeyframe.h"
14 #include "core/css/StyleRuleNamespace.h" 14 #include "core/css/StyleRuleNamespace.h"
15 #include "core/css/StyleSheetContents.h" 15 #include "core/css/StyleSheetContents.h"
16 #include "core/css/parser/CSSAtRuleID.h" 16 #include "core/css/parser/CSSAtRuleID.h"
17 #include "core/css/parser/CSSLazyPropertyParserImpl.h"
17 #include "core/css/parser/CSSParserObserver.h" 18 #include "core/css/parser/CSSParserObserver.h"
18 #include "core/css/parser/CSSParserObserverWrapper.h" 19 #include "core/css/parser/CSSParserObserverWrapper.h"
19 #include "core/css/parser/CSSParserSelector.h" 20 #include "core/css/parser/CSSParserSelector.h"
20 #include "core/css/parser/CSSPropertyParser.h" 21 #include "core/css/parser/CSSPropertyParser.h"
21 #include "core/css/parser/CSSSelectorParser.h" 22 #include "core/css/parser/CSSSelectorParser.h"
22 #include "core/css/parser/CSSSupportsParser.h" 23 #include "core/css/parser/CSSSupportsParser.h"
23 #include "core/css/parser/CSSTokenizer.h" 24 #include "core/css/parser/CSSTokenizer.h"
24 #include "core/css/parser/CSSVariableParser.h" 25 #include "core/css/parser/CSSVariableParser.h"
25 #include "core/css/parser/MediaQueryParser.h" 26 #include "core/css/parser/MediaQueryParser.h"
26 #include "core/dom/Document.h" 27 #include "core/dom/Document.h"
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 if (!rule) 186 if (!rule)
186 return nullptr; // Parse error, failed to consume rule 187 return nullptr; // Parse error, failed to consume rule
187 range.consumeWhitespace(); 188 range.consumeWhitespace();
188 if (!rule || !range.atEnd()) 189 if (!rule || !range.atEnd())
189 return nullptr; // Parse error, trailing garbage 190 return nullptr; // Parse error, trailing garbage
190 return rule; 191 return rule;
191 } 192 }
192 193
193 void CSSParserImpl::parseStyleSheet(const String& string, 194 void CSSParserImpl::parseStyleSheet(const String& string,
194 const CSSParserContext& context, 195 const CSSParserContext& context,
195 StyleSheetContents* styleSheet) { 196 StyleSheetContents* styleSheet,
197 bool deferPropertyParsing) {
196 TRACE_EVENT_BEGIN2("blink,blink_style", "CSSParserImpl::parseStyleSheet", 198 TRACE_EVENT_BEGIN2("blink,blink_style", "CSSParserImpl::parseStyleSheet",
197 "baseUrl", context.baseURL().getString().utf8(), "mode", 199 "baseUrl", context.baseURL().getString().utf8(), "mode",
198 context.mode()); 200 context.mode());
199 201
200 TRACE_EVENT_BEGIN0("blink,blink_style", 202 TRACE_EVENT_BEGIN0("blink,blink_style",
201 "CSSParserImpl::parseStyleSheet.tokenize"); 203 "CSSParserImpl::parseStyleSheet.tokenize");
202 CSSTokenizer::Scope scope(string); 204 CSSTokenizer::Scope scope(string);
203 TRACE_EVENT_END0("blink,blink_style", 205 TRACE_EVENT_END0("blink,blink_style",
204 "CSSParserImpl::parseStyleSheet.tokenize"); 206 "CSSParserImpl::parseStyleSheet.tokenize");
205 207
206 TRACE_EVENT_BEGIN0("blink,blink_style", 208 TRACE_EVENT_BEGIN0("blink,blink_style",
207 "CSSParserImpl::parseStyleSheet.parse"); 209 "CSSParserImpl::parseStyleSheet.parse");
208 CSSParserImpl parser(context, styleSheet); 210 CSSParserImpl parser(context, styleSheet);
211 if (deferPropertyParsing) {
212 parser.m_lazyState =
213 new CSSLazyParsingState(context, scope.releaseEscapedStrings(), string);
esprehn 2016/10/24 21:20:21 std::move(scope.takeEscapedStrings()) ?
Charlie Harrison 2016/10/25 15:56:55 Done.
Charlie Harrison 2016/10/25 15:56:55 Done, but takeEscapedStrings() implies that we're
214 }
209 bool firstRuleValid = parser.consumeRuleList( 215 bool firstRuleValid = parser.consumeRuleList(
210 scope.tokenRange(), TopLevelRuleList, [&styleSheet](StyleRuleBase* rule) { 216 scope.tokenRange(), TopLevelRuleList, [&styleSheet](StyleRuleBase* rule) {
211 if (rule->isCharsetRule()) 217 if (rule->isCharsetRule())
212 return; 218 return;
213 styleSheet->parserAppendRule(rule); 219 styleSheet->parserAppendRule(rule);
214 }); 220 });
215 styleSheet->setHasSyntacticallyValidCSSHeader(firstRuleValid); 221 styleSheet->setHasSyntacticallyValidCSSHeader(firstRuleValid);
216 TRACE_EVENT_END0("blink,blink_style", "CSSParserImpl::parseStyleSheet.parse"); 222 TRACE_EVENT_END0("blink,blink_style", "CSSParserImpl::parseStyleSheet.parse");
217 223
218 TRACE_EVENT_END2("blink,blink_style", "CSSParserImpl::parseStyleSheet", 224 TRACE_EVENT_END2("blink,blink_style", "CSSParserImpl::parseStyleSheet",
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after
324 CSSTokenizer::Scope scope(string, wrapper); 330 CSSTokenizer::Scope scope(string, wrapper);
325 bool firstRuleValid = parser.consumeRuleList( 331 bool firstRuleValid = parser.consumeRuleList(
326 scope.tokenRange(), TopLevelRuleList, [&styleSheet](StyleRuleBase* rule) { 332 scope.tokenRange(), TopLevelRuleList, [&styleSheet](StyleRuleBase* rule) {
327 if (rule->isCharsetRule()) 333 if (rule->isCharsetRule())
328 return; 334 return;
329 styleSheet->parserAppendRule(rule); 335 styleSheet->parserAppendRule(rule);
330 }); 336 });
331 styleSheet->setHasSyntacticallyValidCSSHeader(firstRuleValid); 337 styleSheet->setHasSyntacticallyValidCSSHeader(firstRuleValid);
332 } 338 }
333 339
340 StylePropertySet* CSSParserImpl::parseDeclarationListForLazyStyle(
341 CSSParserTokenRange block,
342 const CSSParserContext& context) {
343 CSSParserImpl parser(context);
344 parser.consumeDeclarationList(std::move(block), StyleRule::Style);
345 return createStylePropertySet(parser.m_parsedProperties, context.mode());
346 }
347
334 static CSSParserImpl::AllowedRulesType computeNewAllowedRules( 348 static CSSParserImpl::AllowedRulesType computeNewAllowedRules(
335 CSSParserImpl::AllowedRulesType allowedRules, 349 CSSParserImpl::AllowedRulesType allowedRules,
336 StyleRuleBase* rule) { 350 StyleRuleBase* rule) {
337 if (!rule || allowedRules == CSSParserImpl::KeyframeRules || 351 if (!rule || allowedRules == CSSParserImpl::KeyframeRules ||
338 allowedRules == CSSParserImpl::NoRules) 352 allowedRules == CSSParserImpl::NoRules)
339 return allowedRules; 353 return allowedRules;
340 ASSERT(allowedRules <= CSSParserImpl::RegularRules); 354 ASSERT(allowedRules <= CSSParserImpl::RegularRules);
341 if (rule->isCharsetRule() || rule->isImportRule()) 355 if (rule->isCharsetRule() || rule->isImportRule())
342 return CSSParserImpl::AllowImportRules; 356 return CSSParserImpl::AllowImportRules;
343 if (rule->isNamespaceRule()) 357 if (rule->isNamespaceRule())
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after
758 wrapper.observer().endRuleHeader(wrapper.endOffset(originalRange)); 772 wrapper.observer().endRuleHeader(wrapper.endOffset(originalRange));
759 } 773 }
760 774
761 StyleRule* CSSParserImpl::consumeStyleRule(CSSParserTokenRange prelude, 775 StyleRule* CSSParserImpl::consumeStyleRule(CSSParserTokenRange prelude,
762 CSSParserTokenRange block) { 776 CSSParserTokenRange block) {
763 CSSSelectorList selectorList = 777 CSSSelectorList selectorList =
764 CSSSelectorParser::parseSelector(prelude, m_context, m_styleSheet); 778 CSSSelectorParser::parseSelector(prelude, m_context, m_styleSheet);
765 if (!selectorList.isValid()) 779 if (!selectorList.isValid())
766 return nullptr; // Parse error, invalid selector list 780 return nullptr; // Parse error, invalid selector list
767 781
768 if (m_observerWrapper) 782 // TODO(csharrison): How should we lazily parse css that needs the observer?
783 if (m_observerWrapper) {
769 observeSelectors(*m_observerWrapper, prelude); 784 observeSelectors(*m_observerWrapper, prelude);
770 785 } else if (m_lazyState &&
786 m_lazyState->shouldLazilyParseProperties(selectorList)) {
787 DCHECK(m_styleSheet);
788 return StyleRule::createLazy(
789 std::move(selectorList),
790 new CSSLazyPropertyParserImpl(block, m_lazyState));
791 }
771 consumeDeclarationList(block, StyleRule::Style); 792 consumeDeclarationList(block, StyleRule::Style);
772
773 return StyleRule::create( 793 return StyleRule::create(
774 std::move(selectorList), 794 std::move(selectorList),
775 createStylePropertySet(m_parsedProperties, m_context.mode())); 795 createStylePropertySet(m_parsedProperties, m_context.mode()));
776 } 796 }
777 797
778 void CSSParserImpl::consumeDeclarationList(CSSParserTokenRange range, 798 void CSSParserImpl::consumeDeclarationList(CSSParserTokenRange range,
779 StyleRule::RuleType ruleType) { 799 StyleRule::RuleType ruleType) {
780 ASSERT(m_parsedProperties.isEmpty()); 800 ASSERT(m_parsedProperties.isEmpty());
781 801
782 bool useObserver = m_observerWrapper && (ruleType == StyleRule::Style || 802 bool useObserver = m_observerWrapper && (ruleType == StyleRule::Style ||
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after
932 else 952 else
933 return nullptr; // Parser error, invalid value in keyframe selector 953 return nullptr; // Parser error, invalid value in keyframe selector
934 if (range.atEnd()) 954 if (range.atEnd())
935 return result; 955 return result;
936 if (range.consume().type() != CommaToken) 956 if (range.consume().type() != CommaToken)
937 return nullptr; // Parser error 957 return nullptr; // Parser error
938 } 958 }
939 } 959 }
940 960
941 } // namespace blink 961 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698