From d095c836d7e27fb842b278d4313a2932ce5bf384 Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Thu, 29 Feb 2024 13:34:06 +0100 Subject: [PATCH] fix missing colon for formatting `:before` and `:after` --- index.js | 10 +++++++++- test/selectors.test.js | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 44b4705..c0e1b69 100644 --- a/index.js +++ b/index.js @@ -141,7 +141,15 @@ function print_simple_selector(node, css) { break } case 'PseudoClassSelector': { - buffer += ':' + child.name + buffer += ':' + + // Special case for `:before` and `:after` which were used in CSS2 and are usually minified + // as `:before` and `:after`, but we want to keep them as `::before` and `::after` + if (child.name === 'before' || child.name === 'after') { + buffer += ':' + } + + buffer += child.name if (child.children) { buffer += '(' + print_simple_selector(child, css) + ')' diff --git a/test/selectors.test.js b/test/selectors.test.js index 35d983f..bf54902 100644 --- a/test/selectors.test.js +++ b/test/selectors.test.js @@ -73,6 +73,24 @@ test("formats nested selector combinators", () => { } }) +test('formats pseudo selectors', () => { + let css = ` + a::before, + a::after, + b:before, + b:after, + c::first-letter {} + ` + let expected = `a::before, +a::after, +b::before, +b::after, +c::first-letter {}` + + let actual = format(css) + assert.equal(actual, expected) +}) + test("formats selectors with Nth", () => { let fixtures = [ [`li:nth-child(3n-2) {}`, `li:nth-child(3n -2) {}`],