diff --git a/package.json b/package.json index c82f502..6b55535 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "knip": "knip" }, "dependencies": { - "@projectwallace/css-parser": "^0.19.6" + "@projectwallace/css-parser": "^0.19.9" }, "devDependencies": { "@projectwallace/preset-oxlint": "^0.0.11", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e145fa1..fe96f9c 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@projectwallace/css-parser': - specifier: ^0.19.6 - version: 0.19.6 + specifier: ^0.19.9 + version: 0.19.9 devDependencies: '@projectwallace/preset-oxlint': specifier: ^0.0.11 @@ -567,8 +567,8 @@ packages: cpu: [x64] os: [win32] - '@projectwallace/css-parser@0.19.6': - resolution: {integrity: sha512-B59Rr+gS9bdr9NIMTBAxX8Ld4D66//zqe2ZD/zyeS0coZBP0+eJ6r2kXYDjGfnVJNo1GuD6eNOqglJLlCRdVlA==} + '@projectwallace/css-parser@0.19.9': + resolution: {integrity: sha512-TrviEhwjmjI6kC9cDzymiZzWbHI/ep5R2Uci0DHrKZv/TyycN8gXdzMVfWU4YdpM9Kj7WhxWiy1NYZ5pzDZxkw==} engines: {pnpm: '>=11.0.0'} '@projectwallace/preset-oxlint@0.0.11': @@ -1687,7 +1687,7 @@ snapshots: '@oxlint/binding-win32-x64-msvc@1.89.0': optional: true - '@projectwallace/css-parser@0.19.6': {} + '@projectwallace/css-parser@0.19.9': {} '@projectwallace/preset-oxlint@0.0.11(oxlint@1.89.0)': dependencies: diff --git a/src/lib/index.ts b/src/lib/index.ts index 841d648..ea8f1a2 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -9,6 +9,7 @@ import { is_url, is_string, is_operator, + is_identifier, is_raw, is_selector_list, is_type_selector, @@ -401,6 +402,11 @@ function print_selector_argument(node: CSSNode, optional_space = SPACE): string return print_string(node.name ?? EMPTY_STRING) } + // Non-selector pseudo-class arguments, e.g. `:unknown(foo.bar)` + if (is_identifier(node)) { + return node.text + } + return print_selector(node as Selector, optional_space) } @@ -415,6 +421,10 @@ function print_selector_list( ): string { let parts: string[] = [] node.for_each((child) => { + if (is_operator(child)) { + // Commas between arguments are emitted below + return + } if (is_selector_list(child)) { parts.push(print_selector_list(child, optional_space)) } else { diff --git a/test/selectors.test.ts b/test/selectors.test.ts index db3ed11..37efbaa 100644 --- a/test/selectors.test.ts +++ b/test/selectors.test.ts @@ -1,5 +1,5 @@ import { test, expect } from 'vitest' -import { format } from '../src/lib/index.js' +import { format, minify } from '../src/lib/index.js' test('A single selector is rendered without a trailing comma', () => { let actual = format('a {}') @@ -292,3 +292,26 @@ test('whitespace before a comma does not leave a trailing combinator', () => { let expected = 'a > b,\nc {}' expect(actual).toEqual(expected) }) + +test('formats :lang() with multiple identifiers', () => { + let actual = format(`:lang(en,nl),a:lang(en-US , nl){}`) + let expected = `:lang("en", "nl"), +a:lang("en-US", "nl") {}` + expect(actual).toEqual(expected) +}) + +test('formats single-argument :lang()', () => { + expect(format(`:lang(en){}`)).toEqual(`:lang("en") {}`) +}) + +test('formats unknown pseudo with a single identifier argument', () => { + expect(format(`:unknown(foo.bar){}`)).toEqual(`:unknown(foo.bar) {}`) +}) + +test('minifies :lang() with multiple arguments', () => { + expect(minify(`:lang(en, nl) { color: red }`)).toEqual(`:lang("en","nl"){color:red}`) +}) + +test('minifies unknown pseudo with multiple arguments', () => { + expect(minify(`:unknown(a.b, c) {}`)).toEqual(`:unknown(a.b,c){}`) +})