Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
12 changes: 12 additions & 0 deletions test/values.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down