Chromium Code Reviews| 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() const { |
| 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. |
|
alancutter (OOO until 2018)
2017/05/11 04:34:04
It doesn't look like it's necessary to copy this c
rjwright
2017/05/12 04:47:34
Only a small amount of code is copied from there.
alancutter (OOO until 2018)
2017/05/12 04:53:15
A low priority follow up patch SGTM.
| |
| 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 CSSStyleValueVector style_value_vector; |
|
alancutter (OOO until 2018)
2017/05/11 04:34:04
This is never used.
rjwright
2017/05/12 04:47:34
Done.
| |
| 73 | 51 const ComputedStyle* style = UpdateStyle(); |
| 74 Node* node = this->GetNode(); | 52 if (!style) |
| 75 if (!node || !node->InActiveDocument()) { | 53 return CSSStyleValueVector(); |
| 76 return style_value_vector; | 54 const CSSValue* css_value = ComputedStyleCSSValueMapping::Get( |
| 77 } | 55 property_id, *style, nullptr /* layoutObject */); |
|
alancutter (OOO until 2018)
2017/05/11 04:34:04
This should say "layout_object".
rjwright
2017/05/12 04:47:34
Done.
| |
| 78 | 56 if (!css_value) |
| 79 // Update style before getting the value for the property | 57 return CSSStyleValueVector(); |
| 80 node->GetDocument().UpdateStyleAndLayoutTreeForNode(node); | 58 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 } | 59 } |
| 158 | 60 |
| 159 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( | 61 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( |
| 160 AtomicString custom_property_name) { | 62 AtomicString custom_property_name) { |
| 161 const CSSValue* css_value = | 63 const ComputedStyle* style = UpdateStyle(); |
| 162 computed_style_declaration_->GetPropertyCSSValue(custom_property_name); | 64 if (!style) |
| 65 return CSSStyleValueVector(); | |
| 66 const CSSValue* css_value = ComputedStyleCSSValueMapping::Get( | |
| 67 custom_property_name, *style, node_->GetDocument().GetPropertyRegistry()); | |
| 163 if (!css_value) | 68 if (!css_value) |
| 164 return CSSStyleValueVector(); | 69 return CSSStyleValueVector(); |
| 165 return StyleValueFactory::CssValueToStyleValueVector(CSSPropertyInvalid, | 70 return StyleValueFactory::CssValueToStyleValueVector(*css_value); |
| 166 *css_value); | |
| 167 } | 71 } |
| 168 | 72 |
| 169 Vector<String> ComputedStylePropertyMap::getProperties() { | 73 Vector<String> ComputedStylePropertyMap::getProperties() { |
| 170 Vector<String> result; | 74 Vector<String> result; |
| 171 for (CSSPropertyID property_id : | 75 for (CSSPropertyID property_id : |
| 172 CSSComputedStyleDeclaration::ComputableProperties()) { | 76 CSSComputedStyleDeclaration::ComputableProperties()) { |
| 173 result.push_back(getPropertyNameString(property_id)); | 77 result.push_back(getPropertyNameString(property_id)); |
| 174 } | 78 } |
| 175 return result; | 79 return result; |
| 176 } | 80 } |
| 177 | 81 |
| 178 } // namespace blink | 82 } // namespace blink |
| OLD | NEW |