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

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

Issue 2791193004: [Typed CSSOM] New design for computed styles which includes custom properties (Closed)
Patch Set: replace node check with dcheck Created 3 years, 7 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"
8 #include "core/css/ComputedStyleCSSValueMapping.h" 7 #include "core/css/ComputedStyleCSSValueMapping.h"
9 #include "core/css/cssom/CSSCalcLength.h"
10 #include "core/css/cssom/CSSKeywordValue.h"
11 #include "core/css/cssom/CSSNumberValue.h"
12 #include "core/css/cssom/CSSSimpleLength.h"
13 #include "core/css/cssom/CSSUnsupportedStyleValue.h"
14 #include "core/css/cssom/StyleValueFactory.h" 8 #include "core/css/cssom/StyleValueFactory.h"
15 #include "core/css/resolver/StyleResolver.h"
16 #include "core/dom/PseudoElement.h" 9 #include "core/dom/PseudoElement.h"
17 #include "core/dom/StyleEngine.h"
18 10
19 namespace blink { 11 namespace blink {
20 12
21 namespace { 13 Node* ComputedStylePropertyMap::StyledNode() const {
22 14 DCHECK(node_);
23 CSSStyleValue* StyleValueForLength(const Length& length) { 15 if (!pseudo_id_)
24 if (length.IsAuto()) {
25 return CSSKeywordValue::Create("auto");
26 }
27 if (length.IsFixed()) {
28 return CSSSimpleLength::Create(length.Pixels(),
29 CSSPrimitiveValue::UnitType::kPixels);
30 }
31 if (length.IsPercent()) {
32 return CSSSimpleLength::Create(length.Percent(),
33 CSSPrimitiveValue::UnitType::kPercentage);
34 }
35 if (length.IsCalculated()) {
36 return CSSCalcLength::FromLength(length);
37 }
38 NOTREACHED();
39 return nullptr;
40 }
41
42 } // namespace
43
44 Node* ComputedStylePropertyMap::GetNode() const {
45 if (!node_) {
46 return nullptr;
47 }
48 if (!pseudo_id_) {
49 return node_; 16 return node_;
50 }
51 if (node_->IsElementNode()) { 17 if (node_->IsElementNode()) {
52 // Seems to only support before, after, backdrop, first-letter. See 18 // Seems to only support before, after, backdrop, first-letter. See
53 // PseudoElementData::GetPseudoElement. 19 // PseudoElementData::GetPseudoElement.
54 if (PseudoElement* element = 20 if (PseudoElement* element =
55 (ToElement(node_))->GetPseudoElement(pseudo_id_)) { 21 (ToElement(node_))->GetPseudoElement(pseudo_id_)) {
56 return element; 22 return element;
57 } 23 }
58 } 24 }
59 return nullptr; 25 return nullptr;
60 } 26 }
61 27
62 // ComputedStylePropertyMap::GetAllInternal/Get should return computed styles 28 const ComputedStyle* ComputedStylePropertyMap::UpdateStyle() const {
63 // (as opposed to resolved styles a la getComputedStyle()). 29 Node* node = StyledNode();
64 // 30 if (!node || !node->InActiveDocument())
65 // Property values are read from an up-to-date ComputedStyle and converted into 31 return nullptr;
66 // CSSStyleValues. This has not been implemented for all properties yet. 32
67 // Unsupported properties fall back to using resolved styles & converting them 33 // Update style before getting the value for the property
68 // to CSSStyleValues via StyleValueFactory. For some types of values, such as 34 // This could cause the node to be blown away.
meade_UTC10 2017/05/09 04:59:36 Nit: Add a comment here pointing to where this was
69 // images, the difference between the two is minor. 35 node->GetDocument().UpdateStyleAndLayoutTreeForNode(node);
36 node = StyledNode();
37 if (!node)
38 return nullptr;
39 // This is copied from CSSComputedStyleDeclaration::computeComputedStyle().
40 // PseudoIdNone must be used if node() is a PseudoElement.
41 const ComputedStyle* style = node->EnsureComputedStyle(
42 node->IsPseudoElement() ? kPseudoIdNone : pseudo_id_);
43 node = StyledNode();
44 if (!node || !node->InActiveDocument() || !style)
45 return nullptr;
46 return style;
47 }
48
70 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( 49 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal(
71 CSSPropertyID property_id) { 50 CSSPropertyID property_id) {
72 CSSStyleValueVector style_value_vector; 51 CSSStyleValueVector style_value_vector;
73 52 const ComputedStyle* style = UpdateStyle();
74 Node* node = this->GetNode(); 53 if (!style)
75 if (!node || !node->InActiveDocument()) { 54 return CSSStyleValueVector();
76 return style_value_vector; 55 const CSSValue* css_value = ComputedStyleCSSValueMapping::Get(
77 } 56 property_id, *style, nullptr /* layoutObject */);
78 57 if (!css_value)
79 // Update style before getting the value for the property 58 return CSSStyleValueVector();
80 node->GetDocument().UpdateStyleAndLayoutTreeForNode(node); 59 return StyleValueFactory::CssValueToStyleValueVector(property_id, *css_value);
81 node = this->GetNode();
82 if (!node) {
83 return style_value_vector;
84 }
85 // I have copied this from
86 // CSSComputedStyleDeclaration::ComputeComputedStyle(). I don't know if there
87 // is any use in passing pseudo_id_ if node is not already a PseudoElement,
88 // but passing pseudo_id_ when it IS already a PseudoElement leads to
89 // disaster.
90 const ComputedStyle* style = node->EnsureComputedStyle(
91 node->IsPseudoElement() ? kPseudoIdNone : pseudo_id_);
92 node = this->GetNode();
93 if (!node || !node->InActiveDocument() || !style) {
94 return style_value_vector;
95 }
96
97 CSSStyleValue* style_value = nullptr;
98
99 switch (property_id) {
100 // TODO(rjwright): Generate this code.
101 case CSSPropertyLeft:
102 style_value = StyleValueForLength(style->Left());
103 break;
104 case CSSPropertyRight:
105 style_value = StyleValueForLength(style->Right());
106 break;
107 case CSSPropertyTop:
108 style_value = StyleValueForLength(style->Top());
109 break;
110 case CSSPropertyBottom:
111 style_value = StyleValueForLength(style->Bottom());
112 break;
113 case CSSPropertyHeight:
114 style_value = StyleValueForLength(style->Height());
115 break;
116 case CSSPropertyWidth:
117 style_value = StyleValueForLength(style->Width());
118 break;
119 case CSSPropertyLineHeight: {
120 // LineHeight is represented as a Length in ComputedStyle, even though it
121 // can be a number or the "normal" keyword. "normal" is encoded as a
122 // negative percent, and numbers (which must be positive) are encoded as
123 // percents.
124 Length line_height = style->LineHeight();
125 if (line_height.IsNegative()) {
126 style_value = CSSKeywordValue::Create("normal");
127 break;
128 }
129 if (line_height.IsPercent()) {
130 style_value = CSSNumberValue::Create(line_height.Percent());
131 break;
132 }
133 if (line_height.IsFixed()) {
134 style_value = CSSSimpleLength::Create(
135 line_height.Pixels(), CSSPrimitiveValue::UnitType::kPixels);
136 break;
137 }
138 NOTREACHED();
139 break;
140 }
141 default:
142 // For properties not yet handled above, fall back to using resolved
143 // style.
144 const CSSValue* value = ComputedStyleCSSValueMapping::Get(
145 property_id, *style, nullptr, node, false);
146 if (value) {
147 return StyleValueFactory::CssValueToStyleValueVector(property_id,
148 *value);
149 }
150 break;
151 }
152
153 if (style_value) {
154 style_value_vector.push_back(style_value);
155 }
156 return style_value_vector;
157 } 60 }
158 61
159 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( 62 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal(
160 AtomicString custom_property_name) { 63 AtomicString custom_property_name) {
161 const CSSValue* css_value = 64 const ComputedStyle* style = UpdateStyle();
162 computed_style_declaration_->GetPropertyCSSValue(custom_property_name); 65 if (!style)
66 return CSSStyleValueVector();
67 const CSSValue* css_value = ComputedStyleCSSValueMapping::Get(
68 custom_property_name, *style, node_->GetDocument().GetPropertyRegistry());
163 if (!css_value) 69 if (!css_value)
164 return CSSStyleValueVector(); 70 return CSSStyleValueVector();
165 return StyleValueFactory::CssValueToStyleValueVector(CSSPropertyInvalid, 71 return StyleValueFactory::CssValueToStyleValueVector(*css_value);
166 *css_value);
167 } 72 }
168 73
169 Vector<String> ComputedStylePropertyMap::getProperties() { 74 Vector<String> ComputedStylePropertyMap::getProperties() {
170 Vector<String> result; 75 Vector<String> result;
171 for (CSSPropertyID property_id : 76 for (CSSPropertyID property_id :
172 CSSComputedStyleDeclaration::ComputableProperties()) { 77 CSSComputedStyleDeclaration::ComputableProperties()) {
173 result.push_back(getPropertyNameString(property_id)); 78 result.push_back(getPropertyNameString(property_id));
174 } 79 }
175 return result; 80 return result;
176 } 81 }
177 82
178 } // namespace blink 83 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698