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..9d3123459c3ab7802b7cbc83ed416bb1e3d99250 |
| --- /dev/null |
| +++ b/third_party/WebKit/Source/core/css/cssom/InlineStylePropertyMap.cpp |
| @@ -0,0 +1,163 @@ |
| +// 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) |
| +{ |
| + 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.
|
| + |
| + StyleValueVector styleVector = getAll(propertyID); |
| + if (styleVector.isEmpty()) |
| + return nullptr; |
| + |
| + return styleVector.at(0); |
| +} |
| + |
| +StyleValueVector InlineStylePropertyMap::getAll(CSSPropertyID propertyID) |
| +{ |
| + ASSERT(propertyID != CSSPropertyInvalid); |
| + |
| + RawPtr<CSSValue> cssValue = m_ownerElement->ensureMutableInlineStyle().getPropertyCSSValue(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.
|
| + if (!cssValue) |
| + return StyleValueVector(); |
| + |
| + StyleValueVector styleValueVector; |
| + |
| + if (!cssValue->isValueList()) { |
| + StyleValue* styleValue = StyleValueFactory::create(propertyID, *cssValue); |
| + if (styleValue) |
| + styleValueVector.append(styleValue); |
| + return styleValueVector; |
| + } |
| + |
| + 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
|
| + StyleValue* styleValue = StyleValueFactory::create(propertyID, *value); |
| + 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.
|
| + styleValueVector.append(styleValue); |
| + } |
| + |
| + 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. |
| + RawPtr<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 if (item.isString()) { |
| + // Parse it. |
| + // TODO(meade): Implement this. |
| + exceptionState.throwTypeError("Not implemented yet"); |
| + } else { |
| + // 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.
|
| + ASSERT(item.isNull()); |
| + } |
| +} |
| + |
| +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; |
| + } |
| + |
| + RawPtr<CSSValue> cssValue = m_ownerElement->ensureMutableInlineStyle().getPropertyCSSValue(propertyID); |
| + 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.
|
| + |
| + if (item.isStyleValue()) { |
| + StyleValue* styleValue = item.getAsStyleValue(); |
| + if (!CSSOMTypes::propertyCanTake(propertyID, *styleValue)) { |
| + exceptionState.throwTypeError("Invalid type for property"); |
| + return; |
| + } |
| + 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
|
| + } 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 { |
| + // 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.
|
| + ASSERT(item.isNull()); |
| + return; |
| + } |
| + |
| + m_ownerElement->setInlineStyleProperty(propertyID, cssValueList); |
| +} |
| + |
| +void InlineStylePropertyMap::remove(CSSPropertyID propertyID, ExceptionState& exceptionState) |
| +{ |
| + ASSERT(propertyID != CSSPropertyInvalid); |
| + m_ownerElement->removeInlineStyleProperty(propertyID); |
| +} |
| + |
| +} // namespace blink |
| + |