Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(812)

Side by Side Diff: third_party/WebKit/Source/core/css/resolver/CSSVariableResolver.cpp

Issue 2366313006: CSS Properties and Values API: Support non-inherited custom properties (Closed)
Patch Set: use de morgan's law Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/CSSPendingSubstitutionValue.h" 11 #include "core/css/CSSPendingSubstitutionValue.h"
12 #include "core/css/CSSUnsetValue.h" 12 #include "core/css/CSSUnsetValue.h"
13 #include "core/css/CSSVariableData.h" 13 #include "core/css/CSSVariableData.h"
14 #include "core/css/CSSVariableReferenceValue.h" 14 #include "core/css/CSSVariableReferenceValue.h"
15 #include "core/css/PropertyRegistry.h" 15 #include "core/css/PropertyRegistry.h"
16 #include "core/css/parser/CSSParserToken.h" 16 #include "core/css/parser/CSSParserToken.h"
17 #include "core/css/parser/CSSParserTokenRange.h" 17 #include "core/css/parser/CSSParserTokenRange.h"
18 #include "core/css/parser/CSSPropertyParser.h" 18 #include "core/css/parser/CSSPropertyParser.h"
19 #include "core/css/resolver/StyleBuilder.h" 19 #include "core/css/resolver/StyleBuilder.h"
20 #include "core/css/resolver/StyleBuilderConverter.h" 20 #include "core/css/resolver/StyleBuilderConverter.h"
21 #include "core/css/resolver/StyleResolverState.h" 21 #include "core/css/resolver/StyleResolverState.h"
22 #include "core/style/StyleVariableData.h" 22 #include "core/style/StyleInheritedVariables.h"
23 #include "core/style/StyleNonInheritedVariables.h"
23 #include "wtf/Vector.h" 24 #include "wtf/Vector.h"
24 25
25 namespace blink { 26 namespace blink {
26 27
27 bool CSSVariableResolver::resolveFallback(CSSParserTokenRange range, Vector<CSSP arserToken>& result) 28 bool CSSVariableResolver::resolveFallback(CSSParserTokenRange range, Vector<CSSP arserToken>& result)
28 { 29 {
29 if (range.atEnd()) 30 if (range.atEnd())
30 return false; 31 return false;
31 ASSERT(range.peek().type() == CommaToken); 32 ASSERT(range.peek().type() == CommaToken);
32 range.consume(); 33 range.consume();
33 return resolveTokenRange(range, result); 34 return resolveTokenRange(range, result);
34 } 35 }
35 36
36 CSSVariableData* CSSVariableResolver::valueForCustomProperty(AtomicString name) 37 CSSVariableData* CSSVariableResolver::valueForCustomProperty(AtomicString name)
37 { 38 {
38 if (m_variablesSeen.contains(name)) { 39 if (m_variablesSeen.contains(name)) {
39 m_cycleStartPoints.add(name); 40 m_cycleStartPoints.add(name);
40 return nullptr; 41 return nullptr;
41 } 42 }
42 43
43 DCHECK(m_registry || !RuntimeEnabledFeatures::cssVariables2Enabled()); 44 DCHECK(m_registry || !RuntimeEnabledFeatures::cssVariables2Enabled());
44 const PropertyRegistry::Registration* registration = m_registry ? m_registry ->registration(name) : nullptr; 45 const PropertyRegistry::Registration* registration = m_registry ? m_registry ->registration(name) : nullptr;
45 46
46 CSSVariableData* variableData = nullptr; 47 CSSVariableData* variableData = nullptr;
47 if (m_styleVariableData) 48 if (!registration || registration->inherits()) {
48 variableData = m_styleVariableData->getVariable(name); 49 if (m_inheritedVariables)
50 variableData = m_inheritedVariables->getVariable(name);
51 } else {
52 variableData = m_nonInheritedVariables->getVariable(name);
53 }
49 if (!variableData) 54 if (!variableData)
50 return registration ? registration->initialVariableData() : nullptr; 55 return registration ? registration->initialVariableData() : nullptr;
51 if (!variableData->needsVariableResolution()) 56 if (!variableData->needsVariableResolution())
52 return variableData; 57 return variableData;
53 58
54 RefPtr<CSSVariableData> newVariableData = resolveCustomProperty(name, *varia bleData); 59 RefPtr<CSSVariableData> newVariableData = resolveCustomProperty(name, *varia bleData);
55 if (registration) { 60 if (!registration) {
56 const CSSValue* parsedValue = nullptr; 61 m_inheritedVariables->setVariable(name, newVariableData);
57 if (newVariableData) {
58 parsedValue = newVariableData->parseForSyntax(registration->syntax() );
59 if (parsedValue)
60 parsedValue = &StyleBuilderConverter::convertRegisteredPropertyV alue(m_styleResolverState, *parsedValue);
61 else
62 newVariableData = nullptr;
63 }
64 m_styleVariableData->setVariable(name, newVariableData);
65 m_styleVariableData->setRegisteredInheritedProperty(name, parsedValue);
66 if (!newVariableData)
67 return registration->initialVariableData();
68 return newVariableData.get(); 62 return newVariableData.get();
69 } 63 }
70 64
71 m_styleVariableData->setVariable(name, newVariableData); 65 const CSSValue* parsedValue = nullptr;
66 if (newVariableData) {
67 parsedValue = newVariableData->parseForSyntax(registration->syntax());
68 if (parsedValue)
69 parsedValue = &StyleBuilderConverter::convertRegisteredPropertyValue (m_styleResolverState, *parsedValue);
70 else
71 newVariableData = nullptr;
72 }
73 if (registration->inherits()) {
74 m_inheritedVariables->setVariable(name, newVariableData);
75 m_inheritedVariables->setRegisteredVariable(name, parsedValue);
76 } else {
77 m_nonInheritedVariables->setVariable(name, newVariableData);
78 m_nonInheritedVariables->setRegisteredVariable(name, parsedValue);
79 }
80 if (!newVariableData)
81 return registration->initialVariableData();
72 return newVariableData.get(); 82 return newVariableData.get();
73 } 83 }
74 84
75 PassRefPtr<CSSVariableData> CSSVariableResolver::resolveCustomProperty(AtomicStr ing name, const CSSVariableData& variableData) 85 PassRefPtr<CSSVariableData> CSSVariableResolver::resolveCustomProperty(AtomicStr ing name, const CSSVariableData& variableData)
76 { 86 {
77 ASSERT(variableData.needsVariableResolution()); 87 ASSERT(variableData.needsVariableResolution());
78 88
79 Vector<CSSParserToken> tokens; 89 Vector<CSSParserToken> tokens;
80 m_variablesSeen.add(name); 90 m_variablesSeen.add(name);
81 bool success = resolveTokenRange(variableData.tokens(), tokens); 91 bool success = resolveTokenRange(variableData.tokens(), tokens);
82 m_variablesSeen.remove(name); 92 m_variablesSeen.remove(name);
83 93
84 // The old variable data holds onto the backing string the new resolved CSSV ariableData 94 // The old variable data holds onto the backing string the new resolved CSSV ariableData
85 // relies on. Ensure it will live beyond us overwriting the RefPtr in StyleV ariableData. 95 // relies on. Ensure it will live beyond us overwriting the RefPtr in StyleI nheritedVariables.
86 ASSERT(variableData.refCount() > 1); 96 ASSERT(variableData.refCount() > 1);
87 97
88 if (!success || !m_cycleStartPoints.isEmpty()) { 98 if (!success || !m_cycleStartPoints.isEmpty()) {
89 m_cycleStartPoints.remove(name); 99 m_cycleStartPoints.remove(name);
90 return nullptr; 100 return nullptr;
91 } 101 }
92 return CSSVariableData::createResolved(tokens, variableData); 102 return CSSVariableData::createResolved(tokens, variableData);
93 } 103 }
94 104
95 bool CSSVariableResolver::resolveVariableReference(CSSParserTokenRange range, Ve ctor<CSSParserToken>& result) 105 bool CSSVariableResolver::resolveVariableReference(CSSParserTokenRange range, Ve ctor<CSSParserToken>& result)
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
209 219
210 if (value) 220 if (value)
211 return value; 221 return value;
212 222
213 return CSSUnsetValue::create(); 223 return CSSUnsetValue::create();
214 } 224 }
215 225
216 226
217 void CSSVariableResolver::resolveVariableDefinitions(const StyleResolverState& s tate) 227 void CSSVariableResolver::resolveVariableDefinitions(const StyleResolverState& s tate)
218 { 228 {
219 StyleVariableData* variables = state.style()->variables(); 229 StyleInheritedVariables* inheritedVariables = state.style()->inheritedVariab les();
220 if (!variables) 230 StyleNonInheritedVariables* nonInheritedVariables = state.style()->nonInheri tedVariables();
231 if (!inheritedVariables && !nonInheritedVariables)
221 return; 232 return;
222 233
223 CSSVariableResolver resolver(state); 234 CSSVariableResolver resolver(state);
224 for (auto& variable : variables->m_data) 235 if (inheritedVariables) {
225 resolver.valueForCustomProperty(variable.key); 236 for (auto& variable : inheritedVariables->m_data)
237 resolver.valueForCustomProperty(variable.key);
238 }
239 if (nonInheritedVariables) {
240 for (auto& variable : nonInheritedVariables->m_data)
241 resolver.valueForCustomProperty(variable.key);
242 }
226 } 243 }
227 244
228 CSSVariableResolver::CSSVariableResolver(const StyleResolverState& state) 245 CSSVariableResolver::CSSVariableResolver(const StyleResolverState& state)
229 : m_styleResolverState(state) 246 : m_styleResolverState(state)
230 , m_styleVariableData(state.style()->variables()) 247 , m_inheritedVariables(state.style()->inheritedVariables())
248 , m_nonInheritedVariables(state.style()->nonInheritedVariables())
231 , m_registry(state.document().propertyRegistry()) 249 , m_registry(state.document().propertyRegistry())
232 { 250 {
233 } 251 }
234 252
235 DEFINE_TRACE(CSSVariableResolver) { visitor->trace(m_registry); } 253 DEFINE_TRACE(CSSVariableResolver) { visitor->trace(m_registry); }
236 254
237 } // namespace blink 255 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698