Skip to content

Commit 7bf9653

Browse files
domenictrooneycodex
committed
Invalidate styles after CSSOM and control state changes
Changes to stylesheet rules and control state can affect selector matching on siblings and ancestors, and inherited styles on descendants. Refresh computed styles after declaration, selector and media edits, including edits in imported sheets and inserted grouping rules, and after changes to checkedness, indeterminacy, selectedness, values and custom validity. Notify selector and style caches from the underlying state setters so internal changes during reset and activation receive the same handling as public setters. Selection changes also refresh existing selectedOptions collections without rebuilding unrelated structural collections. Keep stylesheet parsing on the internal declaration setter to avoid repeatedly serializing a growing declaration block. Closes #4377 by superseding it. Co-authored-by: Tyler Rooney <[email protected]> Co-authored-by: codex <[email protected]>
1 parent fe9009c commit 7bf9653

27 files changed

Lines changed: 738 additions & 67 deletions

‎lib/jsdom/living/constraint-validation/DefaultConstraintValidation-impl.js‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ exports.implementation = class DefaultConstraintValidationImpl {
3434

3535
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-setcustomvalidity
3636
setCustomValidity(message) {
37-
this._customValidityErrorMessage = message;
37+
if (this._customValidityErrorMessage !== message) {
38+
this._customValidityErrorMessage = message;
39+
this._invalidateSelectorState();
40+
}
3841
}
3942

4043
// https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#dom-cva-reportvalidity

‎lib/jsdom/living/css/CSSGroupingRule-impl.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class CSSGroupingRuleImpl extends CSSRuleImpl {
1313

1414
insertRule(rule, index) {
1515
const result = this.cssRules._insert(rule, index, this._isInNestingContext(), this, this.parentStyleSheet);
16-
this.parentStyleSheet?.ownerNode?._ownerDocument._clearStyleCache();
16+
this._invalidateStyleCache();
1717
return result;
1818
}
1919

2020
deleteRule(index) {
2121
this.cssRules._remove(index);
22-
this.parentStyleSheet?.ownerNode?._ownerDocument._clearStyleCache();
22+
this._invalidateStyleCache();
2323
}
2424

2525
_isInNestingContext() {

‎lib/jsdom/living/css/CSSMediaRule-impl.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ class CSSMediaRuleImpl extends CSSConditionRuleImpl {
1010
super(globalObject, args, privateData);
1111

1212
this.media = MediaList.createImpl(globalObject, [], {
13-
mediaText: this._conditionText
13+
mediaText: this._conditionText,
14+
onChange: () => this._invalidateStyleCache()
1415
});
1516
}
1617

‎lib/jsdom/living/css/CSSRule-impl.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@ class CSSRuleImpl {
77
this.parentStyleSheet = privateData.parentStyleSheet;
88
}
99

10+
_invalidateStyleCache() {
11+
this.parentStyleSheet?._invalidateStyleCache();
12+
}
13+
14+
_setParentStyleSheet(sheet) {
15+
this.parentStyleSheet = sheet;
16+
if (this.cssRules) {
17+
for (const rule of this.cssRules._list) {
18+
rule._setParentStyleSheet(sheet);
19+
}
20+
}
21+
}
22+
1023
// Subclasses must override type and cssText
1124
get type() {
1225
return 0;

‎lib/jsdom/living/css/CSSRuleList-impl.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class CSSRuleListImpl {
159159
}
160160

161161
newRule.parentRule = parentRule;
162-
newRule.parentStyleSheet = parentStyleSheet;
162+
newRule._setParentStyleSheet(parentStyleSheet);
163163
if (CSSImportRule.isImpl(newRule) && newRule.styleSheet) {
164164
newRule.styleSheet.parentStyleSheet = parentStyleSheet;
165165
}

‎lib/jsdom/living/css/CSSStyleDeclaration-impl.js‎

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ class CSSStyleDeclarationImpl {
108108
"NoModificationAllowedError"
109109
]);
110110
}
111+
const originalText = this.parentRule ? this.cssText : null;
111112
this.#cachedCssText = null;
112113
this.#values.clear();
113114
this._priorities.clear();
@@ -170,7 +171,9 @@ class CSSStyleDeclarationImpl {
170171
}
171172
}
172173
this.#updating = false;
173-
this.#updateStyleAttribute();
174+
if (!this.parentRule || this.cssText !== originalText) {
175+
this.#notifyChange();
176+
}
174177
}
175178

176179
/**
@@ -286,7 +289,7 @@ class CSSStyleDeclarationImpl {
286289
this.#cachedCssText = null;
287290
this.#values.delete(property);
288291
this._priorities.delete(property);
289-
this.#updateStyleAttribute();
292+
this.#notifyChange();
290293
return prevValue;
291294
}
292295

@@ -338,7 +341,7 @@ class CSSStyleDeclarationImpl {
338341
}
339342

340343
_updateStyle(update) {
341-
if (this.#updating || !this.#ownerNode) {
344+
if (this.#updating || this._computed || (!this.#ownerNode && !this.parentRule)) {
342345
update();
343346
return;
344347
}
@@ -350,18 +353,26 @@ class CSSStyleDeclarationImpl {
350353
} finally {
351354
this.#updating = false;
352355
if (this.#pendingStyleUpdate && this.cssText !== originalText) {
353-
this.#updateStyleAttribute();
356+
this.#notifyChange();
354357
}
355358
}
356359
}
357360

358-
// https://drafts.csswg.org/cssom/#update-style-attribute-for
359-
#updateStyleAttribute() {
361+
#notifyChange() {
360362
if (this.#updating) {
361363
this.#pendingStyleUpdate = true;
362364
return;
363365
}
364-
if (this._computed || !this.#ownerNode || this.#ownerNode._settingCssText) {
366+
if (this._computed) {
367+
return;
368+
}
369+
if (this.parentRule) {
370+
this.parentRule._invalidateStyleCache();
371+
return;
372+
}
373+
374+
// https://drafts.csswg.org/cssom/#update-style-attribute-for
375+
if (!this.#ownerNode || this.#ownerNode._settingCssText) {
365376
return;
366377
}
367378
this.#ownerNode._settingCssText = true;
@@ -395,7 +406,7 @@ class CSSStyleDeclarationImpl {
395406
if (this.#updating) {
396407
this.#pendingStyleUpdate = true;
397408
} else if (this.#ownerNode && this.cssText !== originalText) {
398-
this.#updateStyleAttribute();
409+
this.#notifyChange();
399410
}
400411
}
401412

‎lib/jsdom/living/css/CSSStyleRule-impl.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class CSSStyleRuleImpl extends CSSGroupingRuleImpl {
2929
return;
3030
}
3131
this.#selectorText = value;
32+
this._selectorSubjects = null;
33+
this._invalidateStyleCache();
3234
}
3335

3436
get type() {

‎lib/jsdom/living/css/CSSStyleSheet-impl.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class CSSStyleSheetImpl extends StyleSheetImpl {
3838
}
3939

4040
const result = this.cssRules._insertParsed(parsed, index, null, this);
41-
this.ownerNode?._ownerDocument._clearStyleCache();
41+
this._invalidateStyleCache();
4242
return result;
4343
}
4444

@@ -47,7 +47,7 @@ class CSSStyleSheetImpl extends StyleSheetImpl {
4747
this.#checkModificationAllowed();
4848

4949
this.cssRules._remove(index);
50-
this.ownerNode?._ownerDocument._clearStyleCache();
50+
this._invalidateStyleCache();
5151
}
5252

5353
// Legacy method
@@ -85,7 +85,7 @@ class CSSStyleSheetImpl extends StyleSheetImpl {
8585
// Parse and populate
8686
cssParser.parseIntoStyleSheet(text, this._globalObject, this);
8787

88-
this.ownerNode?._ownerDocument._clearStyleCache();
88+
this._invalidateStyleCache();
8989
}
9090

9191
replace(text) {
@@ -112,7 +112,7 @@ class CSSStyleSheetImpl extends StyleSheetImpl {
112112
this.cssRules._clear();
113113
cssParser.parseIntoStyleSheet(text, this._globalObject, this);
114114
this.#disallowModification = false;
115-
this.ownerNode?._ownerDocument._clearStyleCache();
115+
this._invalidateStyleCache();
116116

117117
// webidl2js doesn't auto-wrap return values: https://github.com/jsdom/webidl2js/issues/257
118118
resolve(wrapperForImpl(this));

‎lib/jsdom/living/css/MediaList-impl.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@ function canonicalize(stored) {
5757
}
5858

5959
class MediaListImpl {
60+
#onChange;
61+
6062
constructor(globalObject, args, privateData) {
6163
this._globalObject = globalObject;
6264
this._list = [];
65+
this.#onChange = privateData.onChange;
6366

6467
if (privateData.mediaText) {
6568
this._parse(privateData.mediaText);
@@ -71,10 +74,14 @@ class MediaListImpl {
7174
}
7275

7376
set mediaText(value) {
77+
const originalText = this.mediaText;
7478
this._list.length = 0;
7579
if (value !== "") {
7680
this._parse(value);
7781
}
82+
if (this.mediaText !== originalText) {
83+
this.#onChange?.();
84+
}
7885
}
7986

8087
get length() {
@@ -95,6 +102,7 @@ class MediaListImpl {
95102
}
96103
}
97104
this._list.push(normalized);
105+
this.#onChange?.();
98106
}
99107

100108
deleteMedium(medium) {
@@ -108,6 +116,7 @@ class MediaListImpl {
108116
]);
109117
}
110118
this._list.splice(index, 1);
119+
this.#onChange?.();
111120
}
112121

113122
get [idlUtils.supportedPropertyIndices]() {

‎lib/jsdom/living/css/StyleSheet-impl.js‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,18 @@ class StyleSheetImpl {
1414
this.ownerNode = privateData.ownerNode || null;
1515
this.parentStyleSheet = privateData.parentStyleSheet || null;
1616
this.media = MediaList.createImpl(globalObject, [], {
17-
mediaText: privateData.mediaText || ""
17+
mediaText: privateData.mediaText || "",
18+
onChange: () => this._invalidateStyleCache()
1819
});
1920
}
21+
22+
_invalidateStyleCache() {
23+
if (this.parentStyleSheet) {
24+
this.parentStyleSheet._invalidateStyleCache();
25+
} else if (this.ownerNode) {
26+
this.ownerNode._ownerDocument._clearStyleCache();
27+
}
28+
}
2029
}
2130

2231
exports.implementation = StyleSheetImpl;

0 commit comments

Comments
 (0)