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 fil | |
| 4 | |
| 5 #include "core/css/cssom/InlineStylePropertyMap.h" | |
| 6 | |
| 7 #include "core/CSSPropertyNames.h" | |
| 8 #include "core/css/CSSPrimitiveValue.h" | |
| 9 #include "core/css/CSSValueList.h" | |
| 10 #include "core/css/StylePropertySet.h" | |
| 11 #include "core/css/cssom/CSSOMTypes.h" | |
| 12 #include "core/css/cssom/SimpleLength.h" | |
| 13 | |
| 14 namespace blink { | |
| 15 | |
| 16 StyleValue* InlineStylePropertyMap::get(CSSPropertyID propertyID) | |
| 17 { | |
| 18 if (!m_cleanStyles.get(propertyID)) { | |
|
Timothy Loh
2016/02/09 07:29:32
Maybe better to move the duplicated logic here and
meade_UTC10
2016/02/10 05:55:39
Done.
| |
| 19 // First update the property if it is not clean. | |
| 20 updateProperty(propertyID, m_ownerElement->ensureMutableInlineStyle().ge tPropertyCSSValue(propertyID)); | |
| 21 } | |
| 22 | |
| 23 if (!m_styles.contains(propertyID)) { | |
| 24 return nullptr; | |
| 25 } | |
| 26 HeapVector<Member<StyleValue>>& styleVector = ensurePropertyList(propertyID) ; | |
| 27 if (styleVector.size() < 1) { | |
|
Timothy Loh
2016/02/09 07:29:32
There's probably an empty() or isEmpty() function
meade_UTC10
2016/02/10 05:55:39
Done. AFAIK it's supposed to return the first valu
| |
| 28 return nullptr; | |
| 29 } | |
| 30 return styleVector.at(0); | |
| 31 } | |
| 32 | |
| 33 HeapVector<Member<StyleValue>> InlineStylePropertyMap::getAll(CSSPropertyID prop ertyID) | |
| 34 { | |
| 35 if (!m_cleanStyles.get(propertyID)) { | |
| 36 // First update the property if it is not clean. | |
| 37 updateProperty(propertyID, m_ownerElement->ensureMutableInlineStyle().ge tPropertyCSSValue(propertyID)); | |
| 38 } | |
| 39 | |
| 40 if (!m_styles.contains(propertyID)) | |
| 41 return HeapVector<Member<StyleValue>>(); | |
| 42 | |
| 43 return ensurePropertyList(propertyID); | |
| 44 } | |
| 45 | |
| 46 bool InlineStylePropertyMap::has(CSSPropertyID propertyID) | |
| 47 { | |
| 48 return m_styles.contains(propertyID) && (ensurePropertyList(propertyID).size () > 0); | |
| 49 } | |
| 50 | |
| 51 Vector<String> InlineStylePropertyMap::getProperties() | |
| 52 { | |
| 53 Vector<String> result; | |
| 54 for (const auto key : m_styles.keys()) { | |
|
Timothy Loh
2016/02/09 07:29:32
Using a "const auto" type does a copy of the value
meade_UTC10
2016/02/10 05:55:39
Done.
| |
| 55 result.append(getPropertyNameString(key)); | |
| 56 } | |
| 57 return result; | |
| 58 } | |
| 59 | |
| 60 void InlineStylePropertyMap::set(CSSPropertyID propertyID, StyleValueOrStyleValu eSequenceOrString& item, ExceptionState& exceptionState) | |
| 61 { | |
| 62 if (!item.isNull()) { | |
| 63 if (m_styles.contains(propertyID)) | |
|
Timothy Loh
2016/02/09 07:29:32
I think remove() does this check, so we don't need
meade_UTC10
2016/02/10 05:55:39
Done.
| |
| 64 m_styles.remove(propertyID); | |
| 65 append(propertyID, item, exceptionState); | |
|
Timothy Loh
2016/02/09 07:29:32
doesn't this not work for non-lists? because appen
meade_UTC10
2016/02/10 05:55:39
As implemented, it only does that if it's a singlu
| |
| 66 } else { | |
| 67 // Clear it. | |
| 68 remove(propertyID, exceptionState); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 void InlineStylePropertyMap::append(CSSPropertyID propertyID, StyleValueOrStyleV alueSequenceOrString& item, ExceptionState& exceptionState) | |
| 73 { | |
| 74 ASSERT(propertyID != CSSPropertyInvalid); | |
| 75 | |
| 76 // TODO other pre-validation? | |
| 77 HeapVector<Member<StyleValue>>& values = ensurePropertyList(propertyID); | |
| 78 | |
| 79 if (item.isStyleValue()) { | |
| 80 // Setting a single value. | |
| 81 StyleValue* value = item.getAsStyleValue(); | |
| 82 if (!CSSOMTypes::propertyCanTake(propertyID, value->type())) { | |
| 83 exceptionState.throwTypeError("Invalid type for property"); | |
| 84 return; | |
| 85 } | |
| 86 if (values.size() != 0 && !CSSOMTypes::propertySupportsMultiple(property ID)) { | |
| 87 exceptionState.throwTypeError("Property does not support multiple va lues"); | |
| 88 return; | |
| 89 } | |
| 90 values.append(value); | |
| 91 | |
| 92 } else if (item.isStyleValueSequence()) { | |
| 93 if (!CSSOMTypes::propertySupportsMultiple(propertyID)) { | |
| 94 exceptionState.throwTypeError("Property does not support multiple va lues"); | |
| 95 } | |
| 96 // Check all the values in the sequence to make sure they're valid. | |
| 97 const HeapVector<Member<StyleValue>> specifiedValues = item.getAsStyleVa lueSequence(); | |
| 98 for (const auto value : specifiedValues) { | |
| 99 if (!CSSOMTypes::propertyCanTake(propertyID, value->type())) { | |
| 100 exceptionState.throwTypeError("Invalid type for property"); | |
| 101 return; | |
| 102 } | |
| 103 } | |
| 104 values.appendVector(specifiedValues); | |
| 105 | |
| 106 } else if (item.isString()) { | |
| 107 // Parse it. | |
| 108 // TODO(meade): Implement this. | |
| 109 exceptionState.throwTypeError("Not implemented yet"); | |
| 110 } | |
| 111 | |
| 112 if (values.size() == 1) { | |
| 113 m_ownerElement->setInlineStyleProperty(propertyID, values[0]->toCSSValue ()); | |
|
Timothy Loh
2016/02/09 07:29:32
Properties which accept list-of-T probably wouldn'
meade_UTC10
2016/02/10 05:55:39
Done.
| |
| 114 } else { | |
| 115 RefPtrWillBeRawPtr<CSSValueList> valueList = CSSValueList::createSpaceSe parated(); | |
| 116 for (const auto value : values) { | |
| 117 valueList->append(value->toCSSValue()); | |
| 118 } | |
| 119 m_ownerElement->setInlineStyleProperty(propertyID, valueList); | |
| 120 } | |
| 121 m_cleanStyles.set(propertyID, true); | |
| 122 } | |
| 123 | |
| 124 void InlineStylePropertyMap::remove(CSSPropertyID propertyID, ExceptionState& ex ceptionState) | |
| 125 { | |
| 126 if (!m_styles.contains(propertyID)) { | |
|
Timothy Loh
2016/02/09 07:29:32
Better to do something like below and avoid an ext
meade_UTC10
2016/02/10 05:55:39
Done.
| |
| 127 return; | |
| 128 } | |
| 129 m_styles.remove(propertyID); | |
| 130 m_ownerElement->removeInlineStyleProperty(propertyID); | |
| 131 m_cleanStyles.set(propertyID, true); | |
| 132 } | |
| 133 | |
| 134 void InlineStylePropertyMap::updateProperty(CSSPropertyID propertyID, PassRefPtr WillBeRawPtr<CSSValue> cssValue) | |
| 135 { | |
| 136 HeapVector<Member<StyleValue>>& values = ensurePropertyList(propertyID); | |
| 137 values.clear(); | |
| 138 | |
| 139 if (cssValue && !cssValue->isValueList() && !cssValue->isValuePair()) { | |
|
Timothy Loh
2016/02/09 07:29:32
I think <position> types get stored as pairs, migh
meade_UTC10
2016/02/10 05:55:39
hmm yeah. Removed.
| |
| 140 StyleValue* styleValue = StyleValue::create(*cssValue); | |
| 141 if (styleValue) { | |
| 142 values.append(*styleValue); | |
| 143 } | |
| 144 } | |
|
Timothy Loh
2016/02/09 07:29:32
missing a TODO to handle the other case(s)?
meade_UTC10
2016/02/10 05:55:39
Done.
| |
| 145 | |
| 146 m_cleanStyles.set(propertyID, true); | |
| 147 } | |
| 148 | |
| 149 void InlineStylePropertyMap::updateCustomProperty(const String& propertyName, Pa ssRefPtrWillBeRawPtr<CSSValue> cssValue) | |
| 150 { | |
| 151 // TODO(meade): Implement. | |
| 152 } | |
| 153 | |
| 154 HeapVector<Member<StyleValue>>& InlineStylePropertyMap::ensurePropertyList(CSSPr opertyID propertyID) | |
| 155 { | |
| 156 if (!m_styles.contains(propertyID)) { | |
| 157 m_styles.set(propertyID, HeapVector<Member<StyleValue>>()); | |
| 158 } | |
| 159 // Note: If you write return m_style.get(propertyID) here, you get a copy. | |
| 160 return m_styles.find(propertyID)->value; | |
| 161 } | |
| 162 | |
| 163 } // namespace blink | |
| 164 | |
| OLD | NEW |