Skip to content

Commit 9a114f6

Browse files
authored
Preserve the BOM when stringifying (#2119)
PostCSS aims for byte-to-byte equal output, but the stringifier dropped a leading byte order mark: `parse('a{}').toString()` returned `a{}`. The BOM is already detected (`Input#hasBOM`) but was never re-emitted, so the `bom.css` round-trip test had to be skipped. Emit the BOM in `Stringifier#root` when the source had one, and re-enable the previously skipped test.
1 parent 9069261 commit 9a114f6

2 files changed

Lines changed: 3 additions & 2 deletions

File tree

‎lib/stringifier.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,9 @@ class Stringifier {
424424
}
425425

426426
root(node) {
427+
if (node.source && node.source.input.hasBOM) {
428+
this.builder('\uFEFF', node, 'start')
429+
}
427430
this.body(node)
428431
if (node.raws.after) {
429432
let after = node.raws.after

‎test/stringify.test.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import { is } from 'uvu/assert'
55
import { parse, stringify } from '../lib/postcss.js'
66

77
eachTest((name, css) => {
8-
if (name === 'bom.css') return
9-
108
test(`stringifies ${name}`, () => {
119
let root = parse(css)
1210
let result = ''

0 commit comments

Comments
 (0)