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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
11 changes: 6 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 30 additions & 22 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 = ' '
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -295,6 +295,14 @@ export function format_selector(
.join(EMPTY_STRING)
}

export function format_selector_list(
node: SelectorList,
{ minify = false }: Pick<FormatOptions, 'minify'> = {},
): 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
Expand Down
26 changes: 16 additions & 10 deletions test/api.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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')
})
})
Expand Down