-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcss.test.js
More file actions
72 lines (61 loc) · 2.49 KB
/
css.test.js
File metadata and controls
72 lines (61 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* eslint-disable no-restricted-syntax */
import assert from 'node:assert';
import { describe, test } from 'node:test';
import { escapeCSS } from './css.js';
describe('escapeCSS', () => {
test('passes through valid identifiers', () => {
assert.strictEqual(escapeCSS('foo'), 'foo');
assert.strictEqual(escapeCSS('foo-bar'), 'foo-bar');
assert.strictEqual(escapeCSS('foo_bar'), 'foo_bar');
assert.strictEqual(escapeCSS('FOO'), 'FOO');
});
test('escapes syntax characters', () => {
// Class, ID, Pseudo-classes
assert.strictEqual(escapeCSS('foo.bar'), 'foo\\.bar');
assert.strictEqual(escapeCSS('foo#bar'), 'foo\\#bar');
assert.strictEqual(escapeCSS('foo:bar'), 'foo\\:bar');
// Brackets and attributes
assert.strictEqual(escapeCSS('[data=val]'), '\\[data\\=val\\]');
});
test('handles leading digits (must be hex escaped)', () => {
// 1 -> \31 + space
assert.strictEqual(escapeCSS('123'), '\\31 23');
// 9 -> \39 + space
assert.strictEqual(escapeCSS('987'), '\\39 87');
// 0 -> \30 + space
assert.strictEqual(escapeCSS('0abc'), '\\30 abc');
});
test('handles leading hyphen followed by digit', () => {
// Hyphen remains, digit becomes hex escaped
assert.strictEqual(escapeCSS('-123'), '-\\31 23');
assert.strictEqual(escapeCSS('-0'), '-\\30 ');
});
test('handles single hyphen vs double hyphen', () => {
// Single hyphen is a reserved syntax if standalone, must escape
assert.strictEqual(escapeCSS('-'), '\\-');
// Double hyphen is valid (starts a custom property), passes through
assert.strictEqual(escapeCSS('--var'), '--var');
});
test('handles control characters', () => {
// Null byte -> Replacement Character (U+FFFD)
assert.strictEqual(escapeCSS('\u0000'), '\uFFFD');
// C0 Control (0x01-0x1F) -> Hex escape + space
assert.strictEqual(escapeCSS('\x01'), '\\1 ');
assert.strictEqual(escapeCSS('\x1F'), '\\1f ');
// Delete (0x7F) -> Hex escape + space
assert.strictEqual(escapeCSS('\x7F'), '\\7f ');
});
test('handles high ASCII and Unicode', () => {
// Should pass through untouched
assert.strictEqual(escapeCSS('©'), '©');
assert.strictEqual(escapeCSS('💩'), '💩');
assert.strictEqual(escapeCSS('über'), 'über');
});
test('converts non-strings to strings (IDL behavior)', () => {
assert.strictEqual(escapeCSS(123), '\\31 23');
// String(null) -> "null" (valid identifier)
assert.strictEqual(escapeCSS(null), 'null');
// String(undefined) -> "undefined" (valid identifier)
assert.strictEqual(escapeCSS(undefined), 'undefined');
});
});