Skip to content

Commit 4e3007c

Browse files
committed
Include all configuration in the <style /> element
This fixes a bug in the prefixIds plugin. It assumed that the <style /> element will have a single text fragment underneath. So this works: <style> <!-- Leading comment ignored --> .class1 {} ... .classN {} </style> The above parses so that node.children[0] is a "text" node with all the CSS classes where "node" is the style element. And this fails: <style> <!-- Ignored comment --> .class1 {} <!-- comment --> .class2 {} </style> This fails because only node.children[0] is processed and node.children looks something like: [ .class1 (type = "text") comment (type = "comment") .class2 (type = "text") ]
1 parent aa5d667 commit 4e3007c

File tree

1 file changed

+44
-42
lines changed

1 file changed

+44
-42
lines changed

plugins/prefixIds.js

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -101,55 +101,57 @@ exports.fn = (_root, params, info) => {
101101
}
102102

103103
// parse styles
104-
let cssText = '';
105-
if (
106-
node.children[0].type === 'text' ||
107-
node.children[0].type === 'cdata'
108-
) {
109-
cssText = node.children[0].value;
110-
}
111-
/**
112-
* @type {null | csstree.CssNode}
113-
*/
114-
let cssAst = null;
115-
try {
116-
cssAst = csstree.parse(cssText, {
117-
parseValue: true,
118-
parseCustomProperty: false,
119-
});
120-
} catch {
121-
return;
122-
}
123-
124-
csstree.walk(cssAst, (node) => {
125-
// #ID, .class selectors
104+
node.children.forEach(child => {
105+
let cssText = '';
126106
if (
127-
(prefixIds && node.type === 'IdSelector') ||
128-
(prefixClassNames && node.type === 'ClassSelector')
107+
child.type === 'text' ||
108+
child.type === 'cdata'
129109
) {
130-
node.name = prefixId(prefix, node.name);
110+
cssText = child.value;
111+
}
112+
/**
113+
* @type {null | csstree.CssNode}
114+
*/
115+
let cssAst = null;
116+
try {
117+
cssAst = csstree.parse(cssText, {
118+
parseValue: true,
119+
parseCustomProperty: false,
120+
});
121+
} catch {
131122
return;
132123
}
133-
// url(...) references
134-
// csstree v2 changed this type
135-
if (node.type === 'Url' && toAny(node.value).length > 0) {
136-
const prefixed = prefixReference(
137-
prefix,
138-
unquote(toAny(node.value))
139-
);
140-
if (prefixed != null) {
141-
toAny(node).value = prefixed;
124+
125+
csstree.walk(cssAst, (node) => {
126+
// #ID, .class selectors
127+
if (
128+
(prefixIds && node.type === 'IdSelector') ||
129+
(prefixClassNames && node.type === 'ClassSelector')
130+
) {
131+
node.name = prefixId(prefix, node.name);
132+
return;
142133
}
134+
// url(...) references
135+
// csstree v2 changed this type
136+
if (node.type === 'Url' && toAny(node.value).length > 0) {
137+
const prefixed = prefixReference(
138+
prefix,
139+
unquote(toAny(node.value))
140+
);
141+
if (prefixed != null) {
142+
toAny(node).value = prefixed;
143+
}
144+
}
145+
});
146+
147+
// update styles
148+
if (
149+
child.type === 'text' ||
150+
child.type === 'cdata'
151+
) {
152+
child.value = csstree.generate(cssAst);
143153
}
144154
});
145-
146-
// update styles
147-
if (
148-
node.children[0].type === 'text' ||
149-
node.children[0].type === 'cdata'
150-
) {
151-
node.children[0].value = csstree.generate(cssAst);
152-
}
153155
return;
154156
}
155157

0 commit comments

Comments
 (0)