Commit f01caa59 authored by meade's avatar meade Committed by Commit bot

Implement iterable<DOMString, (StyleValue or sequence<StyleValue>)> for InlineStylePropertyMap

Spec: https://drafts.css-houdini.org/css-typed-om/#the-stylepropertymap

BUG=545318

Review-Url: https://codereview.chromium.org/1874623002
Cr-Commit-Position: refs/heads/master@{#398190}
parent b9164c43
...@@ -47,6 +47,6 @@ test(function() { ...@@ -47,6 +47,6 @@ test(function() {
test(function() { test(function() {
assert_true(computedStyleMap.has('width')); assert_true(computedStyleMap.has('width'));
}, 'has() returns true for a set property.'); }, 'has() returns true for a supported property.');
</script> </script>
This is a testharness.js-based test.
PASS Iteration over empty StyleMap returns empty iterator
PASS Iterator for single entry returns iterator with a single value
PASS Iterator for single key returns iterator with a single value
PASS Iterator for single value returns iterator with a single value
FAIL Iterating entries over border element expansion assert_equals: expected (string) "solid" but got (undefined) undefined
Harness: the test ran to completion.
<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<div id="testElement"></div>
<script>
test(function() {
testElement.style = "";
var entryIterator = testElement.styleMap.entries();
var keyIterator = testElement.styleMap.keys();
var valueIterator = testElement.styleMap.values();
assert_true(entryIterator.next().done);
assert_true(keyIterator.next().done);
assert_true(valueIterator.next().done);
}, "Iteration over empty StyleMap returns empty iterator");
test(function() {
testElement.style = "width: 50px";
var iterator = testElement.styleMap.entries();
var entry = iterator.next();
assert_false(entry.done);
// Should only be one entry.
assert_true(iterator.next().done);
assert_equals(entry.value[0], 'width');
assert_true(entry.value[1] instanceof CSSSimpleLength);
assert_equals(entry.value[1].cssString, '50px');
}, "Iterator for single entry returns iterator with a single value");
test(function() {
testElement.style = "width: 60px";
var iterator = testElement.styleMap.keys();
var entry = iterator.next();
assert_false(entry.done);
// Should only be one entry.
assert_true(iterator.next().done);
assert_equals(entry.value, 'width');
}, "Iterator for single key returns iterator with a single value");
test(function() {
testElement.style = "width: 70px";
var iterator = testElement.styleMap.values();
var entry = iterator.next();
assert_false(entry.done);
// Should only be one entry.
assert_true(iterator.next().done);
assert_true(entry.value instanceof CSSSimpleLength);
assert_equals(entry.value.cssString, '70px');
}, "Iterator for single value returns iterator with a single value");
test(function() {
testElement.style = "border: 5px solid lightcoral";
var entries = {};
var numEntries = 0;
for (let value of testElement.styleMap.entries()) {
numEntries++;
entries[value[0]] = value[1];
}
assert_equals(numEntries, 17);
assert_equals(entries['border-top-width'].cssString, '5px');
assert_equals(entries['border-right-width'].cssString, '5px');
assert_equals(entries['border-bottom-width'].cssString, '5px');
assert_equals(entries['border-left-width'].cssString, '5px');
assert_equals(entries['border-top-style'].cssString, 'solid');
assert_equals(entries['border-right-style'].cssString, 'solid');
assert_equals(entries['border-bottom-style'].cssString, 'solid');
assert_equals(entries['border-left-style'].cssString, 'solid');
assert_equals(entries['border-top-color'].cssString, 'lightcoral');
assert_equals(entries['border-right-color'].cssString, 'lightcoral');
assert_equals(entries['border-bottom-color'].cssString, 'lightcoral');
assert_equals(entries['border-left-color'].cssString, 'lightcoral');
assert_equals(entries['border-image-source'].cssString, 'initial');
assert_equals(entries['border-image-slice'].cssString, 'initial');
assert_equals(entries['border-image-width'].cssString, 'initial');
assert_equals(entries['border-image-outset'].cssString, 'initial');
assert_equals(entries['border-image-repeat'].cssString, 'initial');
}, "Iterating entries over border element expansion");
</script>
<!DOCTYPE html>
<script src="../../resources/testharness.js"></script>
<script src="../../resources/testharnessreport.js"></script>
<div id="testElement"></div>
<script>
test(function() {
testElement.style = "width: 60px; border-left-width: 30px;";
var iterator = testElement.styleMap.entries();
var entry = iterator.next();
assert_equals(entry.value[0], 'width');
assert_true(entry.value[1] instanceof CSSSimpleLength);
assert_equals(entry.value[1].cssString, '60px');
// This shouldn't appear in the iterator.
testElement.style.borderTopWidth = '10px';
entry = iterator.next();
assert_equals(entry.value[0], 'border-left-width');
assert_true(entry.value[1] instanceof CSSSimpleLength);
assert_equals(entry.value[1].cssString, '30px');
assert_true(iterator.next().done);
}, "Adding a property while iterating over entries() doesn't affect iterator");
test(function() {
testElement.style = "width: 60px; border-left-width: 30px;";
var iterator = testElement.styleMap.values();
var entry = iterator.next();
assert_true(entry.value instanceof CSSSimpleLength);
assert_equals(entry.value.cssString, '60px');
// This shouldn't appear in the iterator.
testElement.style.borderTopWidth = '10px';
entry = iterator.next();
assert_true(entry.value instanceof CSSSimpleLength);
assert_equals(entry.value.cssString, '30px');
assert_true(iterator.next().done);
}, "Adding a property while iterating over values() doesn't affect current iterator");
test(function() {
testElement.style = "width: 60px; border-left-width: 30px;";
var iterator = testElement.styleMap.keys();
var entry = iterator.next();
assert_equals(entry.value, 'width');
// This shouldn't appear in the iterator.
testElement.style.borderTopWidth = '10px';
entry = iterator.next();
assert_equals(entry.value, 'border-left-width');
assert_true(iterator.next().done);
}, "Adding a property while iterating over keys() doesn't affect iterator");
</script>
...@@ -33,7 +33,7 @@ protected: ...@@ -33,7 +33,7 @@ protected:
: ImmutableStylePropertyMap() : ImmutableStylePropertyMap()
, m_computedStyleDeclaration(computedStyleDeclaration) { } , m_computedStyleDeclaration(computedStyleDeclaration) { }
IterationSource* startIteration(ScriptState*, ExceptionState&) override { return nullptr; } HeapVector<StylePropertyMapEntry> getIterationEntries() override { return HeapVector<StylePropertyMapEntry>(); }
Member<CSSStyleDeclaration> m_computedStyleDeclaration; Member<CSSStyleDeclaration> m_computedStyleDeclaration;
}; };
......
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
#include "core/css/cssom/InlineStylePropertyMap.h" #include "core/css/cssom/InlineStylePropertyMap.h"
#include "bindings/core/v8/Iterable.h"
#include "core/CSSPropertyNames.h" #include "core/CSSPropertyNames.h"
#include "core/css/CSSPrimitiveValue.h" #include "core/css/CSSPrimitiveValue.h"
#include "core/css/CSSPropertyMetadata.h" #include "core/css/CSSPropertyMetadata.h"
...@@ -117,5 +118,23 @@ void InlineStylePropertyMap::remove(CSSPropertyID propertyID, ExceptionState& ex ...@@ -117,5 +118,23 @@ void InlineStylePropertyMap::remove(CSSPropertyID propertyID, ExceptionState& ex
m_ownerElement->removeInlineStyleProperty(propertyID); m_ownerElement->removeInlineStyleProperty(propertyID);
} }
HeapVector<StylePropertyMap::StylePropertyMapEntry> InlineStylePropertyMap::getIterationEntries()
{
HeapVector<StylePropertyMap::StylePropertyMapEntry> result;
StylePropertySet& inlineStyleSet = m_ownerElement->ensureMutableInlineStyle();
for (unsigned i = 0; i < inlineStyleSet.propertyCount(); i++) {
StylePropertySet::PropertyReference propertyReference = inlineStyleSet.propertyAt(i);
CSSPropertyID propertyID = propertyReference.id();
StyleValueVector styleValueVector = cssValueToStyleValueVector(propertyID, *propertyReference.value());
StyleValueOrStyleValueSequence value;
if (styleValueVector.size() == 1)
value.setStyleValue(styleValueVector[0]);
else
value.setStyleValueSequence(styleValueVector);
result.append(std::make_pair(getPropertyNameString(propertyID), value));
}
return result;
}
} // namespace blink } // namespace blink
...@@ -29,6 +29,9 @@ public: ...@@ -29,6 +29,9 @@ public:
MutableStylePropertyMap::trace(visitor); MutableStylePropertyMap::trace(visitor);
} }
protected:
HeapVector<StylePropertyMapEntry> getIterationEntries() override;
private: private:
Member<Element> m_ownerElement; Member<Element> m_ownerElement;
}; };
......
...@@ -12,6 +12,40 @@ ...@@ -12,6 +12,40 @@
namespace blink { namespace blink {
namespace {
class StylePropertyMapIterationSource final : public PairIterable<String, StyleValueOrStyleValueSequence>::IterationSource {
public:
explicit StylePropertyMapIterationSource(HeapVector<StylePropertyMap::StylePropertyMapEntry> values)
: m_index(0)
, m_values(values)
{
}
bool next(ScriptState*, String& key, StyleValueOrStyleValueSequence& value, ExceptionState&) override
{
if (m_index >= m_values.size())
return false;
const StylePropertyMap::StylePropertyMapEntry& pair = m_values.at(m_index++);
key = pair.first;
value = pair.second;
return true;
}
DEFINE_INLINE_VIRTUAL_TRACE()
{
visitor->trace(m_values);
PairIterable<String, StyleValueOrStyleValueSequence>::IterationSource::trace(visitor);
}
private:
size_t m_index;
const HeapVector<StylePropertyMap::StylePropertyMapEntry> m_values;
};
} // namespace
StyleValue* StylePropertyMap::get(const String& propertyName, ExceptionState& exceptionState) StyleValue* StylePropertyMap::get(const String& propertyName, ExceptionState& exceptionState)
{ {
CSSPropertyID propertyID = cssPropertyID(propertyName); CSSPropertyID propertyID = cssPropertyID(propertyName);
...@@ -103,4 +137,9 @@ StylePropertyMap::StyleValueVector StylePropertyMap::cssValueToStyleValueVector( ...@@ -103,4 +137,9 @@ StylePropertyMap::StyleValueVector StylePropertyMap::cssValueToStyleValueVector(
return styleValueVector; return styleValueVector;
} }
StylePropertyMap::IterationSource* StylePropertyMap::startIteration(ScriptState*, ExceptionState&)
{
return new StylePropertyMapIterationSource(getIterationEntries());
}
} // namespace blink } // namespace blink
...@@ -22,6 +22,7 @@ class CORE_EXPORT StylePropertyMap : public GarbageCollectedFinalized<StylePrope ...@@ -22,6 +22,7 @@ class CORE_EXPORT StylePropertyMap : public GarbageCollectedFinalized<StylePrope
DEFINE_WRAPPERTYPEINFO(); DEFINE_WRAPPERTYPEINFO();
public: public:
typedef HeapVector<Member<StyleValue>> StyleValueVector; typedef HeapVector<Member<StyleValue>> StyleValueVector;
typedef std::pair<String, StyleValueOrStyleValueSequence> StylePropertyMapEntry;
virtual ~StylePropertyMap() { } virtual ~StylePropertyMap() { }
...@@ -47,7 +48,8 @@ public: ...@@ -47,7 +48,8 @@ public:
protected: protected:
StylePropertyMap() { } StylePropertyMap() { }
IterationSource* startIteration(ScriptState*, ExceptionState&) override { return nullptr; } virtual HeapVector<StylePropertyMapEntry> getIterationEntries() = 0;
IterationSource* startIteration(ScriptState*, ExceptionState&) override;
StyleValueVector cssValueToStyleValueVector(CSSPropertyID, const CSSValue&); StyleValueVector cssValueToStyleValueVector(CSSPropertyID, const CSSValue&);
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment