Skip to content

Commit f49d691

Browse files
Fix custom property losing its semicolon before a comment (#2117)
A custom property declaration that is the last non-comment child but is followed by comment siblings was stringified without its terminating semicolon. Unlike a normal declaration, a custom property keeps everything up to the next `;` or `}` as its value, so on re-parse the trailing comments were folded into the value and the comment nodes disappeared. Building such a tree with append()/insertAfter()/after() therefore silently dropped the comments. Emit the semicolon when a custom property still has following siblings so the output round-trips, mirroring the existing handling for childless at-rules.
1 parent 28e0daf commit f49d691

2 files changed

Lines changed: 32 additions & 5 deletions

File tree

‎lib/stringifier.js‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,15 @@ function pushBody(str, stack, node) {
6262
for (let i = nodes.length - 1; i >= 0; i--) {
6363
let child = nodes[i]
6464
let childSemicolon = last !== i || semicolon
65-
// A childless at-rule that still has following siblings must be
66-
// terminated. Without the semicolon those trailing comments are folded
67-
// into the at-rule's prelude and disappear when the output is re-parsed.
65+
// A childless at-rule or a custom property declaration that still has
66+
// following siblings must be terminated. Without the semicolon those
67+
// trailing comments are folded into the at-rule's prelude or the custom
68+
// property's value and disappear when the output is re-parsed.
6869
if (
6970
!childSemicolon &&
7071
i < nodes.length - 1 &&
71-
child.type === 'atrule' &&
72-
!child.nodes
72+
((child.type === 'atrule' && !child.nodes) ||
73+
(child.type === 'decl' && child.prop.startsWith('--')))
7374
) {
7475
childSemicolon = true
7576
}

‎test/stringifier.test.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,32 @@ test('terminates nested childless at-rule followed by a comment', () => {
188188
)
189189
})
190190

191+
test('terminates custom property followed by a comment', () => {
192+
let css = parse('a{--x:red}')
193+
css.first.append(new Comment({ text: 'note' }))
194+
195+
is(css.toString(), 'a{--x:red;/* note */}')
196+
is(
197+
parse(css.toString())
198+
.first.nodes.map(i => i.type)
199+
.join(','),
200+
'decl,comment'
201+
)
202+
})
203+
204+
test('terminates custom property with !important before a comment', () => {
205+
let css = parse('a{--x:red !important}')
206+
css.first.first.after(new Comment({ text: 'note' }))
207+
208+
is(css.toString(), 'a{--x:red !important;/* note */}')
209+
is(
210+
parse(css.toString())
211+
.first.nodes.map(i => i.type)
212+
.join(','),
213+
'decl,comment'
214+
)
215+
})
216+
191217
test('clones only spaces in before', () => {
192218
let css = parse('a{*one:1}')
193219
css.first.append({ prop: 'two', value: '2' })

0 commit comments

Comments
 (0)