OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "config.h" | |
6 #include "core/css/parser/CSSVariableParser.h" | |
7 | |
8 #include "core/css/CSSCustomPropertyDeclaration.h" | |
9 #include "core/css/parser/CSSParserTokenRange.h" | |
10 #include "core/css/parser/CSSParserValues.h" | |
11 | |
12 namespace blink { | |
13 | |
14 bool CSSVariableParser::isValidVariableName(const CSSParserToken& token) | |
15 { | |
16 if (token.type() != IdentToken) | |
17 return false; | |
18 | |
19 CSSParserString value = token.value(); | |
20 return value.length() >= 2 && value[0] == '-' && value[1] == '-'; | |
21 } | |
22 | |
23 bool isValidVariableReference(CSSParserTokenRange); | |
24 | |
25 bool classifyBlock(CSSParserTokenRange range, bool& hasReferences, bool isTopLev elBlock = true) | |
26 { | |
27 while (!range.atEnd()) { | |
28 if (range.peek().blockType() == CSSParserToken::BlockStart) { | |
29 const CSSParserToken& token = range.peek(); | |
30 CSSParserTokenRange block = range.consumeBlock(); | |
31 if (token.functionId() == CSSValueVar) { | |
32 if (!isValidVariableReference(block)) | |
33 return false; // Bail if any references are invalid | |
34 hasReferences = true; | |
35 continue; | |
36 } | |
37 if (!classifyBlock(block, hasReferences, false)) | |
38 return false; | |
39 continue; | |
40 } | |
41 | |
42 ASSERT(range.peek().blockType() != CSSParserToken::BlockEnd); | |
43 | |
44 const CSSParserToken& token = range.consume(); | |
45 switch (token.type()) { | |
46 case DelimiterToken: { | |
47 if (token.delimiter() == '!' && isTopLevelBlock) | |
48 return false; | |
49 break; | |
50 } | |
51 case RightParenthesisToken: | |
52 case RightBraceToken: | |
53 case RightBracketToken: | |
54 case BadStringToken: | |
55 case BadUrlToken: | |
56 return false; | |
57 case WhitespaceToken: | |
58 break; | |
Timothy Loh
2015/10/28 05:12:41
Same as the default case, no need to have this
leviw_travelin_and_unemployed
2015/10/30 21:41:16
Done.
| |
59 case SemicolonToken: | |
60 if (isTopLevelBlock) | |
61 return false; | |
62 break; | |
63 default: | |
64 break; | |
65 } | |
66 } | |
67 return true; | |
68 } | |
69 | |
70 bool isValidVariableReference(CSSParserTokenRange range) | |
71 { | |
72 range.consumeWhitespace(); | |
73 if (!CSSVariableParser::isValidVariableName(range.consumeIncludingWhitespace ())) | |
74 return false; | |
75 if (range.atEnd()) | |
76 return true; | |
77 | |
78 if (range.consume().type() != CommaToken) | |
79 return false; | |
80 if (range.atEnd()) | |
81 return false; | |
82 | |
83 bool hasReferences = false; | |
84 return classifyBlock(range, hasReferences); | |
85 } | |
86 | |
87 static CSSValueID classifyVariableRange(CSSParserTokenRange range, bool& hasRefe rences) | |
88 { | |
89 hasReferences = false; | |
90 | |
91 range.consumeWhitespace(); | |
92 if (range.peek().type() == IdentToken) { | |
93 CSSValueID id = range.consumeIncludingWhitespace().id(); | |
94 if (range.atEnd() && (id == CSSValueInherit || id == CSSValueInitial || id == CSSValueUnset)) | |
95 return id; | |
96 } | |
97 | |
98 if (classifyBlock(range, hasReferences)) | |
99 return CSSValueInternalVariableValue; | |
100 return CSSValueInvalid; | |
101 } | |
102 | |
103 bool CSSVariableParser::containsValidVariableReferences(CSSParserTokenRange rang e) | |
104 { | |
105 bool hasReferences; | |
106 CSSValueID type = classifyVariableRange(range, hasReferences); | |
107 return type == CSSValueInternalVariableValue && hasReferences; | |
108 } | |
109 | |
110 PassRefPtrWillBeRawPtr<CSSCustomPropertyDeclaration> CSSVariableParser::parseDec larationValue(const AtomicString& variableName, CSSParserTokenRange range) | |
111 { | |
112 if (range.atEnd()) | |
113 return nullptr; | |
114 | |
115 bool hasReferences; | |
116 CSSValueID type = classifyVariableRange(range, hasReferences); | |
117 | |
118 if (type == CSSValueInvalid) | |
119 return nullptr; | |
120 if (type == CSSValueInternalVariableValue) | |
121 return CSSCustomPropertyDeclaration::create(variableName, CSSVariableDat a::create(range, hasReferences)); | |
122 return CSSCustomPropertyDeclaration::create(variableName, type); | |
123 } | |
124 | |
125 } // namespace blink | |
OLD | NEW |