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

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

Issue 962093002: CSS Tokenizer: Add an on-stack tokenizer scope (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 5 years, 9 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
« no previous file with comments | « Source/core/css/parser/CSSParserImpl.h ('k') | Source/core/css/parser/CSSParserValuesTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "config.h" 5 #include "config.h"
6 #include "core/css/parser/CSSParserImpl.h" 6 #include "core/css/parser/CSSParserImpl.h"
7 7
8 #include "core/css/CSSKeyframesRule.h" 8 #include "core/css/CSSKeyframesRule.h"
9 #include "core/css/CSSStyleSheet.h" 9 #include "core/css/CSSStyleSheet.h"
10 #include "core/css/StylePropertySet.h" 10 #include "core/css/StylePropertySet.h"
11 #include "core/css/StyleRuleImport.h" 11 #include "core/css/StyleRuleImport.h"
12 #include "core/css/StyleRuleKeyframe.h" 12 #include "core/css/StyleRuleKeyframe.h"
13 #include "core/css/StyleRuleNamespace.h" 13 #include "core/css/StyleRuleNamespace.h"
14 #include "core/css/StyleSheetContents.h" 14 #include "core/css/StyleSheetContents.h"
15 #include "core/css/parser/CSSParserValues.h" 15 #include "core/css/parser/CSSParserValues.h"
16 #include "core/css/parser/CSSPropertyParser.h" 16 #include "core/css/parser/CSSPropertyParser.h"
17 #include "core/css/parser/CSSSelectorParser.h" 17 #include "core/css/parser/CSSSelectorParser.h"
18 #include "core/css/parser/CSSSupportsParser.h" 18 #include "core/css/parser/CSSSupportsParser.h"
19 #include "core/css/parser/CSSTokenizer.h" 19 #include "core/css/parser/CSSTokenizer.h"
20 #include "core/css/parser/MediaQueryParser.h" 20 #include "core/css/parser/MediaQueryParser.h"
21 #include "core/dom/Document.h" 21 #include "core/dom/Document.h"
22 #include "core/dom/Element.h" 22 #include "core/dom/Element.h"
23 #include "core/frame/UseCounter.h" 23 #include "core/frame/UseCounter.h"
24 #include "wtf/BitArray.h" 24 #include "wtf/BitArray.h"
25 25
26 namespace blink { 26 namespace blink {
27 27
28 CSSParserImpl::CSSParserImpl(const CSSParserContext& context, const String& stri ng, StyleSheetContents* styleSheet) 28 CSSParserImpl::CSSParserImpl(const CSSParserContext& context, StyleSheetContents * styleSheet)
29 : m_context(context) 29 : m_context(context)
30 , m_defaultNamespace(starAtom) 30 , m_defaultNamespace(starAtom)
31 , m_styleSheet(styleSheet) 31 , m_styleSheet(styleSheet)
32 { 32 {
33 CSSTokenizer::tokenize(string, m_tokens);
34 } 33 }
35 34
36 bool CSSParserImpl::parseValue(MutableStylePropertySet* declaration, CSSProperty ID propertyID, const String& string, bool important, const CSSParserContext& con text) 35 bool CSSParserImpl::parseValue(MutableStylePropertySet* declaration, CSSProperty ID propertyID, const String& string, bool important, const CSSParserContext& con text)
37 { 36 {
38 CSSParserImpl parser(context, string); 37 CSSParserImpl parser(context);
39 StyleRule::Type ruleType = StyleRule::Style; 38 StyleRule::Type ruleType = StyleRule::Style;
40 if (declaration->cssParserMode() == CSSViewportRuleMode) 39 if (declaration->cssParserMode() == CSSViewportRuleMode)
41 ruleType = StyleRule::Viewport; 40 ruleType = StyleRule::Viewport;
42 parser.consumeDeclarationValue(CSSParserTokenRange(parser.m_tokens), propert yID, important, ruleType); 41 CSSTokenizer::Scope scope(string);
42 parser.consumeDeclarationValue(scope.tokenRange(), propertyID, important, ru leType);
43 if (parser.m_parsedProperties.isEmpty()) 43 if (parser.m_parsedProperties.isEmpty())
44 return false; 44 return false;
45 declaration->addParsedProperties(parser.m_parsedProperties); 45 declaration->addParsedProperties(parser.m_parsedProperties);
46 return true; 46 return true;
47 } 47 }
48 48
49 static inline void filterProperties(bool important, const WillBeHeapVector<CSSPr operty, 256>& input, WillBeHeapVector<CSSProperty, 256>& output, size_t& unusedE ntries, BitArray<numCSSProperties>& seenProperties) 49 static inline void filterProperties(bool important, const WillBeHeapVector<CSSPr operty, 256>& input, WillBeHeapVector<CSSProperty, 256>& output, size_t& unusedE ntries, BitArray<numCSSProperties>& seenProperties)
50 { 50 {
51 // Add properties in reverse order so that highest priority definitions are reached first. Duplicate definitions can then be ignored when found. 51 // Add properties in reverse order so that highest priority definitions are reached first. Duplicate definitions can then be ignored when found.
52 for (size_t i = input.size(); i--; ) { 52 for (size_t i = input.size(); i--; ) {
(...skipping 19 matching lines...) Expand all
72 72
73 return ImmutableStylePropertySet::create(results.data() + unusedEntries, res ults.size() - unusedEntries, mode); 73 return ImmutableStylePropertySet::create(results.data() + unusedEntries, res ults.size() - unusedEntries, mode);
74 } 74 }
75 75
76 PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> CSSParserImpl::parseInlineStyl eDeclaration(const String& string, Element* element) 76 PassRefPtrWillBeRawPtr<ImmutableStylePropertySet> CSSParserImpl::parseInlineStyl eDeclaration(const String& string, Element* element)
77 { 77 {
78 Document& document = element->document(); 78 Document& document = element->document();
79 CSSParserContext context = CSSParserContext(document.elementSheet().contents ()->parserContext(), UseCounter::getFrom(&document)); 79 CSSParserContext context = CSSParserContext(document.elementSheet().contents ()->parserContext(), UseCounter::getFrom(&document));
80 CSSParserMode mode = element->isHTMLElement() && !document.inQuirksMode() ? HTMLStandardMode : HTMLQuirksMode; 80 CSSParserMode mode = element->isHTMLElement() && !document.inQuirksMode() ? HTMLStandardMode : HTMLQuirksMode;
81 context.setMode(mode); 81 context.setMode(mode);
82 CSSParserImpl parser(context, string); 82 CSSParserImpl parser(context);
83 parser.consumeDeclarationList(CSSParserTokenRange(parser.m_tokens), StyleRul e::Style); 83 CSSTokenizer::Scope scope(string);
84 parser.consumeDeclarationList(scope.tokenRange(), StyleRule::Style);
84 return createStylePropertySet(parser.m_parsedProperties, mode); 85 return createStylePropertySet(parser.m_parsedProperties, mode);
85 } 86 }
86 87
87 bool CSSParserImpl::parseDeclaration(MutableStylePropertySet* declaration, const String& string, const CSSParserContext& context) 88 bool CSSParserImpl::parseDeclaration(MutableStylePropertySet* declaration, const String& string, const CSSParserContext& context)
88 { 89 {
89 CSSParserImpl parser(context, string); 90 CSSParserImpl parser(context);
90 StyleRule::Type ruleType = StyleRule::Style; 91 StyleRule::Type ruleType = StyleRule::Style;
91 if (declaration->cssParserMode() == CSSViewportRuleMode) 92 if (declaration->cssParserMode() == CSSViewportRuleMode)
92 ruleType = StyleRule::Viewport; 93 ruleType = StyleRule::Viewport;
93 parser.consumeDeclarationList(CSSParserTokenRange(parser.m_tokens), ruleType ); 94 CSSTokenizer::Scope scope(string);
95 parser.consumeDeclarationList(scope.tokenRange(), ruleType);
94 if (parser.m_parsedProperties.isEmpty()) 96 if (parser.m_parsedProperties.isEmpty())
95 return false; 97 return false;
96 declaration->addParsedProperties(parser.m_parsedProperties); 98 declaration->addParsedProperties(parser.m_parsedProperties);
97 return true; 99 return true;
98 } 100 }
99 101
100 PassRefPtrWillBeRawPtr<StyleRuleBase> CSSParserImpl::parseRule(const String& str ing, const CSSParserContext& context, AllowedRulesType allowedRules) 102 PassRefPtrWillBeRawPtr<StyleRuleBase> CSSParserImpl::parseRule(const String& str ing, const CSSParserContext& context, AllowedRulesType allowedRules)
101 { 103 {
102 CSSParserImpl parser(context, string); 104 CSSParserImpl parser(context);
103 CSSParserTokenRange range(parser.m_tokens); 105 CSSTokenizer::Scope scope(string);
106 CSSParserTokenRange range = scope.tokenRange();
104 range.consumeWhitespaceAndComments(); 107 range.consumeWhitespaceAndComments();
105 if (range.atEnd()) 108 if (range.atEnd())
106 return nullptr; // Parse error, empty rule 109 return nullptr; // Parse error, empty rule
107 RefPtrWillBeRawPtr<StyleRuleBase> rule; 110 RefPtrWillBeRawPtr<StyleRuleBase> rule;
108 if (range.peek().type() == AtKeywordToken) 111 if (range.peek().type() == AtKeywordToken)
109 rule = parser.consumeAtRule(range, allowedRules); 112 rule = parser.consumeAtRule(range, allowedRules);
110 else 113 else
111 rule = parser.consumeQualifiedRule(range, allowedRules); 114 rule = parser.consumeQualifiedRule(range, allowedRules);
112 if (!rule) 115 if (!rule)
113 return nullptr; // Parse error, failed to consume rule 116 return nullptr; // Parse error, failed to consume rule
114 range.consumeWhitespaceAndComments(); 117 range.consumeWhitespaceAndComments();
115 if (!rule || !range.atEnd()) 118 if (!rule || !range.atEnd())
116 return nullptr; // Parse error, trailing garbage 119 return nullptr; // Parse error, trailing garbage
117 return rule; 120 return rule;
118 } 121 }
119 122
120 void CSSParserImpl::parseStyleSheet(const String& string, const CSSParserContext & context, StyleSheetContents* styleSheet) 123 void CSSParserImpl::parseStyleSheet(const String& string, const CSSParserContext & context, StyleSheetContents* styleSheet)
121 { 124 {
122 CSSParserImpl parser(context, string, styleSheet); 125 CSSParserImpl parser(context, styleSheet);
123 parser.consumeRuleList(parser.m_tokens, TopLevelRuleList, [&styleSheet](Pass RefPtrWillBeRawPtr<StyleRuleBase> rule) { 126 CSSTokenizer::Scope scope(string);
127 parser.consumeRuleList(scope.tokenRange(), TopLevelRuleList, [&styleSheet](P assRefPtrWillBeRawPtr<StyleRuleBase> rule) {
124 styleSheet->parserAppendRule(rule); 128 styleSheet->parserAppendRule(rule);
125 }); 129 });
126 } 130 }
127 131
128 PassOwnPtr<Vector<double>> CSSParserImpl::parseKeyframeKeyList(const String& key List) 132 PassOwnPtr<Vector<double>> CSSParserImpl::parseKeyframeKeyList(const String& key List)
129 { 133 {
130 Vector<CSSParserToken> tokens; 134 return consumeKeyframeKeyList(CSSTokenizer::Scope(keyList).tokenRange());
131 CSSTokenizer::tokenize(keyList, tokens);
132 return consumeKeyframeKeyList(tokens);
133 } 135 }
134 136
135 bool CSSParserImpl::supportsDeclaration(CSSParserTokenRange& range) 137 bool CSSParserImpl::supportsDeclaration(CSSParserTokenRange& range)
136 { 138 {
137 ASSERT(m_parsedProperties.isEmpty()); 139 ASSERT(m_parsedProperties.isEmpty());
138 consumeDeclaration(range, StyleRule::Style); 140 consumeDeclaration(range, StyleRule::Style);
139 bool result = !m_parsedProperties.isEmpty(); 141 bool result = !m_parsedProperties.isEmpty();
140 m_parsedProperties.clear(); 142 m_parsedProperties.clear();
141 return result; 143 return result;
142 } 144 }
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after
559 else 561 else
560 return nullptr; // Parser error, invalid value in keyframe selector 562 return nullptr; // Parser error, invalid value in keyframe selector
561 if (range.atEnd()) 563 if (range.atEnd())
562 return result.release(); 564 return result.release();
563 if (range.consume().type() != CommaToken) 565 if (range.consume().type() != CommaToken)
564 return nullptr; // Parser error 566 return nullptr; // Parser error
565 } 567 }
566 } 568 }
567 569
568 } // namespace blink 570 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/css/parser/CSSParserImpl.h ('k') | Source/core/css/parser/CSSParserValuesTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698