An agent skill for creating detailed, interactive isometric illustrations of real-world machines and complex systems. Generates polished SVGs with optional WebGL shaders and 3D effects.
fix(audit): decode native SVG attributes
Rudy Celekli6b8f5d79f27b
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex fe3a3ed..d62a4e9 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -12,6 +12,8 @@ All notable changes to Anatomy are recorded here. The format follows [Keep a Cha### Fixed+- Decode valid SVG attribute quotes, whitespace and XML character references in audit bounds.+- `scripts/drive.mjs` now fails when a browser evaluation throws or rejects, rather than reporting a clean successful session.- The arcade cabinet now accepts coins with reduced motion enabled, preserving the meter, credit and power-on events when the coin animation is skipped.- The turning dial stops its idle tour when reduced motion is enabled while the page is open and cancels its pending automatic restart while preserving keyboard and pointer input.diff --git a/kit/audit.mjs b/kit/audit.mjsindex 7fa64e1..9f0f3ef 100644--- a/kit/audit.mjs+++ b/kit/audit.mjs@@ -1386,11 +1386,19 @@ function matrixOf(text) {const NUMBER = /-?(?:\d+\.?\d*|\.\d+)(?:e[-+]?\d+)?/gi;+const attributeOf = (attrs, name) => {+ const hit = [...attrs.matchAll(/(?:^|\s)([^\s=/>]+)\s*=\s*(["'])([\s\S]*?)\2/g)].find((match) => match[1] === name);+ if (!hit) return null;+ const named = { amp: "&", lt: "<", gt: ">", quot: '"', apos: "'" };+ return hit[3].replace(/&(#x[0-9a-fA-F]+|#\d+|amp|lt|gt|quot|apos);/g, (all, code) => {+ if (code[0] !== "#") return named[code];+ const value = code[1] === "x" ? parseInt(code.slice(2), 16) : Number(code.slice(1));+ return value > 0 && value <= 0x10ffff ? String.fromCodePoint(value) : all;+ });+};+function pointsOfShape(tag, attrs) {- const get = (name) => {- const hit = attrs.match(new RegExp(`(?:^|\\s)${name}="([^"]*)"`));- return hit ? hit[1] : null;- };+ const get = (name) => attributeOf(attrs, name);const num = (name) => Number(get(name) ?? 0);if (tag === "path") {const d = get("d") ?? "";@@ -1458,12 +1466,12 @@ const meetBox = (a, b) => (!a || !b ? a ?? null : [Math.max(a[0], b[0]), Math.maconst areaOf = (box) => (box ? Math.max(0, box[2] - box[0]) * Math.max(0, box[3] - box[1]) : 0);export function markupBox(svg) {- const tags = [...svg.matchAll(/<(\/?)([a-zA-Z]+)([^>]*?)(\/?)>/g)];+ const tags = [...svg.matchAll(/<(\/?)([a-zA-Z]+)((?:"[^"]*"|'[^']*'|[^'">])*?)(\/?)>/g)];const clips = new Map();let clipId = null;for (const [, close, tag, attrs, self] of tags) {if (tag === "clipPath" && !close) {- clipId = self ? null : (attrs.match(/id="([^"]*)"/) ?? [])[1] ?? null;+ clipId = self ? null : attributeOf(attrs, "id");continue;}if (clipId) {@@ -1471,7 +1479,7 @@ export function markupBox(svg) {clipId = null;continue;}- clips.set(clipId, unionBox(clips.get(clipId), boxOfPoints(pointsOfShape(tag, attrs), matrixOf((attrs.match(/transform="([^"]*)"/) ?? [])[1]))));+ clips.set(clipId, unionBox(clips.get(clipId), boxOfPoints(pointsOfShape(tag, attrs), matrixOf(attributeOf(attrs, "transform")))));}}const stack = [{ m: [1, 0, 0, 1, 0, 0], clip: null, skip: false }];@@ -1482,10 +1490,10 @@ export function markupBox(svg) {if (stack.length > 1) stack.pop();continue;}- const m = multiply(top.m, matrixOf((attrs.match(/transform="([^"]*)"/) ?? [])[1]));- const ref = (attrs.match(/clip-path="url\(#([^)]*)\)"/) ?? [])[1];+ const m = multiply(top.m, matrixOf(attributeOf(attrs, "transform")));+ const ref = ((attributeOf(attrs, "clip-path") ?? "").match(/url\(#([^)]*)\)/) ?? [])[1];const own = ref && clips.get(ref) ? boxOfPoints([[clips.get(ref)[0], clips.get(ref)[1]], [clips.get(ref)[2], clips.get(ref)[3]]], m) : null;- const frame = { m, clip: own ? meetBox(top.clip ?? own, own) : top.clip, skip: top.skip || ["defs", "clipPath", "mask", "linearGradient", "radialGradient", "pattern", "text", "title"].includes(tag) || /class="[^"]*iso-halo/.test(attrs) };+ const frame = { m, clip: own ? meetBox(top.clip ?? own, own) : top.clip, skip: top.skip || ["defs", "clipPath", "mask", "linearGradient", "radialGradient", "pattern", "text", "title"].includes(tag) || (attributeOf(attrs, "class") ?? "").includes("iso-halo") };if (!frame.skip) {const shape = boxOfPoints(pointsOfShape(tag, attrs), m);if (shape) box = unionBox(box, frame.clip ? meetBox(shape, frame.clip) : shape);diff --git a/scripts/audit-svg-attributes.test.mjs b/scripts/audit-svg-attributes.test.mjsnew file mode 100644index 0000000..168f3d9--- /dev/null+++ b/scripts/audit-svg-attributes.test.mjs@@ -0,0 +1,46 @@+import assert from "node:assert/strict";+import { execFile } from "node:child_process";+import { mkdtemp, rm, writeFile } from "node:fs/promises";+import { tmpdir } from "node:os";+import { join } from "node:path";+import { promisify } from "node:util";+import test from "node:test";+import * as A from "../kit/audit.mjs";+import { turning } from "../kit/turn-build.mjs";+const P={origin:[0,0],scale:1,azimuth:45,elevation:30};+const fixtures=[+ ["single quoted rectangle", "<rect x='10' y='20' width='30' height='40'/>"],+ ["spaces around attribute equals", '<rect x = "10" y = "20" width = "30" height = "40"/>'],+ ["single quoted nested transform", "<g transform = 'translate(5 3)'><path d='M10 10L20 10L20 20Z'/></g>"],+ ["numeric XML path references", '<path d="M10 20L40 20L40 60Z"/>'],+ ["numeric XML transform references", '<g transform="translate(10 5)"><circle cx="10" cy="10" r="5"/></g>'],+ ["mixed attribute quotes and multiline path", "<path d='M10 20\nL40 20L40 60Z' transform=\"translate(2 3)\"/>"],+ ["single quoted ellipse", "<ellipse cx='10' cy='20' rx='4' ry='2'/>"],+ ["quoted attribute text is not geometry", `<rect data-label="x='99'" x='10' y='20' width='30' height='40'/>`],+ ["quoted greater-than delimiter", `<rect data-label='a > b' x="10" y="20" width="30" height="40"/>`],+ ["quoted less-than delimiter", `<rect data-label="a < b" x='10' y='20' width='30' height='40'/>`],+ ["ordinary double quoted rectangle", '<rect x="10" y="20" width="30" height="40"/>'],+ ["ordinary transformed circle", '<g transform="translate(2 3)"><circle cx="10" cy="10" r="5"/></g>'],+];+let native;const directory=await mkdtemp(join(tmpdir(),"anatomy-svg-attributes-"));+try{+ const page=join(directory,"figure.html");await writeFile(page,`<!doctype html><div class="iso-plate"><svg width="100" height="100">${fixtures.map(([,m],i)=>`<g id="f${i}">${m}</g>`).join("")}</svg></div>`);+ const expression=`Array.from({length:${fixtures.length}},(_,i)=>{const b=document.querySelector('#f'+i).getBBox();return[b.x,b.y,b.x+b.width,b.y+b.height]})`;+ const result=await promisify(execFile)(process.execPath,["scripts/drive.mjs",page,JSON.stringify([["eval",expression]])],{timeout:30000});native=JSON.parse(result.stdout.match(/eval: (.*)/)[1]);+}finally{await rm(directory,{recursive:true,force:true});}+fixtures.forEach(([name,markup],i)=>test(`public SVG bounds and fixed parts decode ${name}`,()=>{+ assert.deepEqual(A.markupBox(markup),native[i]);+ const T=turning(P);T.layer("figure");const part=T.fixed({name:"fixed",svg:markup,anchor:[0,0,0],shapes:[A.ball([0,0,0],50)]});+ assert.deepEqual(part.spec.box,native[i].map((v,j)=>v+(j<2?-.5:.5)));+}));+test("coverage rejects a stand-in for single quoted real geometry",()=>{+ const item={name:"quoted",svg:fixtures[0][1],shapes:[A.ball([0,0,0],.1)]};assert.equal(A.coverage([item],P).length,1);+ assert.equal(A.coverage([{...item,shapes:[A.ball([0,0,0],50)]}],P).length,0);+});+test("quoted and escaped clip identifiers retain authored clipping",()=>{+ const markup="<defs><clipPath id = 'cut&1'><rect x='2' y='3' width='4' height='5'/></clipPath></defs><rect width='100' height='100' clip-path = 'url(#cut&1)'/>";+ assert.deepEqual(A.markupBox(markup),[2,3,6,8]);+});+test("quoted halo class remains excluded from authored bounds",()=>{+ assert.deepEqual(A.markupBox("<circle class = 'iso-halo' cx='50' cy='50' r='40'/><rect x='2' y='3' width='4' height='5'/>"),[2,3,6,8]);+});