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
31 changes: 31 additions & 0 deletions src/parse-escapes.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +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 { 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)
Expand All @@ -15,3 +19,30 @@ 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)
})

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'])
})
51 changes: 39 additions & 12 deletions src/tokenize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -904,31 +904,58 @@ 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 (a-z,A-Z,0-9,-,_) and non-ASCII are never newlines — use pos++
while (pos < source_length) {
const ch = source.charCodeAt(pos)
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_AT_KEYWORD, start, pos, start_line, start_column)
}

consume_hash(start_line: number, start_column: number): TokenType {
// 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
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) {
// 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++
}
return pos
}

// 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
const start = this.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)
Expand Down
Loading