Chromium Code Reviews| 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 m_cycleStartPoints.remove(variableName); |
|
Timothy Loh
2016/02/16 07:26:02
I un-added this line, it seems to not actually be
| |
| 86 return false; | |
| 87 } | |
| 88 | |
| 89 return resolveFallback(range, result); | |
| 90 } | |
| 91 | |
| 92 bool CSSVariableResolver::resolveVariableReferencesFromTokens(CSSParserTokenRang e range, | |
| 93 Vector<CSSParserToken>& result) | |
| 94 { | |
| 95 bool success = true; | |
| 96 while (!range.atEnd()) { | |
| 97 if (range.peek().functionId() == CSSValueVar) { | |
| 98 success &= resolveVariableTokensRecursive(range.consumeBlock(), resu lt); | |
| 99 } else { | |
| 100 result.append(range.consume()); | |
| 88 } | 101 } |
| 89 } | 102 } |
| 90 | 103 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 } | 104 } |
| 110 | 105 |
| 111 PassRefPtrWillBeRawPtr<CSSValue> CSSVariableResolver::resolveVariableReferences( StyleVariableData* styleVariableData, CSSPropertyID id, const CSSVariableReferen ceValue& value) | 106 PassRefPtrWillBeRawPtr<CSSValue> CSSVariableResolver::resolveVariableReferences( StyleVariableData* styleVariableData, CSSPropertyID id, const CSSVariableReferen ceValue& value) |
| 112 { | 107 { |
| 113 ASSERT(!isShorthandProperty(id)); | 108 ASSERT(!isShorthandProperty(id)); |
| 114 | 109 |
| 115 CSSVariableResolver resolver(styleVariableData); | 110 CSSVariableResolver resolver(styleVariableData); |
| 116 Vector<CSSParserToken> tokens; | 111 Vector<CSSParserToken> tokens; |
| 117 ResolutionState resolutionContext; | 112 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(); | 113 return cssValuePool().createUnsetValue(); |
| 122 | 114 |
| 123 CSSParserContext context(HTMLStandardMode, nullptr); | 115 CSSParserContext context(HTMLStandardMode, nullptr); |
| 124 WillBeHeapVector<CSSProperty, 256> parsedProperties; | 116 WillBeHeapVector<CSSProperty, 256> parsedProperties; |
| 125 // TODO(timloh): This should be CSSParser::parseSingleValue and not need a v ector. | 117 // 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)) | 118 if (!CSSPropertyParser::parseValue(id, false, CSSParserTokenRange(tokens), c ontext, parsedProperties, StyleRule::Type::Style)) |
| 127 return cssValuePool().createUnsetValue(); | 119 return cssValuePool().createUnsetValue(); |
| 128 ASSERT(parsedProperties.size() == 1); | 120 ASSERT(parsedProperties.size() == 1); |
| 129 return parsedProperties[0].value(); | 121 return parsedProperties[0].value(); |
| 130 } | 122 } |
| 131 | 123 |
| 132 void CSSVariableResolver::resolveAndApplyVariableReferences(StyleResolverState& state, CSSPropertyID id, const CSSVariableReferenceValue& value) | 124 void CSSVariableResolver::resolveAndApplyVariableReferences(StyleResolverState& state, CSSPropertyID id, const CSSVariableReferenceValue& value) |
| 133 { | 125 { |
| 134 | 126 |
| 135 // TODO(leviw): This should be a stack | 127 // TODO(leviw): This should be a stack |
| 136 CSSVariableResolver resolver(state.style()->variables()); | 128 CSSVariableResolver resolver(state.style()->variables()); |
| 137 | 129 |
| 138 Vector<CSSParserToken> tokens; | 130 Vector<CSSParserToken> tokens; |
| 139 ResolutionState resolutionContext; | 131 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); | 132 CSSParserContext context(HTMLStandardMode, 0); |
| 144 | 133 |
| 145 WillBeHeapVector<CSSProperty, 256> parsedProperties; | 134 WillBeHeapVector<CSSProperty, 256> parsedProperties; |
| 146 | 135 |
| 147 if (CSSPropertyParser::parseValue(id, false, CSSParserTokenRange(tokens) , context, parsedProperties, StyleRule::Type::Style)) { | 136 if (CSSPropertyParser::parseValue(id, false, CSSParserTokenRange(tokens) , context, parsedProperties, StyleRule::Type::Style)) { |
| 148 unsigned parsedPropertiesCount = parsedProperties.size(); | 137 unsigned parsedPropertiesCount = parsedProperties.size(); |
| 149 for (unsigned i = 0; i < parsedPropertiesCount; ++i) | 138 for (unsigned i = 0; i < parsedPropertiesCount; ++i) |
| 150 StyleBuilder::applyProperty(parsedProperties[i].id(), state, par sedProperties[i].value()); | 139 StyleBuilder::applyProperty(parsedProperties[i].id(), state, par sedProperties[i].value()); |
| 151 return; | 140 return; |
| 152 } | 141 } |
| 153 } | 142 } |
| 154 | 143 |
| 155 RefPtrWillBeRawPtr<CSSUnsetValue> unset = cssValuePool().createUnsetValue(); | 144 RefPtrWillBeRawPtr<CSSUnsetValue> unset = cssValuePool().createUnsetValue(); |
| 156 if (isShorthandProperty(id)) { | 145 if (isShorthandProperty(id)) { |
| 157 StylePropertyShorthand shorthand = shorthandForProperty(id); | 146 StylePropertyShorthand shorthand = shorthandForProperty(id); |
| 158 for (unsigned i = 0; i < shorthand.length(); i++) | 147 for (unsigned i = 0; i < shorthand.length(); i++) |
| 159 StyleBuilder::applyProperty(shorthand.properties()[i], state, unset. get()); | 148 StyleBuilder::applyProperty(shorthand.properties()[i], state, unset. get()); |
| 160 return; | 149 return; |
| 161 } | 150 } |
| 162 | 151 |
| 163 StyleBuilder::applyProperty(id, state, unset.get()); | 152 StyleBuilder::applyProperty(id, state, unset.get()); |
| 164 } | 153 } |
| 165 | 154 |
| 166 void CSSVariableResolver::resolveVariableDefinitions(StyleVariableData* variable s) | 155 void CSSVariableResolver::resolveVariableDefinitions(StyleVariableData* variable s) |
| 167 { | 156 { |
| 168 if (!variables) | 157 if (!variables) |
| 169 return; | 158 return; |
| 170 | 159 |
| 171 for (auto& variable : variables->m_data) { | 160 for (auto& variable : variables->m_data) { |
| 172 if (!variable.value->needsVariableResolution()) | 161 if (!variable.value || !variable.value->needsVariableResolution()) |
| 173 continue; | 162 continue; |
| 174 Vector<CSSParserToken> resolvedTokens; | 163 Vector<CSSParserToken> resolvedTokens; |
| 175 | 164 |
| 176 CSSVariableResolver resolver(variables, variable.key); | 165 CSSVariableResolver resolver(variables, variable.key); |
| 177 ResolutionState context; | 166 if (resolver.resolveVariableReferencesFromTokens(variable.value->tokens( ), resolvedTokens)) |
| 178 resolver.resolveVariableReferencesFromTokens(variable.value->tokens(), r esolvedTokens, context); | 167 variable.value = CSSVariableData::createResolved(resolvedTokens, var iable.value); |
| 179 | 168 else |
| 180 variable.value = CSSVariableData::createResolved(resolvedTokens, variabl e.value); | 169 variable.value = nullptr; |
| 181 } | 170 } |
| 182 } | 171 } |
| 183 | 172 |
| 184 CSSVariableResolver::CSSVariableResolver(StyleVariableData* styleVariableData) | 173 CSSVariableResolver::CSSVariableResolver(StyleVariableData* styleVariableData) |
| 185 : m_styleVariableData(styleVariableData) | 174 : m_styleVariableData(styleVariableData) |
| 186 { | 175 { |
| 187 } | 176 } |
| 188 | 177 |
| 189 CSSVariableResolver::CSSVariableResolver(StyleVariableData* styleVariableData, A tomicString& variable) | 178 CSSVariableResolver::CSSVariableResolver(StyleVariableData* styleVariableData, A tomicString& variable) |
| 190 : m_styleVariableData(styleVariableData) | 179 : m_styleVariableData(styleVariableData) |
| 191 { | 180 { |
| 192 m_variablesSeen.add(variable); | 181 m_variablesSeen.add(variable); |
| 193 } | 182 } |
| 194 | 183 |
| 195 } // namespace blink | 184 } // namespace blink |
| OLD | NEW |