Skip to content

Commit 4c03cba

Browse files
authored
Modify CSSStyleDeclaration for better jsdom integration
1 parent 2d6f8af commit 4c03cba

3 files changed

Lines changed: 174 additions & 193 deletions

File tree

lib/CSSStyleDeclaration.js

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,46 +23,41 @@ class CSSStyleDeclaration {
2323
/**
2424
* Creates a new CSSStyleDeclaration instance.
2525
*
26-
* @param {Function} [onChangeCallback] - Callback triggered when style changes.
26+
* @param {object} globalObject - The global object (Window).
27+
* @param {Array} args - The arguments (not actually used).
2728
* @param {object} [opt] - Options.
28-
* @param {object} [opt.context] - The context object (Window, Element, or CSSRule).
29+
* @param {boolean} [opt.computed] - The computed flag.
30+
* @param {object} [opt.context] - The context object (Element, or CSSRule).
31+
* @param {number} [opt.fontSizeMedium] - Font size in pixels for the keyword "medium".
32+
* @param {Function} [opt.onChangeCallback] - Callback triggered when style changes.
33+
* @param {object} [opt.systemColors] - The system colors.
2934
*/
30-
constructor(onChangeCallback, { context } = {}) {
31-
// Internals for jsdom
32-
this._global = globalThis;
35+
constructor(globalObject, args, { computed, context, fontSizeMedium, onChangeCallback, systemColors } = {}) {
36+
// Internals for jsdom.
37+
this._global = globalObject;
3338
this._onChange = onChangeCallback;
39+
this._fontSizeMedium = fontSizeMedium;
40+
this._systemColors = systemColors;
3441

35-
// Internals for CSS declaration block
42+
// Internals for CSS declaration block.
3643
// @see https://drafts.csswg.org/cssom/#css-declaration-blocks
37-
this._computed = false;
44+
this._computed = Boolean(computed);
3845
this._ownerNode = null;
3946
this._parentRule = null;
4047
this._readonly = false;
4148
this._updating = false;
4249

43-
// Other internals
50+
// Other internals.
4451
this._length = 0;
4552
this._propertyIndices = new Map();
4653
this._priorities = new Map();
4754
this._values = new Map();
4855

4956
if (context) {
50-
if (typeof context.getComputedStyle === "function") {
51-
this._global = context;
52-
this._computed = true;
53-
// FIXME: The `_readonly` flag should initially be `false` to be editable,
54-
// but should eventually be set to `true`.
55-
// this._readonly = true;
56-
} else if (context.nodeType === 1 && Object.hasOwn(context, "style")) {
57-
this._global = context.ownerDocument.defaultView;
57+
if (context.nodeType === 1) {
5858
this._ownerNode = context;
5959
} else if (Object.hasOwn(context, "parentRule")) {
6060
this._parentRule = context;
61-
// Find Window from the owner node of the StyleSheet.
62-
const window = context?.parentStyleSheet?.ownerNode?.ownerDocument?.defaultView;
63-
if (window) {
64-
this._global = window;
65-
}
6661
}
6762
}
6863
}
@@ -318,7 +313,7 @@ class CSSStyleDeclaration {
318313
this.removeProperty(property);
319314
return;
320315
}
321-
// Custom property
316+
// Custom property.
322317
if (property.startsWith("--")) {
323318
this._setProperty(property, value, priority);
324319
return;

0 commit comments

Comments
 (0)