Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .context/TECHSTACK.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ data-doop-webfonts>` for the roster families the frame names and does not load i
does not answer within its 15s is unreadable, and the server renders), `inlineDocument` turns it
into one SVG sized to the frame, every image, stylesheet (both `@import` forms, cycles cut)
and font fetched and inlined as a data: URL (an SVG drawn as an image loads nothing itself;
scripts and `srcset` are dropped, a link's `media` kept; a nested document (iframe, object, embed) sends
scripts and `srcset` are dropped, as are comments and attribute names only HTML reads (`@click`, `:class`), which no XML parser takes back (#508), a link's `media` kept; a nested document (iframe, object, embed) sends
the frame to the server, since it would be a hole in the picture),
and the SVG is drawn onto a canvas at 2x from a data: URL (a blob: URL taints the canvas in
Chromium) and encoded. A resource the browser cannot fetch (an image host without CORS)
Expand Down
27 changes: 27 additions & 0 deletions src/lib/frameRaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,32 @@ function stripExecutable(doc: Document): void {
}
}

/* an XML name without a colon (XML 1.0 NCName) */
const NAME_START =
'A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D' +
'\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD\\u{10000}-\\u{EFFFF}'
const NAME_REST = `${NAME_START}\\-.0-9\\u00B7\\u0300-\\u036F\\u203F-\\u2040`
const XML_ATTR_NAME = new RegExp(`^(xml:)?[${NAME_START}][${NAME_REST}]*$`, 'u')

/* HTML the XML serializer writes out verbatim but no XML parser reads back,
which would make the SVG unreadable (and its raster fail): a comment
holding `--` or ending in `-`, and an attribute named the way only HTML
allows (`@click`, `:class`, `x-on:click`). Neither paints anything, the
bindings least of all once the scripts are gone, so both go. A template's
content is serialized too, though no walk of the document reaches it. */
function dropNonXml(root: Document | DocumentFragment): void {
const comments = (root.ownerDocument ?? (root as Document)).createTreeWalker(root, NodeFilter.SHOW_COMMENT)
const found: Node[] = []
while (comments.nextNode()) found.push(comments.currentNode)
for (const comment of found) comment.parentNode?.removeChild(comment)
for (const el of root.querySelectorAll('*')) {
for (const attr of Array.from(el.attributes)) {
if (attr.namespaceURI === null && !XML_ATTR_NAME.test(attr.name)) el.removeAttributeNode(attr)
}
if (el instanceof HTMLTemplateElement) dropNonXml(el.content)
}
}

/** One SVG, sized to the frame, holding the document with every external
* resource inlined. Exported for tests. */
export async function inlineDocument(html: string, opts: InlineOptions): Promise<string> {
Expand Down Expand Up @@ -249,6 +275,7 @@ async function inlineInto(
/* nothing runs inside one either: scripts, and the <base> that has done its job */
for (const el of doc.querySelectorAll('script, base, link:not([rel~="stylesheet"])')) el.remove()
stripExecutable(doc)
dropNonXml(doc)

const work: Promise<void>[] = []
for (const link of doc.querySelectorAll('link[rel~="stylesheet"][href]')) {
Expand Down
34 changes: 34 additions & 0 deletions tests/frameRasterBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,40 @@ describe.skipIf(!findBrowserPath())('rasterizing a frame in the browser', () =>
})
})

/* #508: HTML the XML serializer copies verbatim but no XML parser reads */
const HTML_ONLY_SYNTAX = `<html lang="en"><head><style>
* { margin: 0 }
.block { position: absolute; left: 0; top: 0; width: 100px; height: 100px; background: rgb(255, 0, 0) }
</style></head><body>
<!-- Hero -- CTA -->
<!-- ends in a dash --->
<div class="block" data-keep="yes" xml:lang="en" @click="open = true" :class="{ on: open }" x-on:click="go()"
v-bind:title="t" #slot="x"></div>
<template><!-- inside -- a template --><span @click="x"></span></template>
</body></html>`

it('writes an SVG any XML parser reads when the frame holds HTML-only comments and attribute names', async () => {
const text = await page.evaluate(
(h) => (window as unknown as { svgText: (o: unknown) => Promise<string> }).svgText({ html: h }),
HTML_ONLY_SYNTAX,
)
const parsed = await page.evaluate((t) => {
const doc = new DOMParser().parseFromString(t, 'image/svg+xml')
const block = doc.querySelector('.block')
return {
error: doc.querySelector('parsererror')?.textContent ?? null,
kept: block ? Array.from(block.attributes, (a) => a.name).sort() : null,
}
}, text)
expect(parsed.error).toBeNull()
expect(parsed.kept).toEqual(['class', 'data-keep', 'xml:lang'])
})

it('paints such a frame in the browser rather than giving up on it', async () => {
const out = await render({ html: HTML_ONLY_SYNTAX })
expect(out.block).toEqual([255, 0, 0, 255])
})

it('keeps only the clipped box, drawn from the whole document, for an element export', async () => {
const out = await render({ scale: 2, clip: { x: 200, y: 0, width: 100, height: 100 } })
expect([out.width, out.height]).toEqual([200, 200])
Expand Down
Loading