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); | |
| 21 | |
| 22 updatePropertyIfNeeded(propertyID); | |
| 23 | |
| 24 if (!m_styles.contains(propertyID)) { | |
| 25 return nullptr; | |
| 26 } | |
| 27 StyleValueVector& styleVector = ensurePropertyList(propertyID); | |
| 28 if (styleVector.isEmpty()) { | |
| 29 return nullptr; | |
| 30 } | |
| 31 return styleVector.at(0); | |
| 32 } | |
| 33 | |
| 34 StyleValueVector InlineStylePropertyMap::getAll(CSSPropertyID propertyID) | |
| 35 { | |
| 36 ASSERT(propertyID != CSSPropertyInvalid); | |
| 37 | |
| 38 updatePropertyIfNeeded(propertyID); | |
| 39 | |
| 40 if (!m_styles.contains(propertyID)) | |
| 41 return StyleValueVector(); | |
| 42 | |
| 43 return ensurePropertyList(propertyID); | |
| 44 } | |
| 45 | |
| 46 bool InlineStylePropertyMap::has(CSSPropertyID propertyID) | |
| 47 { | |
| 48 ASSERT(propertyID != CSSPropertyInvalid); | |
| 49 return m_styles.contains(propertyID) && (!ensurePropertyList(propertyID).isE mpty()); | |
| 50 } | |
| 51 | |
| 52 Vector<String> InlineStylePropertyMap::getProperties() | |
| 53 { | |
| 54 Vector<String> result; | |
| 55 StylePropertySet& inlineStyleSet = m_ownerElement->ensureMutableInlineStyle( ); | |
| 56 for (unsigned i = 0; i < inlineStyleSet.propertyCount(); i++) { | |
| 57 CSSPropertyID cssPropertyID = inlineStyleSet.propertyAt(i).id(); | |
| 58 // TODO(meade): Consider whether this is necessary here. | |
| 59 updatePropertyIfNeeded(cssPropertyID); | |
| 60 result.append(getPropertyNameString(cssPropertyID)); | |
| 61 } | |
| 62 return result; | |
| 63 } | |
| 64 | |
| 65 void InlineStylePropertyMap::set(CSSPropertyID propertyID, StyleValueOrStyleValu eSequenceOrString& item, ExceptionState& exceptionState) | |
| 66 { | |
| 67 ASSERT(propertyID != CSSPropertyInvalid); | |
| 68 if (!item.isNull()) { | |
|
Timothy Loh
2016/03/23 03:45:09
IMO clearer to not negate here and switch the orde
meade_UTC10
2016/03/29 06:27:36
Done.
| |
| 69 m_styles.remove(propertyID); | |
| 70 append(propertyID, item, exceptionState); | |
| 71 } else { | |
| 72 // Clear it. | |
|
Timothy Loh
2016/03/23 03:45:08
I don't think this is a useful comment.
meade_UTC10
2016/03/29 06:27:36
Removed.
| |
| 73 remove(propertyID, exceptionState); | |
| 74 } | |
| 75 } | |
| 76 | |
| 77 void InlineStylePropertyMap::append(CSSPropertyID propertyID, StyleValueOrStyleV alueSequenceOrString& item, ExceptionState& exceptionState) | |
| 78 { | |
| 79 ASSERT(propertyID != CSSPropertyInvalid); | |
| 80 | |
| 81 StyleValueVector& values = ensurePropertyList(propertyID); | |
| 82 bool supportsMultiple = CSSPropertyMetadata::propertySupportsMultiple(proper tyID); | |
| 83 | |
| 84 if (item.isStyleValue()) { | |
| 85 // Setting a single value. | |
| 86 StyleValue* value = item.getAsStyleValue(); | |
| 87 if (!CSSOMTypes::propertyCanTake(propertyID, *value)) { | |
| 88 exceptionState.throwTypeError("Invalid type for property"); | |
| 89 return; | |
| 90 } | |
| 91 if (!values.isEmpty() && !supportsMultiple) { | |
| 92 exceptionState.throwTypeError("Property does not support multiple va lues"); | |
| 93 return; | |
| 94 } | |
| 95 values.append(value); | |
| 96 | |
| 97 } else if (item.isStyleValueSequence()) { | |
| 98 if (!CSSPropertyMetadata::propertySupportsMultiple(propertyID)) { | |
|
Timothy Loh
2016/03/23 03:45:09
!supportsMultiple
meade_UTC10
2016/03/29 06:27:36
Done.
| |
| 99 exceptionState.throwTypeError("Property does not support multiple va lues"); | |
|
Timothy Loh
2016/03/23 03:45:09
need a return here?
meade_UTC10
2016/03/29 06:27:36
Done.
| |
| 100 } | |
| 101 // Check all the values in the sequence to make sure they're valid. | |
| 102 const StyleValueVector specifiedValues = item.getAsStyleValueSequence(); | |
| 103 for (const Member<StyleValue> value : specifiedValues) { | |
| 104 if (!CSSOMTypes::propertyCanTake(propertyID, *value)) { | |
| 105 exceptionState.throwTypeError("Invalid type for property"); | |
| 106 return; | |
| 107 } | |
| 108 } | |
| 109 values.appendVector(specifiedValues); | |
| 110 | |
| 111 } else if (item.isNull()) { | |
|
Timothy Loh
2016/03/23 03:45:09
IMO put this last (so the if structure matches the
meade_UTC10
2016/03/29 06:27:36
Done.
| |
| 112 // No-op | |
| 113 return; | |
| 114 } else if (item.isString()) { | |
| 115 // Parse it. | |
| 116 // TODO(meade): Implement this. | |
| 117 exceptionState.throwTypeError("Not implemented yet"); | |
| 118 return; | |
| 119 } | |
| 120 | |
| 121 if (supportsMultiple) { | |
| 122 RefPtrWillBeRawPtr<CSSValueList> valueList = CSSValueList::createSpaceSe parated(); | |
| 123 for (const Member<StyleValue> value : values) { | |
| 124 valueList->append(value->toCSSValue()); | |
| 125 } | |
| 126 m_ownerElement->setInlineStyleProperty(propertyID, valueList); | |
| 127 } else { | |
| 128 m_ownerElement->setInlineStyleProperty(propertyID, values[0]->toCSSValue ()); | |
| 129 } | |
| 130 m_cleanStyles.set(propertyID, true); | |
| 131 } | |
| 132 | |
| 133 void InlineStylePropertyMap::remove(CSSPropertyID propertyID, ExceptionState& ex ceptionState) | |
| 134 { | |
| 135 ASSERT(propertyID != CSSPropertyInvalid); | |
| 136 | |
| 137 const StyleVectorMap::iterator& iterator = m_styles.find(propertyID); | |
| 138 if (iterator == m_styles.end()) | |
| 139 return; | |
| 140 | |
| 141 m_styles.remove(iterator); | |
| 142 m_ownerElement->removeInlineStyleProperty(propertyID); | |
| 143 m_cleanStyles.set(propertyID, true); | |
| 144 } | |
| 145 | |
| 146 void InlineStylePropertyMap::updatePropertyIfNeeded(CSSPropertyID propertyID) | |
| 147 { | |
| 148 if (m_cleanStyles.contains(propertyID) && m_cleanStyles.get(propertyID)) { | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 RefPtrWillBeRawPtr<CSSValue> cssValue = m_ownerElement->ensureMutableInlineS tyle().getPropertyCSSValue(propertyID); | |
| 153 if (!cssValue) { | |
| 154 m_styles.remove(propertyID); | |
| 155 m_cleanStyles.set(propertyID, true); | |
| 156 return; | |
| 157 } | |
| 158 | |
| 159 StyleValueVector& values = ensurePropertyList(propertyID); | |
| 160 values.clear(); | |
| 161 | |
| 162 if (!cssValue->isValueList()) { | |
| 163 StyleValue* styleValue = StyleValueFactory::create(propertyID, *cssValue ); | |
| 164 if (styleValue) { | |
| 165 values.append(*styleValue); | |
| 166 } | |
| 167 } | |
| 168 | |
| 169 if (values.isEmpty()) | |
| 170 m_styles.remove(propertyID); | |
| 171 | |
| 172 // TODO(meade) implement the other cases. | |
| 173 | |
| 174 m_cleanStyles.set(propertyID, true); | |
| 175 } | |
| 176 | |
| 177 void InlineStylePropertyMap::updateCustomProperty(const String& propertyName, Pa ssRefPtrWillBeRawPtr<CSSValue> cssValue) | |
| 178 { | |
| 179 // TODO(meade): Implement. | |
| 180 } | |
| 181 | |
| 182 StyleValueVector& InlineStylePropertyMap::ensurePropertyList(CSSPropertyID prope rtyID) | |
| 183 { | |
| 184 if (!m_styles.contains(propertyID)) { | |
|
Timothy Loh
2016/03/23 03:45:09
Can probably make this a little more efficient in
meade_UTC10
2016/03/29 06:27:36
Done.
| |
| 185 m_styles.set(propertyID, StyleValueVector()); | |
| 186 } | |
| 187 // Note: If you write return m_style.get(propertyID) here, you get a copy. | |
| 188 return m_styles.find(propertyID)->value; | |
| 189 } | |
| 190 | |
| 191 } // namespace blink | |
| 192 | |
| OLD | NEW |