Skip to content

Commit 3b3be70

Browse files
domenicFedgeNocodex
committed
Preserve CSS priorities across declaration updates
Pass the requested priority through property handlers so rejected values leave existing declarations, serialized styles, and mutation records unchanged. This also covers values that pass grammar validation but fail color serialization, and prevents invalid cssText declarations from leaving a priority without a value. Preserve requested priorities for variable expressions and CSS-wide keywords, and use an empty priority for direct property assignments. Honor explicit longhand priorities after important shorthands and preserve the shared priority when rebuilding margin and padding shorthands. Closes #4343. Co-authored-by: FedgeNo <[email protected]> Co-authored-by: codex <[email protected]>
1 parent 97b2758 commit 3b3be70

67 files changed

Lines changed: 324 additions & 365 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,6 @@ class CSSStyleDeclarationImpl {
166166
const parsedProperties = prepareProperties(properties);
167167
for (const [property, item] of parsedProperties) {
168168
const { priority, value } = item;
169-
this._priorities.set(property, priority);
170169
this.setProperty(property, value, priority);
171170
}
172171
}
@@ -327,13 +326,7 @@ class CSSStyleDeclarationImpl {
327326
if (!Object.hasOwn(propertyDescriptors, property)) {
328327
return;
329328
}
330-
this.#cachedCssText = null;
331-
if (priority) {
332-
this._priorities.set(property, priority);
333-
} else {
334-
this._priorities.delete(property);
335-
}
336-
propertyDescriptors[property].set.call(this, value);
329+
propertyDescriptors[property].set.call(this, value, priority);
337330
}
338331

339332
get [idlUtils.supportedPropertyIndices]() {
@@ -760,17 +753,12 @@ class CSSStyleDeclarationImpl {
760753
if (!shorthandProperty || !shorthandProperties.has(shorthandProperty)) {
761754
return;
762755
}
763-
const shorthandPriority = this._priorities.get(shorthandProperty);
764756
this.removeProperty(shorthandProperty);
765757
if (typeof priority !== "string") {
766758
priority = this._priorities.get(property) ?? "";
767759
}
768760
this.removeProperty(property);
769-
if (shorthandPriority && priority) {
770-
this._setProperty(property, value);
771-
} else {
772-
this._setProperty(property, value, priority);
773-
}
761+
this._setProperty(property, value, priority);
774762
if (value && !cssValues.hasVarFunc(value)) {
775763
const longhandValues = [];
776764
const shorthandItem = shorthandProperties.get(shorthandProperty);
@@ -868,17 +856,12 @@ class CSSStyleDeclarationImpl {
868856
if (!shorthandProperty || !shorthandProperties.has(shorthandProperty)) {
869857
return;
870858
}
871-
const shorthandPriority = this._priorities.get(shorthandProperty);
872859
this.removeProperty(shorthandProperty);
873860
if (typeof priority !== "string") {
874861
priority = this._priorities.get(property) ?? "";
875862
}
876863
this.removeProperty(property);
877-
if (shorthandPriority && priority) {
878-
this._setProperty(property, value);
879-
} else {
880-
this._setProperty(property, value, priority);
881-
}
864+
this._setProperty(property, value, priority);
882865
if (value && !cssValues.hasVarFunc(value)) {
883866
const longhandValues = [];
884867
const { shorthandFor, position: shorthandPosition } = shorthandProperties.get(shorthandProperty);
@@ -892,7 +875,7 @@ class CSSStyleDeclarationImpl {
892875
}
893876
if (longhandValues.length === shorthandFor.size) {
894877
const replacedValue = getPositionValue(longhandValues, shorthandPosition);
895-
this._setProperty(shorthandProperty, replacedValue);
878+
this._setProperty(shorthandProperty, replacedValue, priority);
896879
}
897880
}
898881
}

‎lib/jsdom/living/css/helpers/generic-property-descriptor.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,14 @@ const { AST_TYPES } = parsers;
2222
*/
2323
function createGenericPropertyDescriptor(property, { caseSensitive, dimensionTypes = {}, functionTypes = {} }) {
2424
return {
25-
set(v) {
25+
set(v, priority = "") {
2626
v = v.trim();
2727
if (parsers.hasVarFunc(v)) {
28-
this._setProperty(property, v);
28+
this._setProperty(property, v, priority);
2929
} else {
3030
const parsedValue = parsers.parsePropertyValue(property, v, {
3131
caseSensitive
3232
});
33-
const priority = this._priorities.get(property) ?? "";
3433
if (Array.isArray(parsedValue)) {
3534
if (parsedValue.length === 1) {
3635
const {

‎lib/jsdom/living/css/properties/background.js‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,20 @@ const serializationLonghands = [
5252
];
5353

5454
const descriptor = {
55-
set(v) {
55+
set(v, priority = "") {
5656
v = v.trim();
5757
if (v === "" || parsers.hasVarFunc(v)) {
5858
for (const [key] of shorthandFor) {
5959
this._setProperty(key, "");
6060
}
61-
this._setProperty(property, v);
61+
this._setProperty(property, v, priority);
6262
} else {
6363
const bgValues = parse(v);
6464
if (typeof bgValues === "string") {
6565
for (const [key] of shorthandFor) {
66-
this._setProperty(key, bgValues);
66+
this._setProperty(key, bgValues, priority);
6767
}
68-
this._setProperty(property, bgValues);
68+
this._setProperty(property, bgValues, priority);
6969
return;
7070
}
7171
if (!Array.isArray(bgValues)) {
@@ -132,7 +132,6 @@ const descriptor = {
132132
}
133133
backgrounds.push(bg.join(" "));
134134
}
135-
const priority = this._priorities.get(property) ?? "";
136135
for (const [longhand, value] of bgMap) {
137136
this._setProperty(longhand, value.join(", "), priority);
138137
}

‎lib/jsdom/living/css/properties/backgroundAttachment.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ const property = "background-attachment";
66
const shorthand = "background";
77

88
const descriptor = {
9-
set(v) {
9+
set(v, priority = "") {
1010
v = v.trim();
1111
if (parsers.hasVarFunc(v)) {
1212
this._setProperty(shorthand, "");
13-
this._setProperty(property, v);
13+
this._setProperty(property, v, priority);
1414
} else {
1515
const val = parse(v);
1616
if (typeof val === "string") {
17-
const priority =
18-
!this._priorities.get(shorthand) && this._priorities.has(property) ? this._priorities.get(property) : "";
1917
this._setProperty(property, val, priority);
2018
}
2119
}

‎lib/jsdom/living/css/properties/backgroundClip.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ const property = "background-clip";
66
const shorthand = "background";
77

88
const descriptor = {
9-
set(v) {
9+
set(v, priority = "") {
1010
v = v.trim();
1111
if (parsers.hasVarFunc(v)) {
1212
this._setProperty(shorthand, "");
13-
this._setProperty(property, v);
13+
this._setProperty(property, v, priority);
1414
} else {
1515
const val = parse(v);
1616
if (typeof val === "string") {
17-
const priority =
18-
!this._priorities.get(shorthand) && this._priorities.has(property) ? this._priorities.get(property) : "";
1917
this._setProperty(property, val, priority);
2018
}
2119
}

‎lib/jsdom/living/css/properties/backgroundColor.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ const property = "background-color";
66
const shorthand = "background";
77

88
const descriptor = {
9-
set(v) {
9+
set(v, priority = "") {
1010
v = v.trim();
1111
if (parsers.hasVarFunc(v)) {
1212
this._setProperty(shorthand, "");
13-
this._setProperty(property, v);
13+
this._setProperty(property, v, priority);
1414
} else {
1515
const val = parse(v);
1616
if (typeof val === "string") {
17-
const priority =
18-
!this._priorities.get(shorthand) && this._priorities.has(property) ? this._priorities.get(property) : "";
1917
this._setProperty(property, val, priority);
2018
}
2119
}

‎lib/jsdom/living/css/properties/backgroundImage.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ const property = "background-image";
66
const shorthand = "background";
77

88
const descriptor = {
9-
set(v) {
9+
set(v, priority = "") {
1010
v = v.trim();
1111
if (parsers.hasVarFunc(v)) {
1212
this._setProperty(shorthand, "");
13-
this._setProperty(property, v);
13+
this._setProperty(property, v, priority);
1414
} else {
1515
const val = parse(v);
1616
if (typeof val === "string") {
17-
const priority =
18-
!this._priorities.get(shorthand) && this._priorities.has(property) ? this._priorities.get(property) : "";
1917
this._setProperty(property, val, priority);
2018
}
2119
}

‎lib/jsdom/living/css/properties/backgroundOrigin.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,14 @@ const property = "background-origin";
66
const shorthand = "background";
77

88
const descriptor = {
9-
set(v) {
9+
set(v, priority = "") {
1010
v = v.trim();
1111
if (parsers.hasVarFunc(v)) {
1212
this._setProperty(shorthand, "");
13-
this._setProperty(property, v);
13+
this._setProperty(property, v, priority);
1414
} else {
1515
const val = parse(v);
1616
if (typeof val === "string") {
17-
const priority =
18-
!this._priorities.get(shorthand) && this._priorities.has(property) ? this._priorities.get(property) : "";
1917
this._setProperty(property, val, priority);
2018
}
2119
}

‎lib/jsdom/living/css/properties/backgroundPosition.js‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,16 @@ const shorthandFor = new Map([
2121
]);
2222

2323
const descriptor = {
24-
set(v) {
24+
set(v, priority = "") {
2525
v = v.trim();
2626
if (parsers.hasVarFunc(v)) {
2727
this._setProperty(background, "");
2828
this._setProperty(propertyX, "");
2929
this._setProperty(propertyY, "");
30-
this._setProperty(property, v);
30+
this._setProperty(property, v, priority);
3131
} else {
3232
const val = parseLonghands(v);
3333
if (val) {
34-
const priority =
35-
!this._priorities.get(background) && this._priorities.has(property) ?
36-
this._priorities.get(property) :
37-
"";
3834
this._setProperty(background, "");
3935
this._setProperty(propertyX, val.x, priority);
4036
this._setProperty(propertyY, val.y, priority);

‎lib/jsdom/living/css/properties/backgroundPositionX.js‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ const property = backgroundPosition.propertyX;
77
const shorthand = backgroundPosition.property;
88

99
const descriptor = {
10-
set(v) {
10+
set(v, priority = "") {
1111
v = v.trim();
1212
if (parsers.hasVarFunc(v)) {
1313
this._setProperty("background", "");
1414
this._setProperty(shorthand, "");
15-
this._setProperty(property, v);
15+
this._setProperty(property, v, priority);
1616
} else {
1717
const val = parse(v);
1818
if (typeof val === "string") {
19-
const priority =
20-
!this._priorities.get(shorthand) && this._priorities.has(property) ? this._priorities.get(property) : "";
2119
backgroundPosition.setLonghand(this, property, val, priority);
2220
}
2321
}

0 commit comments

Comments
 (0)