Skip to content

Commit

Permalink
rehype-minify-whitespace: do not collapse in script, style
Browse files Browse the repository at this point in the history
Closes GH-44.
  • Loading branch information
wooorm committed Mar 30, 2022
1 parent a43b6ba commit ca840b0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 6 additions & 1 deletion packages/rehype-minify-whitespace/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,8 @@ function collapseFactory(replace) {
}

/**
* We don’t support void elements here (so `nobr wbr` -> `normal` is ignored).
* We don’t need to support void elements here (so `nobr wbr` -> `normal` is
* ignored).
*
* @param {Root|Element} node
* @param {Context} context
Expand All @@ -327,8 +328,12 @@ function collapseFactory(replace) {
function inferWhiteSpace(node, context) {
if ('tagName' in node && node.properties) {
switch (node.tagName) {
// Whitespace in script/style, while not displayed by CSS as significant,
// could have some meaning in JS/CSS, so we can’t touch them.
case 'listing':
case 'plaintext':
case 'script':
case 'style':
case 'xmp':
return 'pre'
case 'nobr':
Expand Down
14 changes: 11 additions & 3 deletions packages/rehype-minify-whitespace/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,10 +419,10 @@ test('rehype-minify-whitespace', (t) => {
h('meta', {charSet: 'utf8'}),
h('title', 'a'),
h('link'),
h('script', 'b'),
h('script', ' b '),
h('meta'),
h('script', 'c'),
h('style', 'd')
h('script', ' c '),
h('style', ' d ')
])
]),
'should trim whitespace in head'
Expand Down Expand Up @@ -827,5 +827,13 @@ test('rehype-minify-whitespace', (t) => {
'should collapse and trim whitespace in tables'
)

t.deepEqual(
rehype()
.use(min)
.runSync(u('root', [h('script', ' // a \n b ')])),
u('root', [h('script', ' // a \n b ')]),
'should not collapse whitespace in scripts'
)

t.end()
})

0 comments on commit ca840b0

Please sign in to comment.