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) }