Skip to content

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

fix(checker): scope finite_svg to numeric attributes (#372) (#373)

Adrianf63111441860
diff --git a/archify.zip b/archify.zip
index b4f4d8af..a4963eb5 100644
Binary files a/archify.zip and b/archify.zip differ
diff --git a/archify/scripts/check-render-output.mjs b/archify/scripts/check-render-output.mjs
index d24ce7f6..d59a69f4 100644
--- a/archify/scripts/check-render-output.mjs
+++ b/archify/scripts/check-render-output.mjs
@@ -59,6 +59,32 @@ let composition = {
issues: [],
};
+const NON_FINITE_TOKEN = /\b(?:NaN|undefined|Infinity)\b/;
+// Consume comments/CDATA as whole tokens, including any tag-like prose.
+const SVG_TAG_TOKEN = /<!--[\s\S]*?(?:-->|$)|<!\[CDATA\[[\s\S]*?(?:\]\]>|$)|<(\/?)([A-Za-z][\w:-]*)(?=[\s/>])(?:[^>"']|"[^"]*"|'[^']*')*>/g;
+const HTML_VOID_ELEMENTS = new Set(['area', 'base', 'br', 'col', 'embed', 'hr', 'img', 'input', 'link', 'meta', 'param', 'source', 'track', 'wbr']);
+const SVG_HTML_INTEGRATION_POINTS = new Set(['foreignobject', 'desc', 'title']);
+const HTML_ATTRIBUTE = /([A-Za-z_:][\w:.-]*)\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'=<>`]+))/g;
+const NUMERIC_ATTRS = new Set([
+ 'x', 'y', 'x1', 'y1', 'x2', 'y2', 'dx', 'dy', 'cx', 'cy', 'r', 'rx', 'ry', 'fx', 'fy',
+ 'fr', 'width', 'height', 'd', 'points', 'pathlength', 'transform', 'viewbox', 'offset',
+ 'opacity', 'fill-opacity', 'flood-opacity', 'stop-opacity', 'stroke-opacity', 'font-size',
+ 'font-size-adjust', 'font-weight', 'letter-spacing', 'word-spacing', 'kerning', 'stroke-width',
+ 'stroke-dasharray', 'stroke-dashoffset', 'stroke-miterlimit', 'textlength', 'startoffset',
+ 'rotate', 'markerwidth', 'markerheight', 'refx', 'refy', 'orient', 'patterntransform',
+ 'gradienttransform', 'filterres', 'stddeviation', 'basefrequency', 'numoctaves', 'seed',
+ 'surfacescale', 'diffuseconstant', 'specularconstant', 'specularexponent',
+ 'limitingconeangle', 'azimuth', 'elevation', 'pointsatx', 'pointsaty', 'pointsatz',
+ 'kernelmatrix', 'order', 'divisor', 'bias', 'targetx', 'targety', 'kernelunitlength',
+ 'scale', 'radius', 'k1', 'k2', 'k3', 'k4', 'tablevalues', 'slope', 'intercept',
+ 'amplitude', 'exponent',
+]);
+const ELEMENT_NUMERIC_ATTRS = new Map([
+ ['fecolormatrix', new Set(['values'])],
+ ['fepointlight', new Set(['z'])],
+ ['fespotlight', new Set(['z'])],
+]);
+
function addCheck(name, ok, details = []) {
checks.push({ name, ok, details });
}
@@ -72,7 +98,8 @@ if (svgMatches.length === 1) {
const svgAttrs = parseAttrs(svgRoot);
const qualityProfile = svgAttrs['data-quality-profile'] || 'standard';
const qualityGatesEnforced = svgAttrs['data-quality-gates'] !== 'advisory';
- addCheck('finite_svg', !/\b(?:NaN|undefined|Infinity|-Infinity)\b/.test(svg));
+ const nonFiniteAttrs = collectNonFiniteAttrs(svg);
+ addCheck('finite_svg', nonFiniteAttrs.length === 0, nonFiniteAttrs);
const legendStart = svg.indexOf('<!-- Legend -->');
const beforeLegend = legendStart >= 0 ? svg.slice(0, legendStart) : svg;
const desktopReadabilityIssue = collectDesktopReadability(svgAttrs, beforeLegend);
@@ -813,10 +840,63 @@ function padBox(box, padding) {
};
}
+// Only geometry/numeric attributes are scanned: authored prose such as node
+// tags, <title> text, aria-labels, or data-* attributes may legitimately
+// mention "NaN" or "Infinity" without any coordinate being non-finite.
+function collectNonFiniteAttrs(svg) {
+ const details = [];
+ const stack = [];
+ for (const match of svg.matchAll(SVG_TAG_TOKEN)) {
+ if (!match[2]) continue;
+ const element = match[2].toLowerCase();
+ if (match[1]) {
+ const index = stack.map(entry => entry.element).lastIndexOf(element);
+ if (index >= 0) stack.length = index;
+ continue;
+ }
+ const parent = stack[stack.length - 1];
+ const inSvg = element === 'svg' || !parent
+ || (parent.inSvg && !SVG_HTML_INTEGRATION_POINTS.has(parent.element));
+ if (inSvg) {
+ for (const [name, value] of attrEntries(match[0])) {
+ if (!isNumericAttr(element, name) || !NON_FINITE_TOKEN.test(decodeNumericReferences(value))) continue;
+ details.push(`${match[2]} ${name}="${value}"`);
+ }
+ }
+ // HTML void elements do not open a context; SVG self-closing tags do not
+ // either. A nested <svg> inside foreignObject restores SVG checking.
+ if (!(inSvg ? /\/\s*>$/.test(match[0]) : HTML_VOID_ELEMENTS.has(element))) {
+ stack.push({ element, inSvg });
+ }
+ }
+ return details;
+}
+
+function decodeNumericReferences(value) {
+ // Numeric references can encode every letter of NaN/Infinity/undefined.
+ // Decode once, locally: other checks and diagnostic evidence keep raw values.
+ return value.replace(/&#(?:x([0-9a-f]+)|([0-9]+));?/gi, (_, hex, decimal) => {
+ const point = Number.parseInt(hex || decimal, hex ? 16 : 10);
+ return point > 0 && point <= 0x10ffff && !(point >= 0xd800 && point <= 0xdfff)
+ ? String.fromCodePoint(point) : '\uFFFD';
+ });
+}
+
+function isNumericAttr(elementName, attrName) {
+ const normalizedAttr = attrName.toLowerCase();
+ return NUMERIC_ATTRS.has(normalizedAttr)
+ || ELEMENT_NUMERIC_ATTRS.get(elementName.toLowerCase())?.has(normalizedAttr);
+}
+
+function attrEntries(tag) {
+ return [...tag.matchAll(HTML_ATTRIBUTE)].map((match) => [
+ match[1],
+ match[2] ?? match[3] ?? match[4],
+ ]);
+}
+
function parseAttrs(tag) {
- const attrs = {};
- for (const match of tag.matchAll(/([\w:-]+)\s*=\s*"([^"]*)"/g)) attrs[match[1]] = match[2];
- return attrs;
+ return Object.fromEntries(attrEntries(tag));
}
function numberAttr(attrs, name) {
diff --git a/archify/test/render-output-checks.test.mjs b/archify/test/render-output-checks.test.mjs
index 0a7bbd47..55033122 100644
--- a/archify/test/render-output-checks.test.mjs
+++ b/archify/test/render-output-checks.test.mjs
@@ -433,3 +433,143 @@ test('render output check: endpoint stubs from 8px pass while cramped interior t
});
process.on('exit', () => fs.rmSync(tmp, { recursive: true, force: true }));
+
+test('render output check: finite_svg ignores comments and CDATA but still checks real elements', () => {
+ const { result } = checkHtml('finite-non-elements', `
+ <!-- <rect x="NaN"/> -->
+ <![CDATA[<path d="Infinity"/>]]>
+ <rect x="12" y="20" width="50" height="40"/>
+ <circle cx="NaN" cy="20" r="5"/>
+ `);
+ const check = result.checks.find(item => item.name === 'finite_svg');
+ assert.equal(check.ok, false);
+ assert.deepEqual(check.details, ['circle cx="NaN"']);
+});
+
+test('render output check: finite_svg decodes numeric references once and reports raw evidence', () => {
+ const { result } = checkHtml('finite-encoded-values', `
+ <rect x="&#78;aN" y="&#x49;&#110;&#102;&#105;&#110;&#105;&#116;&#121;"/>
+ <circle cx="&#x4eaN" cy="&amp;#78;aN" r="10"/>
+ <path d="M 0 0 L &#x4e;aN 12"/>
+ <text x="10" y="10" data-note="&#78;aN">&#78;aN</text>
+ `);
+ const check = result.checks.find(item => item.name === 'finite_svg');
+ assert.equal(check.ok, false);
+ assert.deepEqual(check.details, [
+ 'rect x="&#78;aN"',
+ 'rect y="&#x49;&#110;&#102;&#105;&#110;&#105;&#116;&#121;"',
+ 'path d="M 0 0 L &#x4e;aN 12"',
+ ]);
+});
+
+test('render output check: finite_svg separates foreignObject HTML from SVG geometry', () => {
+ const { result } = checkHtml('finite-foreign-object', `
+ <foreignObject x="NaN" y="0" width="100" height="100">
+ <div xmlns="http://www.w3.org/1999/xhtml" x="NaN" width="Infinity">
+ <input width="NaN"><br><div y="Infinity">HTML content</div>
+ </div>
+ </foreignObject>
+ <rect x="Infinity"/>
+ `);
+ const check = result.checks.find(item => item.name === 'finite_svg');
+ assert.equal(check.ok, false);
+ assert.deepEqual(check.details, ['foreignObject x="NaN"', 'rect x="Infinity"']);
+ const nested = checkHtml('finite-foreign-nested-svg', `
+ <foreignObject><div x="NaN"><svg><rect x="NaN"/></svg></div></foreignObject>
+ `).result.checks.find(item => item.name === 'finite_svg');
+ assert.deepEqual(nested.details, ['rect x="NaN"']);
+});
+
+test('render output check: finite_svg ignores prose that mentions NaN or Infinity', () => {
+ const { result } = checkHtml('finite-prose', `
+ <g data-node-id="solver" data-node-tag="returns a NaN leaf" aria-label="Focus Solver">
+ <title>Solver · returns a NaN leaf · Infinity guard</title>
+ <rect x="40" y="40" width="200" height="74" rx="6" class="c-mask"/>
+ <text x="140" y="70" class="t-primary" font-size="11" text-anchor="middle">Solver</text>
+ <text data-detail="fine" x="140" y="106" class="t-backend" font-size="7">returns a NaN leaf</text>
+ </g>
+ <!-- Legend -->
+ <text x="40" y="140" class="t-primary" font-size="10">Legend</text>
+ `);
+ const check = result.checks.find((item) => item.name === 'finite_svg');
+ assert.equal(check.ok, true);
+ assert.deepEqual(check.details, []);
+});
+
+test('render output check: finite_svg reports the attribute carrying a non-finite value', () => {
+ const { code, result } = checkHtml('finite-attr', `
+ <rect x="NaN" y="40" width="200" height="74" rx="6" class="c-mask"/>
+ <path d="M 20 20 L undefined 20" class="a-default" stroke-width="1.4"/>
+ <text x="140" y="Infinity" class="t-primary" font-size="11">Solver</text>
+ <!-- Legend -->
+ <text x="40" y="140" class="t-primary" font-size="10">Legend</text>
+ `);
+ assert.notEqual(code, 0);
+ const check = result.checks.find((item) => item.name === 'finite_svg');
+ assert.equal(check.ok, false);
+ assert.deepEqual(check.details, [
+ 'rect x="NaN"',
+ 'path d="M 20 20 L undefined 20"',
+ 'text y="Infinity"',
+ ]);
+});
+
+test('render output check: finite_svg parses quoted angle brackets and HTML attribute quoting', () => {
+ const { code, result } = checkHtml('finite-attr-syntax', `
+ <rect aria-label="greater > lesser" x="NaN" y="40" width="200" height="74"/>
+ <circle cx='Infinity' cy='40' r='6'/>
+ <line x1=undefined y1=10 x2=20 y2=10/>
+ `);
+ assert.notEqual(code, 0);
+ const check = result.checks.find((item) => item.name === 'finite_svg');
+ assert.equal(check.ok, false);
+ assert.deepEqual(check.details, [
+ 'rect x="NaN"',
+ 'circle cx="Infinity"',
+ 'line x1="undefined"',
+ ]);
+});
+
+test('render output check: finite_svg covers extended SVG numeric attributes', () => {
+ const { code, result } = checkHtml('finite-extended-attrs', `
+ <path d="M 0 0 L 10 0" pathLength="NaN"/>
+ <radialGradient fr="-Infinity"/>
+ <feGaussianBlur stdDeviation="undefined"/>
+ `);
+ assert.notEqual(code, 0);
+ const check = result.checks.find((item) => item.name === 'finite_svg');
+ assert.equal(check.ok, false);
+ assert.deepEqual(check.details, [
+ 'path pathLength="NaN"',
+ 'radialGradient fr="-Infinity"',
+ 'feGaussianBlur stdDeviation="undefined"',
+ ]);
+});
+
+test('render output check: finite_svg covers element-specific filter numeric attributes', () => {
+ const { code, result } = checkHtml('finite-element-specific-filter-attrs', `
+ <filter id="lighting">
+ <feDiffuseLighting><fePointLight x="10" y="10" z="NaN"/></feDiffuseLighting>
+ <feSpecularLighting><feSpotLight x="10" y="10" z="-Infinity"/></feSpecularLighting>
+ <feColorMatrix type="saturate" values="undefined"/>
+ </filter>
+ `);
+ assert.notEqual(code, 0);
+ const check = result.checks.find((item) => item.name === 'finite_svg');
+ assert.equal(check.ok, false);
+ assert.deepEqual(check.details, [
+ 'fePointLight z="NaN"',
+ 'feSpotLight z="-Infinity"',
+ 'feColorMatrix values="undefined"',
+ ]);
+});
+
+test('render output check: finite_svg keeps context-sensitive values attributes scoped', () => {
+ const { code, result } = checkHtml('finite-context-sensitive-values', `
+ <animate attributeName="data-node-tag" values="NaN;Infinity" dur="1s"/>
+ `);
+ assert.equal(code, 0);
+ const check = result.checks.find((item) => item.name === 'finite_svg');
+ assert.equal(check.ok, true);
+ assert.deepEqual(check.details, []);
+});