From 0b585d1c684344ecec3d1386e389243d1adacb30 Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 14 Nov 2026 07:26:14 +0000 Subject: [PATCH 1/2] 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/2] 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)