| 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" |
| 9 #include "core/css/cssom/CSSCalcLength.h" |
| 10 #include "core/css/cssom/CSSKeywordValue.h" |
| 11 #include "core/css/cssom/CSSSimpleLength.h" |
| 12 #include "core/css/cssom/CSSUnsupportedStyleValue.h" |
| 8 #include "core/css/cssom/StyleValueFactory.h" | 13 #include "core/css/cssom/StyleValueFactory.h" |
| 14 #include "core/css/resolver/StyleResolver.h" |
| 15 #include "core/dom/PseudoElement.h" |
| 16 #include "core/dom/StyleEngine.h" |
| 9 | 17 |
| 10 namespace blink { | 18 namespace blink { |
| 11 | 19 |
| 20 namespace { |
| 21 |
| 22 CSSStyleValue* styleValueForLength(const Length& length) { |
| 23 if (length.isAuto()) { |
| 24 return CSSKeywordValue::create("auto"); |
| 25 } |
| 26 if (length.isFixed()) { |
| 27 return CSSSimpleLength::create(length.pixels(), |
| 28 CSSPrimitiveValue::UnitType::Pixels); |
| 29 } |
| 30 if (length.isPercent()) { |
| 31 return CSSSimpleLength::create(length.percent(), |
| 32 CSSPrimitiveValue::UnitType::Percentage); |
| 33 } |
| 34 if (length.isCalculated()) { |
| 35 return CSSCalcLength::fromLength(length); |
| 36 } |
| 37 NOTREACHED(); |
| 38 return nullptr; |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 Node* ComputedStylePropertyMap::node() const { |
| 44 if (!m_node) { |
| 45 return nullptr; |
| 46 } |
| 47 if (!m_pseudoId) { |
| 48 return m_node; |
| 49 } |
| 50 if (m_node->isElementNode()) { |
| 51 // Seems to only support before, after, backdrop, first-letter. See |
| 52 // PseudoElementData::pseudoElement. |
| 53 if (PseudoElement* element = |
| 54 (toElement(m_node))->pseudoElement(m_pseudoId)) { |
| 55 return element; |
| 56 } |
| 57 } |
| 58 return nullptr; |
| 59 } |
| 60 |
| 12 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( | 61 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( |
| 13 CSSPropertyID propertyID) { | 62 CSSPropertyID propertyID) { |
| 14 const CSSValue* cssValue = | 63 CSSStyleValueVector styleValueVector; |
| 15 m_computedStyleDeclaration->getPropertyCSSValueInternal(propertyID); | 64 |
| 16 if (!cssValue) | 65 Node* node = this->node(); |
| 17 return CSSStyleValueVector(); | 66 if (!node || !node->inActiveDocument()) { |
| 18 return StyleValueFactory::cssValueToStyleValueVector(propertyID, *cssValue); | 67 return styleValueVector; |
| 68 } |
| 69 node->document().updateStyleAndLayoutTreeForNode(node); |
| 70 node = this->node(); |
| 71 if (!node) { |
| 72 return styleValueVector; |
| 73 } |
| 74 // I have copied this from |
| 75 // CSSComputedStyleDeclaration::computeComputedStyle(). I don't know if |
| 76 // there is any use in passing m_pseudoId if node is not already a |
| 77 // PseudoElement, but passing |
| 78 // pseudo_Id when it IS already a PseudoElement leads to disaster. |
| 79 const ComputedStyle* style = node->ensureComputedStyle( |
| 80 node->isPseudoElement() ? PseudoIdNone : m_pseudoId); |
| 81 node = this->node(); |
| 82 if (!node || !node->inActiveDocument() || !style) { |
| 83 return styleValueVector; |
| 84 } |
| 85 |
| 86 CSSStyleValue* styleValue = nullptr; |
| 87 |
| 88 switch (propertyID) { |
| 89 // TODO(rjwright): Generate this code. |
| 90 case CSSPropertyLeft: |
| 91 styleValue = styleValueForLength(style->left()); |
| 92 break; |
| 93 case CSSPropertyRight: |
| 94 styleValue = styleValueForLength(style->right()); |
| 95 break; |
| 96 case CSSPropertyTop: |
| 97 styleValue = styleValueForLength(style->top()); |
| 98 break; |
| 99 case CSSPropertyBottom: |
| 100 styleValue = styleValueForLength(style->bottom()); |
| 101 break; |
| 102 case CSSPropertyHeight: |
| 103 styleValue = styleValueForLength(style->height()); |
| 104 break; |
| 105 case CSSPropertyWidth: |
| 106 styleValue = styleValueForLength(style->width()); |
| 107 break; |
| 108 default: |
| 109 // TODO(rjwright): Add a flag argument to |
| 110 // ComputedStyleCSSValyeMapping::get that makes it return |
| 111 // just the raw value off the ComputedStyle, and not zoom adjusted or |
| 112 // anything like that. |
| 113 const CSSValue* value = ComputedStyleCSSValueMapping::get( |
| 114 propertyID, *style, nullptr, node, false); |
| 115 if (value) { |
| 116 return StyleValueFactory::cssValueToStyleValueVector(propertyID, |
| 117 *value); |
| 118 } |
| 119 break; |
| 120 } |
| 121 |
| 122 if (styleValue) { |
| 123 styleValueVector.append(styleValue); |
| 124 } |
| 125 return styleValueVector; |
| 19 } | 126 } |
| 20 | 127 |
| 21 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( | 128 CSSStyleValueVector ComputedStylePropertyMap::getAllInternal( |
| 22 AtomicString customPropertyName) { | 129 AtomicString customPropertyName) { |
| 23 const CSSValue* cssValue = | 130 const CSSValue* cssValue = |
| 24 m_computedStyleDeclaration->getPropertyCSSValueInternal( | 131 m_computedStyleDeclaration->getPropertyCSSValue(customPropertyName); |
| 25 customPropertyName); | |
| 26 if (!cssValue) | 132 if (!cssValue) |
| 27 return CSSStyleValueVector(); | 133 return CSSStyleValueVector(); |
| 28 return StyleValueFactory::cssValueToStyleValueVector(CSSPropertyInvalid, | 134 return StyleValueFactory::cssValueToStyleValueVector(CSSPropertyInvalid, |
| 29 *cssValue); | 135 *cssValue); |
| 30 } | 136 } |
| 31 | 137 |
| 32 Vector<String> ComputedStylePropertyMap::getProperties() { | 138 Vector<String> ComputedStylePropertyMap::getProperties() { |
| 33 Vector<String> result; | 139 Vector<String> result; |
| 34 for (unsigned i = 0; i < m_computedStyleDeclaration->length(); i++) { | 140 for (unsigned i = 0; i < m_computedStyleDeclaration->length(); i++) { |
| 35 result.push_back(m_computedStyleDeclaration->item(i)); | 141 result.push_back(m_computedStyleDeclaration->item(i)); |
| 36 } | 142 } |
| 37 return result; | 143 return result; |
| 38 } | 144 } |
| 39 | 145 |
| 40 } // namespace blink | 146 } // namespace blink |
| OLD | NEW |