From 675004207d70a785a3fdeb28071824545f3f7ee5 Mon Sep 17 00:00:00 2001 From: Aron Griffis Date: Thu, 7 Apr 2022 09:09:35 -0400 Subject: [PATCH] fix minifier bug #366 (#376) --- src/minify/index.js | 13 +++++++++---- test/minify/index.test.js | 10 ++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/minify/index.js b/src/minify/index.js index 459fadba..a9d589bc 100644 --- a/src/minify/index.js +++ b/src/minify/index.js @@ -64,13 +64,18 @@ export const compressSymbols = code => // Only manipulate symbols outside of strings if ( - countOccurences(str, "'") % 2 === 0 && - countOccurences(str, '"') % 2 === 0 + countOccurences(str, "'") % 2 !== 0 || + countOccurences(str, '"') % 2 !== 0 ) { - return str + fragment.trim() + return str + fragment + } + + // Preserve whitespace preceding colon, to avoid joining selectors. + if (/^\s+:/.test(fragment)) { + return str + ' ' + fragment.trim() } - return str + fragment + return str + fragment.trim() }, '') // Detects lines that are exclusively line comments diff --git a/test/minify/index.test.js b/test/minify/index.test.js index 02647b13..893b86c6 100644 --- a/test/minify/index.test.js +++ b/test/minify/index.test.js @@ -113,6 +113,9 @@ describe('minify utils', () => { describe('compressSymbols', () => { it('removes spaces around symbols', () => { + // The whitespace preceding the colon is removed here as part of the + // trailing whitespace on the semi-colon. Contrast to the "preserves" + // test below. const input = '; : { } , ; ' const expected = ';:{},;' @@ -125,5 +128,12 @@ describe('minify utils', () => { expect(compressSymbols(input)).toBe(expected) }) + + it('preserves whitespace preceding colons', () => { + const input = '& :last-child { color: blue; }' + const expected = '& :last-child{color:blue;}' + + expect(compressSymbols(input)).toBe(expected) + }) }) })