|
| 1 | +import { tokenize } from '@csstools/css-tokenizer'; |
| 2 | +import assert from 'assert'; |
| 3 | +import { parseCommaSeparatedListOfComponentValues, sourceIndices } from '@csstools/css-parser-algorithms'; |
| 4 | + |
| 5 | +const onParseError = (err) => { |
| 6 | + throw err; |
| 7 | +}; |
| 8 | + |
| 9 | +{ |
| 10 | + const indicesAndParts = [ |
| 11 | + [[0, 49], '(image-set( url("image1.jpg") calc(2 * 1x)), 50px)'], |
| 12 | + [[0, 49], '(image-set( url("image1.jpg") calc(2 * 1x)), 50px)'], |
| 13 | + [[1, 42], 'image-set( url("image1.jpg") calc(2 * 1x))'], |
| 14 | + [[11, 11], ' '], |
| 15 | + [[12, 28], 'url("image1.jpg")'], |
| 16 | + [[16, 27], '"image1.jpg"'], |
| 17 | + [[29, 29], ' '], |
| 18 | + [[30, 41], 'calc(2 * 1x)'], |
| 19 | + [[35, 35], '2'], |
| 20 | + [[36, 36], ' '], |
| 21 | + [[37, 37], '*'], |
| 22 | + [[38, 38], ' '], |
| 23 | + [[39, 40], '1x'], |
| 24 | + [[43, 43], ','], |
| 25 | + [[44, 44], ' '], |
| 26 | + [[45, 48], '50px'], |
| 27 | + [[51, 65], ' something-else'], |
| 28 | + [[51, 51], ' '], |
| 29 | + [[52, 65], 'something-else'], |
| 30 | + ].reverse(); |
| 31 | + |
| 32 | + const source = '(image-set( url("image1.jpg") calc(2 * 1x)), 50px), something-else'; |
| 33 | + |
| 34 | + const tokens = tokenize({ css: source }, { onParseError: onParseError }); |
| 35 | + const list = parseCommaSeparatedListOfComponentValues(tokens, { onParseError: onParseError }); |
| 36 | + list.forEach((listItem) => { |
| 37 | + { |
| 38 | + const [y, part] = indicesAndParts.pop(); |
| 39 | + const x = sourceIndices(listItem); |
| 40 | + assert.deepStrictEqual(x, y); |
| 41 | + assert.deepStrictEqual(source.slice(x[0], x[1] + 1), part); |
| 42 | + } |
| 43 | + |
| 44 | + listItem.forEach((componentValue) => { |
| 45 | + { |
| 46 | + const [y, part] = indicesAndParts.pop(); |
| 47 | + const x = sourceIndices(componentValue); |
| 48 | + assert.deepStrictEqual(x, y); |
| 49 | + assert.deepStrictEqual(source.slice(x[0], x[1] + 1), part); |
| 50 | + } |
| 51 | + |
| 52 | + if ('walk' in componentValue) { |
| 53 | + componentValue.walk((entry) => { |
| 54 | + const [y, part] = indicesAndParts.pop(); |
| 55 | + const x = sourceIndices(entry.node); |
| 56 | + assert.deepStrictEqual(x, y); |
| 57 | + assert.deepStrictEqual(source.slice(x[0], x[1] + 1), part); |
| 58 | + }); |
| 59 | + } |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
0 commit comments