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 | 18 // Seems to only support before, after, backdrop, first-letter. See |
|
nainar
2017/05/09 06:07:28
Those are the only ones that need to be supported
| |
| 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. This code is copied from |
| 69 // images, the difference between the two is minor. | 35 // CSSComputedStyleDeclaration::GetPropertyCSSValue. |
| 36 node->GetDocument().UpdateStyleAndLayoutTreeForNode(node); | |
| 37 node = StyledNode(); | |
| 38 if (!node) | |
| 39 return nullptr; | |
| 40 // This is copied from CSSComputedStyleDeclaration::computeComputedStyle(). | |
| 41 // PseudoIdNone must be used if node() is a PseudoElement. | |
| 42 const ComputedStyle* style = node->EnsureComputedStyle( | |
| 43 node->IsPseudoElement() ? kPseudoIdNone : pseudo_id_); | |
| 44 node = StyledNode(); | |
| 45 if (!node || !node->InActiveDocument() || !style) | |
| 46 return nullptr; | |
| 47 return style; | |
| 48 } | |
| 49 | |
| 70 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( | 50 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( |
| 71 CSSPropertyID property_id) { | 51 CSSPropertyID property_id) { |
| 72 CSSStyleValueVector style_value_vector; | 52 CSSStyleValueVector style_value_vector; |
| 73 | 53 const ComputedStyle* style = UpdateStyle(); |
| 74 Node* node = this->GetNode(); | 54 if (!style) |
| 75 if (!node || !node->InActiveDocument()) { | 55 return CSSStyleValueVector(); |
| 76 return style_value_vector; | 56 const CSSValue* css_value = ComputedStyleCSSValueMapping::Get( |
| 77 } | 57 property_id, *style, nullptr /* layoutObject */); |
| 78 | 58 if (!css_value) |
| 79 // Update style before getting the value for the property | 59 return CSSStyleValueVector(); |
| 80 node->GetDocument().UpdateStyleAndLayoutTreeForNode(node); | 60 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 } | 61 } |
| 158 | 62 |
| 159 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( | 63 CSSStyleValueVector ComputedStylePropertyMap::GetAllInternal( |
| 160 AtomicString custom_property_name) { | 64 AtomicString custom_property_name) { |
| 161 const CSSValue* css_value = | 65 const ComputedStyle* style = UpdateStyle(); |
| 162 computed_style_declaration_->GetPropertyCSSValue(custom_property_name); | 66 if (!style) |
| 67 return CSSStyleValueVector(); | |
| 68 const CSSValue* css_value = ComputedStyleCSSValueMapping::Get( | |
| 69 custom_property_name, *style, node_->GetDocument().GetPropertyRegistry()); | |
| 163 if (!css_value) | 70 if (!css_value) |
| 164 return CSSStyleValueVector(); | 71 return CSSStyleValueVector(); |
| 165 return StyleValueFactory::CssValueToStyleValueVector(CSSPropertyInvalid, | 72 return StyleValueFactory::CssValueToStyleValueVector(*css_value); |
| 166 *css_value); | |
| 167 } | 73 } |
| 168 | 74 |
| 169 Vector<String> ComputedStylePropertyMap::getProperties() { | 75 Vector<String> ComputedStylePropertyMap::getProperties() { |
| 170 Vector<String> result; | 76 Vector<String> result; |
| 171 for (CSSPropertyID property_id : | 77 for (CSSPropertyID property_id : |
| 172 CSSComputedStyleDeclaration::ComputableProperties()) { | 78 CSSComputedStyleDeclaration::ComputableProperties()) { |
| 173 result.push_back(getPropertyNameString(property_id)); | 79 result.push_back(getPropertyNameString(property_id)); |
| 174 } | 80 } |
| 175 return result; | 81 return result; |
| 176 } | 82 } |
| 177 | 83 |
| 178 } // namespace blink | 84 } // namespace blink |
| OLD | NEW |