Fix missing space when AtRule#params is set after parsing (#2113)

This commit is contained in:
Sarath Francis
2026-07-19 10:23:15 +02:00
committed by GitHub
parent 8b102a2574
commit 680ebe0244
2 changed files with 30 additions and 5 deletions
+10 -5
View File
@@ -6,6 +6,10 @@
const STYLE_TAG = /(<)(\/?style\b)/gi
const COMMENT_OPEN = /(<)(!--)/g
// Characters that end an at-rule name, mirroring RE_AT_END in the tokenizer.
// Params starting with anything else need a space to stay separate tokens.
const AT_NAME_END = /[\t\n\f\r "#'()/;[\\\]{}]/
function escapeHTMLInCSS(str) {
if (typeof str !== 'string') return str
if (!str.includes('<')) return str
@@ -34,14 +38,15 @@ function capitalize(str) {
function atruleStart(str, node) {
let name = '@' + node.name
let params = node.params ? str.rawValue(node, 'params') : ''
let afterName = node.raws.afterName
if (typeof node.raws.afterName !== 'undefined') {
name += node.raws.afterName
} else if (params) {
name += ' '
if (typeof afterName === 'undefined') {
afterName = params ? ' ' : ''
} else if (afterName === '' && params && !AT_NAME_END.test(params[0])) {
afterName = ' '
}
return name + params
return name + afterName + params
}
function pushBody(str, stack, node) {
+20
View File
@@ -358,6 +358,26 @@ test('always calls raw to retrieve raws', () => {
)
})
test('adds space before params set on an at-rule parsed without them', () => {
let root = parse('@layer{a{color:black}}')
root.first.params = 'utilities'
is(root.toString(), '@layer utilities{a{color:black}}')
let media = parse('@media;').first
media.params = 'print'
is(media.toString(), '@media print')
})
test('keeps params glued to at-rule name when CSS allows it', () => {
let root = parse('@media(min-width:0){}')
root.first.params = '(min-width:1px)'
is(root.toString(), '@media(min-width:1px){}')
let imported = parse('@import"a.css"').first
imported.params = '"b.css"'
is(imported.toString(), '@import"b.css"')
})
test('supports subclasses with overridden traversal methods', () => {
class CustomStringifier extends Stringifier {
rule(node) {