@@ -22,6 +22,8 @@ const {
2222} = require ( "./parsers" ) ;
2323const { asciiLowercase } = require ( "./utils/strings" ) ;
2424
25+ const ELEMENT_NODE = 1 ;
26+
2527/**
2628 * @see https://drafts.csswg.org/cssom/#the-cssstyledeclaration-interface
2729 */
@@ -32,38 +34,39 @@ class CSSStyleDeclaration {
3234 * @param {object } opt.context - Element or CSSStyleRule
3335 * @param {string } opt.format - "specifiedValue" or "computedValue"
3436 * @param {Function } opt.onChange - Callback when cssText is changed or the property is removed
35- * @param {boolean } opt.readOnly - The read-only flag
3637 */
37- constructor ( globalObject = globalThis , opt = { } ) {
38- const { context, format, onChange, readOnly, ...styleOpts } = opt ;
38+ constructor ( globalObject , opt = { } ) {
39+ const { context, format, onChange } = opt ;
40+ // These help interface with jsdom.
3941 this . _global = globalObject ;
42+ this . _onChange = onChange ;
43+
44+ // These correspond to https://drafts.csswg.org/cssom/#css-declaration-block.
45+ this . _computed = format === "computedValue" ;
4046 this . _ownerNode = null ;
4147 this . _parentRule = null ;
4248 if ( context ) {
43- if ( context . nodeType === 1 ) {
49+ // The context is an element.
50+ if ( context . nodeType === ELEMENT_NODE ) {
4451 this . _ownerNode = context ;
52+ // The context is a CSSStyleRule.
4553 } else if ( Object . hasOwn ( context , "parentRule" ) ) {
4654 this . _parentRule = context ;
4755 }
4856 }
49- this . _onChange = onChange ;
50- this . _readonly = readOnly ?? false ;
51- this . _computed = undefined ;
52- const styleOptions = {
53- ...styleOpts
54- } ;
55- if ( format === "computedValue" ) {
56- this . _computed = true ;
57- styleOptions . format = format ;
58- } else if ( format !== "specifiedValue" ) {
59- // Set the default format value.
60- styleOptions . format = "specifiedValue" ;
61- }
62- this . _options = styleOptions ;
57+ this . _readonly = false ;
58+ this . _updating = false ;
59+
60+ // These correspond to the specification's "declarations".
6361 this . _values = new Map ( ) ;
6462 this . _priorities = new Map ( ) ;
6563 this . _length = 0 ;
66- this . _updating = undefined ;
64+
65+ // This is used internally by parsers. e.g. parsers.resolveCalc(), parsers.parseColor(), etc.
66+ // Note that options may be updated later to resolve getComputedStyle(). See setOptions() below.
67+ this . _options = {
68+ format : format === "computedValue" ? format : "specifiedValue"
69+ } ;
6770 }
6871
6972 get cssText ( ) {
@@ -301,10 +304,21 @@ class CSSStyleDeclaration {
301304 */
302305 setOptions ( opt = { } ) {
303306 for ( const [ key , value ] of Object . entries ( opt ) ) {
304- if ( key === "readOnly" ) {
305- this . _readonly = value ;
306- } else {
307- this . _options [ key ] = value ;
307+ switch ( key ) {
308+ case "format" : {
309+ if ( value === "computedValue" ) {
310+ this . _computed = true ;
311+ this . _options [ key ] = value ;
312+ }
313+ break ;
314+ }
315+ case "readOnly" : {
316+ this . _readonly = value === true ;
317+ break ;
318+ }
319+ default : {
320+ this . _options [ key ] = value ;
321+ }
308322 }
309323 }
310324 }
0 commit comments