Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

test(xml): validate top-level SVG documents

sunsunsun2a09bf296d6a
diff --git a/archify/test/cli.test.mjs b/archify/test/cli.test.mjs
index 524c9061..b0b756ab 100644
--- a/archify/test/cli.test.mjs
+++ b/archify/test/cli.test.mjs
@@ -6,7 +6,7 @@ import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
-import { extractInlineSvgs, parseXml } from './helpers/xml.mjs';
+import { extractSvgs, parseXml } from './helpers/xml.mjs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const skillRoot = path.resolve(__dirname, '..');
@@ -403,9 +403,12 @@ test('cli: deliver works from an installed skill without node_modules', () => {
assert.equal(result.status, 0, `${label}: ${result.stderr}`);
assert.equal(JSON.parse(result.stdout).validation.checkCount, 9, label);
assert.equal(fs.existsSync(out), true, label);
- const [svg = ''] = extractInlineSvgs(fs.readFileSync(out, 'utf8'));
- assert.notEqual(svg, '', `${label}: expected one delivered SVG`);
- assert.doesNotThrow(() => parseXml(svg), `${label}: delivered SVG must be well-formed XML`);
+ const extracted = extractSvgs(fs.readFileSync(out, 'utf8'));
+ assert.equal(extracted.direct.length, 1, `${label}: expected one delivered SVG`);
+ assert.doesNotThrow(
+ () => parseXml(extracted.direct[0]),
+ `${label}: delivered SVG must be well-formed XML`,
+ );
}
});
diff --git a/archify/test/generated-artifact-xml.test.mjs b/archify/test/generated-artifact-xml.test.mjs
index ceb828b3..3ed8d14a 100644
--- a/archify/test/generated-artifact-xml.test.mjs
+++ b/archify/test/generated-artifact-xml.test.mjs
@@ -4,7 +4,7 @@ import { spawnSync } from 'node:child_process';
import fs from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
-import { extractSvgs, parseXml } from './helpers/svg-xml.mjs';
+import { extractSvgs, parseXml } from './helpers/xml.mjs';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const skillRoot = path.resolve(__dirname, '..');
@@ -28,30 +28,37 @@ function trackedHtmlArtifacts() {
.sort();
}
-test('artifact SVG extraction follows HTML quoting and nested SVG structure', () => {
+test('artifact SVG extraction follows HTML quoting and preserves SVG document boundaries', () => {
const extracted = extractSvgs(`
<script>const ignored = '<svg data-node-label></svg>';</script>
<template><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1 1"/></template>
<iframe srcdoc='&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot;&gt;&lt;svg viewBox=&quot;0 0 1 1&quot;/&gt;&lt;/svg&gt;'></iframe>
`);
assert.equal(extracted.direct.length, 1, 'template SVG is markup while script text is not');
- assert.equal(extracted.embedded.length, 2, 'single-quoted srcdoc retains both nested SVG elements');
+ assert.equal(extracted.embedded.length, 1, 'srcdoc contributes one top-level SVG document');
for (const svg of [...extracted.direct, ...extracted.embedded]) assert.doesNotThrow(() => parseXml(svg));
+
+ const inheritedNamespace = extractSvgs(`
+ <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
+ <svg><use xlink:href="#icon"/></svg>
+ </svg>
+ `);
+ assert.equal(inheritedNamespace.direct.length, 1, 'nested SVG remains inside its XML document');
+ assert.doesNotThrow(() => parseXml(inheritedNamespace.direct[0]));
});
-test('tracked generated artifacts embed well-formed XML SVG', () => {
+test('tracked browsable HTML embeds well-formed XML SVG', () => {
const artifacts = trackedHtmlArtifacts();
- assert.ok(artifacts.includes('docs/gallery.html'), 'expected the tracked generated Gallery page inventory');
- let embeddedSvgCount = 0;
- const checkedArtifacts = [];
+ const checkoutArtifact = 'examples/checkout-platform-delta.html';
+ assert.ok(artifacts.includes(checkoutArtifact), 'expected the tracked Checkout compare artifact');
+ let checkoutSvgs;
for (const relative of artifacts) {
const html = fs.readFileSync(path.join(repoRoot, relative), 'utf8');
const extracted = extractSvgs(html);
- embeddedSvgCount += extracted.embedded.length;
+ if (relative === checkoutArtifact) checkoutSvgs = extracted;
const svgs = [...extracted.direct, ...extracted.embedded];
if (svgs.length === 0) continue;
- checkedArtifacts.push(relative);
for (const [index, svg] of svgs.entries()) {
assert.doesNotThrow(
() => parseXml(svg),
@@ -60,7 +67,6 @@ test('tracked generated artifacts embed well-formed XML SVG', () => {
}
}
- assert.ok(checkedArtifacts.length > 35, 'expected the tracked generated SVG artifact inventory');
- assert.ok(checkedArtifacts.includes('examples/checkout-platform-delta.html'), 'expected the Compare artifact');
- assert.ok(embeddedSvgCount >= 2, 'expected Compare base/head srcdoc SVG snapshots');
+ assert.equal(checkoutSvgs?.direct.length, 1, 'Checkout must contain one comparison SVG');
+ assert.equal(checkoutSvgs?.embedded.length, 2, 'Checkout must retain its base/head SVG snapshots');
});
diff --git a/archify/test/helpers/svg-xml.mjs b/archify/test/helpers/svg-xml.mjs
deleted file mode 100644
index 80d0bdd1..00000000
--- a/archify/test/helpers/svg-xml.mjs
+++ /dev/null
@@ -1,36 +0,0 @@
-import { parse, parseFragment } from 'parse5';
-import { SaxesParser } from 'saxes';
-
-function visit(node, callback) {
- callback(node);
- for (const child of node.childNodes || []) visit(child, callback);
- if (node.content) visit(node.content, callback);
-}
-
-export function parseXml(source) {
- return new SaxesParser({ xmlns: true }).write(source).close();
-}
-
-export function extractSvgs(markup, fragment = false) {
- const document = fragment
- ? parseFragment(markup, { sourceCodeLocationInfo: true })
- : parse(markup, { sourceCodeLocationInfo: true });
- const direct = [];
- const srcdocs = [];
-
- visit(document, (node) => {
- if (node.tagName === 'svg' && node.sourceCodeLocation) {
- direct.push(markup.slice(node.sourceCodeLocation.startOffset, node.sourceCodeLocation.endOffset));
- }
- const srcdoc = node.attrs?.find((attribute) => attribute.name === 'srcdoc');
- if (srcdoc) srcdocs.push(srcdoc.value);
- });
-
- return {
- direct,
- embedded: srcdocs.flatMap((srcdoc) => {
- const nested = extractSvgs(srcdoc, true);
- return [...nested.direct, ...nested.embedded];
- }),
- };
-}
diff --git a/archify/test/helpers/xml.mjs b/archify/test/helpers/xml.mjs
index 0a8d3cee..84c006df 100644
--- a/archify/test/helpers/xml.mjs
+++ b/archify/test/helpers/xml.mjs
@@ -1,9 +1,37 @@
+import { parse, parseFragment } from 'parse5';
import { SaxesParser } from 'saxes';
-export function extractInlineSvgs(source) {
- return source.match(/<svg\b[\s\S]*?<\/svg>/g) || [];
+function visit(node, callback, insideSvg = false) {
+ callback(node, insideSvg);
+ const childInsideSvg = insideSvg || node.tagName === 'svg';
+ for (const child of node.childNodes || []) visit(child, callback, childInsideSvg);
+ if (node.content) visit(node.content, callback, childInsideSvg);
}
export function parseXml(source) {
return new SaxesParser({ xmlns: true }).write(source).close();
}
+
+export function extractSvgs(markup, fragment = false) {
+ const document = fragment
+ ? parseFragment(markup, { sourceCodeLocationInfo: true })
+ : parse(markup, { sourceCodeLocationInfo: true });
+ const direct = [];
+ const srcdocs = [];
+
+ visit(document, (node, insideSvg) => {
+ if (node.tagName === 'svg' && !insideSvg && node.sourceCodeLocation) {
+ direct.push(markup.slice(node.sourceCodeLocation.startOffset, node.sourceCodeLocation.endOffset));
+ }
+ const srcdoc = node.attrs?.find((attribute) => attribute.name === 'srcdoc');
+ if (srcdoc) srcdocs.push(srcdoc.value);
+ });
+
+ return {
+ direct,
+ embedded: srcdocs.flatMap((srcdoc) => {
+ const nested = extractSvgs(srcdoc, true);
+ return [...nested.direct, ...nested.embedded];
+ }),
+ };
+}