From 0b585d1c684344ecec3d1386e389243d1adacb30 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 07:26:14 +0000 Subject: [PATCH 1/3] fix: include escapes in hash tokens so #md\:hidden is a single IdSelector --- src/parse-escapes.test.ts | 13 +++++++++++++ src/tokenize.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/parse-escapes.test.ts b/src/parse-escapes.test.ts index 99060d3..71b0db1 100644 --- a/src/parse-escapes.test.ts +++ b/src/parse-escapes.test.ts @@ -1,5 +1,7 @@ import { test, expect } from 'vitest' import { parse } from './parse' +import { parse_selector } from './parse-selector' +import { ID_SELECTOR } from './constants' test('escaped quote in selector', () => { expect(parse('.a{color:red} .x-\\"{color:red} .b{color:blue}').children.length).toBe(3) @@ -15,3 +17,14 @@ test('tailwind arbitrary value', () => { ).children.length, ).toBe(4) }) + +test.each([ + ['#md\\:hidden', '#md\\:hidden'], + ['#\\31 23', '#\\31 23'], + ['#a\\.b', '#a\\.b'], +])('escape in ID selector %s is a single IdSelector', (selector, text) => { + const root = parse_selector(selector) + expect(root.children.length).toBe(1) + expect(root.first_child!.type).toBe(ID_SELECTOR) + expect(root.first_child!.text).toBe(text) +}) diff --git a/src/tokenize.ts b/src/tokenize.ts index f0a7ffd..b54f64d 100644 --- a/src/tokenize.ts +++ b/src/tokenize.ts @@ -917,6 +917,26 @@ export class Lexer { return this.make_token(TOKEN_AT_KEYWORD, start, pos, start_line, start_column) } + // Consume a valid escape at this.pos (a backslash not followed by a newline/EOF) + consume_escape(): void { + const source = this.source + const source_length = source.length + const next = source.charCodeAt(this.pos + 1) + this.pos += 2 // backslash + first escaped char: never newlines + if (is_hex_digit(next)) { + for (let i = 0; i < 5 && this.pos < source_length; i++) { + if (!is_hex_digit(source.charCodeAt(this.pos))) break + this.pos++ + } + if (this.pos < source_length) { + const ws = source.charCodeAt(this.pos) + if (ws < 128 && (char_types[ws] & CHAR_WHITESPACE_OR_NEWLINE) !== 0) { + this.advance() + } + } + } + } + consume_hash(start_line: number, start_column: number): TokenType { const source = this.source const source_length = source.length @@ -926,6 +946,14 @@ export class Lexer { // Ident chars and non-ASCII are never newlines — use pos++ while (pos < source_length) { const ch = source.charCodeAt(pos) + if (ch === CHAR_BACKSLASH) { + // Valid escape (not at EOF or before a newline) is part of the hash name + if (pos + 1 >= source_length || is_newline(source.charCodeAt(pos + 1))) break + this.pos = pos + this.consume_escape() + pos = this.pos + continue + } if (ch < 0x80 && (char_types[ch] & CHAR_IDENT) === 0) break pos++ } From 0378142644dcf470eb8e461c7dfbe540a18f12e8 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 07:34:57 +0000 Subject: [PATCH 2/3] fix: include escapes in at-keyword tokens --- src/parse-escapes.test.ts | 20 ++++++++++++++++++- src/tokenize.ts | 41 +++++++++++++++++++-------------------- 2 files changed, 39 insertions(+), 22 deletions(-) diff --git a/src/parse-escapes.test.ts b/src/parse-escapes.test.ts index 71b0db1..2e16437 100644 --- a/src/parse-escapes.test.ts +++ b/src/parse-escapes.test.ts @@ -1,7 +1,9 @@ import { test, expect } from 'vitest' import { parse } from './parse' import { parse_selector } from './parse-selector' -import { ID_SELECTOR } from './constants' +import { ID_SELECTOR, AT_RULE } from './constants' +import { parse_value } from './parse-value' +import type { Atrule } from './node-types' test('escaped quote in selector', () => { expect(parse('.a{color:red} .x-\\"{color:red} .b{color:blue}').children.length).toBe(3) @@ -28,3 +30,19 @@ test.each([ expect(root.first_child!.type).toBe(ID_SELECTOR) expect(root.first_child!.text).toBe(text) }) + +test.each([ + ['@\\6dedia screen{}', '@\\6dedia'], + ['@m\\:x a{}', '@m\\:x'], +])('escape in at-rule name %s is part of the name', (css, name) => { + const atrule = parse(css).first_child as Atrule + expect(atrule.type).toBe(AT_RULE) + expect(atrule.name).toBe(name.slice(1)) + expect(atrule.prelude?.text).toBe(css.includes('screen') ? 'screen' : 'a') +}) + +test('escape-like hack after a number stays a separate node', () => { + // Legacy hacks like `0\9` and `100px\9` must not become escaped units + const value = parse_value('100px\\9') + expect([...value.children].map((c) => c.type_name)).toEqual(['Dimension', 'Identifier']) +}) diff --git a/src/tokenize.ts b/src/tokenize.ts index b54f64d..8685404 100644 --- a/src/tokenize.ts +++ b/src/tokenize.ts @@ -904,17 +904,31 @@ export class Lexer { const source = this.source const source_length = source.length const start = this.pos - let pos = start + 1 // Skip @ — never a newline + const pos = this.consume_name(start + 1) // Skip @ — never a newline - // Ident chars (a-z,A-Z,0-9,-,_) and non-ASCII are never newlines — use pos++ + this.pos = pos + return this.make_token(TOKEN_AT_KEYWORD, start, pos, start_line, start_column) + } + + // Consume ident chars and valid escapes from `pos`, returns the end position + consume_name(pos: number): number { + const source = this.source + const source_length = source.length + // Ident chars and non-ASCII are never newlines — use pos++ while (pos < source_length) { const ch = source.charCodeAt(pos) + if (ch === CHAR_BACKSLASH) { + // A backslash at EOF or before a newline is not an escape + if (pos + 1 >= source_length || is_newline(source.charCodeAt(pos + 1))) break + this.pos = pos + this.consume_escape() + pos = this.pos + continue + } if (ch < 0x80 && (char_types[ch] & CHAR_IDENT) === 0) break pos++ } - - this.pos = pos - return this.make_token(TOKEN_AT_KEYWORD, start, pos, start_line, start_column) + return pos } // Consume a valid escape at this.pos (a backslash not followed by a newline/EOF) @@ -941,22 +955,7 @@ export class Lexer { const source = this.source const source_length = source.length const start = this.pos - let pos = start + 1 // Skip # — never a newline - - // Ident chars and non-ASCII are never newlines — use pos++ - while (pos < source_length) { - const ch = source.charCodeAt(pos) - if (ch === CHAR_BACKSLASH) { - // Valid escape (not at EOF or before a newline) is part of the hash name - if (pos + 1 >= source_length || is_newline(source.charCodeAt(pos + 1))) break - this.pos = pos - this.consume_escape() - pos = this.pos - continue - } - if (ch < 0x80 && (char_types[ch] & CHAR_IDENT) === 0) break - pos++ - } + const pos = this.consume_name(start + 1) // Skip # — never a newline this.pos = pos return this.make_token(TOKEN_HASH, start, pos, start_line, start_column) From b8a2dfb886b1bedad74f14b426f82de1767ee404 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 07:56:22 +0000 Subject: [PATCH 3/3] fix!: tokenize escapes in dimension units and hyphen-prefixed idents per spec --- package.json | 2 +- src/node-types.test.ts | 8 ++++---- src/parse-escapes.test.ts | 34 ++++++++++++++++++++++++++++------ src/tokenize.ts | 20 ++++++++++++-------- 4 files changed, 45 insertions(+), 19 deletions(-) diff --git a/package.json b/package.json index 8bfc893..e15b850 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@projectwallace/css-parser", - "version": "0.19.9", + "version": "0.19.10", "description": "High-performance CSS lexer and parser, optimized for CSS inspection and analysis", "keywords": [ "ast", diff --git a/src/node-types.test.ts b/src/node-types.test.ts index 2748263..20d6840 100644 --- a/src/node-types.test.ts +++ b/src/node-types.test.ts @@ -344,16 +344,16 @@ describe('type narrowing — compile-time', () => { expect(dim.type_name).toBe('Dimension') }) - test('MediaFeature exposes children, including extra hack nodes like 0\\0', () => { + test('MediaFeature exposes children, including escaped units like 0\\0', () => { const atrule = parse('@media (min-width:0\\0) {}').first_child! as Atrule const query = atrule.prelude!.first_child! as MediaQuery const feature = query.first_child as MediaFeature expect(feature.has_children).toBeTypeOf('boolean') expect(feature.child_count).toBeTypeOf('number') expect(feature.has_children).toBe(true) - expect(feature.child_count).toBe(2) - expect([...feature].map((c) => c.type_name)).toEqual(['Number', 'Identifier']) - expect(feature.value?.type_name).toBe('Number') + expect(feature.child_count).toBe(1) + expect([...feature].map((c) => c.type_name)).toEqual(['Dimension']) + expect(feature.value?.type_name).toBe('Dimension') }) test('ContainerQuery children include PreludeOperator for and/or/not, without casting', () => { diff --git a/src/parse-escapes.test.ts b/src/parse-escapes.test.ts index 2e16437..a7d5de6 100644 --- a/src/parse-escapes.test.ts +++ b/src/parse-escapes.test.ts @@ -1,9 +1,9 @@ import { test, expect } from 'vitest' import { parse } from './parse' import { parse_selector } from './parse-selector' -import { ID_SELECTOR, AT_RULE } from './constants' +import { ID_SELECTOR, AT_RULE, DIMENSION, IDENTIFIER } from './constants' import { parse_value } from './parse-value' -import type { Atrule } from './node-types' +import type { Atrule, Dimension } from './node-types' test('escaped quote in selector', () => { expect(parse('.a{color:red} .x-\\"{color:red} .b{color:blue}').children.length).toBe(3) @@ -41,8 +41,30 @@ test.each([ expect(atrule.prelude?.text).toBe(css.includes('screen') ? 'screen' : 'a') }) -test('escape-like hack after a number stays a separate node', () => { - // Legacy hacks like `0\9` and `100px\9` must not become escaped units - const value = parse_value('100px\\9') - expect([...value.children].map((c) => c.type_name)).toEqual(['Dimension', 'Identifier']) +test.each([ + ['1\\70x', '1', '\\70x'], + ['-1\\70x', '-1', '\\70x'], + ['.5\\70x', '.5', '\\70x'], + ['1e\\33', '1', 'e\\33'], + ['100px\\9', '100', 'px\\9'], + ['0\\9', '0', '\\9'], +])('escape in dimension unit %s is part of the unit', (css, value, unit) => { + const children = [...parse_value(css).children] as Dimension[] + expect(children.length).toBe(1) + expect(children[0].type).toBe(DIMENSION) + expect(children[0].value).toBe(Number(value)) + expect(children[0].unit).toBe(unit) +}) + +test.each(['-\\31 x', '-\\31', '-\\3a b'])('ident %s starting with hyphen and escape', (css) => { + const children = [...parse_value(css).children] + expect(children.length).toBe(1) + expect(children[0].type).toBe(IDENTIFIER) + expect(children[0].text).toBe(css) +}) + +test.each(['.-\\31 x', '.-\\31', '#-\\31 x'])('selector %s with hyphen and escape', (selector) => { + const root = parse_selector(selector) + expect(root.children.length).toBe(1) + expect(root.first_child!.text).toBe(selector) }) diff --git a/src/tokenize.ts b/src/tokenize.ts index 8685404..9f84bfa 100644 --- a/src/tokenize.ts +++ b/src/tokenize.ts @@ -294,6 +294,13 @@ export class Lexer { return this.consume_ident_or_function(start_line, start_column) } + // -\31 x: a hyphen followed by a valid escape is still an ident + if (next === CHAR_BACKSLASH && this.pos + 2 < source_length) { + if (!is_newline(source.charCodeAt(this.pos + 2))) { + return this.consume_ident_or_function(start_line, start_column) + } + } + if (next < 128 && (char_types[next] & CHAR_DIGIT) !== 0) { return this.consume_number(start_line, start_column) } @@ -753,17 +760,14 @@ export class Lexer { this.pos = pos return this.make_token(TOKEN_PERCENTAGE, start, pos, start_line, start_column) } + const next = pos + 1 < source_length ? source.charCodeAt(pos + 1) : 0 if ( is_ident_start(ch) || - (ch === CHAR_HYPHEN && - is_ident_start(pos + 1 < source_length ? source.charCodeAt(pos + 1) : 0)) + (ch === CHAR_HYPHEN && is_ident_start(next)) || + (ch === CHAR_BACKSLASH && next !== 0 && !is_newline(next)) ) { - // Unit: px, em, rem, etc — ident chars and non-ASCII are never newlines - while (pos < source_length) { - const ch = source.charCodeAt(pos) - if (ch < 0x80 && (char_types[ch] & CHAR_IDENT) === 0) break - pos++ - } + // Unit: px, em, rem, etc, including escapes like 1\70x + pos = this.consume_name(pos) this.pos = pos return this.make_token(TOKEN_DIMENSION, start, pos, start_line, start_column) }