Chromium Code Reviews| Index: third_party/WebKit/Source/core/css/cssom/InlineStylePropertyMap.cpp |
| diff --git a/third_party/WebKit/Source/core/css/cssom/InlineStylePropertyMap.cpp b/third_party/WebKit/Source/core/css/cssom/InlineStylePropertyMap.cpp |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d36d4550de2274649253590a1658655033890720 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/css/cssom/InlineStylePropertyMap.cpp |
| @@ -0,0 +1,166 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "core/css/cssom/InlineStylePropertyMap.h" |
| + |
| +#include "core/CSSPropertyNames.h" |
| +#include "core/css/CSSPrimitiveValue.h" |
| +#include "core/css/CSSPropertyMetadata.h" |
| +#include "core/css/CSSValueList.h" |
| +#include "core/css/StylePropertySet.h" |
| +#include "core/css/cssom/CSSOMTypes.h" |
| +#include "core/css/cssom/SimpleLength.h" |
| +#include "core/css/cssom/StyleValueFactory.h" |
| + |
| +namespace blink { |
| + |
| +StyleValue* InlineStylePropertyMap::get(CSSPropertyID propertyID) |
| +{ |
| + StyleValueVector styleVector = getAll(propertyID); |
| + if (styleVector.isEmpty()) |
| + return nullptr; |
| + |
| + return styleVector.at(0); |
| +} |
| + |
| +StyleValueVector InlineStylePropertyMap::getAll(CSSPropertyID propertyID) |
| +{ |
| + ASSERT(propertyID != CSSPropertyInvalid); |
| + |
| + CSSValue* cssValue = m_ownerElement->ensureMutableInlineStyle().getPropertyCSSValue(propertyID); |
| + if (!cssValue) |
| + return StyleValueVector(); |
| + |
| + StyleValueVector styleValueVector; |
| + |
| + if (!cssValue->isValueList()) { |
| + StyleValue* styleValue = StyleValueFactory::create(propertyID, *cssValue); |
| + if (styleValue) |
| + styleValueVector.append(styleValue); |
| + return styleValueVector; |
| + } |
| + |
| + for (CSSValue* value : *toCSSValueList(cssValue)) { |
| + StyleValue* styleValue = StyleValueFactory::create(propertyID, *value); |
| + if (!styleValue) { |
| + return StyleValueVector(); |
| + } |
|
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.
|
| + } |
| + |
| + return styleValueVector; |
| +} |
| + |
| +bool InlineStylePropertyMap::has(CSSPropertyID propertyID) |
| +{ |
| + ASSERT(propertyID != CSSPropertyInvalid); |
| + return !getAll(propertyID).isEmpty(); |
| +} |
| + |
| +Vector<String> InlineStylePropertyMap::getProperties() |
| +{ |
| + Vector<String> result; |
| + StylePropertySet& inlineStyleSet = m_ownerElement->ensureMutableInlineStyle(); |
| + for (unsigned i = 0; i < inlineStyleSet.propertyCount(); i++) { |
| + CSSPropertyID propertyID = inlineStyleSet.propertyAt(i).id(); |
| + result.append(getPropertyNameString(propertyID)); |
| + } |
| + return result; |
| +} |
| + |
| +void InlineStylePropertyMap::set(CSSPropertyID propertyID, StyleValueOrStyleValueSequenceOrString& item, ExceptionState& exceptionState) |
| +{ |
| + ASSERT(propertyID != CSSPropertyInvalid); |
| + if (item.isNull()) { |
| + remove(propertyID, exceptionState); |
| + return; |
| + } |
| + |
| + if (item.isStyleValue()) { |
| + StyleValue* styleValue = item.getAsStyleValue(); |
| + if (!CSSOMTypes::propertyCanTake(propertyID, *styleValue)) { |
| + exceptionState.throwTypeError("Invalid type for property"); |
| + return; |
| + } |
| + m_ownerElement->setInlineStyleProperty(propertyID, styleValue->toCSSValue()); |
| + } else if (item.isStyleValueSequence()) { |
| + if (!CSSPropertyMetadata::propertySupportsMultiple(propertyID)) { |
| + exceptionState.throwTypeError("Property does not support multiple values"); |
| + return; |
| + } |
| + |
| + // TODO(meade): This won't always work. Figure out what kind of CSSValueList to create properly. |
| + CSSValueList* valueList = CSSValueList::createSpaceSeparated(); |
| + StyleValueVector styleValueVector = item.getAsStyleValueSequence(); |
| + for (const Member<StyleValue> value : styleValueVector) { |
| + if (!CSSOMTypes::propertyCanTake(propertyID, *value)) { |
| + exceptionState.throwTypeError("Invalid type for property"); |
| + return; |
| + } |
| + valueList->append(value->toCSSValue()); |
| + } |
| + |
| + m_ownerElement->setInlineStyleProperty(propertyID, valueList); |
| + } else { |
| + // Parse it. |
| + ASSERT(item.isString()); |
| + // TODO(meade): Implement this. |
| + exceptionState.throwTypeError("Not implemented yet"); |
| + } |
| +} |
| + |
| +void InlineStylePropertyMap::append(CSSPropertyID propertyID, StyleValueOrStyleValueSequenceOrString& item, ExceptionState& exceptionState) |
| +{ |
| + ASSERT(propertyID != CSSPropertyInvalid); |
| + |
| + if (!CSSPropertyMetadata::propertySupportsMultiple(propertyID)) { |
| + exceptionState.throwTypeError("Property does not support multiple values"); |
| + return; |
| + } |
| + |
| + CSSValue* cssValue = m_ownerElement->ensureMutableInlineStyle().getPropertyCSSValue(propertyID); |
| + CSSValueList* cssValueList = nullptr; |
| + if (cssValue->isValueList()) { |
| + cssValueList = toCSSValueList(cssValue); |
| + } else { |
| + // TODO(meade): Figure out which separator to use here properly. |
| + 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.
|
| + cssValueList->append(cssValue); |
| + } |
| + |
| + if (item.isStyleValue()) { |
| + StyleValue* styleValue = item.getAsStyleValue(); |
| + if (!CSSOMTypes::propertyCanTake(propertyID, *styleValue)) { |
| + exceptionState.throwTypeError("Invalid type for property"); |
| + return; |
| + } |
| + cssValueList->append(item.getAsStyleValue()->toCSSValue()); |
| + } else if (item.isStyleValueSequence()) { |
| + for (StyleValue* styleValue : item.getAsStyleValueSequence()) { |
| + if (!CSSOMTypes::propertyCanTake(propertyID, *styleValue)) { |
| + exceptionState.throwTypeError("Invalid type for property"); |
| + return; |
| + } |
| + cssValueList->append(styleValue->toCSSValue()); |
| + } |
| + } else if (item.isString()) { |
| + // Parse it. |
| + // TODO(meade): Implement this. |
| + exceptionState.throwTypeError("Not implemented yet"); |
| + return; |
| + } else { |
| + exceptionState.throwTypeError("Cannot append null to property"); |
| + return; |
| + } |
| + |
| + m_ownerElement->setInlineStyleProperty(propertyID, cssValueList); |
| +} |
| + |
| +void InlineStylePropertyMap::remove(CSSPropertyID propertyID, ExceptionState& exceptionState) |
| +{ |
| + ASSERT(propertyID != CSSPropertyInvalid); |
| + m_ownerElement->removeInlineStyleProperty(propertyID); |
| +} |
| + |
| +} // namespace blink |
| + |