diff --git a/docs/packages/cli.mdx b/docs/packages/cli.mdx index e11f16fca4..9c4c6cc9cc 100644 --- a/docs/packages/cli.mdx +++ b/docs/packages/cli.mdx @@ -428,7 +428,7 @@ way to tell them apart is to open the page yourself. | `size-floor` | Fetched, then judged too small to be a real asset rather than a spacer or tracking pixel. | | `budget-exhausted` | `--capture-budget` ran out before this one was reached. Raise it, or pass `--skip-vision` to buy time. | | `cap-reached` | A per-run or per-family limit was already met: 30 inline SVGs, 30 fonts, 6 faces per family. | -| `unavailable` | The request or the write failed: network error, timeout, refused address, bad status, disk. | +| `unavailable` | The request or the write failed: network error, timeout, refused address, bad status, disk. Also an inline SVG that is more than a plain image (it carries a script, an event handler, embedded HTML, a link, or a reference to another file): it stays in `extracted/page.html` but is not saved as a file under `assets/svgs/`. | Three of those are decisions the capture made and one is a failure it hit, so a run that is thin with an all-zero `dropped` is a thin page, and a run that is diff --git a/packages/cli/src/capture/assetDownloader.test.ts b/packages/cli/src/capture/assetDownloader.test.ts index 62e44dc90c..c60f0de4f8 100644 --- a/packages/cli/src/capture/assetDownloader.test.ts +++ b/packages/cli/src/capture/assetDownloader.test.ts @@ -469,6 +469,33 @@ describe("drop counts — why a referenced asset is not in the capture", () => { expect(drops["size-ceiling"]).toBe(1); }); }); + + it("writes only inline SVGs that are plain images as files", async () => { + await withTempDir(async (dir) => { + const plain = + ''; + const shape = ''; + const notPlain = [ + `${shape}`, + `${shape}`, + `${shape}`, + `${shape}`, + `${shape}`, + ]; + const { assets, drops } = await downloadAssets( + { + svgs: [plain, ...notPlain].map((outerHTML) => ({ outerHTML, isLogo: false })), + sections: [], + ogImage: "", + } as unknown as DesignTokens, + dir, + ); + expect(readdirSync(join(dir, "assets", "svgs"))).toHaveLength(1); + expect(assets).toHaveLength(1); + expect(readFileSync(join(dir, assets[0]!.localPath), "utf-8")).toBe(toStandaloneSvg(plain)); + expect(drops.unavailable).toBe(notPlain.length); + }); + }); }); describe("asset fetches present the same identity as the page navigation", () => { diff --git a/packages/cli/src/capture/assetDownloader.ts b/packages/cli/src/capture/assetDownloader.ts index fe5abd2dbc..533461f560 100644 --- a/packages/cli/src/capture/assetDownloader.ts +++ b/packages/cli/src/capture/assetDownloader.ts @@ -22,7 +22,7 @@ import { type DownloadByteBudget, } from "./readBoundedResponse.js"; import { captureFontExtension, captureFontFilename } from "./captureFontValidation.js"; -import { captureImageExtension } from "./captureImageValidation.js"; +import { captureImageExtension, passiveInlineSvg } from "./captureImageValidation.js"; import { loadSharp } from "../utils/optionalPackages.js"; interface DownloadBudgetOptions { @@ -352,6 +352,10 @@ export async function downloadAssets( drops["size-ceiling"]++; continue; } + if (!passiveInlineSvg(svgFile)) { + drops.unavailable++; + continue; + } const slug = svgContentHashSlug(svgFile, !!svg.isLogo); let finalSlug = slug; let suffix = 2; diff --git a/packages/cli/src/capture/captureImageValidation.ts b/packages/cli/src/capture/captureImageValidation.ts index d6d054e652..efb3645e98 100644 --- a/packages/cli/src/capture/captureImageValidation.ts +++ b/packages/cli/src/capture/captureImageValidation.ts @@ -75,6 +75,10 @@ function passiveSvg(bytes: Buffer, embedded: Set): boolean { }); } +export function passiveInlineSvg(markup: string): boolean { + return passiveSvg(Buffer.from(markup, "utf8"), new Set()); +} + function passiveSvgAttribute(name: string, value: string, embedded: Set): boolean { if (name.startsWith("on") || name === "xml:base") return false; if (["href", "xlink:href", "src"].includes(name)) return localImageReference(value, embedded);