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

Side by Side Diff: third_party/WebKit/Source/core/css/cssom/ComputedStylePropertyMap.cpp

Issue 2695093007: [Typed CSSOM] Get computed values for custom properties
Patch Set: Fix unregistered props and registered but unset props Created 3 years, 9 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 2016 the Chromium Authors. All rights reserved. 1 // Copyright 2016 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/cssom/ComputedStylePropertyMap.h" 5 #include "core/css/cssom/ComputedStylePropertyMap.h"
6 6
7 #include "core/css/CSSComputedStyleDeclaration.h" 7 #include "core/css/CSSComputedStyleDeclaration.h"
8 #include "core/css/ComputedStyleCSSValueMapping.h" 8 #include "core/css/ComputedStyleCSSValueMapping.h"
9 #include "core/css/PropertyRegistry.h"
9 #include "core/css/cssom/CSSCalcLength.h" 10 #include "core/css/cssom/CSSCalcLength.h"
10 #include "core/css/cssom/CSSKeywordValue.h" 11 #include "core/css/cssom/CSSKeywordValue.h"
11 #include "core/css/cssom/CSSNumberValue.h" 12 #include "core/css/cssom/CSSNumberValue.h"
12 #include "core/css/cssom/CSSSimpleLength.h" 13 #include "core/css/cssom/CSSSimpleLength.h"
13 #include "core/css/cssom/CSSUnsupportedStyleValue.h" 14 #include "core/css/cssom/CSSUnsupportedStyleValue.h"
14 #include "core/css/cssom/StyleValueFactory.h" 15 #include "core/css/cssom/StyleValueFactory.h"
15 #include "core/css/resolver/StyleResolver.h" 16 #include "core/css/resolver/StyleResolver.h"
16 #include "core/dom/PseudoElement.h" 17 #include "core/dom/PseudoElement.h"
17 #include "core/dom/StyleEngine.h" 18 #include "core/dom/StyleEngine.h"
18 19
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 // Seems to only support before, after, backdrop, first-letter. See 53 // Seems to only support before, after, backdrop, first-letter. See
53 // PseudoElementData::pseudoElement. 54 // PseudoElementData::pseudoElement.
54 if (PseudoElement* element = 55 if (PseudoElement* element =
55 (toElement(m_node))->pseudoElement(m_pseudoId)) { 56 (toElement(m_node))->pseudoElement(m_pseudoId)) {
56 return element; 57 return element;
57 } 58 }
58 } 59 }
59 return nullptr; 60 return nullptr;
60 } 61 }
61 62
62 // ComputedStylePropertyMap::getAllInternal/get should return computed styles 63 const ComputedStyle* ComputedStylePropertyMap::updateStyle() const {
63 // (as opposed to resolved styles a la getComputedStyle()).
64 //
65 // Property values are read from an up-to-date ComputedStyle and converted into
66 // CSSStyleValues. This has not been implemented for all properties yet.
67 // Unsupported properties fall back to using resolved styles & converting them
68 // to CSSStyleValues via StyleValueFactory. For some types of values, such as
69 // images, the difference between the two is minor.
70 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal(
71 CSSPropertyID propertyID) {
72 CSSStyleValueVector styleValueVector;
73
74 Node* node = this->node(); 64 Node* node = this->node();
75 if (!node || !node->inActiveDocument()) { 65 if (!node || !node->inActiveDocument()) {
76 return styleValueVector; 66 return nullptr;
77 } 67 }
78 68
79 // Update style before getting the value for the property 69 // Update style before getting the value for the property
80 node->document().updateStyleAndLayoutTreeForNode(node); 70 node->document().updateStyleAndLayoutTreeForNode(node);
81 node = this->node(); 71 node = this->node();
82 if (!node) { 72 if (!node) {
83 return styleValueVector; 73 return nullptr;
84 } 74 }
85 // I have copied this from 75 // I have copied this from
86 // CSSComputedStyleDeclaration::computeComputedStyle(). I don't know if there 76 // CSSComputedStyleDeclaration::computeComputedStyle(). I don't know if there
87 // is any use in passing m_pseudoId if node is not already a PseudoElement, 77 // is any use in passing m_pseudoId if node is not already a PseudoElement,
88 // but passing pseudo_Id when it IS already a PseudoElement leads to disaster. 78 // but passing pseudo_Id when it IS already a PseudoElement leads to disaster.
89 const ComputedStyle* style = node->ensureComputedStyle( 79 const ComputedStyle* style = node->ensureComputedStyle(
90 node->isPseudoElement() ? PseudoIdNone : m_pseudoId); 80 node->isPseudoElement() ? PseudoIdNone : m_pseudoId);
91 node = this->node(); 81 node = this->node();
92 if (!node || !node->inActiveDocument() || !style) { 82 if (!node || !node->inActiveDocument() || !style) {
83 return nullptr;
84 }
85 return style;
86 }
87
88 // ComputedStylePropertyMap::getAllInternal/get should return computed styles
89 // (as opposed to resolved styles a la getComputedStyle()).
90 //
91 // Property values are read from an up-to-date ComputedStyle and converted into
92 // CSSStyleValues. This has not been implemented for all properties yet.
93 // Unsupported properties fall back to using resolved styles & converting them
94 // to CSSStyleValues via StyleValueFactory. For some types of values, such as
95 // images, the difference between the two is minor.
96 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal(
97 CSSPropertyID propertyID) {
98 CSSStyleValueVector styleValueVector;
99 const ComputedStyle* style = this->updateStyle();
100 if (!style)
93 return styleValueVector; 101 return styleValueVector;
94 }
95 102
96 CSSStyleValue* styleValue = nullptr; 103 CSSStyleValue* styleValue = nullptr;
97
98 switch (propertyID) { 104 switch (propertyID) {
99 // TODO(rjwright): Generate this code. 105 // TODO(rjwright): Generate this code.
100 case CSSPropertyLeft: 106 case CSSPropertyLeft:
101 styleValue = styleValueForLength(style->left()); 107 styleValue = styleValueForLength(style->left());
102 break; 108 break;
103 case CSSPropertyRight: 109 case CSSPropertyRight:
104 styleValue = styleValueForLength(style->right()); 110 styleValue = styleValueForLength(style->right());
105 break; 111 break;
106 case CSSPropertyTop: 112 case CSSPropertyTop:
107 styleValue = styleValueForLength(style->top()); 113 styleValue = styleValueForLength(style->top());
(...skipping 26 matching lines...) Expand all
134 lineHeight.pixels(), CSSPrimitiveValue::UnitType::Pixels); 140 lineHeight.pixels(), CSSPrimitiveValue::UnitType::Pixels);
135 break; 141 break;
136 } 142 }
137 NOTREACHED(); 143 NOTREACHED();
138 break; 144 break;
139 } 145 }
140 default: 146 default:
141 // For properties not yet handled above, fall back to using resolved 147 // For properties not yet handled above, fall back to using resolved
142 // style. 148 // style.
143 const CSSValue* value = ComputedStyleCSSValueMapping::get( 149 const CSSValue* value = ComputedStyleCSSValueMapping::get(
144 propertyID, *style, nullptr, node, false); 150 propertyID, *style, nullptr, this->node(), false);
145 if (value) { 151 if (value) {
146 return StyleValueFactory::cssValueToStyleValueVector(propertyID, 152 return StyleValueFactory::cssValueToStyleValueVector(propertyID,
147 *value); 153 *value);
148 } 154 }
149 break; 155 break;
150 } 156 }
151 157
152 if (styleValue) { 158 if (styleValue) {
153 styleValueVector.push_back(styleValue); 159 styleValueVector.push_back(styleValue);
154 } 160 }
155 return styleValueVector; 161 return styleValueVector;
156 } 162 }
157 163
158 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( 164 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal(
159 AtomicString customPropertyName) { 165 AtomicString customPropertyName,
160 const CSSValue* cssValue = 166 ExceptionState& exceptionState) {
161 m_computedStyleDeclaration->getPropertyCSSValue(customPropertyName); 167 const ComputedStyle* style = this->updateStyle();
162 if (!cssValue) 168 if (!style) {
163 return CSSStyleValueVector(); 169 return CSSStyleValueVector();
164 return StyleValueFactory::cssValueToStyleValueVector(CSSPropertyInvalid, 170 }
165 *cssValue); 171 const CSSValue* value = ComputedStyleCSSValueMapping::get(
172 customPropertyName, *style, m_node->document().propertyRegistry());
173 if (!value) {
174 return CSSStyleValueVector();
175 }
176 return StyleValueFactory::cssValueToStyleValueVector(*value);
166 } 177 }
167 178
168 Vector<String> ComputedStylePropertyMap::getProperties() { 179 Vector<String> ComputedStylePropertyMap::getProperties() {
169 Vector<String> result; 180 Vector<String> result;
170 for (unsigned i = 0; i < m_computedStyleDeclaration->length(); i++) { 181 for (unsigned i = 0; i < m_computedStyleDeclaration->length(); i++) {
171 result.push_back(m_computedStyleDeclaration->item(i)); 182 result.push_back(m_computedStyleDeclaration->item(i));
172 } 183 }
173 return result; 184 return result;
174 } 185 }
175 186
176 } // namespace blink 187 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698