diff --git a/src/parse-selector.test.ts b/src/parse-selector.test.ts index a79288f..0a63d64 100644 --- a/src/parse-selector.test.ts +++ b/src/parse-selector.test.ts @@ -1,6 +1,7 @@ import { describe, expect, test } from 'vitest' import { SelectorParser, parse_selector, parse_selector_list } from './parse-selector' import { CSSDataArena } from './arena' +import type { CSSNode } from './css-node' import type { AttributeSelector, ClassSelector, @@ -30,6 +31,8 @@ import { NTH_SELECTOR, NTH_OF_SELECTOR, LANG_SELECTOR, + IDENTIFIER, + OPERATOR, } from './arena' // Helper for low-level testing @@ -2097,9 +2100,11 @@ describe('Selector Nodes', () => { expect(langPseudoClass.type).toBe(PSEUDO_CLASS_SELECTOR) expect(langPseudoClass.text).toBe(':lang("nl", "de")') - // :lang() has 2 children - language identifiers + // :lang() has 3 children - 2 language identifiers and the comma between them expect(langPseudoClass.has_children).toBe(true) - expect(langPseudoClass.children).toHaveLength(2) + expect(langPseudoClass.children).toHaveLength(3) + expect(langPseudoClass.children[1].type).toBe(OPERATOR) + expect(langPseudoClass.children[1].text).toBe(',') // First language identifier: "nl" const lang1 = langPseudoClass.children[0] @@ -2107,7 +2112,7 @@ describe('Selector Nodes', () => { expect(lang1.text).toBe('"nl"') // Second language identifier: "de" - const lang2 = langPseudoClass.children[1] + const lang2 = langPseudoClass.children[2] expect(lang2.type).toBe(LANG_SELECTOR) expect(lang2.text).toBe('"de"') }) @@ -2121,8 +2126,9 @@ describe('Selector Nodes', () => { expect(langPseudoClass.type).toBe(PSEUDO_CLASS_SELECTOR) expect(langPseudoClass.text).toBe(':lang(en, fr)') - // :lang() has 2 children - language identifiers - expect(langPseudoClass.children).toHaveLength(2) + // :lang() has 3 children - 2 language identifiers and the comma between them + expect(langPseudoClass.children).toHaveLength(3) + expect(langPseudoClass.children[1].type).toBe(OPERATOR) // First language identifier: en const lang1 = langPseudoClass.children[0] @@ -2130,7 +2136,7 @@ describe('Selector Nodes', () => { expect(lang1.text).toBe('en') // Second language identifier: fr - const lang2 = langPseudoClass.children[1] + const lang2 = langPseudoClass.children[2] expect(lang2.type).toBe(LANG_SELECTOR) expect(lang2.text).toBe('fr') }) @@ -2980,6 +2986,90 @@ describe('Selector Nodes', () => { }) }) + describe('Non-selector pseudo arguments', () => { + const args = (css: string) => { + const pseudo = parse_selector_list(css).first_child?.first_child + return kids(pseudo).map((c) => [c.type, c.name]) + } + const ident = (name: string) => [IDENTIFIER, name] + const comma = [OPERATOR, undefined] + const kids = (n: unknown) => [...((n as CSSNode | undefined)?.children ?? [])] + + test.each([ + ['::highlight(x)', ['x']], + [':state(checked)', ['checked']], + [':dir(rtl)', ['rtl']], + ['::part(a b)', ['a', 'b']], + ['::view-transition-old(*)', ['*']], + ['::view-transition-group(.a.b)', ['.a.b']], + [':foo(bar)', ['bar']], + ])('%s has identifier children only', (css, names) => { + expect(args(css)).toEqual(names.map((n) => ident(n))) + }) + + test('html:active-view-transition-type(forwards, backwards)', () => { + const selector = parse_selector_list( + 'html:active-view-transition-type(forwards, backwards)', + ).first_child + const [type, pseudo] = kids(selector) + expect(type.type).toBe(TYPE_SELECTOR) + expect(kids(pseudo).map((c) => [c.type, c.name])).toEqual([ + ident('forwards'), + comma, + ident('backwards'), + ]) + }) + + test('tabbed-custom-element::part(tab), ::part(tab active)', () => { + const root = parse_selector_list( + 'tabbed-custom-element::part(tab),\ntabbed-custom-element::part(tab active)', + ) + const [a, b] = root.children + const [a_type, a_pe] = kids(a) + const [, b_pe] = kids(b) + expect([a_type.type, a_pe.type]).toEqual([TYPE_SELECTOR, PSEUDO_ELEMENT_SELECTOR]) + expect(kids(a_pe).map((c) => c.name)).toEqual(['tab']) + expect(kids(b_pe).map((c) => [c.type, c.name])).toEqual([ident('tab'), ident('active')]) + }) + + test('comments and newlines separate arguments', () => { + expect(args('::part(a/* x */b)')).toEqual([ident('a'), ident('b')]) + // Commas are kept as Operator children (not dropped) so the CSS can be printed back + expect(args('::part(a\n b,\n/* c */ d)')).toEqual([ + ident('a'), + ident('b'), + comma, + ident('d'), + ]) + expect(args('::part(/* x */ a)')).toEqual([ident('a')]) + }) + + test('positions of arguments across lines', () => { + const pe = kids(parse_selector_list('a::part(x\n y)').first_child)[1] + expect(kids(pe).map((c) => [c.text, c.line, c.column])).toEqual([ + ['x', 1, 9], + ['y', 2, 3], + ]) + }) + + test(':lang() positions across lines', () => { + const pe = kids(parse_selector_list('a:lang(en,\n fr)').first_child)[1] + expect(kids(pe).map((c) => [c.text, c.line, c.column])).toEqual([ + ['en', 1, 8], + [',', 1, 10], + ['fr', 2, 3], + ]) + }) + + test.each([':is(a)', ':where(a)', ':not(a)', ':has(a)', ':host(a)', '::slotted(a)'])( + '%s still parses a selector list', + (css) => { + const pseudo = parse_selector_list(css).first_child?.first_child + expect(pseudo?.first_child?.type).toBe(SELECTOR_LIST) + }, + ) + }) + describe('Dashed idents in pseudo-element functions', () => { test('should not parse dashed ident as type selector in ::view-transition-new()', () => { const root = parse_selector_list('::view-transition-new(--light-box-img)') diff --git a/src/parse-selector.ts b/src/parse-selector.ts index 3c74939..88bc9b9 100644 --- a/src/parse-selector.ts +++ b/src/parse-selector.ts @@ -16,6 +16,8 @@ import { NTH_OF_SELECTOR, LANG_SELECTOR, DIMENSION, + IDENTIFIER, + OPERATOR, FLAG_HAS_PARENS, FLAG_HAS_NAMESPACE, } from './arena' @@ -64,6 +66,25 @@ import { ANplusBParser } from './parse-anplusb' import { CSSNode } from './css-node' import type { Selector, SelectorList } from './node-types' +// Pseudos whose argument is a selector (list). All others get plain identifier arguments. +const SELECTOR_PSEUDOS = new Set([ + 'is', + 'where', + 'not', + 'has', + 'matches', + '-webkit-any', + '-moz-any', + 'host', + 'host-context', + 'slotted', + 'cue', + 'cue-region', + 'current', + 'past', + 'future', +]) + /** @internal */ export class SelectorParser { private lexer: Lexer @@ -744,6 +765,8 @@ export class SelectorParser { ): number | null { const func_name_start = this.lexer.token_start const func_name_end = this.lexer.token_end - 1 // Exclude the '(' + const func_line = this.lexer.token_line + const func_column = this.lexer.token_column // Capture content start (right after the '(') const content_start = this.lexer.pos @@ -793,8 +816,16 @@ export class SelectorParser { } } else if (str_equals_range(this.source, func_name_start, func_name_end, 'lang')) { // Parse as :lang() - comma-separated language identifiers - this.parse_lang_identifiers(content_start, content_end, node) - } else { + this.parse_lang_identifiers( + content_start, + content_end, + node, + func_line, + func_column + content_start - func_name_start, + ) + } else if ( + SELECTOR_PSEUDOS.has(this.source.substring(func_name_start, func_name_end).toLowerCase()) + ) { // Parse as selector (for :is(), :where(), :has(), etc.) // Save current lexer state and selector_end const saved_selector_end = this.selector_end @@ -819,6 +850,15 @@ export class SelectorParser { if (child_selector !== null) { this.arena.set_first_child(node, child_selector) } + } else { + // Not a selector argument (::part(), :state(), ::highlight(), :dir(), ...) + this.parse_ident_arguments( + content_start, + content_end, + node, + func_line, + func_column + content_start - func_name_start, + ) } } @@ -837,11 +877,101 @@ export class SelectorParser { ) } + // Parse non-selector pseudo arguments. Whitespace, commas and comments separate + // arguments; adjacent tokens (e.g. `.a.b`, `*`) merge into one IDENTIFIER node. + private parse_ident_arguments( + start: number, + end: number, + parent_node: number, + line: number, + column: number, + ): void { + const saved_position = this.lexer.save_position() + const saved_selector_end = this.selector_end + this.lexer.seek(start, line, column) + this.selector_end = end + + let first_child: number | null = null + let last_child: number | null = null + let run_start = -1 + let run_end = -1 + let run_line = 0 + let run_column = 0 + + const append = (node: number) => { + if (first_child === null) { + first_child = node + } + if (last_child !== null) { + this.arena.set_next_sibling(last_child, node) + } + last_child = node + } + + const flush = () => { + if (run_start === -1) { + return + } + append(this.create_node(IDENTIFIER, run_start, run_end, run_line, run_column)) + run_start = -1 + } + + while (this.lexer.pos < end) { + this.lexer.next_token(false) + const type = this.lexer.token_type + if (type === TOKEN_EOF) { + break + } + if (type === TOKEN_WHITESPACE) { + flush() + continue + } + if (type === TOKEN_COMMA) { + flush() + // Keep the comma so the original CSS can be printed back + append( + this.create_node( + OPERATOR, + this.lexer.token_start, + this.lexer.token_end, + this.lexer.token_line, + this.lexer.token_column, + ), + ) + continue + } + // A gap before this token means a comment was skipped: start a new argument + if (run_start !== -1 && this.lexer.token_start !== run_end) { + flush() + } + if (run_start === -1) { + run_start = this.lexer.token_start + run_line = this.lexer.token_line + run_column = this.lexer.token_column + } + run_end = this.lexer.token_end + } + flush() + + if (first_child !== null) { + this.arena.set_first_child(parent_node, first_child) + } + + this.selector_end = saved_selector_end + this.lexer.restore_position(saved_position) + } + // Parse :lang() content - comma-separated language identifiers // Accepts both quoted strings: :lang("en", "fr") and unquoted: :lang(en, fr) - private parse_lang_identifiers(start: number, end: number, parent_node: number): void { + private parse_lang_identifiers( + start: number, + end: number, + parent_node: number, + line: number, + column: number, + ): void { const saved_position = this.lexer.save_position() - this.lexer.seek(start, this.lexer.line, this.lexer.column) + this.lexer.seek(start, line, column) const saved_selector_end = this.selector_end this.selector_end = end @@ -860,8 +990,16 @@ export class SelectorParser { continue } - // Skip commas + // Keep commas as operators so the original CSS can be printed back if (token_type === TOKEN_COMMA) { + const comma_node = this.create_node(OPERATOR, token_start, token_end) + if (first_child === null) { + first_child = comma_node + } + if (last_child !== null) { + this.arena.set_next_sibling(last_child, comma_node) + } + last_child = comma_node continue }