This commit is contained in:
Timothy Jaeryang Baek committed 2026-11-11 16:55:02 +09:00
1 parent 262a8c6f71
commit eecba38c5d
3 files changed
+65 -2

No files matched your search

@@ -10,7 +10,11 @@
parseVisualizationArguments,
type VisualizationArguments
} from '$lib/utils/visualizations/arguments';
import { getVisualizationVendor, visualizationDocument } from '$lib/utils/visualizations/frame';
import {
getVisualizationVendor,
visualizationDocument,
repairVisualizationHtml
} from '$lib/utils/visualizations/frame';
export let call: OutputItem;
export let result: OutputItem | undefined = undefined;
@@ -130,7 +134,7 @@
if (generation !== current || submittedKey !== key) return;
channel.postMessage({
type: 'finalize',
html: args.html,
html: repairVisualizationHtml(args.html),
chartSource,
executeScripts: execute
});
+57
View File
@@ -1,3 +1,4 @@
import { javascriptLanguage } from '@codemirror/lang-javascript';
import { base } from '$app/paths';
import { injectCsp } from '$lib/utils/csp';
import { findSafeCut } from './safeCut';
@@ -38,3 +39,59 @@ export function getVisualizationVendor(name: 'purifier' | 'chartjs') {
}
return sources.get(file)!;
}
export function repairVisualizationScript(source: string) {
// Models sometimes put Markdown fences inside a script element.
let script = source.replace(/^\s*```(?:javascript|js)?[ \t]*\r?\n([\s\S]*?)\r?\n```\s*$/i, '$1');
let tree = javascriptLanguage.parser.parse(script);
let fixedCssValue = false;
tree.iterate({
enter(node) {
if (!node.type.isError || node.node.parent?.name !== 'Property' || script[node.from] !== ')')
return;
const value = tree.resolveInner(node.from, -1);
if (
value.name !== 'String' ||
!/^(['"])var\(--[\w-]+\1$/.test(script.slice(value.from, value.to))
)
return;
// color: 'var(--muted') -> color: 'var(--muted)'
script =
script.slice(0, node.from - 1) + ')' + script[node.from - 1] + script.slice(node.from + 1);
fixedCssValue = true;
}
});
if (fixedCssValue) tree = javascriptLanguage.parser.parse(script);
let firstError = Infinity;
const braces: number[] = [];
tree.iterate({
enter(node) {
if (node.type.isError) firstError = Math.min(firstError, node.from);
if (node.name === '}') braces.push(node.from);
}
});
if (firstError === Infinity) return script;
// Try one stray closing brace near the first error; never touch strings or comments.
for (const position of braces
.filter((position) => position <= firstError)
.slice(-8)
.reverse()) {
const candidate = script.slice(0, position) + script.slice(position + 1);
let valid = true;
javascriptLanguage.parser.parse(candidate).iterate({
enter(node) {
if (node.type.isError) valid = false;
}
});
if (valid) return candidate;
}
return script;
}
export function repairVisualizationHtml(html: string) {
return html.replace(
/(<script\b(?:[^>"']|"[^"]*"|'[^']*')*>)([\s\S]*?)(<\/script\s*>)/gi,
(_, opening, script, closing) => opening + repairVisualizationScript(script) + closing
);
}
+2
View File
@@ -138,6 +138,8 @@ export function visualizationRuntime(safeCut: (source: string) => number) {
).join('');
const fragment = win.DOMPurify.sanitize(head + doc.body.innerHTML, {
RETURN_DOM_FRAGMENT: true,
// This opaque frame runs authored scripts; keep IDs such as "reset" usable by them.
SANITIZE_DOM: false,
FORCE_BODY: true, // Retain leading styles and script placeholders in the fragment.
USE_PROFILES: { html: true, svg: true, svgFilters: true },
ADD_TAGS: ['style'],