@@ -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 */
@@ -34,36 +36,39 @@ class CSSStyleDeclaration {
3436 * @param {Function } opt.onChange - Callback when cssText is changed or the property is removed
3537 * @param {boolean } opt.readOnly - The read-only flag
3638 */
37- constructor ( globalObject = globalThis , opt = { } ) {
39+ constructor ( globalObject , opt = { } ) {
3840 const { context, format, onChange, readOnly, ...styleOpts } = opt ;
41+ // These help interface with jsdom.
3942 this . _global = globalObject ;
43+ this . _onChange = onChange ;
44+
45+ // These correspond to https://drafts.csswg.org/cssom/#css-declaration-block.
46+ this . _computed = format === "computedValue" ;
4047 this . _ownerNode = null ;
4148 this . _parentRule = null ;
4249 if ( context ) {
43- if ( context . nodeType === 1 ) {
50+ // The context is an element.
51+ if ( context . nodeType === ELEMENT_NODE ) {
4452 this . _ownerNode = context ;
53+ // The context is a CSSStyleRule.
4554 } else if ( Object . hasOwn ( context , "parentRule" ) ) {
4655 this . _parentRule = context ;
4756 }
4857 }
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 ;
58+ this . _readonly = readOnly === true ;
59+ this . _updating = false ;
60+
61+ // These correspond to the specification's "declarations".
6362 this . _values = new Map ( ) ;
6463 this . _priorities = new Map ( ) ;
6564 this . _length = 0 ;
66- this . _updating = undefined ;
65+
66+ // This is used internally by parsers. e.g. parsers.resolveCalc(), parsers.parseColor(), etc.
67+ // Note that options may be updated later to resolve getComputedStyle(). See setOptions() below.
68+ this . _options = {
69+ ...styleOpts ,
70+ format : format === "computedValue" ? format : "specifiedValue"
71+ } ;
6772 }
6873
6974 get cssText ( ) {
@@ -301,10 +306,21 @@ class CSSStyleDeclaration {
301306 */
302307 setOptions ( opt = { } ) {
303308 for ( const [ key , value ] of Object . entries ( opt ) ) {
304- if ( key === "readOnly" ) {
305- this . _readonly = value ;
306- } else {
307- this . _options [ key ] = value ;
309+ switch ( key ) {
310+ case "format" : {
311+ if ( value === "computedValue" ) {
312+ this . _computed = true ;
313+ this . _options [ key ] = value ;
314+ }
315+ break ;
316+ }
317+ case "readOnly" : {
318+ this . _readonly = value === true ;
319+ break ;
320+ }
321+ default : {
322+ this . _options [ key ] = value ;
323+ }
308324 }
309325 }
310326 }
0 commit comments