Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/css/cssom/InlineStylePropertyMap.h" | |
| 6 | |
| 7 #include "core/CSSPropertyNames.h" | |
| 8 #include "core/css/CSSPrimitiveValue.h" | |
| 9 #include "core/css/CSSPropertyMetadata.h" | |
| 10 #include "core/css/CSSValueList.h" | |
| 11 #include "core/css/StylePropertySet.h" | |
| 12 #include "core/css/cssom/CSSOMTypes.h" | |
| 13 #include "core/css/cssom/SimpleLength.h" | |
| 14 #include "core/css/cssom/StyleValueFactory.h" | |
| 15 | |
| 16 namespace blink { | |
| 17 | |
| 18 StyleValue* InlineStylePropertyMap::get(CSSPropertyID propertyID) | |
| 19 { | |
| 20 StyleValueVector styleVector = getAll(propertyID); | |
| 21 if (styleVector.isEmpty()) | |
| 22 return nullptr; | |
| 23 | |
| 24 return styleVector.at(0); | |
| 25 } | |
| 26 | |
| 27 StyleValueVector InlineStylePropertyMap::getAll(CSSPropertyID propertyID) | |
| 28 { | |
| 29 ASSERT(propertyID != CSSPropertyInvalid); | |
| 30 | |
| 31 CSSValue* cssValue = m_ownerElement->ensureMutableInlineStyle().getPropertyC SSValue(propertyID); | |
| 32 if (!cssValue) | |
| 33 return StyleValueVector(); | |
| 34 | |
| 35 StyleValueVector styleValueVector; | |
| 36 | |
| 37 if (!cssValue->isValueList()) { | |
| 38 StyleValue* styleValue = StyleValueFactory::create(propertyID, *cssValue ); | |
| 39 if (styleValue) | |
| 40 styleValueVector.append(styleValue); | |
| 41 return styleValueVector; | |
| 42 } | |
| 43 | |
| 44 for (CSSValue* value : *toCSSValueList(cssValue)) { | |
| 45 StyleValue* styleValue = StyleValueFactory::create(propertyID, *value); | |
| 46 if (!styleValue) { | |
| 47 return StyleValueVector(); | |
| 48 } | |
|
Timothy Loh
2016/04/07 03:42:53
did you forget to append here? :s
meade_UTC10
2016/04/07 05:41:29
argh, fixed.
| |
| 49 } | |
| 50 | |
| 51 return styleValueVector; | |
| 52 } | |
| 53 | |
| 54 bool InlineStylePropertyMap::has(CSSPropertyID propertyID) | |
| 55 { | |
| 56 ASSERT(propertyID != CSSPropertyInvalid); | |
| 57 return !getAll(propertyID).isEmpty(); | |
| 58 } | |
| 59 | |
| 60 Vector<String> InlineStylePropertyMap::getProperties() | |
| 61 { | |
| 62 Vector<String> result; | |
| 63 StylePropertySet& inlineStyleSet = m_ownerElement->ensureMutableInlineStyle( ); | |
| 64 for (unsigned i = 0; i < inlineStyleSet.propertyCount(); i++) { | |
| 65 CSSPropertyID propertyID = inlineStyleSet.propertyAt(i).id(); | |
| 66 result.append(getPropertyNameString(propertyID)); | |
| 67 } | |
| 68 return result; | |
| 69 } | |
| 70 | |
| 71 void InlineStylePropertyMap::set(CSSPropertyID propertyID, StyleValueOrStyleValu eSequenceOrString& item, ExceptionState& exceptionState) | |
| 72 { | |
| 73 ASSERT(propertyID != CSSPropertyInvalid); | |
| 74 if (item.isNull()) { | |
| 75 remove(propertyID, exceptionState); | |
| 76 return; | |
| 77 } | |
| 78 | |
| 79 if (item.isStyleValue()) { | |
| 80 StyleValue* styleValue = item.getAsStyleValue(); | |
| 81 if (!CSSOMTypes::propertyCanTake(propertyID, *styleValue)) { | |
| 82 exceptionState.throwTypeError("Invalid type for property"); | |
| 83 return; | |
| 84 } | |
| 85 m_ownerElement->setInlineStyleProperty(propertyID, styleValue->toCSSValu e()); | |
| 86 } else if (item.isStyleValueSequence()) { | |
| 87 if (!CSSPropertyMetadata::propertySupportsMultiple(propertyID)) { | |
| 88 exceptionState.throwTypeError("Property does not support multiple va lues"); | |
| 89 return; | |
| 90 } | |
| 91 | |
| 92 // TODO(meade): This won't always work. Figure out what kind of CSSValue List to create properly. | |
| 93 CSSValueList* valueList = CSSValueList::createSpaceSeparated(); | |
| 94 StyleValueVector styleValueVector = item.getAsStyleValueSequence(); | |
| 95 for (const Member<StyleValue> value : styleValueVector) { | |
| 96 if (!CSSOMTypes::propertyCanTake(propertyID, *value)) { | |
| 97 exceptionState.throwTypeError("Invalid type for property"); | |
| 98 return; | |
| 99 } | |
| 100 valueList->append(value->toCSSValue()); | |
| 101 } | |
| 102 | |
| 103 m_ownerElement->setInlineStyleProperty(propertyID, valueList); | |
| 104 } else { | |
| 105 // Parse it. | |
| 106 ASSERT(item.isString()); | |
| 107 // TODO(meade): Implement this. | |
| 108 exceptionState.throwTypeError("Not implemented yet"); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void InlineStylePropertyMap::append(CSSPropertyID propertyID, StyleValueOrStyleV alueSequenceOrString& item, ExceptionState& exceptionState) | |
| 113 { | |
| 114 ASSERT(propertyID != CSSPropertyInvalid); | |
| 115 | |
| 116 if (!CSSPropertyMetadata::propertySupportsMultiple(propertyID)) { | |
| 117 exceptionState.throwTypeError("Property does not support multiple values "); | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 CSSValue* cssValue = m_ownerElement->ensureMutableInlineStyle().getPropertyC SSValue(propertyID); | |
| 122 CSSValueList* cssValueList = nullptr; | |
| 123 if (cssValue->isValueList()) { | |
| 124 cssValueList = toCSSValueList(cssValue); | |
| 125 } else { | |
| 126 // TODO(meade): Figure out which separator to use here properly. | |
| 127 cssValueList = CSSValueList::createSpaceSeparated(); | |
|
Timothy Loh
2016/04/07 03:42:53
This isn't really what I meant. We can't just expe
meade_UTC10
2016/04/07 05:41:29
Done.
| |
| 128 cssValueList->append(cssValue); | |
| 129 } | |
| 130 | |
| 131 if (item.isStyleValue()) { | |
| 132 StyleValue* styleValue = item.getAsStyleValue(); | |
| 133 if (!CSSOMTypes::propertyCanTake(propertyID, *styleValue)) { | |
| 134 exceptionState.throwTypeError("Invalid type for property"); | |
| 135 return; | |
| 136 } | |
| 137 cssValueList->append(item.getAsStyleValue()->toCSSValue()); | |
| 138 } else if (item.isStyleValueSequence()) { | |
| 139 for (StyleValue* styleValue : item.getAsStyleValueSequence()) { | |
| 140 if (!CSSOMTypes::propertyCanTake(propertyID, *styleValue)) { | |
| 141 exceptionState.throwTypeError("Invalid type for property"); | |
| 142 return; | |
| 143 } | |
| 144 cssValueList->append(styleValue->toCSSValue()); | |
| 145 } | |
| 146 } else if (item.isString()) { | |
| 147 // Parse it. | |
| 148 // TODO(meade): Implement this. | |
| 149 exceptionState.throwTypeError("Not implemented yet"); | |
| 150 return; | |
| 151 } else { | |
| 152 exceptionState.throwTypeError("Cannot append null to property"); | |
| 153 return; | |
| 154 } | |
| 155 | |
| 156 m_ownerElement->setInlineStyleProperty(propertyID, cssValueList); | |
| 157 } | |
| 158 | |
| 159 void InlineStylePropertyMap::remove(CSSPropertyID propertyID, ExceptionState& ex ceptionState) | |
| 160 { | |
| 161 ASSERT(propertyID != CSSPropertyInvalid); | |
| 162 m_ownerElement->removeInlineStyleProperty(propertyID); | |
| 163 } | |
| 164 | |
| 165 } // namespace blink | |
| 166 | |
| OLD | NEW |