diff --git a/README.md b/README.md index 356f5df..f527417 100644 --- a/README.md +++ b/README.md @@ -81,7 +81,7 @@ Everything is exported from the package root. Each part is also available from i - `/walker` — `walk`, `traverse`, `walk_declarations`, `walk_selectors`, `walk_values`, `SKIP`, `BREAK` - `/nodes` — node type constants (`STYLE_RULE`, `DECLARATION`, …), `NODE_TYPES` - `/node-types` — type guards (`is_declaration`, `is_rule`, …) and node types (`CSSNode`, `Rule`, …) -- `/string-utils` — `is_custom`, `is_vendor_prefixed`, `strip_vendor_prefix`, `strip_value_hack`, `property_basename`, `str_equals`, `str_starts_with`, `str_index_of` +- `/string-utils` — `is_custom`, `is_vendor_prefixed`, `strip_vendor_prefix`, `strip_value_hack`, `unescape_css`, `property_basename`, `str_equals`, `str_starts_with`, `str_index_of` - `/token-types` — token type constants (`TOKEN_*`) ### `parse(source, options?)` @@ -211,6 +211,7 @@ strip_vendor_prefix('-webkit-transform') // "transform" str_equals('media', 'MEDIA') // true str_starts_with('webkit', 'WEBKIT-transform') // true str_index_of('color', 'background-COLOR') // 11 +unescape_css('.\\31 23\\:x') // ".123:x" ``` ## Non-goals diff --git a/src/index.ts b/src/index.ts index 8ab848d..55c301f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,7 @@ export { strip_vendor_prefix, property_basename, strip_value_hack, + unescape_css, str_equals, str_starts_with, str_index_of, diff --git a/src/parse-escapes.test.ts b/src/parse-escapes.test.ts index a7d5de6..18736dc 100644 --- a/src/parse-escapes.test.ts +++ b/src/parse-escapes.test.ts @@ -1,4 +1,5 @@ -import { test, expect } from 'vitest' +import { test, expect, describe } from 'vitest' +import { unescape_css } from './string-utils' import { parse } from './parse' import { parse_selector } from './parse-selector' import { ID_SELECTOR, AT_RULE, DIMENSION, IDENTIFIER } from './constants' @@ -68,3 +69,37 @@ test.each(['.-\\31 x', '.-\\31', '#-\\31 x'])('selector %s with hyphen and escap expect(root.children.length).toBe(1) expect(root.first_child!.text).toBe(selector) }) + +describe('unescape_css', () => { + test('hex escapes', () => { + expect(unescape_css('\\31 23')).toBe('123') + expect(unescape_css('\\000041')).toBe('A') + expect(unescape_css('\\41\nb')).toBe('Ab') + expect(unescape_css('\\0 \\110000 \\d800')).toBe('\uFFFD\uFFFD\uFFFD') + expect(unescape_css('\\1F600')).toBe('😀') + }) + test('character escapes and edge cases', () => { + expect(unescape_css('plain')).toBe('plain') + expect(unescape_css('a\\:b\\\\c')).toBe('a:b\\c') + expect(unescape_css('a\\\nb')).toBe('ab') + expect(unescape_css('a\\')).toBe('a\uFFFD') + }) + test('selectors', () => { + const cls = parse_selector('.md\\:hidden').first_child! + expect(unescape_css(cls.text)).toBe('.md:hidden') + const id = parse_selector('#\\31 23').first_child! + expect(unescape_css(id.text)).toBe('#123') + }) + test('values', () => { + const ident = parse_value('\\46oo\\ Bar').first_child! + expect(unescape_css(ident.text)).toBe('Foo Bar') + const str = parse_value('"a\\"b"').first_child! + expect(unescape_css(str.text)).toBe('"a"b"') + }) + test('atrules', () => { + const rule = parse('@media (min-width:1px){} @layer a\\.b{}').children[1] as Atrule + expect(unescape_css(rule.prelude!.text)).toBe('a.b') + const kf = parse('@keyframes \\66 oo{}').first_child as Atrule + expect(unescape_css(kf.prelude!.text)).toBe('foo') + }) +}) diff --git a/src/string-helpers.ts b/src/string-helpers.ts index ebea2ad..49d6aa6 100644 --- a/src/string-helpers.ts +++ b/src/string-helpers.ts @@ -6,6 +6,7 @@ export { strip_vendor_prefix, property_basename, strip_value_hack, + unescape_css, str_equals, str_starts_with, str_index_of, diff --git a/src/string-utils.ts b/src/string-utils.ts index dcbb0f2..45b056a 100644 --- a/src/string-utils.ts +++ b/src/string-utils.ts @@ -300,3 +300,59 @@ export function strip_value_hack(str: string): string { while (end > 0 && is_whitespace(str.charCodeAt(end - 1))) end-- return str.substring(0, end) } + +function is_hex_digit(ch: number): boolean { + return (ch >= 0x30 && ch <= 0x39) || (ch >= 0x41 && ch <= 0x46) || (ch >= 0x61 && ch <= 0x66) +} + +/** + * Resolve CSS escape sequences: `\31 23` → `123`, `\:` → `:`, `\\` → `\`. + * Hex escapes take 1-6 digits plus one optional trailing whitespace; invalid code points + * (0, surrogates, > 10FFFF) become U+FFFD. An escaped newline (string line continuation) is removed. + */ +export function unescape_css(str: string): string { + let i = str.indexOf('\\') + if (i === -1) return str + let out = str.substring(0, i) + const len = str.length + while (i < len) { + const ch = str.charCodeAt(i) + if (ch !== CHAR_BACKSLASH) { + let next = str.indexOf('\\', i) + if (next === -1) next = len + out += str.substring(i, next) + i = next + continue + } + i++ + if (i >= len) { + out += '\uFFFD' + break + } + const c = str.charCodeAt(i) + if (is_hex_digit(c)) { + let end = i + while (end < len && end - i < 6 && is_hex_digit(str.charCodeAt(end))) end++ + const code = parseInt(str.substring(i, end), 16) + out += + code === 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff) + ? '\uFFFD' + : String.fromCodePoint(code) + i = end + // One optional whitespace after a hex escape (CRLF counts as one) + if (i < len && is_whitespace(str.charCodeAt(i))) { + i += str.charCodeAt(i) === 0x0d && str.charCodeAt(i + 1) === 0x0a ? 2 : 1 + } + } else if (c === 0x0a || c === 0x0c) { + i++ + } else if (c === 0x0d) { + i += str.charCodeAt(i + 1) === 0x0a ? 2 : 1 + } else { + const cp = str.codePointAt(i)! + const s = String.fromCodePoint(cp) + out += s + i += s.length + } + } + return out +}