From 5eb0e4ccca6423ac9e3999f964db9803ca782f25 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 20:27:48 +0000 Subject: [PATCH 1/4] fix: parse non-selector pseudo arguments as identifiers (#354) --- src/parse-selector.test.ts | 55 ++++++++++++++++++++++++++++++++++++++ src/parse-selector.ts | 55 +++++++++++++++++++++++++++++++++++++- src/string-utils.ts | 1 + 3 files changed, 110 insertions(+), 1 deletion(-) diff --git a/src/parse-selector.test.ts b/src/parse-selector.test.ts index a79288f..b262e9b 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,7 @@ import { NTH_SELECTOR, NTH_OF_SELECTOR, LANG_SELECTOR, + IDENTIFIER, } from './arena' // Helper for low-level testing @@ -2980,6 +2982,59 @@ 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 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'), + 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.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..6e7a7ba 100644 --- a/src/parse-selector.ts +++ b/src/parse-selector.ts @@ -16,6 +16,7 @@ import { NTH_OF_SELECTOR, LANG_SELECTOR, DIMENSION, + IDENTIFIER, FLAG_HAS_PARENS, FLAG_HAS_NAMESPACE, } from './arena' @@ -53,6 +54,7 @@ import { is_combinator, CHAR_EQUALS, CHAR_PIPE, + CHAR_COMMA, CHAR_DOLLAR, CHAR_CARET, CHAR_SINGLE_QUOTE, @@ -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 @@ -794,7 +815,9 @@ 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 { + } 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 +842,9 @@ 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) } } @@ -837,6 +863,33 @@ export class SelectorParser { ) } + // Parse non-selector pseudo arguments: whitespace/comma separated runs become IDENTIFIER nodes + private parse_ident_arguments(start: number, end: number, parent_node: number): void { + let first_child: number | null = null + let last_child: number | null = null + let i = start + while (i < end) { + const ch = this.source.charCodeAt(i) + if (is_whitespace(ch) || ch === CHAR_COMMA) { + i++ + continue + } + const run_start = i + while (i < end) { + const c = this.source.charCodeAt(i) + if (is_whitespace(c) || c === CHAR_COMMA) break + i++ + } + const ident = this.create_node(IDENTIFIER, run_start, i) + if (first_child === null) first_child = ident + if (last_child !== null) this.arena.set_next_sibling(last_child, ident) + last_child = ident + } + if (first_child !== null) { + this.arena.set_first_child(parent_node, first_child) + } + } + // 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 { diff --git a/src/string-utils.ts b/src/string-utils.ts index dcbb0f2..319b0e1 100644 --- a/src/string-utils.ts +++ b/src/string-utils.ts @@ -18,6 +18,7 @@ export const CHAR_GREATER_THAN = 0x3e // > export const CHAR_AMPERSAND = 0x26 // & export const CHAR_EQUALS = 0x3d // = export const CHAR_PIPE = 0x7c // | +export const CHAR_COMMA = 0x2c // , export const CHAR_DOLLAR = 0x24 // $ export const CHAR_CARET = 0x5e // ^ export const CHAR_COLON = 0x3a // : From 9d65ec12213a9a2b6c32814463476f8b00f14b09 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 20:30:43 +0000 Subject: [PATCH 2/4] refactor: tokenize non-selector pseudo arguments instead of scanning chars --- src/parse-selector.test.ts | 14 ++++++++ src/parse-selector.ts | 73 ++++++++++++++++++++++++++++---------- src/string-utils.ts | 1 - 3 files changed, 69 insertions(+), 19 deletions(-) diff --git a/src/parse-selector.test.ts b/src/parse-selector.test.ts index b262e9b..0df3230 100644 --- a/src/parse-selector.test.ts +++ b/src/parse-selector.test.ts @@ -3026,6 +3026,20 @@ describe('Selector Nodes', () => { 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')]) + expect(args('::part(a\n b,\n/* c */ d)')).toEqual([ident('a'), ident('b'), 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.each([':is(a)', ':where(a)', ':not(a)', ':has(a)', ':host(a)', '::slotted(a)'])( '%s still parses a selector list', (css) => { diff --git a/src/parse-selector.ts b/src/parse-selector.ts index 6e7a7ba..d712b87 100644 --- a/src/parse-selector.ts +++ b/src/parse-selector.ts @@ -54,7 +54,6 @@ import { is_combinator, CHAR_EQUALS, CHAR_PIPE, - CHAR_COMMA, CHAR_DOLLAR, CHAR_CARET, CHAR_SINGLE_QUOTE, @@ -765,6 +764,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 @@ -844,7 +845,13 @@ export class SelectorParser { } } else { // Not a selector argument (::part(), :state(), ::highlight(), :dir(), ...) - this.parse_ident_arguments(content_start, content_end, node) + this.parse_ident_arguments( + content_start, + content_end, + node, + func_line, + func_column + content_start - func_name_start, + ) } } @@ -863,31 +870,61 @@ export class SelectorParser { ) } - // Parse non-selector pseudo arguments: whitespace/comma separated runs become IDENTIFIER nodes - private parse_ident_arguments(start: number, end: number, parent_node: number): void { + // 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 i = start - while (i < end) { - const ch = this.source.charCodeAt(i) - if (is_whitespace(ch) || ch === CHAR_COMMA) { - i++ - continue - } - const run_start = i - while (i < end) { - const c = this.source.charCodeAt(i) - if (is_whitespace(c) || c === CHAR_COMMA) break - i++ - } - const ident = this.create_node(IDENTIFIER, run_start, i) + let run_start = -1 + let run_end = -1 + let run_line = 0 + let run_column = 0 + + const flush = () => { + if (run_start === -1) return + const ident = this.create_node(IDENTIFIER, run_start, run_end, run_line, run_column) if (first_child === null) first_child = ident if (last_child !== null) this.arena.set_next_sibling(last_child, ident) last_child = ident + 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 || type === TOKEN_COMMA) { + flush() + 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 diff --git a/src/string-utils.ts b/src/string-utils.ts index 319b0e1..dcbb0f2 100644 --- a/src/string-utils.ts +++ b/src/string-utils.ts @@ -18,7 +18,6 @@ export const CHAR_GREATER_THAN = 0x3e // > export const CHAR_AMPERSAND = 0x26 // & export const CHAR_EQUALS = 0x3d // = export const CHAR_PIPE = 0x7c // | -export const CHAR_COMMA = 0x2c // , export const CHAR_DOLLAR = 0x24 // $ export const CHAR_CARET = 0x5e // ^ export const CHAR_COLON = 0x3a // : From 7dd2a7b2898f95762ef3fda6547ac5ad23ccf44a Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 20:31:40 +0000 Subject: [PATCH 3/4] fix: correct line/column of :lang() arguments --- src/parse-selector.test.ts | 8 ++++++++ src/parse-selector.ts | 18 +++++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/parse-selector.test.ts b/src/parse-selector.test.ts index 0df3230..1f0a4aa 100644 --- a/src/parse-selector.test.ts +++ b/src/parse-selector.test.ts @@ -3040,6 +3040,14 @@ describe('Selector Nodes', () => { ]) }) + 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], + ['fr', 2, 3], + ]) + }) + test.each([':is(a)', ':where(a)', ':not(a)', ':has(a)', ':host(a)', '::slotted(a)'])( '%s still parses a selector list', (css) => { diff --git a/src/parse-selector.ts b/src/parse-selector.ts index d712b87..9ebcf49 100644 --- a/src/parse-selector.ts +++ b/src/parse-selector.ts @@ -815,7 +815,13 @@ 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) + 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()) ) { @@ -929,9 +935,15 @@ export class SelectorParser { // 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 From 0f9f5204dc0ed0b4c7d782bdc60ff5dec793384e Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 20:48:34 +0000 Subject: [PATCH 4/4] fix: keep commas as operator nodes in pseudo arguments --- src/parse-selector.test.ts | 27 ++++++++++++++----- src/parse-selector.ts | 54 +++++++++++++++++++++++++++++++------- 2 files changed, 65 insertions(+), 16 deletions(-) diff --git a/src/parse-selector.test.ts b/src/parse-selector.test.ts index 1f0a4aa..0a63d64 100644 --- a/src/parse-selector.test.ts +++ b/src/parse-selector.test.ts @@ -32,6 +32,7 @@ import { NTH_OF_SELECTOR, LANG_SELECTOR, IDENTIFIER, + OPERATOR, } from './arena' // Helper for low-level testing @@ -2099,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] @@ -2109,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"') }) @@ -2123,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] @@ -2132,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') }) @@ -2988,6 +2992,7 @@ describe('Selector Nodes', () => { 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([ @@ -3010,6 +3015,7 @@ describe('Selector Nodes', () => { expect(type.type).toBe(TYPE_SELECTOR) expect(kids(pseudo).map((c) => [c.type, c.name])).toEqual([ ident('forwards'), + comma, ident('backwards'), ]) }) @@ -3028,7 +3034,13 @@ describe('Selector Nodes', () => { test('comments and newlines separate arguments', () => { expect(args('::part(a/* x */b)')).toEqual([ident('a'), ident('b')]) - expect(args('::part(a\n b,\n/* c */ d)')).toEqual([ident('a'), ident('b'), ident('d')]) + // 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')]) }) @@ -3044,6 +3056,7 @@ describe('Selector Nodes', () => { 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], ]) }) diff --git a/src/parse-selector.ts b/src/parse-selector.ts index 9ebcf49..88bc9b9 100644 --- a/src/parse-selector.ts +++ b/src/parse-selector.ts @@ -17,6 +17,7 @@ import { LANG_SELECTOR, DIMENSION, IDENTIFIER, + OPERATOR, FLAG_HAS_PARENS, FLAG_HAS_NAMESPACE, } from './arena' @@ -897,25 +898,52 @@ export class SelectorParser { 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 - const ident = this.create_node(IDENTIFIER, run_start, run_end, run_line, run_column) - if (first_child === null) first_child = ident - if (last_child !== null) this.arena.set_next_sibling(last_child, ident) - last_child = ident + 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 || type === TOKEN_COMMA) { + 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 && this.lexer.token_start !== run_end) { + flush() + } if (run_start === -1) { run_start = this.lexer.token_start run_line = this.lexer.token_line @@ -962,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 }