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/cssom/CSSCalcLength.h" | 9 #include "core/css/cssom/CSSCalcLength.h" |
| 10 #include "core/css/cssom/CSSKeywordValue.h" | 10 #include "core/css/cssom/CSSKeywordValue.h" |
| 11 #include "core/css/cssom/CSSNumberValue.h" | 11 #include "core/css/cssom/CSSNumberValue.h" |
| 12 #include "core/css/cssom/CSSSimpleLength.h" | 12 #include "core/css/cssom/CSSSimpleLength.h" |
| 13 #include "core/css/cssom/CSSUnsupportedStyleValue.h" | 13 #include "core/css/cssom/CSSUnsupportedStyleValue.h" |
| 14 #include "core/css/cssom/StyleValueFactory.h" | 14 #include "core/css/cssom/StyleValueFactory.h" |
| 15 #include "core/css/resolver/StyleResolver.h" | 15 #include "core/css/resolver/StyleResolver.h" |
| 16 #include "core/dom/PseudoElement.h" | 16 #include "core/dom/PseudoElement.h" |
| 17 #include "core/dom/StyleEngine.h" | 17 #include "core/dom/StyleEngine.h" |
| 18 | 18 |
| 19 namespace blink { | 19 namespace blink { |
| 20 | 20 |
| 21 namespace { | 21 namespace { |
| 22 | 22 |
| 23 CSSStyleValue* styleValueForLength(const Length& length) { | 23 String styleValueVectorToString(CSSPropertyID propertyId, |
| 24 if (length.isAuto()) { | 24 const CSSStyleValueVector& vector) { |
| 25 return CSSKeywordValue::create("auto"); | 25 DCHECK(CSSPropertyMetadata::propertyIsRepeated(propertyId)); |
| 26 StringBuilder builder; | |
| 27 for (unsigned i = 1; i < vector.size(); i++) { | |
| 28 builder.append(CSSPropertyMetadata::repetitionSeparator(propertyId)); | |
| 29 builder.append(vector[i]->cssText()); | |
| 26 } | 30 } |
| 27 if (length.isFixed()) { | 31 return builder.toString(); |
| 28 return CSSSimpleLength::create(length.pixels(), | 32 } |
| 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 } | 33 } |
| 41 | 34 |
| 42 } // namespace | 35 Node* ComputedStylePropertyMap::node() const { |
| 36 if (!m_node) | |
| 37 return nullptr; | |
| 38 if (!m_pseudoId) | |
| 39 return m_node; | |
| 43 | 40 |
| 44 Node* ComputedStylePropertyMap::node() const { | |
| 45 if (!m_node) { | |
| 46 return nullptr; | |
| 47 } | |
| 48 if (!m_pseudoId) { | |
| 49 return m_node; | |
| 50 } | |
| 51 if (m_node->isElementNode()) { | 41 if (m_node->isElementNode()) { |
| 52 // Seems to only support before, after, backdrop, first-letter. See | 42 // Seems to only support before, after, backdrop, first-letter. See |
| 53 // PseudoElementData::pseudoElement. | 43 // PseudoElementData::pseudoElement. |
| 54 if (PseudoElement* element = | 44 if (PseudoElement* element = |
| 55 (toElement(m_node))->pseudoElement(m_pseudoId)) { | 45 (toElement(m_node))->pseudoElement(m_pseudoId)) { |
| 56 return element; | 46 return element; |
| 57 } | 47 } |
| 58 } | 48 } |
| 59 return nullptr; | 49 return nullptr; |
| 60 } | 50 } |
| 61 | 51 |
| 62 // ComputedStylePropertyMap::getAllInternal/get should return computed styles | |
| 63 // (as opposed to resolved styles a la getComputedStyle()). | |
| 64 // | |
| 65 // Property values are read from an up-to-date ComputedStyle and converted into | |
| 66 // CSSStyleValues. This has not been implemented for all properties yet. | |
| 67 // Unsupported properties fall back to using resolved styles & converting them | |
| 68 // to CSSStyleValues via StyleValueFactory. For some types of values, such as | |
| 69 // images, the difference between the two is minor. | |
| 70 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( | 52 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( |
| 71 CSSPropertyID propertyID) { | 53 CSSPropertyID propertyID) { |
| 72 CSSStyleValueVector styleValueVector; | 54 if (!node() || !node()->inActiveDocument()) |
| 55 return CSSStyleValueVector(); | |
| 73 | 56 |
| 74 Node* node = this->node(); | 57 // Update style before getting the value for the property. |
| 75 if (!node || !node->inActiveDocument()) { | 58 // This could cause the node to be blown away. |
| 76 return styleValueVector; | 59 node()->document().updateStyleAndLayoutTreeForNode(node()); |
| 77 } | 60 if (!node()) |
| 61 return CSSStyleValueVector(); | |
| 78 | 62 |
| 79 // Update style before getting the value for the property | 63 // This is copied from CSSComputedStyleDeclaration::computeComputedStyle(). |
| 80 node->document().updateStyleAndLayoutTreeForNode(node); | 64 // PseudoIdNone must be used if node() is a PseudoElement. |
| 81 node = this->node(); | 65 const ComputedStyle* style = node()->ensureComputedStyle( |
| 82 if (!node) { | 66 node()->isPseudoElement() ? PseudoIdNone : m_pseudoId); |
| 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 | 67 |
| 96 CSSStyleValue* styleValue = nullptr; | 68 // Passing null for the LayoutObject causes the mapping to return the |
| 69 // computed, rather than the resolved style. | |
| 70 const CSSValue* cssValue = ComputedStyleCSSValueMapping::get( | |
| 71 propertyID, *style, nullptr /* layoutObject */, node()); | |
| 97 | 72 |
| 98 switch (propertyID) { | 73 if (!cssValue) |
| 99 // TODO(rjwright): Generate this code. | 74 return CSSStyleValueVector(); |
| 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 | 75 |
| 152 if (styleValue) { | 76 return StyleValueFactory::cssValueToStyleValueVector(propertyID, *cssValue); |
| 153 styleValueVector.push_back(styleValue); | |
| 154 } | |
| 155 return styleValueVector; | |
| 156 } | 77 } |
| 157 | 78 |
| 158 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( | 79 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( |
| 159 AtomicString customPropertyName) { | 80 AtomicString customPropertyName) { |
| 160 const CSSValue* cssValue = | 81 // TODO(meade): Do need to update style? |
|
meade_UTC10
2017/03/15 06:32:35
Need to make a helper function for the style updat
| |
| 161 m_computedStyleDeclaration->getPropertyCSSValue(customPropertyName); | 82 const ComputedStyle* style = node()->ensureComputedStyle( |
| 83 node()->isPseudoElement() ? PseudoIdNone : m_pseudoId); | |
| 84 const CSSValue* cssValue = ComputedStyleCSSValueMapping::get( | |
| 85 customPropertyName, *style, m_node->document().propertyRegistry()); | |
| 162 if (!cssValue) | 86 if (!cssValue) |
| 163 return CSSStyleValueVector(); | 87 return CSSStyleValueVector(); |
| 164 return StyleValueFactory::cssValueToStyleValueVector(CSSPropertyInvalid, | 88 return StyleValueFactory::cssValueToStyleValueVector(*cssValue); |
| 165 *cssValue); | |
| 166 } | 89 } |
| 167 | 90 |
| 168 Vector<String> ComputedStylePropertyMap::getProperties() { | 91 Vector<String> ComputedStylePropertyMap::getProperties() { |
| 169 Vector<String> result; | 92 Vector<String> result; |
| 170 for (unsigned i = 0; i < m_computedStyleDeclaration->length(); i++) { | 93 const Vector<CSSPropertyID>& properties = |
| 171 result.push_back(m_computedStyleDeclaration->item(i)); | 94 CSSComputedStyleDeclaration::computableProperties(); |
| 95 for (const CSSPropertyID propertyID : properties) { | |
| 96 CSSStyleValueVector vector = getAllInternal(propertyID); | |
| 97 if (vector.size() == 1) { | |
| 98 result.push_back(vector[0]->cssText()); | |
| 99 } else { | |
| 100 result.push_back(styleValueVectorToString(propertyID, vector)); | |
| 101 } | |
| 172 } | 102 } |
| 173 return result; | 103 return result; |
| 174 } | 104 } |
| 175 | 105 |
| 176 } // namespace blink | 106 } // namespace blink |
| OLD | NEW |