From 4e5837ab504240399c7d3fca47f48e8034701a22 Mon Sep 17 00:00:00 2001 From: Guillaume Date: Mon, 4 Nov 2024 10:25:20 +0100 Subject: [PATCH] Do not define CSSPropertyRule.initialValue as nullable It should return empty string instead of null, when the descriptor is omitted. w3c/css-houdini-drafts#1115 --- __tests__/stylesheet.js | 4 ++-- lib/cssom/CSSPropertyRule-impl.js | 24 ++++++++++++++++++------ lib/cssom/CSSPropertyRule.webidl | 3 ++- 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/__tests__/stylesheet.js b/__tests__/stylesheet.js index ee17af8..90a0570 100644 --- a/__tests__/stylesheet.js +++ b/__tests__/stylesheet.js @@ -1003,7 +1003,7 @@ describe('CSS grammar', () => { expect(rule.cssRules).toBeUndefined() expect(rule.style).toBeUndefined() expect(rule.color).toBeUndefined() - expect(rule.initialValue).toBeNull() + expect(rule.initialValue).toBe('') expect(rule.inherits).toBe('true') expect(rule.syntax).toBe('"*"') }) @@ -2118,7 +2118,7 @@ describe('CSSPropertyRule', () => { expect(rule.name).toBe('--custom') expect(rule.syntax).toBe('"*"') expect(rule.inherits).toBe('true') - expect(rule.initialValue).toBeNull() + expect(rule.initialValue).toBe('') }) }) describe('CSSScopeRule', () => { diff --git a/lib/cssom/CSSPropertyRule-impl.js b/lib/cssom/CSSPropertyRule-impl.js index 89bc4f1..cabdf7d 100644 --- a/lib/cssom/CSSPropertyRule-impl.js +++ b/lib/cssom/CSSPropertyRule-impl.js @@ -17,9 +17,13 @@ class CSSPropertyRuleImpl extends CSSRuleImpl { super(globalObject, args, privateData) const { prelude, value } = privateData this.name = serializeCSSComponentValue(prelude) - value.declarations.forEach(({ name, value }) => - this[cssPropertyToIDLAttribute(name)] = serializeCSSValue({ name, value })) - this.initialValue ??= null + value.declarations.forEach(({ name, value }) => { + let attribute = cssPropertyToIDLAttribute(name) + if (attribute === 'initialValue') { + attribute = `_${attribute}` + } + this[attribute] = serializeCSSValue({ name, value }) + }) } /** @@ -28,14 +32,22 @@ class CSSPropertyRuleImpl extends CSSRuleImpl { * @see {@link https://drafts.css-houdini.org/css-properties-values-api-1/#serialize-a-csspropertyrule} */ get cssText() { - const { inherits, initialValue, name, syntax } = this + const { inherits, _initialValue, name, syntax } = this let string = `@property ${name} { syntax: ${syntax}; inherits: ${inherits}; ` - if (initialValue !== null) { - string += `initial-value: ${initialValue}; ` + if (_initialValue !== undefined) { + string += `initial-value: ${_initialValue}; ` } string += '}' return string } + + /** + * @see {@link https://drafts.css-houdini.org/css-properties-values-api-1/#dom-csspropertyrule-initialvalue} + * @see {@link https://github.com/w3c/css-houdini-drafts/issues/1115} + */ + get initialValue() { + return this._initialValue ?? '' + } } module.exports = { diff --git a/lib/cssom/CSSPropertyRule.webidl b/lib/cssom/CSSPropertyRule.webidl index 7f53dff..80eefe2 100644 --- a/lib/cssom/CSSPropertyRule.webidl +++ b/lib/cssom/CSSPropertyRule.webidl @@ -1,9 +1,10 @@ // https://drafts.css-houdini.org/css-properties-values-api-1/#csspropertyrule +// https://github.com/w3c/css-houdini-drafts/issues/1115 [Exposed=Window] interface CSSPropertyRule : CSSRule { readonly attribute boolean inherits; - readonly attribute CSSOMString? initialValue; + readonly attribute CSSOMString initialValue; readonly attribute CSSOMString name; readonly attribute CSSOMString syntax; };