// The svg/html preview (serve/web/preview.js), run in Node. The real browser behaviour (what a sandboxed frame can and
// cannot reach) is tested in tools/webtest/browser_test.mjs; here the documents and frames that are built are checked.
// node serve/test_web_preview.js (serve/test_web_preview.py runs it too and skips when node is missing)
"use strict";
const assert = require("node:assert/strict");
const path = require("node:path");
const P = require(path.join(__dirname, "web", "preview.js"));
const light = {dark: false, ink: "#221e1f"}, dark = {dark: true, ink: "#eef1f3"};
const cspOf = (doc) => /Content-Security-Policy" content="([^"]*)"/.exec(doc)[1];
const noImage = {DOMParser: undefined}; // no DOMParser: the inline svg path
const cases = {
can_preview() {
assert.ok(P.canPreview("svg", " ") && P.canPreview("SVG", " "));
assert.ok(P.canPreview("html", "
x
") && P.canPreview("htm", "") && P.canPreview("HTML", ""));
assert.ok(!P.canPreview("python", " ") && !P.canPreview("html", "plain words") && !P.canPreview("svg", "not svg
"));
assert.ok(!P.canPreview("", "x
") && !P.canPreview("svgx", ""), "the fence's language decides");
},
sandbox_flags_are_minimal() {
assert.equal(P.SANDBOX.static, "", "no flags: no scripts, forms, popups, downloads, modals or navigation");
assert.equal(P.SANDBOX.scripts, "allow-scripts", "scripts only");
for (const v of Object.values(P.SANDBOX)) assert.ok(!/same-origin|top-navigation|popups|forms|modals|downloads|storage|pointer-lock|presentation/.test(v));
},
csp_has_no_network() {
const off = P.csp(false), on = P.csp(true);
for (const c of [off, on]) {
assert.match(c, /^default-src 'none'; /, "everything is off unless listed");
assert.ok(!/connect-src|frame-src|font-src|media-src|object-src|https?:|\*/.test(c), "no network source: " + c);
assert.match(c, /img-src data:/); assert.match(c, /base-uri 'none'/); assert.match(c, /form-action 'none'/);
}
assert.ok(!off.includes("script-src"), "off: scripts fall back to default-src 'none'");
assert.match(on, /script-src 'unsafe-inline'/, "on: the block's own inline script, and no source");
assert.ok(!/script-src[^;]*(https?:|\*|'self'|data:)/.test(on));
},
html_documents() {
const frag = P.previewDoc("html", "hi
", false, light);
assert.ok(frag.startsWith(" hi") && frag.endsWith(""));
assert.match(frag, /color-scheme:light/);
const full = P.previewDoc("html", "t x ", false, dark);
assert.ok(full.indexOf("Content-Security-Policy") < full.indexOf(""), "our CSP is before the page's head");
assert.equal((full.match(/x", true, light)), P.csp(true));
assert.equal(P.previewDoc("html", "just words", false, light), null);
assert.equal(P.previewDoc("python", "x
", false, light), null);
},
a_page_cannot_redirect_itself() {
for (const meta of [' ', " ",
' ', ' ']) {
const doc = P.previewDoc("html", `${meta} x`, false, light);
assert.ok(!/refresh/i.test(doc.replace(/Content-Security-Policy[^>]*>/, "")), meta + " -> " + doc);
assert.ok(!/ ]*refresh/i.test(P.previewDoc("html", `x
${meta}`, true, light)), "also in a fragment and with scripts");
}
},
svg_documents() {
const doc = P.previewDoc("svg", ' ', false, light, noImage);
assert.ok(doc.includes("no", false, light), null);
// as an image: with a parser, the svg goes in as a data: URL, with the page's ink as currentColor
const env = {
DOMParser: class { parseFromString(s) {
const style = {textContent: ""}, root = {namespaceURI: "http://www.w3.org/2000/svg", localName: "svg", firstChild: null,
insertBefore(n) { this.style = n; }, hasAttribute: () => false};
return {documentElement: root, querySelector: () => null, createElementNS: () => style, src: s};
} },
XMLSerializer: class { serializeToString(d) { return "" + (d.documentElement.style ? d.documentElement.style.textContent : "") + " "; } },
};
const img = P.previewDoc("svg", ' ', false, {dark: false, ink: "#123456"}, env);
assert.match(img, /