From 63c82fd0f94a2531a6ec0f1dbda3632627ec4c6c Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Sun, 14 Jun 2026 23:29:00 +0200 Subject: [PATCH] fix print_string() surrouuding a `"` with more `"` --- src/lib/index.ts | 14 ++++++++------ test/values.test.ts | 12 ++++++++++++ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/lib/index.ts b/src/lib/index.ts index 81b2b9f..8a11b05 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -62,9 +62,13 @@ export function unquote(str: string): string { return str.replaceAll(/(?:^['"])|(?:['"]$)/g, EMPTY_STRING) } -function print_string(str: string | number | null, quote: '"' | "'" = '"'): string { +function print_string(str: string | number | null, quote?: '"' | "'"): string { str = str?.toString() || '' - return quote + unquote(str) + quote + let inner = unquote(str) + if (quote === undefined) { + quote = inner.includes('"') ? "'" : '"' + } + return quote + inner + quote } function print_url(node: Url): string { @@ -77,10 +81,8 @@ function print_url(node: Url): string { let has_single = unquoted.includes("'") if (has_double && has_single) { inner = print_string(unquoted.replaceAll('"', '%22'), '"') - } else if (has_double) { - inner = print_string(unquoted, "'") - } else if (has_single) { - inner = print_string(unquoted, '"') + } else if (has_double || has_single) { + inner = print_string(unquoted) } else { inner = unquoted } diff --git a/test/values.test.ts b/test/values.test.ts index 9b5cb42..749f037 100644 --- a/test/values.test.ts +++ b/test/values.test.ts @@ -260,6 +260,18 @@ test('does not break space toggles (minified)', () => { expect(actual).toEqual(expected) }) +test('Does not mess up quotes inside `content`', () => { + const actual = format(`a { + content: '"'; + content: "'"; + }`) + const expected = `a { + content: '"'; + content: "'"; +}` + expect(actual).toBe(expected) +}) + test('adds quotes around strings in url()', () => { let actual = format(`a { background-image: url("star.gif");