Commit 13efd583 authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

[css-typed-om] Fix CSSVariableReferenceValue validation.

We change CSSVariableReferenceValue to reject invalid custom property
names as per spec. We also remove .tentative extension from related
tests.

We also remove some unit tests that are replaced by WPTs.

Spec: https://drafts.css-houdini.org/css-typed-om-1/#cssvariablereferencevalue

Bug: 545318
Change-Id: I95cfe1c167a60c94083a1474b384fd584d05cca9
Reviewed-on: https://chromium-review.googlesource.com/895124
Commit-Queue: Darren Shen <shend@chromium.org>
Reviewed-by: default avatarnainar <nainar@chromium.org>
Cr-Commit-Position: refs/heads/master@{#533228}
parent 50b55405
<!doctype html>
<meta charset=utf-8>
<title>CSSVariableReferenceValue IDL</title>
<link rel="help" href="https://drafts.css-houdini.org/css-typed-om-1/#cssvariablereferencevalue">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/resources/WebIDLParser.js"></script>
<script src="/resources/idlharness.js"></script>
<script type="text/plain" id="idl">
[Constructor(DOMString variable, optional CSSUnparsedValue fallback)]
interface CSSVariableReferenceValue {
attribute DOMString variable;
readonly attribute CSSUnparsedValue? fallback;
};
</script>
<script>
'use strict';
const idlArray = new IdlArray();
idlArray.add_idls(document.getElementById('idl').textContent);
idlArray.add_objects({
CSSVariableReferenceValue: [
'new CSSVariableReferenceValue("--foo")',
]
});
idlArray.test();
</script>
<!doctype html>
<meta charset="utf-8">
<title>CSSVariableReferenceValue Error Handling</title>
<link rel="help" href="https://drafts.css-houdini.org/css-typed-om-1/#cssvariablereferencevalue">
<meta name="assert" content="Test CSSVariableReferenceValue constructor and attributes error handling" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<body>
<div id="log">
<script>
'use strict';
test(() => {
assert_throws(new TypeError(), () => new CSSVariableReferenceValue(''));
}, 'Constructing a CSSVariableReferenceValue with an empty variable name ' +
'throws a TypeError');
test(() => {
assert_throws(new TypeError(), () => new CSSVariableReferenceValue('bar'));
}, 'Constructing a CSSVariableReferenceValue with an invalid variable name ' +
'throws SyntaxError');
test(() => {
let result = new CSSVariableReferenceValue('--foo');
assert_throws(new TypeError(), () => result.variable = '');
assert_equals(result.variable, '--foo',
'Variable member should not have changed');
}, 'Updating CSSVariableReferenceValue.variable to an empty variable name ' +
'throws TypeError');
test(() => {
let result = new CSSVariableReferenceValue('--foo');
assert_throws(new TypeError(), () => result.variable = 'bar');
assert_equals(result.variable, '--foo',
'Variable member should not have changed');
}, 'Updating CSSVariableReferenceValue.variable to an invalid variable name ' +
'throws TypeError');
</script>
......@@ -2,41 +2,44 @@
<meta charset="utf-8">
<title>CSSVariableReferenceValue tests</title>
<link rel="help" href="https://drafts.css-houdini.org/css-typed-om-1/#cssvariablereferencevalue">
<meta name="assert" content="Test CSSVariableReferenceValue constructor and attributes" />
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/testhelper.js"></script>
<script>
'use strict';
test(() => {
assert_throws(new SyntaxError(), () => new CSSVariableReferenceValue('bar'));
assert_throws(new SyntaxError(), () => new CSSVariableReferenceValue(''));
}, 'Constructing a CSSVariableReferenceValue with an invalid variable name throws SyntaxError');
test(() => {
let result = new CSSVariableReferenceValue('--foo');
assert_throws(new SyntaxError(), () => result.variable = 'bar');
assert_equals(result.variable, '--foo');
assert_throws(new SyntaxError(), () => result.variable = '');
assert_equals(result.variable, '--foo');
}, 'Updating CSSVariableReferenceValue.variable to an invalid variable name throws SyntaxError');
test(() => {
const result = new CSSVariableReferenceValue('--foo');
assert_equals(result.variable, '--foo');
assert_equals(result.fallback, null);
assert_not_equals(result, null,
'A CSSVariableReferenceValue should be created');
assert_equals(result.variable, '--foo',
'Variable member should be same as passed in the constructor');
assert_equals(result.fallback, null,
'Fallback member should be null');
}, 'CSSVariableReferenceValue can be constructed with no fallback');
test(() => {
const result = new CSSVariableReferenceValue('--foo', new CSSUnparsedValue('lemon'));
assert_equals(result.variable, '--foo');
assert_style_value_equals(result.fallback, new CSSUnparsedValue('lemon'));
const result = new CSSVariableReferenceValue('--foo',
new CSSUnparsedValue('lemon'));
assert_not_equals(result, null,
'A CSSVariableReferenceValue should be created');
assert_equals(result.variable, '--foo',
'Variable member should be same as passed in the constructor');
assert_not_equals(result.fallback, null,
'Fallback member should not be null');
assert_style_value_equals(result.fallback, new CSSUnparsedValue('lemon'),
'Fallback member should be as same as passed in the constructor');
}, 'CSSVariableReferenceValue can be constructed with fallback');
test(() => {
let result = new CSSVariableReferenceValue('--foo');
result.variable = '--bar';
assert_equals(result.variable, '--bar');
assert_equals(result.variable, '--bar',
'Variable member should be updated to new value');
}, 'CSSVariableReferenceValue.variable can updated to a valid variable name');
</script>
This is a testharness.js-based test.
FAIL Constructing a CSSVariableReferenceValue with an invalid variable name throws SyntaxError assert_throws: function "() => new CSSVariableReferenceValue('bar')" did not throw
FAIL Updating CSSVariableReferenceValue.variable to an invalid variable name throws SyntaxError assert_throws: function "() => result.variable = 'bar'" did not throw
PASS CSSVariableReferenceValue can be constructed with no fallback
PASS CSSVariableReferenceValue can be constructed with fallback
PASS CSSVariableReferenceValue.variable can updated to a valid variable name
Harness: the test ran to completion.
......@@ -1698,7 +1698,6 @@ jumbo_source_set("unit_tests") {
"css/cssom/CSSResourceValueTest.cpp",
"css/cssom/CSSStyleImageValueTest.cpp",
"css/cssom/CSSUnitValueTest.cpp",
"css/cssom/CSSVariableReferenceValueTest.cpp",
"css/cssom/FilteredComputedStylePropertyMapTest.cpp",
"css/invalidation/InvalidationSetTest.cpp",
"css/invalidation/StyleInvalidatorTest.cpp",
......
......@@ -350,6 +350,7 @@ blink_core_sources("css") {
"cssom/CSSStyleImageValue.h",
"cssom/CSSStyleValue.cpp",
"cssom/CSSStyleValue.h",
"cssom/CSSStyleVariableReferenceValue.cpp",
"cssom/CSSStyleVariableReferenceValue.h",
"cssom/CSSTransformComponent.cpp",
"cssom/CSSTransformComponent.h",
......
// Copyright 2018 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/CSSStyleVariableReferenceValue.h"
namespace blink {
CSSStyleVariableReferenceValue* CSSStyleVariableReferenceValue::Create(
const String& variable,
ExceptionState& exception_state) {
return Create(variable, nullptr, exception_state);
}
CSSStyleVariableReferenceValue* CSSStyleVariableReferenceValue::Create(
const String& variable,
CSSUnparsedValue* fallback,
ExceptionState& exception_state) {
CSSStyleVariableReferenceValue* result = Create(variable, fallback);
if (!result) {
exception_state.ThrowTypeError("Invalid custom property name");
return nullptr;
}
return result;
}
CSSStyleVariableReferenceValue* CSSStyleVariableReferenceValue::Create(
const String& variable,
CSSUnparsedValue* fallback) {
if (!variable.StartsWith("--"))
return nullptr;
return new CSSStyleVariableReferenceValue(variable, fallback);
}
void CSSStyleVariableReferenceValue::setVariable(
const String& value,
ExceptionState& exception_state) {
if (!value.StartsWith("--")) {
exception_state.ThrowTypeError("Invalid custom property name");
return;
}
variable_ = value;
}
} // namespace blink
......@@ -12,6 +12,8 @@
namespace blink {
class ExceptionState;
// CSSStyleVariableReferenceValue represents a CSS var() value for CSS Typed OM.
// The corresponding idl file is CSSVariableReferenceValue.idl.
class CORE_EXPORT CSSStyleVariableReferenceValue final
......@@ -19,16 +21,19 @@ class CORE_EXPORT CSSStyleVariableReferenceValue final
DEFINE_WRAPPERTYPEINFO();
public:
virtual ~CSSStyleVariableReferenceValue() = default;
static CSSStyleVariableReferenceValue* Create(const String& variable,
ExceptionState&);
static CSSStyleVariableReferenceValue* Create(const String& variable,
CSSUnparsedValue* fallback,
ExceptionState&);
static CSSStyleVariableReferenceValue* Create(
const String& variable,
CSSUnparsedValue* fallback = nullptr) {
return new CSSStyleVariableReferenceValue(variable, fallback);
}
CSSUnparsedValue* fallback = nullptr);
const String& variable() const { return variable_; }
void setVariable(const String& value) { variable_ = value; }
void setVariable(const String&, ExceptionState&);
CSSUnparsedValue* fallback() { return fallback_.Get(); }
const CSSUnparsedValue* fallback() const { return fallback_.Get(); }
......
......@@ -8,8 +8,9 @@
Constructor(DOMString variable, optional CSSUnparsedValue fallback),
Exposed=(Window,PaintWorklet),
RuntimeEnabled=CSSTypedOM,
RaisesException=Constructor,
ImplementedAs=CSSStyleVariableReferenceValue
] interface CSSVariableReferenceValue {
attribute DOMString variable;
[RaisesException=Setter] attribute DOMString variable;
readonly attribute CSSUnparsedValue? fallback;
};
// 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/CSSStyleVariableReferenceValue.h"
#include "core/css/cssom/CSSUnparsedValue.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace blink {
TEST(CSSVariableReferenceValueTest, EmptyList) {
HeapVector<StringOrCSSVariableReferenceValue> fragments;
CSSUnparsedValue* unparsed_value = CSSUnparsedValue::Create(fragments);
CSSStyleVariableReferenceValue* variable_reference_value =
CSSStyleVariableReferenceValue::Create("test", unparsed_value);
EXPECT_EQ(variable_reference_value->variable(), "test");
EXPECT_EQ(variable_reference_value->fallback(), unparsed_value);
}
TEST(CSSVariableReferenceValueTest, MixedList) {
HeapVector<StringOrCSSVariableReferenceValue> fragments;
fragments.push_back(StringOrCSSVariableReferenceValue::FromString("string"));
fragments.push_back(
StringOrCSSVariableReferenceValue::FromCSSVariableReferenceValue(
CSSStyleVariableReferenceValue::Create(
"Variable", CSSUnparsedValue::FromString("Fallback"))));
fragments.push_back(StringOrCSSVariableReferenceValue());
CSSUnparsedValue* unparsed_value = CSSUnparsedValue::Create(fragments);
CSSStyleVariableReferenceValue* variable_reference_value =
CSSStyleVariableReferenceValue::Create("test", unparsed_value);
EXPECT_EQ(variable_reference_value->variable(), "test");
EXPECT_EQ(variable_reference_value->fallback(), unparsed_value);
}
} // namespace blink
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