Skip to content

Commit 2188be1

Browse files
authored
fix minification issue with non-optional spaces in at-rule preludes (#198)
1 parent cde5e32 commit 2188be1

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,10 @@ The package also exports lower-level formatters for individual CSS constructs:
5858

5959
```js
6060
import {
61-
format_value,
62-
format_declaration,
63-
format_selector,
64-
format_atrule_prelude,
61+
format_value,
62+
format_declaration,
63+
format_selector,
64+
format_atrule_prelude,
6565
} from '@projectwallace/format-css'
6666

6767
// Format a CSS value (e.g. the right-hand side of a declaration)

src/lib/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,9 @@ export function format_atrule_prelude(
309309
.replace(/\)([a-zA-Z])/g, ') $1') // force whitespace between closing parenthesis and following text (usually and|or)
310310
.replace(/\s*(=>|>=|<=)\s*/g, `${optional_space}$1${optional_space}`) // add optional spacing around =>, >= and <=
311311
.replace(/([^<>=\s])([<>])([^<>=\s])/g, `$1${optional_space}$2${optional_space}$3`) // add spacing around < or > except when it's part of <=, >=, =>
312-
.replace(/\s+/g, optional_space) // collapse multiple whitespaces into one
312+
.replace(/([^<>=\s])\s+([<>])\s+([^<>=\s])/g, `$1${optional_space}$2${optional_space}$3`) // handle spaces around < or > when they already have surrounding whitespace
313+
.replace(/\s+/g, SPACE) // collapse multiple whitespaces into one
314+
.replace(/([:,]) /g, minify ? '$1' : '$1 ') // in minify mode, remove optional spaces after : and ,
313315
.replace(
314316
/calc\(\s*([^()+\-*/]+)\s*([*/+-])\s*([^()+\-*/]+)\s*\)/g,
315317
(_, left, operator, right) => {

test/atrules.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,12 @@ test('minify: new-fangled comparators (width > 1000px)', () => {
367367
expect(actual).toEqual(expected)
368368
})
369369

370+
test('minify: keeps necessary whitespace between keywords', () => {
371+
let actual = minify(`@media screen or print {}`)
372+
let expected = `@media screen or print{}`
373+
expect(actual).toEqual(expected)
374+
})
375+
370376
test.skip('preserves comments', () => {
371377
let actual = format(`
372378
@media /* comment */ all {}

0 commit comments

Comments
 (0)