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 ASSERT(propertyID != CSSPropertyInvalid); | |
|
Timothy Loh
2016/04/05 07:55:03
I'm personally not a fan of these sorts of asserti
meade_UTC10
2016/04/06 04:33:13
Sure, removed. In this case you'd just end up with
Timothy Loh
2016/04/07 03:42:53
I meant here and all the other functions too.
meade_UTC10
2016/04/07 05:41:29
Done.
| |
| 21 | |
| 22 StyleValueVector styleVector = getAll(propertyID); | |
| 23 if (styleVector.isEmpty()) | |
| 24 return nullptr; | |
| 25 | |
| 26 return styleVector.at(0); | |
| 27 } | |
| 28 | |
| 29 StyleValueVector InlineStylePropertyMap::getAll(CSSPropertyID propertyID) | |
| 30 { | |
| 31 ASSERT(propertyID != CSSPropertyInvalid); | |
| 32 | |
| 33 RawPtr<CSSValue> cssValue = m_ownerElement->ensureMutableInlineStyle().getPr opertyCSSValue(propertyID); | |
|
Timothy Loh
2016/04/05 07:55:03
just write CSSValue*, you shouldn't be writing Raw
meade_UTC10
2016/04/06 04:33:13
Done.
| |
| 34 if (!cssValue) | |
| 35 return StyleValueVector(); | |
| 36 | |
| 37 StyleValueVector styleValueVector; | |
| 38 | |
| 39 if (!cssValue->isValueList()) { | |
| 40 StyleValue* styleValue = StyleValueFactory::create(propertyID, *cssValue ); | |
| 41 if (styleValue) | |
| 42 styleValueVector.append(styleValue); | |
| 43 return styleValueVector; | |
| 44 } | |
| 45 | |
| 46 for (auto& value : *toCSSValueList(cssValue.get())) { | |
|
Timothy Loh
2016/04/05 07:55:03
does it work if you write for (CSSValue* value : .
meade_UTC10
2016/04/06 04:33:13
Yep, done
| |
| 47 StyleValue* styleValue = StyleValueFactory::create(propertyID, *value); | |
| 48 if (styleValue) | |
|
Timothy Loh
2016/04/05 07:55:03
This is weird, is there some case where we end up
meade_UTC10
2016/04/06 04:33:13
Yeah, good point. Returning empty list instead.
| |
| 49 styleValueVector.append(styleValue); | |
| 50 } | |
| 51 | |
| 52 return styleValueVector; | |
| 53 } | |
| 54 | |
| 55 bool InlineStylePropertyMap::has(CSSPropertyID propertyID) | |
| 56 { | |
| 57 ASSERT(propertyID != CSSPropertyInvalid); | |
| 58 return !getAll(propertyID).isEmpty(); | |
| 59 } | |
| 60 | |
| 61 Vector<String> InlineStylePropertyMap::getProperties() | |
| 62 { | |
| 63 Vector<String> result; | |
| 64 StylePropertySet& inlineStyleSet = m_ownerElement->ensureMutableInlineStyle( ); | |
| 65 for (unsigned i = 0; i < inlineStyleSet.propertyCount(); i++) { | |
| 66 CSSPropertyID propertyID = inlineStyleSet.propertyAt(i).id(); | |
| 67 result.append(getPropertyNameString(propertyID)); | |
| 68 } | |
| 69 return result; | |
| 70 } | |
| 71 | |
| 72 void InlineStylePropertyMap::set(CSSPropertyID propertyID, StyleValueOrStyleValu eSequenceOrString& item, ExceptionState& exceptionState) | |
| 73 { | |
| 74 ASSERT(propertyID != CSSPropertyInvalid); | |
| 75 if (item.isNull()) { | |
| 76 remove(propertyID, exceptionState); | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 if (item.isStyleValue()) { | |
| 81 StyleValue* styleValue = item.getAsStyleValue(); | |
| 82 if (!CSSOMTypes::propertyCanTake(propertyID, *styleValue)) { | |
| 83 exceptionState.throwTypeError("Invalid type for property"); | |
| 84 return; | |
| 85 } | |
| 86 m_ownerElement->setInlineStyleProperty(propertyID, styleValue->toCSSValu e()); | |
| 87 } else if (item.isStyleValueSequence()) { | |
| 88 if (!CSSPropertyMetadata::propertySupportsMultiple(propertyID)) { | |
| 89 exceptionState.throwTypeError("Property does not support multiple va lues"); | |
| 90 return; | |
| 91 } | |
| 92 | |
| 93 // TODO(meade): This won't always work. Figure out what kind of CSSValue List to create properly. | |
| 94 RawPtr<CSSValueList> valueList = CSSValueList::createSpaceSeparated(); | |
| 95 StyleValueVector styleValueVector = item.getAsStyleValueSequence(); | |
| 96 for (const Member<StyleValue> value : styleValueVector) { | |
| 97 if (!CSSOMTypes::propertyCanTake(propertyID, *value)) { | |
| 98 exceptionState.throwTypeError("Invalid type for property"); | |
| 99 return; | |
| 100 } | |
| 101 valueList->append(value->toCSSValue()); | |
| 102 } | |
| 103 | |
| 104 m_ownerElement->setInlineStyleProperty(propertyID, valueList); | |
| 105 } else if (item.isString()) { | |
| 106 // Parse it. | |
| 107 // TODO(meade): Implement this. | |
| 108 exceptionState.throwTypeError("Not implemented yet"); | |
| 109 } else { | |
| 110 // Null is a no-op. | |
|
Timothy Loh
2016/04/05 07:55:03
You're already handling this above
meade_UTC10
2016/04/06 04:33:13
Oops, copy pasta error. Fixed.
| |
| 111 ASSERT(item.isNull()); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 void InlineStylePropertyMap::append(CSSPropertyID propertyID, StyleValueOrStyleV alueSequenceOrString& item, ExceptionState& exceptionState) | |
| 116 { | |
| 117 ASSERT(propertyID != CSSPropertyInvalid); | |
| 118 | |
| 119 if (!CSSPropertyMetadata::propertySupportsMultiple(propertyID)) { | |
| 120 exceptionState.throwTypeError("Property does not support multiple values "); | |
| 121 return; | |
| 122 } | |
| 123 | |
| 124 RawPtr<CSSValue> cssValue = m_ownerElement->ensureMutableInlineStyle().getPr opertyCSSValue(propertyID); | |
| 125 CSSValueList* cssValueList = toCSSValueList(cssValue.get()); | |
|
Timothy Loh
2016/04/05 07:55:04
Let's be more defensive here. propertySupportsMult
meade_UTC10
2016/04/06 04:33:13
Done.
| |
| 126 | |
| 127 if (item.isStyleValue()) { | |
| 128 StyleValue* styleValue = item.getAsStyleValue(); | |
| 129 if (!CSSOMTypes::propertyCanTake(propertyID, *styleValue)) { | |
| 130 exceptionState.throwTypeError("Invalid type for property"); | |
| 131 return; | |
| 132 } | |
| 133 cssValueList->append(item.getAsStyleValue()->toCSSValue()); | |
|
Timothy Loh
2016/04/05 07:55:03
OK, although I'm slightly wary here since this is
meade_UTC10
2016/04/06 04:33:12
Right. Would it be better to make a copy of it sin
| |
| 134 } else if (item.isStyleValueSequence()) { | |
| 135 for (StyleValue* styleValue : item.getAsStyleValueSequence()) { | |
| 136 if (!CSSOMTypes::propertyCanTake(propertyID, *styleValue)) { | |
| 137 exceptionState.throwTypeError("Invalid type for property"); | |
| 138 return; | |
| 139 } | |
| 140 cssValueList->append(styleValue->toCSSValue()); | |
| 141 } | |
| 142 } else if (item.isString()) { | |
| 143 // Parse it. | |
| 144 // TODO(meade): Implement this. | |
| 145 exceptionState.throwTypeError("Not implemented yet"); | |
| 146 return; | |
| 147 } else { | |
| 148 // Null is a no-op. | |
|
Timothy Loh
2016/04/05 07:55:03
not an error?
meade_UTC10
2016/04/06 04:33:13
Fixed.
| |
| 149 ASSERT(item.isNull()); | |
| 150 return; | |
| 151 } | |
| 152 | |
| 153 m_ownerElement->setInlineStyleProperty(propertyID, cssValueList); | |
| 154 } | |
| 155 | |
| 156 void InlineStylePropertyMap::remove(CSSPropertyID propertyID, ExceptionState& ex ceptionState) | |
| 157 { | |
| 158 ASSERT(propertyID != CSSPropertyInvalid); | |
| 159 m_ownerElement->removeInlineStyleProperty(propertyID); | |
| 160 } | |
| 161 | |
| 162 } // namespace blink | |
| 163 | |
| OLD | NEW |