Skip to content

Commit bc49deb

Browse files
authored
fix(removeUnknownsAndDefaults): skip if attr used in css selector (#2144)
1 parent 71e1f05 commit bc49deb

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

plugins/removeUnknownsAndDefaults.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import {
77
} from './_collections.js';
88
import { detachNodeFromParent } from '../lib/xast.js';
99
import { visitSkip } from '../lib/util/visit.js';
10-
import { collectStylesheet, computeStyle } from '../lib/style.js';
10+
import {
11+
collectStylesheet,
12+
computeStyle,
13+
includesAttrSelector,
14+
} from '../lib/style.js';
1115

1216
/**
1317
* @typedef RemoveUnknownsAndDefaultsParams
@@ -192,7 +196,12 @@ export const fn = (root, params) => {
192196
attributesDefaults.get(name) === value
193197
) {
194198
// keep defaults if parent has own or inherited style
195-
if (computedParentStyle?.[name] == null) {
199+
if (
200+
computedParentStyle?.[name] == null &&
201+
!stylesheet.rules.some((rule) =>
202+
includesAttrSelector(rule.selector, name),
203+
)
204+
) {
196205
delete node.attributes[name];
197206
}
198207
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
Don't remove unknown attributes or attributes with default values if that
2+
attribute is referenced in an attribute selector in CSS.
3+
4+
See: https://mastodon.social/@sir_pepe/114319751487861964
5+
6+
===
7+
8+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-1 -1 202 40">
9+
<style>
10+
[preserveAspectRatio] { fill: yellow; stroke: black; }
11+
</style>
12+
<svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet">
13+
<path d="M50,10 A40,40,1,1,1,50,90 A40,40,1,1,1,50,10 M30,40 Q36,35,42,40 M58,40 Q64,35,70,40 M30,60 Q50,75,70,60 Q50,75,30,60"/>
14+
</svg>
15+
</svg>
16+
17+
@@@
18+
19+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="-1 -1 202 40">
20+
<style>
21+
[preserveAspectRatio] { fill: yellow; stroke: black; }
22+
</style>
23+
<svg viewBox="0 0 100 100" preserveAspectRatio="xMidYMid meet">
24+
<path d="M50,10 A40,40,1,1,1,50,90 A40,40,1,1,1,50,10 M30,40 Q36,35,42,40 M58,40 Q64,35,70,40 M30,60 Q50,75,70,60 Q50,75,30,60"/>
25+
</svg>
26+
</svg>

0 commit comments

Comments
 (0)