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" | 7 #include "core/css/CSSComputedStyleDeclaration.h" |
| 8 #include "core/css/ComputedStyleCSSValueMapping.h" | 8 #include "core/css/ComputedStyleCSSValueMapping.h" |
| 9 #include "core/css/PropertyRegistry.h" | |
|
alancutter (OOO until 2018)
2017/04/05 04:54:09
I don't think this include is required.
| |
| 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" |
|
meade_UTC10
2017/04/06 03:47:32
These other specific types probably aren't require
| |
| 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 |
| 19 namespace blink { | 20 namespace blink { |
| 20 | 21 |
| 21 namespace { | 22 Node* ComputedStylePropertyMap::styledNode() const { |
| 22 | 23 if (!m_node) |
| 23 CSSStyleValue* styleValueForLength(const Length& length) { | |
| 24 if (length.isAuto()) { | |
| 25 return CSSKeywordValue::create("auto"); | |
| 26 } | |
| 27 if (length.isFixed()) { | |
| 28 return CSSSimpleLength::create(length.pixels(), | |
| 29 CSSPrimitiveValue::UnitType::Pixels); | |
| 30 } | |
| 31 if (length.isPercent()) { | |
| 32 return CSSSimpleLength::create(length.percent(), | |
| 33 CSSPrimitiveValue::UnitType::Percentage); | |
| 34 } | |
| 35 if (length.isCalculated()) { | |
| 36 return CSSCalcLength::fromLength(length); | |
| 37 } | |
| 38 NOTREACHED(); | |
| 39 return nullptr; | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 Node* ComputedStylePropertyMap::node() const { | |
| 45 if (!m_node) { | |
| 46 return nullptr; | 24 return nullptr; |
| 47 } | 25 if (!m_pseudoId) |
| 48 if (!m_pseudoId) { | |
| 49 return m_node; | 26 return m_node; |
| 50 } | |
| 51 if (m_node->isElementNode()) { | 27 if (m_node->isElementNode()) { |
| 52 // Seems to only support before, after, backdrop, first-letter. See | 28 // Seems to only support before, after, backdrop, first-letter. See |
| 53 // PseudoElementData::pseudoElement. | 29 // PseudoElementData::pseudoElement. |
| 54 if (PseudoElement* element = | 30 if (PseudoElement* element = |
| 55 (toElement(m_node))->pseudoElement(m_pseudoId)) { | 31 (toElement(m_node))->pseudoElement(m_pseudoId)) { |
| 56 return element; | 32 return element; |
| 57 } | 33 } |
| 58 } | 34 } |
| 59 return nullptr; | 35 return nullptr; |
| 60 } | 36 } |
| 61 | 37 |
| 62 // ComputedStylePropertyMap::getAllInternal/get should return computed styles | 38 const ComputedStyle* ComputedStylePropertyMap::updateStyle() const { |
| 63 // (as opposed to resolved styles a la getComputedStyle()). | 39 Node* node = styledNode(); |
| 64 // | 40 if (!node || !node->inActiveDocument()) |
| 65 // Property values are read from an up-to-date ComputedStyle and converted into | 41 return nullptr; |
| 66 // CSSStyleValues. This has not been implemented for all properties yet. | 42 |
| 67 // Unsupported properties fall back to using resolved styles & converting them | 43 // Update style before getting the value for the property |
| 68 // to CSSStyleValues via StyleValueFactory. For some types of values, such as | 44 // This could cause the node to be blown away. |
|
alancutter (OOO until 2018)
2017/04/05 04:54:09
Add test case for this. (Set display to none).
| |
| 69 // images, the difference between the two is minor. | 45 node->document().updateStyleAndLayoutTreeForNode(node); |
| 46 node = styledNode(); | |
| 47 if (!node) | |
| 48 return nullptr; | |
| 49 // This is copied from CSSComputedStyleDeclaration::computeComputedStyle(). | |
| 50 // PseudoIdNone must be used if node() is a PseudoElement. | |
| 51 const ComputedStyle* style = node->ensureComputedStyle( | |
| 52 node->isPseudoElement() ? PseudoIdNone : m_pseudoId); | |
| 53 node = styledNode(); | |
| 54 if (!node || !node->inActiveDocument() || !style) | |
| 55 return nullptr; | |
| 56 return style; | |
| 57 } | |
| 58 | |
| 70 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( | 59 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( |
| 71 CSSPropertyID propertyID) { | 60 CSSPropertyID propertyID) { |
| 72 CSSStyleValueVector styleValueVector; | 61 CSSStyleValueVector styleValueVector; |
| 73 | 62 const ComputedStyle* style = updateStyle(); |
| 74 Node* node = this->node(); | 63 if (!style) |
| 75 if (!node || !node->inActiveDocument()) { | 64 return CSSStyleValueVector(); |
| 76 return styleValueVector; | 65 const CSSValue* cssValue = ComputedStyleCSSValueMapping::get( |
| 77 } | 66 propertyID, *style, nullptr /* layoutObject */); |
| 78 | 67 if (!cssValue) |
| 79 // Update style before getting the value for the property | 68 return CSSStyleValueVector(); |
| 80 node->document().updateStyleAndLayoutTreeForNode(node); | 69 return StyleValueFactory::cssValueToStyleValueVector(propertyID, *cssValue); |
| 81 node = this->node(); | |
| 82 if (!node) { | |
| 83 return styleValueVector; | |
| 84 } | |
| 85 // I have copied this from | |
| 86 // CSSComputedStyleDeclaration::computeComputedStyle(). I don't know if there | |
| 87 // 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. | |
| 89 const ComputedStyle* style = node->ensureComputedStyle( | |
| 90 node->isPseudoElement() ? PseudoIdNone : m_pseudoId); | |
| 91 node = this->node(); | |
| 92 if (!node || !node->inActiveDocument() || !style) { | |
| 93 return styleValueVector; | |
| 94 } | |
| 95 | |
| 96 CSSStyleValue* styleValue = nullptr; | |
| 97 | |
| 98 switch (propertyID) { | |
| 99 // TODO(rjwright): Generate this code. | |
| 100 case CSSPropertyLeft: | |
| 101 styleValue = styleValueForLength(style->left()); | |
| 102 break; | |
| 103 case CSSPropertyRight: | |
| 104 styleValue = styleValueForLength(style->right()); | |
| 105 break; | |
| 106 case CSSPropertyTop: | |
| 107 styleValue = styleValueForLength(style->top()); | |
| 108 break; | |
| 109 case CSSPropertyBottom: | |
| 110 styleValue = styleValueForLength(style->bottom()); | |
| 111 break; | |
| 112 case CSSPropertyHeight: | |
| 113 styleValue = styleValueForLength(style->height()); | |
| 114 break; | |
| 115 case CSSPropertyWidth: | |
| 116 styleValue = styleValueForLength(style->width()); | |
| 117 break; | |
| 118 case CSSPropertyLineHeight: { | |
| 119 // LineHeight is represented as a Length in ComputedStyle, even though it | |
| 120 // can be a number or the "normal" keyword. "normal" is encoded as a | |
| 121 // negative percent, and numbers (which must be positive) are encoded as | |
| 122 // percents. | |
| 123 Length lineHeight = style->lineHeight(); | |
| 124 if (lineHeight.isNegative()) { | |
| 125 styleValue = CSSKeywordValue::create("normal"); | |
| 126 break; | |
| 127 } | |
| 128 if (lineHeight.isPercent()) { | |
| 129 styleValue = CSSNumberValue::create(lineHeight.percent()); | |
| 130 break; | |
| 131 } | |
| 132 if (lineHeight.isFixed()) { | |
| 133 styleValue = CSSSimpleLength::create( | |
| 134 lineHeight.pixels(), CSSPrimitiveValue::UnitType::Pixels); | |
| 135 break; | |
| 136 } | |
| 137 NOTREACHED(); | |
| 138 break; | |
| 139 } | |
| 140 default: | |
| 141 // For properties not yet handled above, fall back to using resolved | |
| 142 // style. | |
| 143 const CSSValue* value = ComputedStyleCSSValueMapping::get( | |
| 144 propertyID, *style, nullptr, node, false); | |
| 145 if (value) { | |
| 146 return StyleValueFactory::cssValueToStyleValueVector(propertyID, | |
| 147 *value); | |
| 148 } | |
| 149 break; | |
| 150 } | |
| 151 | |
| 152 if (styleValue) { | |
| 153 styleValueVector.push_back(styleValue); | |
| 154 } | |
| 155 return styleValueVector; | |
| 156 } | 70 } |
| 157 | 71 |
| 158 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( | 72 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( |
| 159 AtomicString customPropertyName) { | 73 AtomicString customPropertyName, |
| 160 const CSSValue* cssValue = | 74 ExceptionState& exceptionState) { |
|
meade_UTC10
2017/04/06 03:47:32
You're not using the exception state - no need to
| |
| 161 m_computedStyleDeclaration->getPropertyCSSValue(customPropertyName); | 75 const ComputedStyle* style = updateStyle(); |
| 76 if (!style) | |
| 77 return CSSStyleValueVector(); | |
| 78 // FIXME(rjwright): propertyRegistry is nullptr in stable Chrome. | |
|
alancutter (OOO until 2018)
2017/04/05 04:54:08
I don't think anything will change here, no need f
| |
| 79 const CSSValue* cssValue = ComputedStyleCSSValueMapping::get( | |
| 80 customPropertyName, *style, m_node->document().propertyRegistry()); | |
| 162 if (!cssValue) | 81 if (!cssValue) |
| 163 return CSSStyleValueVector(); | 82 return CSSStyleValueVector(); |
| 164 return StyleValueFactory::cssValueToStyleValueVector(CSSPropertyInvalid, | 83 return StyleValueFactory::cssValueToStyleValueVector(*cssValue); |
| 165 *cssValue); | |
| 166 } | 84 } |
| 167 | 85 |
| 168 Vector<String> ComputedStylePropertyMap::getProperties() { | 86 Vector<String> ComputedStylePropertyMap::getProperties() { |
| 169 Vector<String> result; | 87 Vector<String> result; |
| 170 for (CSSPropertyID propertyID : | 88 for (CSSPropertyID propertyID : |
| 171 CSSComputedStyleDeclaration::computableProperties()) { | 89 CSSComputedStyleDeclaration::computableProperties()) { |
| 172 result.push_back(getPropertyNameString(propertyID)); | 90 result.push_back(getPropertyNameString(propertyID)); |
| 173 } | 91 } |
| 174 return result; | 92 return result; |
| 175 } | 93 } |
| 176 | 94 |
| 177 } // namespace blink | 95 } // namespace blink |
| OLD | NEW |