OLD | NEW |
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 | |
53 // PseudoElementData::GetPseudoElement. | |
54 if (PseudoElement* element = | 18 if (PseudoElement* element = |
55 (ToElement(node_))->GetPseudoElement(pseudo_id_)) { | 19 (ToElement(node_))->GetPseudoElement(pseudo_id_)) { |
56 return element; | 20 return element; |
57 } | 21 } |
58 } | 22 } |
59 return nullptr; | 23 return nullptr; |
60 } | 24 } |
61 | 25 |
62 // ComputedStylePropertyMap::GetAllInternal/Get should return computed styles | 26 const ComputedStyle* ComputedStylePropertyMap::UpdateStyle() { |
63 // (as opposed to resolved styles a la getComputedStyle()). | 27 Node* node = StyledNode(); |
64 // | 28 if (!node || !node->InActiveDocument()) |
65 // Property values are read from an up-to-date ComputedStyle and converted into | 29 return nullptr; |
66 // CSSStyleValues. This has not been implemented for all properties yet. | 30 |
67 // Unsupported properties fall back to using resolved styles & converting them | 31 // Update style before getting the value for the property |
68 // to CSSStyleValues via StyleValueFactory. For some types of values, such as | 32 // This could cause the node to be blown away. This code is copied from |
69 // images, the difference between the two is minor. | 33 // CSSComputedStyleDeclaration::GetPropertyCSSValue. |
| 34 node->GetDocument().UpdateStyleAndLayoutTreeForNode(node); |
| 35 node = StyledNode(); |
| 36 if (!node) |
| 37 return nullptr; |
| 38 // This is copied from CSSComputedStyleDeclaration::computeComputedStyle(). |
| 39 // PseudoIdNone must be used if node() is a PseudoElement. |
| 40 const ComputedStyle* style = node->EnsureComputedStyle( |
| 41 node->IsPseudoElement() ? kPseudoIdNone : pseudo_id_); |
| 42 node = StyledNode(); |
| 43 if (!node || !node->InActiveDocument() || !style) |
| 44 return nullptr; |
| 45 return style; |
| 46 } |
| 47 |
70 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( | 48 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( |
71 CSSPropertyID property_id) { | 49 CSSPropertyID property_id) { |
72 CSSStyleValueVector style_value_vector; | 50 const ComputedStyle* style = UpdateStyle(); |
73 | 51 if (!style) |
74 Node* node = this->GetNode(); | 52 return CSSStyleValueVector(); |
75 if (!node || !node->InActiveDocument()) { | 53 const CSSValue* css_value = ComputedStyleCSSValueMapping::Get( |
76 return style_value_vector; | 54 property_id, *style, nullptr /* layout_object */); |
77 } | 55 if (!css_value) |
78 | 56 return CSSStyleValueVector(); |
79 // Update style before getting the value for the property | 57 return StyleValueFactory::CssValueToStyleValueVector(property_id, *css_value); |
80 node->GetDocument().UpdateStyleAndLayoutTreeForNode(node); | |
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 } | 58 } |
158 | 59 |
159 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( | 60 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( |
160 AtomicString custom_property_name) { | 61 AtomicString custom_property_name) { |
161 const CSSValue* css_value = | 62 const ComputedStyle* style = UpdateStyle(); |
162 computed_style_declaration_->GetPropertyCSSValue(custom_property_name); | 63 if (!style) |
| 64 return CSSStyleValueVector(); |
| 65 const CSSValue* css_value = ComputedStyleCSSValueMapping::Get( |
| 66 custom_property_name, *style, node_->GetDocument().GetPropertyRegistry()); |
163 if (!css_value) | 67 if (!css_value) |
164 return CSSStyleValueVector(); | 68 return CSSStyleValueVector(); |
165 return StyleValueFactory::CssValueToStyleValueVector(CSSPropertyInvalid, | 69 return StyleValueFactory::CssValueToStyleValueVector(*css_value); |
166 *css_value); | |
167 } | 70 } |
168 | 71 |
169 Vector<String> ComputedStylePropertyMap::getProperties() { | 72 Vector<String> ComputedStylePropertyMap::getProperties() { |
170 Vector<String> result; | 73 Vector<String> result; |
171 for (CSSPropertyID property_id : | 74 for (CSSPropertyID property_id : |
172 CSSComputedStyleDeclaration::ComputableProperties()) { | 75 CSSComputedStyleDeclaration::ComputableProperties()) { |
173 result.push_back(getPropertyNameString(property_id)); | 76 result.push_back(getPropertyNameString(property_id)); |
174 } | 77 } |
175 return result; | 78 return result; |
176 } | 79 } |
177 | 80 |
178 } // namespace blink | 81 } // namespace blink |
OLD | NEW |