Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/node-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
34 changes: 28 additions & 6 deletions src/parse-escapes.test.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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)
})
20 changes: 12 additions & 8 deletions src/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
Loading