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