OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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/CSSVariableParser.h" | 5 #include "core/css/parser/CSSVariableParser.h" |
6 | 6 |
7 #include "core/css/CSSCustomPropertyDeclaration.h" | 7 #include "core/css/CSSCustomPropertyDeclaration.h" |
8 #include "core/css/parser/CSSParserTokenRange.h" | 8 #include "core/css/parser/CSSParserTokenRange.h" |
9 #include "core/css/parser/CSSParserValues.h" | 9 #include "core/css/parser/CSSParserValues.h" |
10 | 10 |
11 namespace blink { | 11 namespace blink { |
12 | 12 |
13 bool CSSVariableParser::isValidVariableName(const CSSParserToken& token) | 13 bool CSSVariableParser::isValidVariableName(const CSSParserToken& token) |
14 { | 14 { |
15 if (token.type() != IdentToken) | 15 if (token.type() != IdentToken) |
16 return false; | 16 return false; |
17 | 17 |
18 CSSParserString value = token.value(); | 18 CSSParserString value = token.value(); |
19 return value.length() >= 2 && value[0] == '-' && value[1] == '-'; | 19 return value.length() >= 2 && value[0] == '-' && value[1] == '-'; |
20 } | 20 } |
21 | 21 |
22 bool CSSVariableParser::isValidVariableName(const String& string) | 22 bool CSSVariableParser::isValidVariableName(const String& string) |
23 { | 23 { |
24 return string.length() >= 2 && string[0] == '-' && string[1] == '-'; | 24 return string.length() >= 2 && string[0] == '-' && string[1] == '-'; |
25 } | 25 } |
26 | 26 |
27 bool isValidVariableReference(CSSParserTokenRange); | 27 bool isValidVariableReference(CSSParserTokenRange, bool& hasAtApplyRule); |
28 | 28 |
29 bool classifyBlock(CSSParserTokenRange range, bool& hasReferences, bool isTopLev
elBlock = true) | 29 bool classifyBlock(CSSParserTokenRange range, bool& hasReferences, bool& hasAtAp
plyRule, bool isTopLevelBlock = true) |
30 { | 30 { |
31 while (!range.atEnd()) { | 31 while (!range.atEnd()) { |
32 if (range.peek().getBlockType() == CSSParserToken::BlockStart) { | 32 if (range.peek().getBlockType() == CSSParserToken::BlockStart) { |
33 const CSSParserToken& token = range.peek(); | 33 const CSSParserToken& token = range.peek(); |
34 CSSParserTokenRange block = range.consumeBlock(); | 34 CSSParserTokenRange block = range.consumeBlock(); |
35 if (token.functionId() == CSSValueVar) { | 35 if (token.functionId() == CSSValueVar) { |
36 if (!isValidVariableReference(block)) | 36 if (!isValidVariableReference(block, hasAtApplyRule)) |
37 return false; // Bail if any references are invalid | 37 return false; // Bail if any references are invalid |
38 hasReferences = true; | 38 hasReferences = true; |
39 continue; | 39 continue; |
40 } | 40 } |
41 if (!classifyBlock(block, hasReferences, false)) | 41 if (!classifyBlock(block, hasReferences, hasAtApplyRule, false)) |
42 return false; | 42 return false; |
43 continue; | 43 continue; |
44 } | 44 } |
45 | 45 |
46 ASSERT(range.peek().getBlockType() != CSSParserToken::BlockEnd); | 46 ASSERT(range.peek().getBlockType() != CSSParserToken::BlockEnd); |
47 | 47 |
48 const CSSParserToken& token = range.consume(); | 48 const CSSParserToken& token = range.consume(); |
49 switch (token.type()) { | 49 switch (token.type()) { |
50 case AtKeywordToken: { | 50 case AtKeywordToken: { |
51 if (token.valueEqualsIgnoringASCIICase("apply")) { | 51 if (token.valueEqualsIgnoringASCIICase("apply")) { |
52 range.consumeWhitespace(); | 52 range.consumeWhitespace(); |
53 const CSSParserToken& variableName = range.consumeIncludingWhite
space(); | 53 const CSSParserToken& variableName = range.consumeIncludingWhite
space(); |
54 if (!CSSVariableParser::isValidVariableName(variableName) | 54 if (!CSSVariableParser::isValidVariableName(variableName) |
55 || !(range.atEnd() || range.peek().type() == SemicolonToken
|| range.peek().type() == RightBraceToken)) | 55 || !(range.atEnd() || range.peek().type() == SemicolonToken
|| range.peek().type() == RightBraceToken)) |
56 return false; | 56 return false; |
57 hasReferences = true; | 57 hasAtApplyRule = true; |
58 } | 58 } |
59 break; | 59 break; |
60 } | 60 } |
61 case DelimiterToken: { | 61 case DelimiterToken: { |
62 if (token.delimiter() == '!' && isTopLevelBlock) | 62 if (token.delimiter() == '!' && isTopLevelBlock) |
63 return false; | 63 return false; |
64 break; | 64 break; |
65 } | 65 } |
66 case RightParenthesisToken: | 66 case RightParenthesisToken: |
67 case RightBraceToken: | 67 case RightBraceToken: |
68 case RightBracketToken: | 68 case RightBracketToken: |
69 case BadStringToken: | 69 case BadStringToken: |
70 case BadUrlToken: | 70 case BadUrlToken: |
71 return false; | 71 return false; |
72 case SemicolonToken: | 72 case SemicolonToken: |
73 if (isTopLevelBlock) | 73 if (isTopLevelBlock) |
74 return false; | 74 return false; |
75 break; | 75 break; |
76 default: | 76 default: |
77 break; | 77 break; |
78 } | 78 } |
79 } | 79 } |
80 return true; | 80 return true; |
81 } | 81 } |
82 | 82 |
83 bool isValidVariableReference(CSSParserTokenRange range) | 83 bool isValidVariableReference(CSSParserTokenRange range, bool& hasAtApplyRule) |
84 { | 84 { |
85 range.consumeWhitespace(); | 85 range.consumeWhitespace(); |
86 if (!CSSVariableParser::isValidVariableName(range.consumeIncludingWhitespace
())) | 86 if (!CSSVariableParser::isValidVariableName(range.consumeIncludingWhitespace
())) |
87 return false; | 87 return false; |
88 if (range.atEnd()) | 88 if (range.atEnd()) |
89 return true; | 89 return true; |
90 | 90 |
91 if (range.consume().type() != CommaToken) | 91 if (range.consume().type() != CommaToken) |
92 return false; | 92 return false; |
93 if (range.atEnd()) | 93 if (range.atEnd()) |
94 return false; | 94 return false; |
95 | 95 |
96 bool hasReferences = false; | 96 bool hasReferences = false; |
97 return classifyBlock(range, hasReferences); | 97 return classifyBlock(range, hasReferences, hasAtApplyRule); |
98 } | 98 } |
99 | 99 |
100 static CSSValueID classifyVariableRange(CSSParserTokenRange range, bool& hasRefe
rences) | 100 static CSSValueID classifyVariableRange(CSSParserTokenRange range, bool& hasRefe
rences, bool& hasAtApplyRule) |
101 { | 101 { |
102 hasReferences = false; | 102 hasReferences = false; |
| 103 hasAtApplyRule = false; |
103 | 104 |
104 range.consumeWhitespace(); | 105 range.consumeWhitespace(); |
105 if (range.peek().type() == IdentToken) { | 106 if (range.peek().type() == IdentToken) { |
106 CSSValueID id = range.consumeIncludingWhitespace().id(); | 107 CSSValueID id = range.consumeIncludingWhitespace().id(); |
107 if (range.atEnd() && (id == CSSValueInherit || id == CSSValueInitial ||
id == CSSValueUnset)) | 108 if (range.atEnd() && (id == CSSValueInherit || id == CSSValueInitial ||
id == CSSValueUnset)) |
108 return id; | 109 return id; |
109 } | 110 } |
110 | 111 |
111 if (classifyBlock(range, hasReferences)) | 112 if (classifyBlock(range, hasReferences, hasAtApplyRule)) |
112 return CSSValueInternalVariableValue; | 113 return CSSValueInternalVariableValue; |
113 return CSSValueInvalid; | 114 return CSSValueInvalid; |
114 } | 115 } |
115 | 116 |
116 bool CSSVariableParser::containsValidVariableReferences(CSSParserTokenRange rang
e) | 117 bool CSSVariableParser::containsValidVariableReferences(CSSParserTokenRange rang
e) |
117 { | 118 { |
118 bool hasReferences; | 119 bool hasReferences; |
119 CSSValueID type = classifyVariableRange(range, hasReferences); | 120 bool hasAtApplyRule; |
120 return type == CSSValueInternalVariableValue && hasReferences; | 121 CSSValueID type = classifyVariableRange(range, hasReferences, hasAtApplyRule
); |
| 122 return type == CSSValueInternalVariableValue && hasReferences && !hasAtApply
Rule; |
121 } | 123 } |
122 | 124 |
123 PassRefPtrWillBeRawPtr<CSSCustomPropertyDeclaration> CSSVariableParser::parseDec
larationValue(const AtomicString& variableName, CSSParserTokenRange range) | 125 PassRefPtrWillBeRawPtr<CSSCustomPropertyDeclaration> CSSVariableParser::parseDec
larationValue(const AtomicString& variableName, CSSParserTokenRange range) |
124 { | 126 { |
125 if (range.atEnd()) | 127 if (range.atEnd()) |
126 return nullptr; | 128 return nullptr; |
127 | 129 |
128 bool hasReferences; | 130 bool hasReferences; |
129 CSSValueID type = classifyVariableRange(range, hasReferences); | 131 bool hasAtApplyRule; |
| 132 CSSValueID type = classifyVariableRange(range, hasReferences, hasAtApplyRule
); |
130 | 133 |
131 if (type == CSSValueInvalid) | 134 if (type == CSSValueInvalid) |
132 return nullptr; | 135 return nullptr; |
133 if (type == CSSValueInternalVariableValue) | 136 if (type == CSSValueInternalVariableValue) |
134 return CSSCustomPropertyDeclaration::create(variableName, CSSVariableDat
a::create(range, hasReferences)); | 137 return CSSCustomPropertyDeclaration::create(variableName, CSSVariableDat
a::create(range, hasReferences || hasAtApplyRule)); |
135 return CSSCustomPropertyDeclaration::create(variableName, type); | 138 return CSSCustomPropertyDeclaration::create(variableName, type); |
136 } | 139 } |
137 | 140 |
138 } // namespace blink | 141 } // namespace blink |
OLD | NEW |