// Every close, dismiss and clear button in a Vue view is a UiCloseButton: no
// SFC template hand-writes a button whose only content is × or the Material
// `close` symbol. Buttons built in script are not read here.
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { readFileSync, readdirSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const SRC_ROOT = fileURLToPath(new URL('../../', import.meta.url));
const REPO_ROOT = path.resolve(SRC_ROOT, '..');
/** Every SFC under src. */
function sfcFiles(directory = SRC_ROOT) {
const files = [];
for (const entry of readdirSync(directory, { withFileTypes: true })) {
const absolute = path.join(directory, entry.name);
if (entry.isDirectory()) files.push(...sfcFiles(absolute));
else if (entry.isFile() && entry.name.endsWith('.vue'))
files.push(absolute);
}
return files.sort();
}
/** The template block: everything outside script and style. */
function template(source) {
return source.replace(/<(script|style)\b[\s\S]*?<\/\1\s*>/g, '');
}
// An opening tag's attributes, quoted values may hold `>` (`() => x`).
const ATTRS = String.raw`(?:[^>"']|"[^"]*"|'[^']*')*`;
const BUTTON = String.raw`(button|UiButton)`;
const CLOSE_GLYPH = [
String.raw`×|×|×|×`,
String.raw`]*\bmaterial-symbols-outlined\b[^>]*>\s*close\s*`,
String.raw``,
].join('|');
const HAND_WRITTEN_CLOSE = new RegExp(
String.raw`<${BUTTON}\b${ATTRS}>\s*(?:${CLOSE_GLYPH})\s*\1\s*>`,
'gi',
);
/** Hand-written close buttons in an SFC's template. */
function handWrittenCloses(source) {
return [...template(source).matchAll(HAND_WRITTEN_CLOSE)].map((m) =>
m[0].replace(/\s+/g, ' '),
);
}
test('the scan finds each hand-written close glyph', () => {
for (const markup of [
'',
'',
'×',
'',
'',
])
assert.equal(handWrittenCloses(markup).length, 1, markup);
for (const markup of [
'',
'',
'',
'',
])
assert.equal(handWrittenCloses(markup).length, 0, markup);
});
test('no SFC hand-writes a close button; each is a UiCloseButton', () => {
const found = [];
for (const file of sfcFiles()) {
const relative = path.relative(REPO_ROOT, file).split(path.sep).join('/');
for (const markup of handWrittenCloses(readFileSync(file, 'utf8')))
found.push(`${relative}: ${markup}`);
}
assert.deepEqual(found, []);
});