224 lines
11 KiB
JavaScript
224 lines
11 KiB
JavaScript
// The CSS / SCSS model of the visual stylesheet editor: parsing, the one-declaration edits, selectors, colours.
|
|
// Run: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/css-model.test.mjs
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
parseStylesheet, outline, declarationOf, setDeclaration, applyEdit, removeNode, duplicateNode, insertRule, moveNode, setSelector,
|
|
resolvedSelector, specificity, maxSpecificity, parseColor, formatColor, colorFormat, findColors, variablesOf, resolveColor, expandBox,
|
|
compactBox, parseShadows, formatShadows, parseGradient, formatGradient, findByPath, pathOf, matchableSelector, flattenScss, lineCol, labelOf, blockAt,
|
|
} from './css-model.js';
|
|
|
|
const CSS = `/* the site */
|
|
:root {
|
|
--brand: skyblue;
|
|
--ink: #223;
|
|
}
|
|
|
|
body {
|
|
margin: 0; /* no gap */
|
|
color: var(--ink);
|
|
font-family: "Inter", sans-serif;
|
|
}
|
|
|
|
@media (max-width: 600px) {
|
|
.card,
|
|
.panel > h2 { padding: 4px 8px }
|
|
}
|
|
|
|
@font-face { font-family: Inter; src: url("fonts/inter.woff2") format("woff2"); }
|
|
@keyframes spin { from { transform: rotate(0) } to { transform: rotate(360deg) } }
|
|
a:hover { background: url(data:image/png;base64,AAA=) no-repeat, linear-gradient(90deg, red, rgba(0, 0, 255, .5) 50%) !important; }
|
|
`;
|
|
|
|
const SCSS = `@use "sass:math";
|
|
$gap: 8px !default;
|
|
$accent: tomato;
|
|
%button-base { border: 0; }
|
|
|
|
// a card
|
|
.card {
|
|
padding: $gap;
|
|
&:hover { color: $accent; }
|
|
&__title, .x & { font-weight: 700; }
|
|
.body {
|
|
margin: #{$gap * 2} 0;
|
|
@media (min-width: 900px) { margin: 0; }
|
|
}
|
|
@include shadow(2);
|
|
}
|
|
`;
|
|
|
|
test('parsing a CSS file finds every block and declaration, without errors', () => {
|
|
const m = parseStylesheet(CSS);
|
|
assert.deepEqual(m.errors, []);
|
|
const labels = outline(m).map((o) => `${' '.repeat(o.depth)}${labelOf(o.node)}`);
|
|
assert.deepEqual(labels, [':root', 'body', '@media (max-width: 600px)', ' .card, .panel > h2', '@font-face', '@keyframes spin', ' from', ' to', 'a:hover']);
|
|
const body = outline(m)[1].node;
|
|
assert.equal(declarationOf(body, 'margin').value, '0');
|
|
assert.equal(declarationOf(body, 'font-family').value, '"Inter", sans-serif');
|
|
const hover = outline(m)[8].node;
|
|
const bg = declarationOf(hover, 'background');
|
|
assert.equal(bg.important, true);
|
|
assert.match(bg.value, /^url\(data:image\/png;base64,AAA=\) no-repeat, linear-gradient/);
|
|
});
|
|
|
|
test('nothing changes when nothing is edited, and setting a value to itself gives the same text', () => {
|
|
const m = parseStylesheet(CSS);
|
|
for (const { node } of outline(m)) {
|
|
for (const d of (node.children || []).filter((n) => n.type === 'decl')) {
|
|
assert.equal(applyEdit(CSS, setDeclaration(CSS, node, d.prop, d.value)), CSS, d.prop);
|
|
}
|
|
}
|
|
});
|
|
|
|
test('editing one value keeps the comments and the formatting around it', () => {
|
|
const m = parseStylesheet(CSS);
|
|
const body = outline(m)[1].node;
|
|
const out = applyEdit(CSS, setDeclaration(CSS, body, 'margin', '0 auto'));
|
|
assert.ok(out.includes(' margin: 0 auto; /* no gap */\n color: var(--ink);'));
|
|
assert.equal(out.length, CSS.length + 5);
|
|
// !important on, then off again: the same text
|
|
const imp = applyEdit(CSS, setDeclaration(CSS, body, 'color', 'var(--ink)', true));
|
|
assert.ok(imp.includes('color: var(--ink) !important;'));
|
|
const m2 = parseStylesheet(imp);
|
|
assert.equal(applyEdit(imp, setDeclaration(imp, outline(m2)[1].node, 'color', 'var(--ink)', false)), CSS);
|
|
});
|
|
|
|
test('a new declaration goes after the last one, at its indentation; an inline rule stays on one line', () => {
|
|
const m = parseStylesheet(CSS);
|
|
const body = outline(m)[1].node;
|
|
const out = applyEdit(CSS, setDeclaration(CSS, body, 'line-height', '1.5'));
|
|
assert.ok(out.includes(' font-family: "Inter", sans-serif;\n line-height: 1.5;\n}'));
|
|
const card = outline(m)[3].node;
|
|
const out2 = applyEdit(CSS, setDeclaration(CSS, card, 'gap', '2px'));
|
|
assert.ok(out2.includes('.panel > h2 { padding: 4px 8px; gap: 2px; }'), out2);
|
|
const empty = 'a {}\nb {\n}\n';
|
|
const em = parseStylesheet(empty);
|
|
assert.equal(applyEdit(empty, setDeclaration(empty, outline(em)[0].node, 'color', 'red')), 'a { color: red; }\nb {\n}\n');
|
|
assert.equal(applyEdit(empty, setDeclaration(empty, outline(em)[1].node, 'color', 'red')), 'a {}\nb {\n color: red;\n}\n');
|
|
});
|
|
|
|
test('removing a declaration takes its line away', () => {
|
|
const m = parseStylesheet(CSS);
|
|
const body = outline(m)[1].node;
|
|
const out = applyEdit(CSS, setDeclaration(CSS, body, 'color', null));
|
|
assert.ok(out.includes(' margin: 0; /* no gap */\n font-family'));
|
|
});
|
|
|
|
test('rules are duplicated, deleted, moved, renamed and added as one edit each', () => {
|
|
const m = parseStylesheet(CSS);
|
|
const [root, body] = outline(m).map((o) => o.node);
|
|
const dup = applyEdit(CSS, duplicateNode(CSS, body));
|
|
assert.equal(dup.split('body {').length, 3);
|
|
assert.equal(parseStylesheet(dup).errors.length, 0);
|
|
const del = applyEdit(CSS, removeNode(CSS, body));
|
|
assert.ok(!del.includes('body {'));
|
|
assert.ok(!/\n\n\n/.test(del));
|
|
const moved = applyEdit(CSS, moveNode(CSS, body, -1));
|
|
assert.ok(moved.indexOf('body {') < moved.indexOf(':root {'));
|
|
assert.equal(moveNode(CSS, root, -1), null);
|
|
const renamed = applyEdit(CSS, setSelector(body, 'html, body'));
|
|
assert.ok(renamed.includes('html, body {\n margin'));
|
|
const added = applyEdit(CSS, insertRule(CSS, '.new', body));
|
|
assert.ok(added.includes('}\n\n.new {\n \n}\n\n@media'));
|
|
const end = applyEdit('a { b: c; }', insertRule('a { b: c; }', '.z'));
|
|
assert.equal(end, 'a { b: c; }\n\n.z {\n \n}\n');
|
|
});
|
|
|
|
test('a node is found again after the text changed', () => {
|
|
const m = parseStylesheet(CSS);
|
|
const card = outline(m)[3].node;
|
|
const path = pathOf(card);
|
|
const changed = applyEdit(CSS, setDeclaration(CSS, outline(m)[1].node, 'line-height', '2'));
|
|
const again = findByPath(parseStylesheet(changed), path, labelOf(card));
|
|
assert.equal(labelOf(again), '.card, .panel > h2');
|
|
assert.equal(labelOf(blockAt(m, CSS.indexOf('padding: 4px'))), '.card, .panel > h2');
|
|
});
|
|
|
|
test('SCSS: nesting, $variables, placeholders, @use / @include, // comments', () => {
|
|
const m = parseStylesheet(SCSS, { scss: true });
|
|
assert.deepEqual(m.errors, []);
|
|
const labels = outline(m).map((o) => `${' '.repeat(o.depth)}${labelOf(o.node)}`);
|
|
assert.deepEqual(labels, ['@use "sass:math"', '%button-base', '.card', ' &:hover', ' &__title, .x &', ' .body', ' @media (min-width: 900px)', ' @include shadow(2)']);
|
|
const nodes = outline(m).map((o) => o.node);
|
|
assert.equal(resolvedSelector(nodes[3]), '.card:hover');
|
|
assert.equal(resolvedSelector(nodes[4]), '.card__title, .x .card');
|
|
assert.equal(resolvedSelector(nodes[5]), '.card .body');
|
|
assert.equal(declarationOf(nodes[5], 'margin').value, '#{$gap * 2} 0');
|
|
const vars = variablesOf(m);
|
|
assert.equal(vars.get('$gap'), '8px');
|
|
assert.deepEqual(resolveColor('$accent', vars), { r: 255, g: 99, b: 71, a: 1 });
|
|
// an edit in a nested rule keeps the rest as it is
|
|
const out = applyEdit(SCSS, setDeclaration(SCSS, nodes[3], 'color', 'red'));
|
|
assert.equal(out, SCSS.replace('&:hover { color: $accent; }', '&:hover { color: red; }'));
|
|
const flat = flattenScss(m);
|
|
assert.ok(flat.includes('.card {\n padding: 8px;\n}'), flat);
|
|
assert.ok(flat.includes('.card:hover {\n color: tomato;\n}'));
|
|
assert.ok(flat.includes('@media (min-width: 900px) {\n.card .body {\n margin: 0;\n}'), flat);
|
|
});
|
|
|
|
test('parse errors are found with their place', () => {
|
|
const m = parseStylesheet('a { color: red;\nb { x }\n');
|
|
assert.ok(m.errors.some((e) => e.message === 'Missing }'));
|
|
const stray = parseStylesheet('a { color: red; } }');
|
|
assert.deepEqual(stray.errors.map((e) => [e.message, lineCol(stray.text, e.offset).col]), [['Unexpected }', 19]]);
|
|
assert.ok(parseStylesheet('color: red;').errors.some((e) => e.message === 'A declaration outside a rule'));
|
|
assert.ok(parseStylesheet('a { "unclosed }').errors.some((e) => e.message === 'Unclosed string'));
|
|
});
|
|
|
|
test('specificity, like the browsers', () => {
|
|
assert.deepEqual(specificity('#nav .item a:hover'), [1, 2, 1]);
|
|
assert.deepEqual(specificity('ul li::before'), [0, 0, 3]);
|
|
assert.deepEqual(specificity(':where(.a, #b) p'), [0, 0, 1]);
|
|
assert.deepEqual(specificity(':is(.a, #b) p'), [1, 0, 1]);
|
|
assert.deepEqual(specificity('input[type="text"]:not(.x)'), [0, 2, 1]);
|
|
assert.deepEqual(specificity('*'), [0, 0, 0]);
|
|
assert.deepEqual(maxSpecificity('a, .b, #c'), [1, 0, 0]);
|
|
assert.equal(matchableSelector('a:hover, .b::before, %x, .c >'), 'a, .b, .c > *');
|
|
});
|
|
|
|
test('colours: parsed in every notation, written back in the chosen one', () => {
|
|
assert.deepEqual(parseColor('skyblue'), { r: 135, g: 206, b: 235, a: 1 });
|
|
assert.deepEqual(parseColor('#f00a'), { r: 255, g: 0, b: 0, a: 0.667 });
|
|
assert.deepEqual(parseColor('rgb(10 20 30 / 50%)'), { r: 10, g: 20, b: 30, a: 0.5 });
|
|
assert.deepEqual(parseColor('rgba(10, 20, 30, .25)'), { r: 10, g: 20, b: 30, a: 0.25 });
|
|
assert.deepEqual(parseColor('hsl(120, 100%, 25%)'), { r: 0, g: 128, b: 0, a: 1 });
|
|
assert.deepEqual(parseColor('oklch(62.8% 0.2577 29.23)'), { r: 255, g: 0, b: 0, a: 1 });
|
|
assert.equal(parseColor('hwb(0 0% 0%)').r, 255);
|
|
assert.equal(parseColor('notacolor'), null);
|
|
assert.equal(parseColor('rgb(a, b, c)'), null);
|
|
const c = { r: 135, g: 206, b: 235, a: 1 };
|
|
assert.equal(formatColor(c, 'hex'), '#87ceeb');
|
|
assert.equal(formatColor(c, 'name'), 'skyblue');
|
|
assert.equal(formatColor({ ...c, a: 0.5 }, 'rgb'), 'rgba(135, 206, 235, 0.5)');
|
|
assert.equal(formatColor({ r: 0, g: 128, b: 0, a: 1 }, 'hsl'), 'hsl(120, 100%, 25.1%)');
|
|
assert.match(formatColor({ r: 255, g: 0, b: 0, a: 1 }, 'oklch'), /^oklch\(62\.\d+% 0\.25\d+ 29\.\d+\)$/);
|
|
assert.equal(formatColor({ ...c, a: 0.5 }, 'hex'), '#87ceeb80');
|
|
assert.equal(colorFormat('HSL(1,2%,3%)'), 'hsl');
|
|
assert.equal(colorFormat('tomato'), 'name');
|
|
});
|
|
|
|
test('the colours of a stylesheet, with variables resolved, never in urls nor font names', () => {
|
|
const m = parseStylesheet(CSS);
|
|
const found = findColors(m).map((f) => [f.text, formatColor(f.color), f.variable]);
|
|
assert.deepEqual(found, [
|
|
['skyblue', '#87ceeb', false], ['#223', '#222233', false], ['var(--ink)', '#222233', true],
|
|
['red', '#ff0000', false], ['rgba(0, 0, 255, .5)', '#0000ff80', false],
|
|
]);
|
|
const s = parseStylesheet(SCSS, { scss: true });
|
|
assert.deepEqual(findColors(s).map((f) => f.text), ['tomato', '$accent']);
|
|
});
|
|
|
|
test('box shorthands, shadows and gradients round-trip', () => {
|
|
assert.deepEqual(expandBox('4px 8px'), ['4px', '8px', '4px', '8px']);
|
|
assert.equal(compactBox(['1px', '2px', '1px', '2px']), '1px 2px');
|
|
assert.equal(compactBox(['1px', '2px', '3px', '2px']), '1px 2px 3px');
|
|
assert.equal(compactBox(['0', '0', '0', '0']), '0');
|
|
const sh = parseShadows('inset 0 1px 2px rgba(0, 0, 0, .3), 2px 2px red');
|
|
assert.deepEqual(sh[0], { inset: true, x: '0', y: '1px', blur: '2px', spread: '', color: 'rgba(0, 0, 0, .3)' });
|
|
assert.equal(formatShadows(sh), 'inset 0 1px 2px rgba(0, 0, 0, .3), 2px 2px red');
|
|
const g = parseGradient('linear-gradient(90deg, red, rgba(0, 0, 255, .5) 50%)');
|
|
assert.deepEqual(g, { type: 'linear', repeating: false, head: '90deg', stops: [{ color: 'red', pos: '' }, { color: 'rgba(0, 0, 255, .5)', pos: '50%' }] });
|
|
assert.equal(formatGradient(g), 'linear-gradient(90deg, red, rgba(0, 0, 255, .5) 50%)');
|
|
assert.equal(parseGradient('red'), null);
|
|
});
|