feat(grid): render HTML in value conversion preview
This commit is contained in:
16 files changed
+50
-29
No files matched your search
@@ -1,3 +1,7 @@
|
||||
import { buildSafeHtmlPreview, SAFE_HTML_PREVIEW_CSP } from "@/lib/common/safeHtmlPreview";
|
||||
|
||||
export { buildSafeHtmlPreview };
|
||||
|
||||
/**
|
||||
* Safe HTML preview shell (V1).
|
||||
*
|
||||
@@ -30,40 +34,13 @@
|
||||
/** Max raw ```html fence body length (characters) that may enter the iframe `srcdoc`. */
|
||||
export const AI_HTML_MAX_CONTENT_CHARS = 512 * 1024;
|
||||
|
||||
export const AI_HTML_PREVIEW_CSP = "default-src 'none'; base-uri 'none'; form-action 'none'; img-src data: blob:; style-src 'unsafe-inline'; font-src data:";
|
||||
export const AI_HTML_PREVIEW_CSP = SAFE_HTML_PREVIEW_CSP;
|
||||
|
||||
/** True when the raw fence body may be previewed / saved. Oversize stays a plain code segment. */
|
||||
export function isAiHtmlPreviewEligible(content: string): boolean {
|
||||
return content.length > 0 && content.length <= AI_HTML_MAX_CONTENT_CHARS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches any `<meta>` carrying `http-equiv=refresh`: case-insensitive on the
|
||||
* tag name and the `http-equiv` key/value, tolerant of unquoted, single- or
|
||||
* double-quoted values in any attribute order with extra attributes present.
|
||||
*/
|
||||
const AI_HTML_META_REFRESH_PATTERN = /<meta\b[^>]*?\bhttp-equiv\s*=\s*(?:"\s*refresh\s*"|'\s*refresh\s*'|refresh\b)[^>]*>/gi;
|
||||
|
||||
/** Inert replacement for a defused refresh meta: a comment navigates nowhere. */
|
||||
const AI_HTML_META_REFRESH_REPLACEMENT = "<!-- meta refresh removed -->";
|
||||
|
||||
/** Strip every `http-equiv=refresh` meta; everything else passes through byte-identical. */
|
||||
function defuseMetaRefresh(content: string): string {
|
||||
return content.replace(AI_HTML_META_REFRESH_PATTERN, AI_HTML_META_REFRESH_REPLACEMENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap raw AI HTML in the safe standalone document. The same bytes back BOTH
|
||||
* the iframe preview and the "Save Safe HTML" payload, so what is previewed is
|
||||
* exactly what gets saved. If the AI content is itself a complete document,
|
||||
* the browser's error recovery nests it inside the body while the outer CSP
|
||||
* meta still applies document-wide. The only rewrite is the refresh meta
|
||||
* defused above — navigation sits outside the CSP fetch model.
|
||||
*/
|
||||
export function buildSafeHtmlPreview(content: string): string {
|
||||
return ["<!doctype html>", "<html>", "<head>", '<meta charset="utf-8">', `<meta http-equiv="Content-Security-Policy" content="${AI_HTML_PREVIEW_CSP}">`, "</head>", "<body>", defuseMetaRefresh(content), "</body>", "</html>"].join("\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* "Copy source" puts the raw AI HTML on the clipboard where it may later be
|
||||
* opened outside DBX. The first copy of a session requires an explicit risk
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
/** CSP used by read-only HTML previews rendered inside sandboxed iframes. */
|
||||
export const SAFE_HTML_PREVIEW_CSP = "default-src 'none'; base-uri 'none'; form-action 'none'; img-src data: blob:; style-src 'unsafe-inline'; font-src data:";
|
||||
|
||||
const META_REFRESH_PATTERN = /<meta\b[^>]*?\bhttp-equiv\s*=\s*(?:"\s*refresh\s*"|'\s*refresh\s*'|refresh\b)[^>]*>/gi;
|
||||
|
||||
/** Wrap untrusted HTML in a CSP-protected document and remove meta refresh navigation. */
|
||||
export function buildSafeHtmlPreview(content: string): string {
|
||||
const safeContent = content.replace(META_REFRESH_PATTERN, "<!-- meta refresh removed -->");
|
||||
return ["<!doctype html>", "<html>", "<head>", '<meta charset="utf-8">', `<meta http-equiv="Content-Security-Policy" content="${SAFE_HTML_PREVIEW_CSP}">`, "</head>", "<body>", safeContent, "</body>", "</html>"].join("\n");
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import { applyColumnFormatter, DataGridDateTimePatterns } from "@/lib/dataGrid/c
|
||||
export const CELL_TRANSFORM_MAX_INPUT = 50_000;
|
||||
export const CELL_TRANSFORM_MAX_OUTPUT = 200_000;
|
||||
export const CELL_TRANSFORM_MAX_RADIX_DIGITS = 4_096;
|
||||
export const CELL_TRANSFORM_KINDS = ["timestamp", "json", "jsonCompact", "xml", "base64Encode", "base64Decode", "urlEncode", "urlDecode", "radix"] as const;
|
||||
export const CELL_TRANSFORM_KINDS = ["timestamp", "json", "jsonCompact", "xml", "html", "base64Encode", "base64Decode", "urlEncode", "urlDecode", "radix"] as const;
|
||||
export type CellTransformKind = (typeof CELL_TRANSFORM_KINDS)[number];
|
||||
export type CellTransformError = "tooLarge" | "invalid" | "invalidTimestamp" | "ambiguousUnit" | "invalidInteger" | "invalidBase64";
|
||||
export interface CellTransformOptions {
|
||||
@@ -65,6 +65,9 @@ export function transformCellValue(source: string, options: CellTransformOptions
|
||||
case "xml":
|
||||
text = formatXmlSource(source);
|
||||
break;
|
||||
case "html":
|
||||
text = source;
|
||||
break;
|
||||
case "urlEncode":
|
||||
text = encodeURIComponent(source);
|
||||
break;
|
||||
|
||||
Reference in new issue
Block a user