diff --git a/package.json b/package.json index 6a471a2..a32030a 100644 --- a/package.json +++ b/package.json @@ -44,7 +44,7 @@ "lint": "oxlint && oxfmt --check" }, "dependencies": { - "@projectwallace/css-parser": "~0.15.0" + "@projectwallace/css-parser": "~0.16.0" }, "devDependencies": { "@codecov/rollup-plugin": "^2.0.1", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b06c71b..45f2ca3 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -9,8 +9,8 @@ importers: .: dependencies: '@projectwallace/css-parser': - specifier: ~0.15.0 - version: 0.15.0 + specifier: ~0.16.0 + version: 0.16.0 devDependencies: '@codecov/rollup-plugin': specifier: ^2.0.1 @@ -435,8 +435,9 @@ packages: cpu: [x64] os: [win32] - '@projectwallace/css-parser@0.15.0': - resolution: {integrity: sha512-93/+vgClR/Zb0m4cWQLGaNOLBzeLRPRXNSEaxW6ZAwEOnri48kcpiaB1uUBicLvt660DPvjNExFHINipdkOB1w==} + '@projectwallace/css-parser@0.16.0': + resolution: {integrity: sha512-4WJOIU/6Zbscp5snGGvqx2UobIgYOfBaZDGbx6Mj7b7PjcMz34fFufus7h3AbDFlS7nXEkHHlPDZJl/Qu5gumg==} + engines: {pnpm: '>=11.0.0'} '@projectwallace/preset-oxlint@0.0.11': resolution: {integrity: sha512-hdrtBKSD3ctKkrug54PfHCJPl0jPs6Uw7Rdl/ZVGvim52jYUXp0lj2fTjEP9GVO1Hy3K/dyXKAeEvaFF1TlC4w==} @@ -1668,7 +1669,7 @@ snapshots: '@oxlint/binding-win32-x64-msvc@1.64.0': optional: true - '@projectwallace/css-parser@0.15.0': {} + '@projectwallace/css-parser@0.16.0': {} '@projectwallace/preset-oxlint@0.0.11(oxlint@1.64.0)': dependencies: diff --git a/src/lib/index.ts b/src/lib/index.ts index 5d3ef74..fe2e005 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -6,11 +6,24 @@ import { is_url, is_string, is_operator, + is_raw, + is_selector_list, + is_type_selector, + is_universal_selector, + is_combinator, + is_pseudo_class_selector, + is_pseudo_element_selector, + is_attribute_selector, + is_nth_selector, + is_nth_of_selector, + is_lang_selector, + is_declaration, + is_rule, + is_atrule, type Operator, type Value, type Declaration, type Raw, - is_raw, type NthSelector, type NthOfSelector, type PseudoClassSelector, @@ -19,22 +32,9 @@ import { type SelectorList, type Block, type Rule, - is_selector_list, type Atrule, type StyleSheet, type CSSNode, - is_type_selector, - is_universal_selector, - is_combinator, - is_pseudo_class_selector, - is_pseudo_element_selector, - is_attribute_selector, - is_nth_selector, - is_nth_of_selector, - is_lang_selector, - is_declaration, - is_rule, - is_atrule, } from '@projectwallace/css-parser' const SPACE = ' ' @@ -258,10 +258,14 @@ function print_inline_selector_list( optional_space = SPACE, ): string { let parts = [] - for (let selector of node) { - parts.push(format_selector(selector, { minify: optional_space === EMPTY_STRING })) - if (selector.has_next) { - parts.push(COMMA, optional_space) + for (let child of node) { + if (is_selector_list(child)) { + parts.push(print_inline_selector_list(child, optional_space)) + } else { + parts.push(format_selector(child, { minify: optional_space === EMPTY_STRING })) + if (child.has_next) { + parts.push(COMMA, optional_space) + } } } return parts.join(EMPTY_STRING) @@ -281,10 +285,6 @@ export function format_selector( return print_nth_of(node, optional_space) } - if (is_selector_list(node)) { - return print_inline_selector_list(node, optional_space) - } - if (is_lang_selector(node)) { return print_string(node.name) } @@ -295,6 +295,14 @@ export function format_selector( .join(EMPTY_STRING) } +export function format_selector_list( + node: SelectorList, + { minify = false }: Pick = {}, +): string { + let optional_space = minify ? EMPTY_STRING : SPACE + return print_inline_selector_list(node, optional_space) +} + /** * Pretty-printing atrule preludes takes an insane amount of rules, * so we're opting for a couple of 'good-enough' string replacements diff --git a/test/api.test.ts b/test/api.test.ts index 15ee988..6f4e2fd 100644 --- a/test/api.test.ts +++ b/test/api.test.ts @@ -1,10 +1,16 @@ import { test, expect, describe } from 'vitest' -import { parse_selector, parse_declaration, parse_value } from '@projectwallace/css-parser' +import { + parse_selector, + parse_selector_list, + parse_declaration, + parse_value, +} from '@projectwallace/css-parser' import { format, minify, format_atrule_prelude, format_selector, + format_selector_list, format_declaration, format_value, unquote, @@ -139,37 +145,37 @@ describe('format_atrule_prelude', () => { describe('format_selector', () => { test('type selector', () => { - let node = parse_selector('div').children[0]! + let node = parse_selector('div') expect(format_selector(node)).toBe('div') }) test('class selector', () => { - let node = parse_selector('.foo').children[0]! + let node = parse_selector('.foo') expect(format_selector(node)).toBe('.foo') }) test('combinator keeps spaces by default', () => { - let node = parse_selector('div > span').children[0]! + let node = parse_selector('div > span') expect(format_selector(node)).toBe('div > span') }) test('combinator removes spaces when minified', () => { - let node = parse_selector('div > span').children[0]! + let node = parse_selector('div > span') expect(format_selector(node, { minify: true })).toBe('div>span') }) test('selector list', () => { - let node = parse_selector('div, span') - expect(format_selector(node)).toBe('div, span') + let node = parse_selector_list('div, span') + expect(format_selector_list(node)).toBe('div, span') }) test('selector list minified', () => { - let node = parse_selector('div, span') - expect(format_selector(node, { minify: true })).toBe('div,span') + let node = parse_selector_list('div, span') + expect(format_selector_list(node, { minify: true })).toBe('div,span') }) test('pseudo-class', () => { - let node = parse_selector('a:hover').children[0]! + let node = parse_selector('a:hover') expect(format_selector(node)).toBe('a:hover') }) })