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

Implement ComputedStylePropertyMap for Typed OM.

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

Uses a  partial IDL interface to attach a getComputedStyleMap method to
window.

Implements the getAll and getProperties.
Moved has() up into the base class from InlineStylePropertyMap.

Not implemented: iteration

BUG=545318

Review-Url: https://codereview.chromium.org/1849653003
Cr-Commit-Position: refs/heads/master@{#397992}
parent 19c6e190
<!DOCTYPE html>
<script src='../../resources/testharness.js'></script>
<script src='../../resources/testharnessreport.js'></script>
<div id="testElement"></div>
<script>
var computedStyleMap = getComputedStyleMap(testElement);
test(function() {
testElement.style.borderTop = '10px solid #000';
var result = computedStyleMap.get('border-top-width');
assert_true(result instanceof CSSSimpleLength);
assert_equals(result.cssString, '10px');
}, "Getting a 10px border-top-width returns a CSSSimpleLength");
test(function() {
testElement.style.borderTop = '20px solid #000';
var result = computedStyleMap.getAll('border-top-width');
assert_equals(result.length, 1);
assert_equals(result[0].cssString, '20px');
}, "getAll for border-top-width returns a single value");
</script>
<!DOCTYPE html>
<script src='../../resources/testharness.js'></script>
<script src='../../resources/testharnessreport.js'></script>
<div id="testElement"></div>
<script>
var computedStyleMap = getComputedStyleMap(testElement);
var computedStyle = getComputedStyle(testElement);
test(function() {
assert_throws(new TypeError(), function() {
getComputedStyleMap(null);
});
}, 'Test that passing null to getComputedStyleMap does not crash');
test(function() {
var properties = computedStyleMap.getProperties();
assert_equals(properties.length, computedStyle.length);
}, "Computed StyleMap.getProperties returns the same number of properties as ComputedStyle");
test(function() {
var properties = computedStyleMap.getProperties();
for (var i = 0; i < properties.length; i++) {
var property = properties[i];
var styleValues = computedStyleMap.getAll(property);
if (styleValues.length == 1) {
// TODO(meade): Remove this if statement once all properties are supported.
// TODO(meade): Handle sequence types.
assert_equals(computedStyleMap.get(property).cssString, computedStyle[property]);
}
}
}, 'Properties retrieved from ComputedStyleMap reflect the same values as from ComputedStyle');
test(function() {
assert_false(computedStyleMap.has('max-zoom'));
}, 'has() return false for an unsupported property.');
test(function() {
assert_throws(null, function() { computedStyleMap.has('bananas'); });
}, 'has() throws for an invalid property.');
test(function() {
assert_false(computedStyleMap.has('border'));
}, 'has() return false for unsupported shorthand properties.');
test(function() {
assert_true(computedStyleMap.has('width'));
}, 'has() returns true for a set property.');
</script>
<!DOCTYPE html>
<script src='../../resources/testharness.js'></script>
<script src='../../resources/testharnessreport.js'></script>
<style>
#testElement {
border: 5px solid purple;
}
#testElement:after {
border: 1px solid black;
}
</style>
<div id="testElement"></div>
<script>
var computedStyleMap = getComputedStyleMap(testElement);
var pseudoComputedStyleMap = getComputedStyleMap(testElement, '::after');
test(function() {
assert_equals(computedStyleMap.get('border-top-width').cssString, '5px');
assert_equals(pseudoComputedStyleMap.get('border-top-width').cssString, '1px');
}, 'get on Computed StyleMap of pseudo element returns correct styles');
test(function() {
// TODO(meade): When actual multiple valued properties are supported, use one of them.
var styleValueList = pseudoComputedStyleMap.getAll('border-top-width');
assert_equals(styleValueList.length, 1);
assert_equals(styleValueList[0].cssString, '1px');
}, 'getAll on Computed StyleMap of pseudo element returns list of correct styles');
</script>
<!DOCTYPE html>
<script src='../../resources/testharness.js'></script>
<script src='../../resources/testharnessreport.js'></script>
<div id="testElement"></div>
<script>
var computedStyleMap = getComputedStyleMap(testElement);
test(function() {
testElement.style.width = '10px';
var result = computedStyleMap.get('width');
assert_true(result instanceof CSSSimpleLength);
assert_equals(result.cssString, '10px');
}, "Getting a 10px width results in a CSSSimpleLength");
test(function() {
testElement.style.width = '20px';
var result = computedStyleMap.getAll('width');
assert_equals(result.length, 1);
assert_equals(result[0].cssString, '20px');
}, "getAll for width returns a single value");
</script>
......@@ -7671,6 +7671,7 @@ interface webkitURL
method focus
method gc
method getComputedStyle
method getComputedStyleMap
method getMatchedCSSRules
method getSelection
method matchMedia
......
......@@ -421,6 +421,7 @@
'animation/DocumentAnimation.idl',
'animation/ElementAnimation.idl',
'css/DocumentFontFaceSet.idl',
'css/cssom/WindowGetComputedStyle.idl',
'dom/ChildNode.idl',
'dom/DocumentFullscreen.idl',
'dom/DocumentOrShadowRoot.idl',
......@@ -1356,6 +1357,8 @@
'css/cssom/CSSSkew.h',
'css/cssom/CSSTranslation.cpp',
'css/cssom/CSSTranslation.h',
'css/cssom/ComputedStylePropertyMap.cpp',
'css/cssom/ComputedStylePropertyMap.h',
'css/cssom/ImmutableStylePropertyMap.h',
'css/cssom/InlineStylePropertyMap.cpp',
'css/cssom/InlineStylePropertyMap.h',
......@@ -1371,6 +1374,7 @@
'css/cssom/TransformComponent.h',
'css/cssom/TransformValue.cpp',
'css/cssom/TransformValue.h',
'css/cssom/WindowGetComputedStyle.h',
'css/invalidation/InvalidationSet.cpp',
'css/invalidation/InvalidationSet.h',
'css/invalidation/PendingInvalidations.h',
......
// 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/ComputedStylePropertyMap.h"
#include "core/css/CSSComputedStyleDeclaration.h"
#include "core/css/cssom/StyleValueFactory.h"
namespace blink {
StylePropertyMap::StyleValueVector ComputedStylePropertyMap::getAll(CSSPropertyID propertyID)
{
CSSValue* cssValue = m_computedStyleDeclaration->getPropertyCSSValueInternal(propertyID);
if (!cssValue)
return StylePropertyMap::StyleValueVector();
return cssValueToStyleValueVector(propertyID, *cssValue);
}
Vector<String> ComputedStylePropertyMap::getProperties()
{
Vector<String> result;
for (unsigned i = 0; i < m_computedStyleDeclaration->length(); i++) {
result.append(m_computedStyleDeclaration->item(i));
}
return result;
}
} // namespace blink
// 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.
#ifndef ComputedStylePropertyMap_h
#define ComputedStylePropertyMap_h
#include "core/css/CSSComputedStyleDeclaration.h"
#include "core/css/cssom/ImmutableStylePropertyMap.h"
namespace blink {
class CORE_EXPORT ComputedStylePropertyMap : public ImmutableStylePropertyMap {
WTF_MAKE_NONCOPYABLE(ComputedStylePropertyMap);
public:
static ComputedStylePropertyMap* create(CSSComputedStyleDeclaration* computedStyleDeclaration)
{
return new ComputedStylePropertyMap(computedStyleDeclaration);
}
StyleValueVector getAll(CSSPropertyID) override;
Vector<String> getProperties() override;
DEFINE_INLINE_VIRTUAL_TRACE()
{
visitor->trace(m_computedStyleDeclaration);
ImmutableStylePropertyMap::trace(visitor);
}
protected:
ComputedStylePropertyMap(CSSComputedStyleDeclaration* computedStyleDeclaration)
: ImmutableStylePropertyMap()
, m_computedStyleDeclaration(computedStyleDeclaration) { }
IterationSource* startIteration(ScriptState*, ExceptionState&) override { return nullptr; }
Member<CSSStyleDeclaration> m_computedStyleDeclaration;
};
} // namespace blink
#endif
......@@ -26,6 +26,9 @@ public:
{
exceptionState.throwTypeError("This StylePropertyMap is immutable.");
}
protected:
ImmutableStylePropertyMap() = default;
};
} // namespace blink
......
......@@ -23,11 +23,6 @@ StylePropertyMap::StyleValueVector InlineStylePropertyMap::getAll(CSSPropertyID
return cssValueToStyleValueVector(propertyID, *cssValue);
}
bool InlineStylePropertyMap::has(CSSPropertyID propertyID)
{
return !getAll(propertyID).isEmpty();
}
Vector<String> InlineStylePropertyMap::getProperties()
{
Vector<String> result;
......
......@@ -17,7 +17,6 @@ public:
: m_ownerElement(ownerElement) { }
StyleValueVector getAll(CSSPropertyID) override;
bool has(CSSPropertyID) override;
Vector<String> getProperties() override;
void set(CSSPropertyID, StyleValueOrStyleValueSequenceOrString&, ExceptionState&) override;
......
......@@ -43,7 +43,7 @@ bool StylePropertyMap::has(const String& propertyName, ExceptionState& exception
{
CSSPropertyID propertyID = cssPropertyID(propertyName);
if (propertyID != CSSPropertyInvalid)
return has(propertyID);
return !getAll(propertyID).isEmpty();
// TODO(meade): Handle custom properties here.
exceptionState.throwTypeError("Invalid propertyName: " + propertyName);
......
......@@ -31,8 +31,6 @@ public:
bool has(const String& propertyName, ExceptionState&);
virtual StyleValueVector getAll(CSSPropertyID) = 0;
virtual bool has(CSSPropertyID) = 0;
virtual Vector<String> getProperties() = 0;
// Modifiers.
......
// 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.
#ifndef WindowGetComputedStyle_h
#define WindowGetComputedStyle_h
#include "core/css/CSSComputedStyleDeclaration.h"
#include "core/css/cssom/ComputedStylePropertyMap.h"
namespace blink {
class WindowGetComputedStyle {
STATIC_ONLY(WindowGetComputedStyle);
public:
static StylePropertyMap* getComputedStyleMap(const DOMWindow&, Element* element, const String& pseudoElement)
{
DCHECK(element);
CSSComputedStyleDeclaration* computedStyleDeclaration = CSSComputedStyleDeclaration::create(element, false, pseudoElement);
return ComputedStylePropertyMap::create(computedStyleDeclaration);
}
};
} // namespace blink
#endif // WindowGetComputedStyle_h
// 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.
// https://drafts.css-houdini.org/css-typed-om/#computed-stylepropertymap-objects
[
RuntimeEnabled=CSSTypedOM,
] partial interface Window {
[NewObject] StylePropertyMap getComputedStyleMap(Element element, optional DOMString? pseudoElement = null);
};
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