| 1 |
let { deepStrictEqual } = require('node:assert') |
| 2 |
let { test } = require('node:test') |
| 3 |
let { Input } = require('postcss') |
| 4 |
|
| 5 |
let tokenize = require('../tokenize') |
| 6 |
|
| 7 |
function run(css, expectedTokens) { |
| 8 |
let tokens = tokenize(new Input(css)) |
| 9 |
deepStrictEqual(tokens, expectedTokens) |
| 10 |
} |
| 11 |
|
| 12 |
test('tokenizes inline comments', () => { |
| 13 |
run('// a', [['comment', '// a', 0, 4, 'inline']]) |
| 14 |
}) |
| 15 |
|
| 16 |
test('tokenizes inline comments and new lines', () => { |
| 17 |
run('// a\r\n', [ |
| 18 |
['comment', '// a', 0, 4, 'inline'], |
| 19 |
['newline', '\r\n', 4, 6] |
| 20 |
]) |
| 21 |
}) |
| 22 |
|
| 23 |
test('tokenizes new lines around spaces', () => { |
| 24 |
run(' \n ', [ |
| 25 |
['space', ' ', 0, 1], |
| 26 |
['newline', '\n', 1, 2], |
| 27 |
['space', ' ', 2, 3] |
| 28 |
]) |
| 29 |
}) |
| 30 |
|
| 31 |
test('tokenizes Windows new lines', () => { |
| 32 |
run('\r\n', [['newline', '\r\n', 0, 2]]) |
| 33 |
}) |
| 34 |
|
| 35 |
test('tokenizes single carriage return', () => { |
| 36 |
run('\ra', [ |
| 37 |
['newline', '\r', 0, 1], |
| 38 |
['word', 'a', 1, 2] |
| 39 |
]) |
| 40 |
}) |
| 41 |
|
| 42 |
test('tokenizes last carriage return', () => { |
| 43 |
run('\r', [['newline', '\r', 0, 1]]) |
| 44 |
}) |
| 45 |
|
| 46 |
test('tokenizes comma', () => { |
| 47 |
run('a,b', [ |
| 48 |
['word', 'a', 0, 1], |
| 49 |
[',', ',', 1, 2], |
| 50 |
['word', 'b', 2, 3] |
| 51 |
]) |
| 52 |
}) |
| 53 |
|
| 54 |
test('escapes control symbols', () => { |
| 55 |
run('\\(\\{\\"\\@\\\\""', [ |
| 56 |
['word', '\\(', 0, 2], |
| 57 |
['word', '\\{', 2, 4], |
| 58 |
['word', '\\"', 4, 6], |
| 59 |
['word', '\\@', 6, 8], |
| 60 |
['word', '\\\\', 8, 10], |
| 61 |
['string', '""', 10, 12] |
| 62 |
]) |
| 63 |
}) |
| 64 |
|
| 65 |
test('escapes new line', () => { |
| 66 |
run('\\\n\\\r\\\r\n\\\f\\\\\n', [ |
| 67 |
['word', '\\\n', 0, 2], |
| 68 |
['word', '\\\r', 2, 4], |
| 69 |
['word', '\\\r\n', 4, 7], |
| 70 |
['word', '\\\f', 7, 9], |
| 71 |
['word', '\\\\', 9, 11], |
| 72 |
['newline', '\n', 11, 12] |
| 73 |
]) |
| 74 |
}) |
| 75 |
|
| 76 |
test('tokenizes close curly brace', () => { |
| 77 |
run('a { color: black; }', [ |
| 78 |
['word', 'a', 0, 1], |
| 79 |
['space', ' ', 1, 2], |
| 80 |
['{', '{', 2, 3], |
| 81 |
['space', ' ', 3, 4], |
| 82 |
['word', 'color', 4, 9], |
| 83 |
[':', ':', 9, 10], |
| 84 |
['space', ' ', 10, 11], |
| 85 |
['word', 'black', 11, 16], |
| 86 |
[';', ';', 16, 17], |
| 87 |
['space', ' ', 17, 18], |
| 88 |
['}', '}', 18, 19] |
| 89 |
]) |
| 90 |
}) |
| 91 |
|
| 92 |
test.run() |