1 | // Copyright 2015 The Chromium Authors. All rights reserved.
|
---|
2 | // Copyright (C) 2016-2021 Apple Inc. All rights reserved.
|
---|
3 | //
|
---|
4 | // Redistribution and use in source and binary forms, with or without
|
---|
5 | // modification, are permitted provided that the following conditions are
|
---|
6 | // met:
|
---|
7 | //
|
---|
8 | // * Redistributions of source code must retain the above copyright
|
---|
9 | // notice, this list of conditions and the following disclaimer.
|
---|
10 | // * Redistributions in binary form must reproduce the above
|
---|
11 | // copyright notice, this list of conditions and the following disclaimer
|
---|
12 | // in the documentation and/or other materials provided with the
|
---|
13 | // distribution.
|
---|
14 | // * Neither the name of Google Inc. nor the names of its
|
---|
15 | // contributors may be used to endorse or promote products derived from
|
---|
16 | // this software without specific prior written permission.
|
---|
17 | //
|
---|
18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
---|
19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
---|
20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
---|
21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
---|
22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
---|
23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
---|
24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
---|
28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
29 |
|
---|
30 | #include "config.h"
|
---|
31 | #include "CSSVariableParser.h"
|
---|
32 |
|
---|
33 | #include "CSSCustomPropertyValue.h"
|
---|
34 | #include "CSSParserContext.h"
|
---|
35 | #include "CSSParserIdioms.h"
|
---|
36 | #include "CSSParserTokenRange.h"
|
---|
37 | #include "CSSPropertyParserHelpers.h"
|
---|
38 |
|
---|
39 | namespace WebCore {
|
---|
40 |
|
---|
41 | bool CSSVariableParser::isValidVariableName(const CSSParserToken& token)
|
---|
42 | {
|
---|
43 | if (token.type() != IdentToken)
|
---|
44 | return false;
|
---|
45 |
|
---|
46 | StringView value = token.value();
|
---|
47 | return value.length() >= 2 && value[0] == '-' && value[1] == '-';
|
---|
48 | }
|
---|
49 |
|
---|
50 | bool CSSVariableParser::isValidVariableName(const String& string)
|
---|
51 | {
|
---|
52 | return string.length() >= 2 && string[0] == '-' && string[1] == '-';
|
---|
53 | }
|
---|
54 |
|
---|
55 | static bool isValidConstantName(const CSSParserToken& token)
|
---|
56 | {
|
---|
57 | return token.type() == IdentToken;
|
---|
58 | }
|
---|
59 |
|
---|
60 | static bool isValidVariableReference(CSSParserTokenRange, const CSSParserContext&);
|
---|
61 | static bool isValidConstantReference(CSSParserTokenRange, const CSSParserContext&);
|
---|
62 |
|
---|
63 | static bool classifyBlock(CSSParserTokenRange range, bool& hasReferences, const CSSParserContext& parserContext, bool isTopLevelBlock = true)
|
---|
64 | {
|
---|
65 | while (!range.atEnd()) {
|
---|
66 | if (range.peek().getBlockType() == CSSParserToken::BlockStart) {
|
---|
67 | const CSSParserToken& token = range.peek();
|
---|
68 | CSSParserTokenRange block = range.consumeBlock();
|
---|
69 | if (token.functionId() == CSSValueVar) {
|
---|
70 | if (!isValidVariableReference(block, parserContext))
|
---|
71 | return false; // Bail if any references are invalid
|
---|
72 | hasReferences = true;
|
---|
73 | continue;
|
---|
74 | }
|
---|
75 | if (token.functionId() == CSSValueEnv && parserContext.constantPropertiesEnabled) {
|
---|
76 | if (!isValidConstantReference(block, parserContext))
|
---|
77 | return false; // Bail if any references are invalid
|
---|
78 | hasReferences = true;
|
---|
79 | continue;
|
---|
80 | }
|
---|
81 | if (!classifyBlock(block, hasReferences, parserContext, false))
|
---|
82 | return false;
|
---|
83 | continue;
|
---|
84 | }
|
---|
85 |
|
---|
86 | ASSERT(range.peek().getBlockType() != CSSParserToken::BlockEnd);
|
---|
87 |
|
---|
88 | const CSSParserToken& token = range.consume();
|
---|
89 | switch (token.type()) {
|
---|
90 | case AtKeywordToken:
|
---|
91 | break;
|
---|
92 | case DelimiterToken: {
|
---|
93 | if (token.delimiter() == '!' && isTopLevelBlock)
|
---|
94 | return false;
|
---|
95 | break;
|
---|
96 | }
|
---|
97 | case RightParenthesisToken:
|
---|
98 | case RightBraceToken:
|
---|
99 | case RightBracketToken:
|
---|
100 | case BadStringToken:
|
---|
101 | case BadUrlToken:
|
---|
102 | return false;
|
---|
103 | case SemicolonToken:
|
---|
104 | if (isTopLevelBlock)
|
---|
105 | return false;
|
---|
106 | break;
|
---|
107 | default:
|
---|
108 | break;
|
---|
109 | }
|
---|
110 | }
|
---|
111 | return true;
|
---|
112 | }
|
---|
113 |
|
---|
114 | bool isValidVariableReference(CSSParserTokenRange range, const CSSParserContext& parserContext)
|
---|
115 | {
|
---|
116 | range.consumeWhitespace();
|
---|
117 | if (!CSSVariableParser::isValidVariableName(range.consumeIncludingWhitespace()))
|
---|
118 | return false;
|
---|
119 | if (range.atEnd())
|
---|
120 | return true;
|
---|
121 |
|
---|
122 | if (!CSSPropertyParserHelpers::consumeCommaIncludingWhitespace(range))
|
---|
123 | return false;
|
---|
124 | if (range.atEnd())
|
---|
125 | return true;
|
---|
126 |
|
---|
127 | bool hasReferences = false;
|
---|
128 | return classifyBlock(range, hasReferences, parserContext);
|
---|
129 | }
|
---|
130 |
|
---|
131 | bool isValidConstantReference(CSSParserTokenRange range, const CSSParserContext& parserContext)
|
---|
132 | {
|
---|
133 | range.consumeWhitespace();
|
---|
134 | if (!isValidConstantName(range.consumeIncludingWhitespace()))
|
---|
135 | return false;
|
---|
136 | if (range.atEnd())
|
---|
137 | return true;
|
---|
138 |
|
---|
139 | if (!CSSPropertyParserHelpers::consumeCommaIncludingWhitespace(range))
|
---|
140 | return false;
|
---|
141 | if (range.atEnd())
|
---|
142 | return true;
|
---|
143 |
|
---|
144 | bool hasReferences = false;
|
---|
145 | return classifyBlock(range, hasReferences, parserContext);
|
---|
146 | }
|
---|
147 |
|
---|
148 | static CSSValueID classifyVariableRange(CSSParserTokenRange range, bool& hasReferences, const CSSParserContext& parserContext)
|
---|
149 | {
|
---|
150 | hasReferences = false;
|
---|
151 |
|
---|
152 | range.consumeWhitespace();
|
---|
153 | if (range.peek().type() == IdentToken) {
|
---|
154 | CSSValueID id = range.consumeIncludingWhitespace().id();
|
---|
155 | if (range.atEnd() && isCSSWideKeyword(id))
|
---|
156 | return id;
|
---|
157 | }
|
---|
158 |
|
---|
159 | if (classifyBlock(range, hasReferences, parserContext))
|
---|
160 | return CSSValueInternalVariableValue;
|
---|
161 | return CSSValueInvalid;
|
---|
162 | }
|
---|
163 |
|
---|
164 | bool CSSVariableParser::containsValidVariableReferences(CSSParserTokenRange range, const CSSParserContext& parserContext)
|
---|
165 | {
|
---|
166 | bool hasReferences;
|
---|
167 | CSSValueID type = classifyVariableRange(range, hasReferences, parserContext);
|
---|
168 | return type == CSSValueInternalVariableValue && hasReferences;
|
---|
169 | }
|
---|
170 |
|
---|
171 | RefPtr<CSSCustomPropertyValue> CSSVariableParser::parseDeclarationValue(const AtomString& variableName, CSSParserTokenRange range, const CSSParserContext& parserContext)
|
---|
172 | {
|
---|
173 | if (range.atEnd())
|
---|
174 | return nullptr;
|
---|
175 |
|
---|
176 | bool hasReferences;
|
---|
177 | CSSValueID type = classifyVariableRange(range, hasReferences, parserContext);
|
---|
178 |
|
---|
179 | if (type == CSSValueInvalid)
|
---|
180 | return nullptr;
|
---|
181 | if (type == CSSValueInternalVariableValue)
|
---|
182 | return CSSCustomPropertyValue::createUnresolved(variableName, CSSVariableReferenceValue::create(range, parserContext));
|
---|
183 | return CSSCustomPropertyValue::createUnresolved(variableName, type);
|
---|
184 | }
|
---|
185 |
|
---|
186 | } // namespace WebCore
|
---|