Commit 6ed59b8d authored by Darren Shen's avatar Darren Shen Committed by Commit Bot

[css-typed-om] Add constructor for CSSVariableReferenceValue.

As per [1], CSSVariableReferenceValue now has a constructor. We can
get rid of our hacky helper to create CSSVariableReferenceValues.

[1] https://github.com/w3c/css-houdini-drafts/issues/514

Bug: 545318
Change-Id: I1f540514e9a75fb315dc7db16bc99e70ffe911ef
Reviewed-on: https://chromium-review.googlesource.com/822318Reviewed-by: default avatarnainar <nainar@chromium.org>
Commit-Queue: Darren Shen <shend@chromium.org>
Cr-Commit-Position: refs/heads/master@{#523989}
parent 88221907
...@@ -60,13 +60,3 @@ const gValidUnits = [ ...@@ -60,13 +60,3 @@ const gValidUnits = [
'turn', 's', 'ms', 'Hz', 'kHz', 'turn', 's', 'ms', 'Hz', 'kHz',
'dpi', 'dpcm', 'dppx', 'fr', 'dpi', 'dpcm', 'dppx', 'fr',
]; ];
// Hacky way of creating a CSSVariableReferenceValue
// since it doesn't expose a constructor.
function createReferenceValue(variable, fallback) {
const varExpr = 'var(' + variable + ')';
const unparsedValue = newDivWithStyle('color:' + varExpr).attributeStyleMap.get('color');
let referenceValue = unparsedValue[0];
referenceValue.fallback = fallback;
return referenceValue;
}
...@@ -22,28 +22,28 @@ const gTestCases = [ ...@@ -22,28 +22,28 @@ const gTestCases = [
{ {
value: 'var(--A)', value: 'var(--A)',
expectedResult: [ expectedResult: [
createReferenceValue('--A', null), new CSSVariableReferenceValue('--A'),
] ]
}, },
{ {
value: 'var(--A, 1em)', value: 'var(--A, 1em)',
expectedResult: [ expectedResult: [
createReferenceValue('--A', new CSSUnparsedValue(' 1em')), new CSSVariableReferenceValue('--A', new CSSUnparsedValue(' 1em')),
] ]
}, },
{ {
value: 'var(--A, var(--B))', value: 'var(--A, var(--B))',
expectedResult: [ expectedResult: [
createReferenceValue('--A', new CSSUnparsedValue(' ', createReferenceValue('--B', null))), new CSSVariableReferenceValue('--A', new CSSUnparsedValue(' ', new CSSVariableReferenceValue('--B'))),
] ]
}, },
{ {
value: 'calc(42px + var(--foo, 15em) + var(--bar, var(--far) + 15px))', value: 'calc(42px + var(--foo, 15em) + var(--bar, var(--far) + 15px))',
expectedResult: [ expectedResult: [
'calc(42px +', 'calc(42px +',
createReferenceValue('--foo', new CSSUnparsedValue(' 15em')), new CSSVariableReferenceValue('--foo', new CSSUnparsedValue(' 15em')),
' + ', ' + ',
createReferenceValue('--bar', new CSSUnparsedValue(' ', createReferenceValue('--far', null), ' + 15px')), new CSSVariableReferenceValue('--bar', new CSSUnparsedValue(' ', new CSSVariableReferenceValue('--far'), ' + 15px')),
')', ')',
] ]
}, },
......
...@@ -14,23 +14,23 @@ test(() => { ...@@ -14,23 +14,23 @@ test(() => {
}, 'CSSUnparsedValue constructed from IDL with strings serializes correctly'); }, 'CSSUnparsedValue constructed from IDL with strings serializes correctly');
test(() => { test(() => {
assert_equals(new CSSUnparsedValue(createReferenceValue('--A')).toString(), 'var(--A)'); assert_equals(new CSSUnparsedValue(new CSSVariableReferenceValue('--A')).toString(), 'var(--A)');
assert_equals(new CSSUnparsedValue( assert_equals(new CSSUnparsedValue(
createReferenceValue('--A'), new CSSVariableReferenceValue('--A'),
createReferenceValue('--B')).toString(), new CSSVariableReferenceValue('--B')).toString(),
'var(--A)var(--B)'); 'var(--A)var(--B)');
assert_equals(new CSSUnparsedValue( assert_equals(new CSSUnparsedValue(
createReferenceValue('--A', new CSSVariableReferenceValue('--A',
new CSSUnparsedValue(createReferenceValue('--B'))), new CSSUnparsedValue(new CSSVariableReferenceValue('--B'))),
createReferenceValue('--C')).toString(), new CSSVariableReferenceValue('--C')).toString(),
'var(--A,var(--B))var(--C)'); 'var(--A,var(--B))var(--C)');
}, 'CSSUnparsedValue constructed from IDL with CSSVariableReferenceValues serializes correctly'); }, 'CSSUnparsedValue constructed from IDL with CSSVariableReferenceValues serializes correctly');
test(() => { test(() => {
assert_equals(new CSSUnparsedValue('foo', 'bar ', assert_equals(new CSSUnparsedValue('foo', 'bar ',
createReferenceValue('--A', new CSSVariableReferenceValue('--A',
new CSSUnparsedValue('baz ', createReferenceValue('--B'), 'lemon')), new CSSUnparsedValue('baz ', new CSSVariableReferenceValue('--B'), 'lemon')),
createReferenceValue('--C', new CSSVariableReferenceValue('--C',
new CSSUnparsedValue('ade'))).toString(), new CSSUnparsedValue('ade'))).toString(),
'foobar var(--A,baz var(--B)lemon)var(--C,ade)'); 'foobar var(--A,baz var(--B)lemon)var(--C,ade)');
}, 'CSSUnparsedValue constructed from IDL with mix of strings and CSSVariableReferenceValues serializes correctly'); }, 'CSSUnparsedValue constructed from IDL with mix of strings and CSSVariableReferenceValues serializes correctly');
......
...@@ -11,9 +11,9 @@ ...@@ -11,9 +11,9 @@
const gValidTestArgs = [ const gValidTestArgs = [
{ args: [], desc: 'no arguments' }, { args: [], desc: 'no arguments' },
{ args: [''], desc: 'an empty string' }, { args: [''], desc: 'an empty string' },
{ args: [createReferenceValue('--foo', null)], desc: 'a CSSVariableReferenceValue' }, { args: [new CSSVariableReferenceValue('--foo')], desc: 'a CSSVariableReferenceValue' },
{ {
args: ['foo', 'bar', createReferenceValue('--A', null), 'baz', createReferenceValue('--B', null)], args: ['foo', 'bar', new CSSVariableReferenceValue('--A'), 'baz', new CSSVariableReferenceValue('--B')],
desc: 'mix of strings and CSSVariableReferenceValues' desc: 'mix of strings and CSSVariableReferenceValues'
}, },
]; ];
......
This is a testharness.js-based test. This is a testharness.js-based test.
FAIL Setting CSSVariableReferenceValue.variable to an invalid variable name throws SyntaxError assert_throws: function "() => result.variable = 'bar'" did not throw 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 PASS CSSVariableReferenceValue.variable can updated to a valid variable name
PASS CSSVariableReferenceValue.fallback can updated to null PASS CSSVariableReferenceValue.fallback can updated to null
PASS CSSVariableReferenceValue.fallback can updated to a CSSUnparsedValue PASS CSSVariableReferenceValue.fallback can updated to a CSSUnparsedValue
......
...@@ -9,27 +9,44 @@ ...@@ -9,27 +9,44 @@
'use strict'; 'use strict';
test(() => { test(() => {
let result = createReferenceValue('--foo', null); 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_throws(new SyntaxError(), () => result.variable = 'bar');
assert_equals(result.variable, '--foo'); assert_equals(result.variable, '--foo');
assert_throws(new SyntaxError(), () => result.variable = ''); assert_throws(new SyntaxError(), () => result.variable = '');
assert_equals(result.variable, '--foo'); assert_equals(result.variable, '--foo');
}, 'Setting CSSVariableReferenceValue.variable to an invalid variable name throws SyntaxError'); }, '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);
}, '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'));
}, 'CSSVariableReferenceValue can be constructed with fallback');
test(() => { test(() => {
let result = createReferenceValue('--foo', null); let result = new CSSVariableReferenceValue('--foo');
result.variable = '--bar'; result.variable = '--bar';
assert_equals(result.variable, '--bar'); assert_equals(result.variable, '--bar');
}, 'CSSVariableReferenceValue.variable can updated to a valid variable name'); }, 'CSSVariableReferenceValue.variable can updated to a valid variable name');
test(() => { test(() => {
let result = createReferenceValue('--foo', new CSSUnparsedValue()); let result = new CSSVariableReferenceValue('--foo', new CSSUnparsedValue());
result.fallback = null; result.fallback = null;
assert_equals(result.fallback, null); assert_equals(result.fallback, null);
}, 'CSSVariableReferenceValue.fallback can updated to null'); }, 'CSSVariableReferenceValue.fallback can updated to null');
test(() => { test(() => {
let result = createReferenceValue('--foo', null); let result = new CSSVariableReferenceValue('--foo');
result.fallback = new CSSUnparsedValue('foo'); result.fallback = new CSSUnparsedValue('foo');
assert_style_value_equals(result.fallback, new CSSUnparsedValue('foo')); assert_style_value_equals(result.fallback, new CSSUnparsedValue('foo'));
}, 'CSSVariableReferenceValue.fallback can updated to a CSSUnparsedValue'); }, 'CSSVariableReferenceValue.fallback can updated to a CSSUnparsedValue');
......
...@@ -21,8 +21,9 @@ class CORE_EXPORT CSSStyleVariableReferenceValue final ...@@ -21,8 +21,9 @@ class CORE_EXPORT CSSStyleVariableReferenceValue final
public: public:
virtual ~CSSStyleVariableReferenceValue() = default; virtual ~CSSStyleVariableReferenceValue() = default;
static CSSStyleVariableReferenceValue* Create(const String& variable, static CSSStyleVariableReferenceValue* Create(
CSSUnparsedValue* fallback) { const String& variable,
CSSUnparsedValue* fallback = nullptr) {
return new CSSStyleVariableReferenceValue(variable, fallback); return new CSSStyleVariableReferenceValue(variable, fallback);
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
// Represents a CSS var() reference in a CSS value. // Represents a CSS var() reference in a CSS value.
// Spec: https://drafts.css-houdini.org/css-typed-om/#cssvariablereferencevalue // Spec: https://drafts.css-houdini.org/css-typed-om/#cssvariablereferencevalue
[ [
Constructor(DOMString variable, optional CSSUnparsedValue fallback),
Exposed=(Window,PaintWorklet), Exposed=(Window,PaintWorklet),
RuntimeEnabled=CSSTypedOM, RuntimeEnabled=CSSTypedOM,
ImplementedAs=CSSStyleVariableReferenceValue ImplementedAs=CSSStyleVariableReferenceValue
......
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