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/resolver/CSSVariableResolver.h" | 5 #include "core/css/resolver/CSSVariableResolver.h" |
6 | 6 |
7 #include "core/CSSPropertyNames.h" | 7 #include "core/CSSPropertyNames.h" |
8 #include "core/CSSValueKeywords.h" | 8 #include "core/CSSValueKeywords.h" |
9 #include "core/StyleBuilderFunctions.h" | 9 #include "core/StyleBuilderFunctions.h" |
10 #include "core/StylePropertyShorthand.h" | 10 #include "core/StylePropertyShorthand.h" |
11 #include "core/css/CSSValuePool.h" | 11 #include "core/css/CSSValuePool.h" |
12 #include "core/css/CSSVariableData.h" | 12 #include "core/css/CSSVariableData.h" |
13 #include "core/css/CSSVariableReferenceValue.h" | 13 #include "core/css/CSSVariableReferenceValue.h" |
14 #include "core/css/parser/CSSParserToken.h" | 14 #include "core/css/parser/CSSParserToken.h" |
15 #include "core/css/parser/CSSParserTokenRange.h" | 15 #include "core/css/parser/CSSParserTokenRange.h" |
16 #include "core/css/parser/CSSParserValues.h" | 16 #include "core/css/parser/CSSParserValues.h" |
17 #include "core/css/parser/CSSPropertyParser.h" | 17 #include "core/css/parser/CSSPropertyParser.h" |
18 #include "core/css/resolver/StyleBuilder.h" | 18 #include "core/css/resolver/StyleBuilder.h" |
19 #include "core/css/resolver/StyleResolverState.h" | 19 #include "core/css/resolver/StyleResolverState.h" |
20 #include "core/style/StyleVariableData.h" | 20 #include "core/style/StyleVariableData.h" |
21 #include "wtf/Vector.h" | 21 #include "wtf/Vector.h" |
22 | 22 |
23 namespace blink { | 23 namespace blink { |
24 | 24 |
25 void CSSVariableResolver::resolveFallback(CSSParserTokenRange range, | 25 bool CSSVariableResolver::resolveFallback(CSSParserTokenRange range, Vector<CSSP
arserToken>& result) |
26 Vector<CSSParserToken>& result, CSSVariableResolver::ResolutionState& contex
t) | |
27 { | 26 { |
28 if (!range.atEnd()) { | 27 if (range.atEnd()) |
29 range.consume(); | 28 return false; |
30 resolveVariableReferencesFromTokens(range, result, context); | 29 ASSERT(range.peek().type() == CommaToken); |
31 } else { | 30 range.consume(); |
32 context.success = false; | 31 return resolveVariableReferencesFromTokens(range, result); |
33 } | |
34 } | 32 } |
35 | 33 |
36 void CSSVariableResolver::resolveVariableTokensRecursive(CSSParserTokenRange ran
ge, | 34 bool CSSVariableResolver::resolveVariableTokensRecursive(CSSParserTokenRange ran
ge, |
37 Vector<CSSParserToken>& result, CSSVariableResolver::ResolutionState& contex
t) | 35 Vector<CSSParserToken>& result) |
38 { | 36 { |
39 Vector<CSSParserToken> trash; | 37 Vector<CSSParserToken> trash; |
40 range.consumeWhitespace(); | 38 range.consumeWhitespace(); |
41 ASSERT(range.peek().type() == IdentToken); | 39 ASSERT(range.peek().type() == IdentToken); |
42 AtomicString variableName = range.consumeIncludingWhitespace().value(); | 40 AtomicString variableName = range.consumeIncludingWhitespace().value(); |
43 ASSERT(range.atEnd() || (range.peek().type() == CommaToken)); | 41 ASSERT(range.atEnd() || (range.peek().type() == CommaToken)); |
44 | 42 |
45 // Cycle detection. | 43 // Cycle detection. |
46 if (m_variablesSeen.contains(variableName)) { | 44 if (m_variablesSeen.contains(variableName)) { |
47 context.success = false; | 45 m_cycleStartPoints.add(variableName); |
48 context.cycleStartPoints.add(variableName); | 46 resolveFallback(range, trash); |
49 resolveFallback(range, trash, context); | 47 return false; |
50 return; | |
51 } | 48 } |
52 | 49 |
53 CSSVariableData* variableData = m_styleVariableData ? m_styleVariableData->g
etVariable(variableName) : nullptr; | 50 CSSVariableData* variableData = m_styleVariableData ? m_styleVariableData->g
etVariable(variableName) : nullptr; |
54 if (variableData) { | 51 if (variableData) { |
55 Vector<CSSParserToken> tokens; | 52 Vector<CSSParserToken> tokens; |
56 if (variableData->needsVariableResolution()) { | 53 if (variableData->needsVariableResolution()) { |
57 m_variablesSeen.add(variableName); | 54 m_variablesSeen.add(variableName); |
58 resolveVariableReferencesFromTokens(variableData->tokens(), tokens,
context); | 55 bool referenceValid = resolveVariableReferencesFromTokens(variableDa
ta->tokens(), tokens); |
59 m_variablesSeen.remove(variableName); | 56 m_variablesSeen.remove(variableName); |
60 | 57 |
61 // The old variable data holds onto the backing string the new resol
ved CSSVariableData | 58 // The old variable data holds onto the backing string the new resol
ved CSSVariableData |
62 // relies on. Ensure it will live beyond us overwriting the RefPtr i
n StyleVariableData. | 59 // relies on. Ensure it will live beyond us overwriting the RefPtr i
n StyleVariableData. |
63 ASSERT(variableData->refCount() > 1); | 60 ASSERT(variableData->refCount() > 1); |
64 | 61 |
| 62 if (!referenceValid || !m_cycleStartPoints.isEmpty()) { |
| 63 m_styleVariableData->setVariable(variableName, nullptr); |
| 64 m_cycleStartPoints.remove(variableName); |
| 65 if (!m_cycleStartPoints.isEmpty()) { |
| 66 resolveFallback(range, trash); |
| 67 return false; |
| 68 } |
| 69 return resolveFallback(range, result); |
| 70 } |
| 71 |
65 m_styleVariableData->setVariable(variableName, CSSVariableData::crea
teResolved(tokens, variableData)); | 72 m_styleVariableData->setVariable(variableName, CSSVariableData::crea
teResolved(tokens, variableData)); |
66 if (!context.cycleStartPoints.isEmpty()) { | |
67 if (context.cycleStartPoints.contains(variableName)) | |
68 context.cycleStartPoints.remove(variableName); | |
69 | |
70 if (!context.cycleStartPoints.isEmpty()) { | |
71 resolveFallback(range, trash, context); | |
72 return; | |
73 } | |
74 } | |
75 } else { | 73 } else { |
76 tokens = variableData->tokens(); | 74 tokens = variableData->tokens(); |
77 } | 75 } |
78 | 76 |
79 if (!tokens.isEmpty()) { | 77 ASSERT(!tokens.isEmpty()); |
80 // Check that loops are not induced by the fallback. | 78 // Check that loops are not induced by the fallback. |
81 resolveFallback(range, trash, context); | 79 resolveFallback(range, trash); |
82 if (context.cycleStartPoints.isEmpty()) { | 80 if (m_cycleStartPoints.isEmpty()) { |
83 // It's OK if the fallback fails to resolve - we're not actually
taking it. | 81 // It's OK if the fallback fails to resolve - we're not actually tak
ing it. |
84 context.success = true; | 82 result.appendVector(tokens); |
85 result.appendVector(tokens); | 83 return true; |
86 } | 84 } |
87 return; | 85 return false; |
| 86 } |
| 87 |
| 88 return resolveFallback(range, result); |
| 89 } |
| 90 |
| 91 bool CSSVariableResolver::resolveVariableReferencesFromTokens(CSSParserTokenRang
e range, |
| 92 Vector<CSSParserToken>& result) |
| 93 { |
| 94 bool success = true; |
| 95 while (!range.atEnd()) { |
| 96 if (range.peek().functionId() == CSSValueVar) { |
| 97 success &= resolveVariableTokensRecursive(range.consumeBlock(), resu
lt); |
| 98 } else { |
| 99 result.append(range.consume()); |
88 } | 100 } |
89 } | 101 } |
90 | 102 return success; |
91 // We're legitimately falling back, so reset success flag. | |
92 context.success = true; | |
93 resolveFallback(range, result, context); | |
94 } | |
95 | |
96 void CSSVariableResolver::resolveVariableReferencesFromTokens(CSSParserTokenRang
e range, | |
97 Vector<CSSParserToken>& result, CSSVariableResolver::ResolutionState& contex
t) | |
98 { | |
99 while (!range.atEnd()) { | |
100 if (range.peek().functionId() != CSSValueVar) { | |
101 result.append(range.consume()); | |
102 } else { | |
103 resolveVariableTokensRecursive(range.consumeBlock(), result, context
); | |
104 } | |
105 } | |
106 if (!context.success) | |
107 result.clear(); | |
108 return; | |
109 } | 103 } |
110 | 104 |
111 PassRefPtrWillBeRawPtr<CSSValue> CSSVariableResolver::resolveVariableReferences(
StyleVariableData* styleVariableData, CSSPropertyID id, const CSSVariableReferen
ceValue& value) | 105 PassRefPtrWillBeRawPtr<CSSValue> CSSVariableResolver::resolveVariableReferences(
StyleVariableData* styleVariableData, CSSPropertyID id, const CSSVariableReferen
ceValue& value) |
112 { | 106 { |
113 ASSERT(!isShorthandProperty(id)); | 107 ASSERT(!isShorthandProperty(id)); |
114 | 108 |
115 CSSVariableResolver resolver(styleVariableData); | 109 CSSVariableResolver resolver(styleVariableData); |
116 Vector<CSSParserToken> tokens; | 110 Vector<CSSParserToken> tokens; |
117 ResolutionState resolutionContext; | 111 if (!resolver.resolveVariableReferencesFromTokens(value.variableDataValue()-
>tokens(), tokens)) |
118 resolver.resolveVariableReferencesFromTokens(value.variableDataValue()->toke
ns(), tokens, resolutionContext); | |
119 | |
120 if (!resolutionContext.success) | |
121 return cssValuePool().createUnsetValue(); | 112 return cssValuePool().createUnsetValue(); |
122 | 113 |
123 CSSParserContext context(HTMLStandardMode, nullptr); | 114 CSSParserContext context(HTMLStandardMode, nullptr); |
124 WillBeHeapVector<CSSProperty, 256> parsedProperties; | 115 WillBeHeapVector<CSSProperty, 256> parsedProperties; |
125 // TODO(timloh): This should be CSSParser::parseSingleValue and not need a v
ector. | 116 // TODO(timloh): This should be CSSParser::parseSingleValue and not need a v
ector. |
126 if (!CSSPropertyParser::parseValue(id, false, CSSParserTokenRange(tokens), c
ontext, parsedProperties, StyleRule::Type::Style)) | 117 if (!CSSPropertyParser::parseValue(id, false, CSSParserTokenRange(tokens), c
ontext, parsedProperties, StyleRule::Type::Style)) |
127 return cssValuePool().createUnsetValue(); | 118 return cssValuePool().createUnsetValue(); |
128 ASSERT(parsedProperties.size() == 1); | 119 ASSERT(parsedProperties.size() == 1); |
129 return parsedProperties[0].value(); | 120 return parsedProperties[0].value(); |
130 } | 121 } |
131 | 122 |
132 void CSSVariableResolver::resolveAndApplyVariableReferences(StyleResolverState&
state, CSSPropertyID id, const CSSVariableReferenceValue& value) | 123 void CSSVariableResolver::resolveAndApplyVariableReferences(StyleResolverState&
state, CSSPropertyID id, const CSSVariableReferenceValue& value) |
133 { | 124 { |
134 | 125 |
135 // TODO(leviw): This should be a stack | 126 // TODO(leviw): This should be a stack |
136 CSSVariableResolver resolver(state.style()->variables()); | 127 CSSVariableResolver resolver(state.style()->variables()); |
137 | 128 |
138 Vector<CSSParserToken> tokens; | 129 Vector<CSSParserToken> tokens; |
139 ResolutionState resolutionContext; | 130 if (resolver.resolveVariableReferencesFromTokens(value.variableDataValue()->
tokens(), tokens)) { |
140 resolver.resolveVariableReferencesFromTokens(value.variableDataValue()->toke
ns(), tokens, resolutionContext); | |
141 | |
142 if (resolutionContext.success) { | |
143 CSSParserContext context(HTMLStandardMode, 0); | 131 CSSParserContext context(HTMLStandardMode, 0); |
144 | 132 |
145 WillBeHeapVector<CSSProperty, 256> parsedProperties; | 133 WillBeHeapVector<CSSProperty, 256> parsedProperties; |
146 | 134 |
147 if (CSSPropertyParser::parseValue(id, false, CSSParserTokenRange(tokens)
, context, parsedProperties, StyleRule::Type::Style)) { | 135 if (CSSPropertyParser::parseValue(id, false, CSSParserTokenRange(tokens)
, context, parsedProperties, StyleRule::Type::Style)) { |
148 unsigned parsedPropertiesCount = parsedProperties.size(); | 136 unsigned parsedPropertiesCount = parsedProperties.size(); |
149 for (unsigned i = 0; i < parsedPropertiesCount; ++i) | 137 for (unsigned i = 0; i < parsedPropertiesCount; ++i) |
150 StyleBuilder::applyProperty(parsedProperties[i].id(), state, par
sedProperties[i].value()); | 138 StyleBuilder::applyProperty(parsedProperties[i].id(), state, par
sedProperties[i].value()); |
151 return; | 139 return; |
152 } | 140 } |
153 } | 141 } |
154 | 142 |
155 RefPtrWillBeRawPtr<CSSUnsetValue> unset = cssValuePool().createUnsetValue(); | 143 RefPtrWillBeRawPtr<CSSUnsetValue> unset = cssValuePool().createUnsetValue(); |
156 if (isShorthandProperty(id)) { | 144 if (isShorthandProperty(id)) { |
157 StylePropertyShorthand shorthand = shorthandForProperty(id); | 145 StylePropertyShorthand shorthand = shorthandForProperty(id); |
158 for (unsigned i = 0; i < shorthand.length(); i++) | 146 for (unsigned i = 0; i < shorthand.length(); i++) |
159 StyleBuilder::applyProperty(shorthand.properties()[i], state, unset.
get()); | 147 StyleBuilder::applyProperty(shorthand.properties()[i], state, unset.
get()); |
160 return; | 148 return; |
161 } | 149 } |
162 | 150 |
163 StyleBuilder::applyProperty(id, state, unset.get()); | 151 StyleBuilder::applyProperty(id, state, unset.get()); |
164 } | 152 } |
165 | 153 |
166 void CSSVariableResolver::resolveVariableDefinitions(StyleVariableData* variable
s) | 154 void CSSVariableResolver::resolveVariableDefinitions(StyleVariableData* variable
s) |
167 { | 155 { |
168 if (!variables) | 156 if (!variables) |
169 return; | 157 return; |
170 | 158 |
171 for (auto& variable : variables->m_data) { | 159 for (auto& variable : variables->m_data) { |
172 if (!variable.value->needsVariableResolution()) | 160 if (!variable.value || !variable.value->needsVariableResolution()) |
173 continue; | 161 continue; |
174 Vector<CSSParserToken> resolvedTokens; | 162 Vector<CSSParserToken> resolvedTokens; |
175 | 163 |
176 CSSVariableResolver resolver(variables, variable.key); | 164 CSSVariableResolver resolver(variables, variable.key); |
177 ResolutionState context; | 165 if (resolver.resolveVariableReferencesFromTokens(variable.value->tokens(
), resolvedTokens)) |
178 resolver.resolveVariableReferencesFromTokens(variable.value->tokens(), r
esolvedTokens, context); | 166 variable.value = CSSVariableData::createResolved(resolvedTokens, var
iable.value); |
179 | 167 else |
180 variable.value = CSSVariableData::createResolved(resolvedTokens, variabl
e.value); | 168 variable.value = nullptr; |
181 } | 169 } |
182 } | 170 } |
183 | 171 |
184 CSSVariableResolver::CSSVariableResolver(StyleVariableData* styleVariableData) | 172 CSSVariableResolver::CSSVariableResolver(StyleVariableData* styleVariableData) |
185 : m_styleVariableData(styleVariableData) | 173 : m_styleVariableData(styleVariableData) |
186 { | 174 { |
187 } | 175 } |
188 | 176 |
189 CSSVariableResolver::CSSVariableResolver(StyleVariableData* styleVariableData, A
tomicString& variable) | 177 CSSVariableResolver::CSSVariableResolver(StyleVariableData* styleVariableData, A
tomicString& variable) |
190 : m_styleVariableData(styleVariableData) | 178 : m_styleVariableData(styleVariableData) |
191 { | 179 { |
192 m_variablesSeen.add(variable); | 180 m_variablesSeen.add(variable); |
193 } | 181 } |
194 | 182 |
195 } // namespace blink | 183 } // namespace blink |
OLD | NEW |