Skip to content

Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.

feat(content): add HTML blocks to a draft and copy its HTML in one click (#1260)

Cihan3c9af37d4977
diff --git a/web/messages/en.json b/web/messages/en.json
index f86843bd9..284af63ad 100644
--- a/web/messages/en.json
+++ b/web/messages/en.json
@@ -3121,13 +3121,36 @@
},
"html": {
"title": "HTML view",
- "hint": "paste or edit HTML; it applies when you switch back. Scripts, iframes and inline styles are removed; schema JSON-LD is kept.",
+ "hint": "paste or edit HTML; it applies when you switch back. Scripts, iframes and inline styles are removed; schema JSON-LD and code between {marker} markers are kept as written.",
"back": "Back to visual",
"label": "The page's HTML",
"applied": "HTML applied · formatting cleaned",
"appliedUnsafe": "HTML applied · {count, plural, one {# unsafe tag} other {# unsafe tags}} removed"
},
"markReady": "Mark ready",
+ "copyHtml": "Copy HTML",
+ "copyHtmlHint": "Copy the page as HTML to publish, HTML blocks and schema included",
+ "copyHtmlDone": "HTML copied",
+ "copyHtmlFailed": "Couldn't copy. Open {html} and copy it from there.",
+ "htmlBlock": {
+ "label": "HTML block",
+ "lines": "{count, plural, one {# line} other {# lines}}",
+ "edit": "Edit",
+ "copy": "Copy HTML",
+ "copied": "Block HTML copied",
+ "copyFailed": "Couldn't copy. Open Edit and copy the code from there.",
+ "addTitle": "Add an HTML block",
+ "editTitle": "Edit the HTML block",
+ "description": "Code the page carries as written: an embed, a widget, a form. Ansvisor never runs it; Copy HTML gives it back unchanged.",
+ "codeLabel": "Code",
+ "placeholder": "For example {example}",
+ "count": "{count} / {max} characters",
+ "tooLong": "Over {max} characters. Split it into two blocks.",
+ "remove": "Remove the block",
+ "cancel": "Cancel",
+ "save": "Save",
+ "insert": "Add block"
+ },
"markedReady": "Marked ready. Publish it on your site when you're ready.",
"markReadyFailed": "Couldn't mark it ready. Try again.",
"markReadyUnsaved": "The page has changes that aren't saved, so it wasn't marked ready.",
@@ -3162,6 +3185,7 @@
"addRow": "Add a row below",
"addColumn": "Add a column to the right",
"deleteTable": "Delete the table",
+ "htmlBlock": "Add an HTML block",
"html": "Edit as HTML",
"htmlBack": "Back to visual",
"openLink": "Open the link in a new tab (or ⌘/Ctrl + click it in the page)",
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
index 60fd7172d..6fa162704 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_draft-editor.tsx
@@ -8,7 +8,7 @@ import StarterKit from '@tiptap/starter-kit';
import { TableKit } from '@tiptap/extension-table';
import Image from '@tiptap/extension-image';
import { CharacterCount, Placeholder } from '@tiptap/extensions';
-import { ArrowLeft, Check, Loader2 } from 'lucide-react';
+import { ArrowLeft, Check, Copy, Loader2 } from 'lucide-react';
import { Link } from '@/i18n/navigation';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
@@ -16,10 +16,16 @@ import { buttonVariants } from '@/components/ui/button-variants';
import { DraftMethodBadge, DraftStatusBadge } from '@/components/content/draft-badges';
import { markContentDraftReady } from '@/lib/actions/content-drafts';
import { CONTENT_LIBRARY_PATH, isPageType, type ContentDraft } from '@/lib/content/drafts';
-import { cleanHtml, formatHtmlSource } from '@/lib/content/clean-html';
+import {
+ cleanHtml,
+ formatHtmlSource,
+ htmlFromSource,
+ publishableHtml,
+} from '@/lib/content/clean-html';
import { draftTitleFromDoc, lengthTarget, safeLinkHref } from '@/lib/content/editor';
import { JsonLd } from '@/lib/content/json-ld-node';
import { cn } from '@/lib/utils';
+import { HtmlBlock, copyToClipboard } from './_html-block';
import { EditorToolbar } from './_toolbar';
import { useDraftAutosave, type SaveState } from './_use-autosave';
@@ -48,7 +54,9 @@ const DOCUMENT_CLASS = cn(
* A paste is cleaned on the way in (CE-005): headings, lists, links and
* tables stay; fonts, colors, styles and tracking parameters go; ⌘/Ctrl +
* Shift + V pastes plain text. The HTML view (CE-020) shows the page's
- * source, and cleans it the same way when it switches back.
+ * source, and cleans it the same way when it switches back. HTML blocks
+ * carry code as written, as many as the page needs, and Copy HTML puts the
+ * whole page, or one block, on the clipboard.
*/
export function DraftEditor({ draft }: { draft: ContentDraft }) {
const t = useTranslations('contentEditor.editor');
@@ -82,6 +90,7 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
JsonLd.configure({
label: (type) => (type ? t('jsonLd', { type }) : t('jsonLdUntyped')),
}),
+ HtmlBlock,
],
content: draft.bodyHtml,
// Rendered on the client only, so the server's HTML can't disagree with it.
@@ -90,7 +99,9 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
attributes: { class: DOCUMENT_CLASS, 'aria-label': t('documentLabel') },
transformPastedHTML: (html) => {
// A copy from this editor keeps its images, and needs no word about it.
- if (html.includes('data-pm-slice')) return cleanHtml(html, { keepImages: true }).html;
+ if (html.includes('data-pm-slice')) {
+ return cleanHtml(html, { keepImages: true, keepHtmlBlocks: true }).html;
+ }
// Images from elsewhere wait for attachments (CE-006): Docs' and
// Word's are temporary addresses or embedded data.
const cleaned = cleanHtml(html, { keepImages: false });
@@ -131,7 +142,10 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
setHtmlSource(null);
return;
}
- const cleaned = cleanHtml(htmlSource, { keepImages: true });
+ const cleaned = cleanHtml(htmlFromSource(htmlSource), {
+ keepImages: true,
+ keepHtmlBlocks: true,
+ });
// Emits an update, so the title and the autosave follow.
editor.commands.setContent(cleaned.html);
setHtmlSource(null);
@@ -150,6 +164,17 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
return () => window.removeEventListener('beforeunload', warn);
}, [htmlEdited, htmlSource]);
+ // The page as HTML to publish; in the HTML view, the source on screen.
+ const copyPageHtml = () => {
+ if (!editor) return;
+ const html = htmlSource !== null ? htmlFromSource(htmlSource) : editor.getHTML();
+ void copyToClipboard(
+ publishableHtml(html),
+ t('copyHtmlDone'),
+ t('copyHtmlFailed', { html: '</> HTML' }),
+ );
+ };
+
const canMarkReady = status === 'draft' || status === 'in_review';
const markReady = async () => {
setMarking(true);
@@ -207,6 +232,10 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
</span>
<div className="ml-auto flex shrink-0 items-center gap-3">
<SaveIndicator state={autosave.state} onRetry={() => void autosave.retry()} />
+ <Button variant="outline" size="sm" onClick={copyPageHtml} title={t('copyHtmlHint')}>
+ <Copy className="h-3.5 w-3.5" />
+ {t('copyHtml')}
+ </Button>
<Button
size="sm"
// A page that can't be saved can't be marked ready; the save state says why.
@@ -241,7 +270,7 @@ export function DraftEditor({ draft }: { draft: ContentDraft }) {
<div className="flex items-center gap-2 border-b bg-muted/40 px-4 py-1.5 text-xs text-muted-foreground">
<span>
<span className="font-medium text-foreground">{t('html.title')}</span> ·{' '}
- {t('html.hint')}
+ {t('html.hint', { marker: '<!-- html-block -->' })}
</span>
<Button size="sm" className="ml-auto h-7" onClick={applyHtmlView}>
{t('html.back')}
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_html-block.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_html-block.tsx
new file mode 100644
index 000000000..fc5b2cabc
--- /dev/null
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_html-block.tsx
@@ -0,0 +1,234 @@
+'use client';
+
+import { useRef, useState } from 'react';
+import { useTranslations } from 'next-intl';
+import { toast } from 'sonner';
+import { Node } from '@tiptap/core';
+import { NodeViewWrapper, ReactNodeViewRenderer, type NodeViewProps } from '@tiptap/react';
+import { Code2, Copy } from 'lucide-react';
+import { Button } from '@/components/ui/button';
+import {
+ Dialog,
+ DialogClose,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from '@/components/ui/dialog';
+import { formatNumber } from '@/lib/format';
+import {
+ MAX_HTML_BLOCK_LENGTH,
+ decodeHtmlBlock,
+ encodeHtmlBlock,
+} from '@/lib/content/html-allowlist';
+import { cn } from '@/lib/utils';
+
+/**
+ * HTML blocks (the user's request beside CE-020, as Webflow's custom code in
+ * rich text): code the page carries as written, a widget, an embed, a form,
+ * as many as the page needs. Ansvisor never runs it: the editor shows it as
+ * text, the draft stores it encoded (html-allowlist.ts), and Copy HTML gives
+ * it back unchanged.
+ */
+export const HtmlBlock = Node.create({
+ name: 'htmlBlock',
+ group: 'block',
+ atom: true,
+ selectable: true,
+ draggable: false,
+
+ addAttributes() {
+ return { code: { default: '', rendered: false } };
+ },
+
+ parseHTML() {
+ return [
+ {
+ tag: 'div[data-html-block]',
+ getAttrs: (el) => {
+ const code = decodeHtmlBlock(el.getAttribute('data-html-block') ?? '');
+ return code ? { code } : false;
+ },
+ },
+ ];
+ },
+
+ renderHTML({ node }) {
+ return ['div', { 'data-html-block': encodeHtmlBlock(node.attrs.code as string) }];
+ },
+
+ addNodeView() {
+ return ReactNodeViewRenderer(HtmlBlockView);
+ },
+});
+
+/**
+ * The browser's older copy command, for when the Clipboard API is refused
+ * (no permission, an embedded or insecure page): a hidden field holding the
+ * text, selected and copied.
+ */
+function copyWithSelection(text: string): boolean {
+ const field = document.createElement('textarea');
+ field.value = text;
+ field.setAttribute('readonly', '');
+ field.style.position = 'fixed';
+ field.style.opacity = '0';
+ document.body.appendChild(field);
+ field.select();
+ try {
+ return document.execCommand('copy');
+ } catch {
+ return false;
+ } finally {
+ field.remove();
+ }
+}
+
+/** Puts text on the clipboard and says so. */
+export async function copyToClipboard(text: string, copied: string, failed: string) {
+ try {
+ await navigator.clipboard.writeText(text);
+ toast.success(copied);
+ } catch {
+ if (copyWithSelection(text)) toast.success(copied);
+ else toast.error(failed);
+ }
+}
+
+const PREVIEW_LINES = 6;
+
+function HtmlBlockView({ node, updateAttributes, deleteNode, selected }: NodeViewProps) {
+ const t = useTranslations('contentEditor.editor.htmlBlock');
+ const [editing, setEditing] = useState(false);
+ const code = node.attrs.code as string;
+ const lines = code.split('\n');
+ const preview = lines.slice(0, PREVIEW_LINES).join('\n');
+
+ return (
+ <NodeViewWrapper className="not-prose my-4" data-html-block-view="">
+ <div
+ contentEditable={false}
+ className={cn(
+ 'overflow-hidden rounded-md border bg-muted/30',
+ selected && 'ring-2 ring-ring/50',
+ )}
+ >
+ <div className="flex items-center gap-2 border-b px-3 py-1.5 text-xs">
+ <Code2 className="h-3.5 w-3.5 text-muted-foreground" aria-hidden="true" />
+ <span className="font-medium">{t('label')}</span>
+ <span className="text-muted-foreground">{t('lines', { count: lines.length })}</span>
+ <div className="ml-auto flex items-center gap-1">
+ <Button variant="ghost" size="xs" onClick={() => setEditing(true)}>
+ {t('edit')}
+ </Button>
+ <Button
+ variant="ghost"
+ size="xs"
+ className="gap-1"
+ onClick={() => void copyToClipboard(code, t('copied'), t('copyFailed'))}
+ >
+ <Copy className="h-3 w-3" aria-hidden="true" />
+ {t('copy')}
+ </Button>
+ </div>
+ </div>
+ {/* Text, never markup: React escapes it. */}
+ <pre className="m-0 max-h-40 overflow-hidden whitespace-pre-wrap break-all bg-transparent px-3 py-2 font-mono text-[12px] leading-relaxed text-muted-foreground">
+ {preview}
+ {lines.length > PREVIEW_LINES && '\n…'}
+ </pre>
+ </div>
+ {editing && (
+ <HtmlBlockDialog
+ initial={code}
+ onSave={(next) => updateAttributes({ code: next })}
+ onRemove={deleteNode}
+ onClose={() => setEditing(false)}
+ />
+ )}
+ </NodeViewWrapper>
+ );
+}
+
+/**
+ * Write or change an HTML block's code: what the toolbar's HTML block opens
+ * (no `onRemove`), and Edit on a block.
+ */
+export function HtmlBlockDialog({
+ initial,
+ onSave,
+ onRemove,
+ onClose,
+}: {
+ initial: string;
+ onSave: (code: string) => void;
+ onRemove?: () => void;
+ onClose: () => void;
+}) {
+ const t = useTranslations('contentEditor.editor.htmlBlock');
+ const [code, setCode] = useState(initial);
+ const codeRef = useRef<HTMLTextAreaElement>(null);
+ const tooLong = code.length > MAX_HTML_BLOCK_LENGTH;
+ const empty = !code.trim();
+
+ const save = () => {
+ if (tooLong || empty) return;
+ onSave(code);
+ onClose();
+ };
+
+ return (
+ <Dialog open onOpenChange={(next) => !next && onClose()}>
+ {/* The code field takes the focus, not the dialog's close button. */}
+ <DialogContent className="sm:max-w-2xl" initialFocus={codeRef}>
+ <DialogHeader>
+ <DialogTitle>{onRemove ? t('editTitle') : t('addTitle')}</DialogTitle>
+ <DialogDescription>{t('description')}</DialogDescription>
+ </DialogHeader>
+ <textarea
+ ref={codeRef}
+ value={code}
+ spellCheck={false}
+ aria-label={t('codeLabel')}
+ placeholder={t('placeholder', {
+ example: '<iframe src="https://www.youtube.com/embed/…"></iframe>',
+ })}
+ onChange={(e) => setCode(e.target.value)}
+ className="h-72 w-full resize-y rounded-md border bg-muted/20 px-3 py-2 font-mono text-[12.5px] leading-relaxed outline-none focus-visible:ring-2 focus-visible:ring-ring/50"
+ />
+ <p
+ className={cn(
+ 'text-right text-xs tabular-nums text-muted-foreground',
+ tooLong && 'text-destructive',
+ )}
+ >
+ {tooLong
+ ? t('tooLong', { max: formatNumber(MAX_HTML_BLOCK_LENGTH) })
+ : t('count', {
+ count: formatNumber(code.length),
+ max: formatNumber(MAX_HTML_BLOCK_LENGTH),
+ })}
+ </p>
+ <DialogFooter>
+ {onRemove && (
+ <Button
+ variant="ghost"
+ className="mr-auto text-muted-foreground hover:text-destructive"
+ onClick={() => {
+ onRemove();
+ onClose();
+ }}
+ >
+ {t('remove')}
+ </Button>
+ )}
+ <DialogClose render={<Button variant="outline" />}>{t('cancel')}</DialogClose>
+ <Button disabled={tooLong || empty} onClick={save}>
+ {onRemove ? t('save') : t('insert')}
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ );
+}
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_toolbar.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_toolbar.tsx
index 35fe4a554..66368d77c 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_toolbar.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/_toolbar.tsx
@@ -1,10 +1,11 @@
'use client';
-import { useState } from 'react';
+import { useRef, useState } from 'react';
import { useTranslations } from 'next-intl';
import { useEditorState, type Editor } from '@tiptap/react';
import {
Bold,
+ Code2,
ExternalLink,
Columns3,
Heading1,
@@ -34,10 +35,11 @@ import { Label } from '@/components/ui/label';
import { formatNumber } from '@/lib/format';
import { safeImageSrc, safeLinkHref } from '@/lib/content/editor';
import { cn } from '@/lib/utils';
+import { HtmlBlockDialog } from './_html-block';
/**
* The editor's toolbar (CE-004, spec §6): H1 H2 H3 · B I link · lists,
- * quote · table, image · </> HTML, and the length counter against the page
+ * quote · table, image, HTML block · </> HTML, and the length counter against the page
* type's target. Inside a table it adds a row, a column, or removes the
* table. While the HTML view is open, only its own button works.
*/
@@ -53,7 +55,7 @@ export function EditorToolbar({
onToggleHtml: () => void;
}) {
const t = useTranslations('contentEditor.editor.toolbar');
- const [dialog, setDialog] = useState<'link' | 'image' | null>(null);
+ const [dialog, setDialog] = useState<'link' | 'image' | 'htmlBlock' | null>(null);
// Re-read only what the toolbar shows, not on every keystroke's render.
const state = useEditorState({
@@ -131,6 +133,7 @@ export function EditorToolbar({
run: () => chain().insertTable({ rows: 3, cols: 3, withHeaderRow: true }).run(),
},
{ key: 'image', icon: ImagePlus, on: false, run: () => setDialog('image') },
+ { key: 'htmlBlock', icon: Code2, on: false, run: () => setDialog('htmlBlock') },
],
];
const tableTools = [
@@ -195,6 +198,15 @@ export function EditorToolbar({
{/* Mounted while open, so each opening starts from the selection. */}
{dialog === 'link' && <LinkDialog editor={editor} onClose={() => setDialog(null)} />}
{dialog === 'image' && <ImageDialog editor={editor} onClose={() => setDialog(null)} />}
+ {dialog === 'htmlBlock' && (
+ <HtmlBlockDialog
+ initial=""
+ onSave={(code) =>
+ editor.chain().focus().insertContent({ type: 'htmlBlock', attrs: { code } }).run()
+ }
+ onClose={() => setDialog(null)}
+ />
+ )}
</div>
);
}
@@ -269,6 +281,7 @@ function LinkDialog({ editor, onClose }: { editor: Editor; onClose: () => void }
const [current] = useState(() => editor.getAttributes('link').href as string | undefined);
const [value, setValue] = useState(current ?? '');
const [invalid, setInvalid] = useState(false);
+ const fieldRef = useRef<HTMLInputElement>(null);
const apply = () => {
const href = safeLinkHref(value);
@@ -291,7 +304,7 @@ function LinkDialog({ editor, onClose }: { editor: Editor; onClose: () => void }
return (
<Dialog open onOpenChange={(next) => !next && onClose()}>
- <DialogContent className="sm:max-w-md">
+ <DialogContent className="sm:max-w-md" initialFocus={fieldRef}>
<DialogHeader>
<DialogTitle>{current ? t('editTitle') : t('title')}</DialogTitle>
</DialogHeader>
@@ -305,7 +318,7 @@ function LinkDialog({ editor, onClose }: { editor: Editor; onClose: () => void }
<Label htmlFor="content-link-href">{t('label')}</Label>
<Input
id="content-link-href"
- autoFocus
+ ref={fieldRef}
value={value}
placeholder={t('placeholder')}
aria-invalid={invalid}
@@ -343,6 +356,7 @@ function ImageDialog({ editor, onClose }: { editor: Editor; onClose: () => void
const [src, setSrc] = useState('');
const [alt, setAlt] = useState('');
const [invalid, setInvalid] = useState(false);
+ const fieldRef = useRef<HTMLInputElement>(null);
const insert = () => {
const safe = safeImageSrc(src);
@@ -360,7 +374,7 @@ function ImageDialog({ editor, onClose }: { editor: Editor; onClose: () => void
return (
<Dialog open onOpenChange={(next) => !next && onClose()}>
- <DialogContent className="sm:max-w-md">
+ <DialogContent className="sm:max-w-md" initialFocus={fieldRef}>
<DialogHeader>
<DialogTitle>{t('title')}</DialogTitle>
</DialogHeader>
@@ -375,7 +389,7 @@ function ImageDialog({ editor, onClose }: { editor: Editor; onClose: () => void
<Label htmlFor="content-image-src">{t('srcLabel')}</Label>
<Input
id="content-image-src"
- autoFocus
+ ref={fieldRef}
value={src}
placeholder="https://"
aria-invalid={invalid}
diff --git a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
index e7a79e937..020241690 100644
--- a/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
+++ b/web/src/app/[locale]/(dashboard)/dashboard/content/editor/[draftId]/page.test.tsx
@@ -39,6 +39,16 @@ vi.mock('sonner', () => ({ toast }));
}
).IS_REACT_ACT_ENVIRONMENT = true;
+// jsdom has no layout, and its ranges can't be measured. ProseMirror measures
+// one when it scrolls the cursor into view (after inserting a block), which
+// otherwise throws after the test, on a timer.
+if (!('getClientRects' in Range.prototype)) {
+ Object.assign(Range.prototype, {
+ getClientRects: () => [] as unknown as DOMRectList,
+ getBoundingClientRect: () => new DOMRect(),
+ });
+}
+
const draft = (over: Partial<ContentDraft> = {}): ContentDraft => ({
id: 'd1',
brandId: 'b1',
@@ -194,6 +204,48 @@ describe('the content editor', () => {
expect(container.querySelector('header')!.textContent).toContain('Saved');
});
+ it('adds HTML blocks, shows their code as text, and copies one or the whole page', async () => {
+ const writeText = vi.fn().mockResolvedValue(undefined);
+ Object.defineProperty(navigator, 'clipboard', { value: { writeText }, configurable: true });
+ getContentDraft.mockResolvedValue(draft({ bodyHtml: '<h1>Trail Runner X</h1>' }));
+ await renderPage();
+
+ const code =
+ '<iframe src="https://www.youtube.com/embed/x"></iframe>\n<script>track()</script>';
+ await act(async () => button('Add an HTML block')!.click());
+ const field = document.querySelector<HTMLTextAreaElement>('textarea[aria-label="Code"]')!;
+ const setValue = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value')!.set!;
+ await act(async () => {
+ setValue.call(field, code);
+ field.dispatchEvent(new Event('input', { bubbles: true }));
+ });
+ await act(async () => {
+ [...document.querySelectorAll('button')].find((b) => b.textContent === 'Add block')!.click();
+ });
+ await act(async () => {});
+
+ const block = container.querySelector('[data-html-block-view]')!;
+ expect(block.textContent).toContain('HTML block');
+ expect(block.textContent).toContain('2 lines');
+ // The code is text on the page, never markup: no iframe, no script runs.
+ expect(block.querySelector('pre')!.textContent).toBe(code);
+ expect(container.querySelector('.ProseMirror iframe, .ProseMirror script')).toBeNull();
+
+ const copyBlock = [...block.querySelectorAll<HTMLButtonElement>('button')].find(
+ (b) => b.textContent === 'Copy HTML',
+ )!;
+ await act(async () => copyBlock.click());
+ expect(writeText).toHaveBeenLastCalledWith(code);
+
+ const copyPage = [...container.querySelectorAll<HTMLButtonElement>('header button')].find(
+ (b) => b.textContent === 'Copy HTML',
+ )!;
+ await act(async () => copyPage.click());
+ // The block went in where the cursor was: the start, as nothing was clicked.
+ expect(writeText).toHaveBeenLastCalledWith(`${code}\n<h1>Trail Runner X</h1>`);
+ expect(toast.success).toHaveBeenLastCalledWith('HTML copied');
+ });
+
it('shows the link the cursor is in, to open in a new tab', async () => {
getContentDraft.mockResolvedValue(
draft({ bodyHtml: '<p><a href="https://gartner.com/x">Gartner</a> says so.</p>' }),
diff --git a/web/src/lib/content/clean-html.test.ts b/web/src/lib/content/clean-html.test.ts
index 67cbd8def..fa2ca4cb7 100644
--- a/web/src/lib/content/clean-html.test.ts
+++ b/web/src/lib/content/clean-html.test.ts
@@ -1,5 +1,6 @@
import { describe, expect, it } from 'vitest';
-import { cleanHtml, formatHtmlSource } from './clean-html';
+import { cleanHtml, formatHtmlSource, htmlFromSource, publishableHtml } from './clean-html';
+import { htmlBlockElement } from './html-allowlist';
// A section copied from Google Docs: the whole copy in a non-bold <b>, bold
// and italic as styles, styled links with tracking parameters, an H5, a
@@ -91,3 +92,32 @@ describe('formatHtmlSource', () => {
);
});
});
+
+describe('HTML blocks', () => {
+ const code = '<iframe src="https://www.youtube.com/embed/x"></iframe>\n<script>track()</script>';
+ const page = `<h1>A</h1>${htmlBlockElement(code)}<p>B</p>`;
+
+ it('come with the editor’s own HTML, never with a paste from elsewhere', () => {
+ expect(cleanHtml(page, { keepImages: true, keepHtmlBlocks: true }).html).toBe(page);
+ const pasted = cleanHtml(page, { keepImages: false });
+ expect(pasted.html).toBe('<h1>A</h1><p>B</p>');
+ expect(pasted.unsafeRemoved).toBe(0);
+ });
+
+ it('show as their code between markers in the HTML view, and go back as blocks', () => {
+ const source = formatHtmlSource(page);
+ expect(source).toBe(`<h1>A</h1>\n<!-- html-block -->\n${code}\n<!-- /html-block -->\n<p>B</p>`);
+ expect(htmlFromSource(source)).toBe(`<h1>A</h1>\n${htmlBlockElement(code)}\n<p>B</p>`);
+ // Code typed between new markers is a block; an empty pair is nothing.
+ expect(
+ htmlFromSource(
+ '<!-- html-block -->\n<b>x</b>\n<!-- /html-block --><!-- html-block --><!-- /html-block -->',
+ ),
+ ).toBe(htmlBlockElement('<b>x</b>'));
+ });
+
+ it('go out as their own code when the page is copied as HTML', () => {
+ const json = '<script type="application/ld+json">{"@type":"Product"}</script>';
+ expect(publishableHtml(`${page}${json}`)).toBe(`<h1>A</h1>\n${code}\n<p>B</p>\n${json}`);
+ });
+});
diff --git a/web/src/lib/content/clean-html.ts b/web/src/lib/content/clean-html.ts
index b5abc3b77..c042a90ff 100644
--- a/web/src/lib/content/clean-html.ts
+++ b/web/src/lib/content/clean-html.ts
@@ -3,11 +3,15 @@ import {
ALLOWED_ATTRIBUTES,
ALLOWED_TAGS,
DEMOTED_HEADINGS,
- JSON_LD_SLOT,
+ HTML_BLOCK_ELEMENT,
+ KEPT_SLOT,
+ MAX_HTML_BLOCK_LENGTH,
UNSAFE_TAGS,
cleanHref,
- extractJsonLd,
- restoreJsonLd,
+ decodeHtmlBlock,
+ extractKeptBlocks,
+ htmlBlockElement,
+ restoreKeptBlocks,
} from './html-allowlist';
/**
@@ -81,8 +85,16 @@ function demoteHeadings(root: HTMLElement) {
}
}
-export function cleanHtml(input: string, { keepImages }: { keepImages: boolean }): CleanedHtml {
- const { html, blocks } = extractJsonLd(input);
+/**
+ * `keepImages`: a paste from elsewhere leaves images out. `keepHtmlBlocks`:
+ * only the editor's own HTML (its copy, the HTML view) brings HTML blocks; a
+ * paste from a web page can't plant code in a draft.
+ */
+export function cleanHtml(
+ input: string,
+ { keepImages, keepHtmlBlocks = false }: { keepImages: boolean; keepHtmlBlocks?: boolean },
+): CleanedHtml {
+ const { html, kept } = extractKeptBlocks(input, { htmlBlocks: keepHtmlBlocks });
const doc = new DOMParser().parseFromString(html, 'text/html');
// Counted in the whole document: a script before any text parses into <head>.
const unsafeRemoved = doc.querySelectorAll(UNSAFE_TAGS.join(',')).length;
@@ -103,12 +115,12 @@ export function cleanHtml(input: string, { keepImages }: { keepImages: boolean }
imagesRemoved += 1;
node.remove();
}
- } else if (node.hasAttribute('data-i') && node.tagName.toLowerCase() !== JSON_LD_SLOT) {
+ } else if (node.hasAttribute('data-i') && node.tagName.toLowerCase() !== KEPT_SLOT) {
node.removeAttribute('data-i');
}
});
const clean = purify.sanitize(doc.body.innerHTML, {
- ALLOWED_TAGS: [...ALLOWED_TAGS, JSON_LD_SLOT],
+ ALLOWED_TAGS: [...ALLOWED_TAGS, KEPT_SLOT],
ALLOWED_ATTR,
ALLOW_DATA_ATTR: false,
// Links and image addresses are checked by the hook above, more strictly
@@ -116,7 +128,7 @@ export function cleanHtml(input: string, { keepImages }: { keepImages: boolean }
});
return {
- html: restoreJsonLd(clean, blocks).replace(/<p>(\s|&nbsp;|<br>)*<\/p>/g, ''),
+ html: restoreKeptBlocks(clean, kept).replace(/<p>(\s|&nbsp;|<br>)*<\/p>/g, ''),
unsafeRemoved,
imagesRemoved,
};
@@ -126,13 +138,53 @@ const BLOCKS = 'h[1-6]|p|ul|ol|li|blockquote|table|thead|tbody|tr|th|td|script';
const BEFORE_OPENING_BLOCK = new RegExp(`><(?=(?:${BLOCKS})\\b)`, 'g');
const AFTER_CLOSING_BLOCK = new RegExp(`(</(?:${BLOCKS})>)(?=<)`, 'g');
+const HTML_BLOCK_OPEN = '<!-- html-block -->';
+const HTML_BLOCK_CLOSE = '<!-- /html-block -->';
+const HTML_BLOCK_MARKED = /<!-- html-block -->\n?([\s\S]*?)\n?<!-- \/html-block -->/g;
+
+function lineByBlock(html: string): string {
+ return html.replace(AFTER_CLOSING_BLOCK, '$1\n').replace(BEFORE_OPENING_BLOCK, '>\n<');
+}
+
+/** Each HTML block's code in place of its element, as `expand` writes it. */
+function expandHtmlBlocks(html: string, expand: (code: string) => string): string {
+ return html.replace(HTML_BLOCK_ELEMENT, (element, encoded: string) => {
+ const code = decodeHtmlBlock(encoded);
+ return code === null ? element : expand(code);
+ });
+}
+
/**
* The HTML view's text: the page's HTML as a draft stores it (without the
* editor's own table widths and link targets), one block per line, so it
- * reads like source. Inline tags stay on their line.
+ * reads like source. Inline tags stay on their line. An HTML block shows as
+ * its code between <!-- html-block --> markers, as written.
*/
export function formatHtmlSource(html: string): string {
- return cleanHtml(html, { keepImages: true })
- .html.replace(AFTER_CLOSING_BLOCK, '$1\n')
- .replace(BEFORE_OPENING_BLOCK, '>\n<');
+ const clean = cleanHtml(html, { keepImages: true, keepHtmlBlocks: true }).html;
+ return expandHtmlBlocks(
+ lineByBlock(clean),
+ (code) => `${HTML_BLOCK_OPEN}\n${code}\n${HTML_BLOCK_CLOSE}`,
+ );
+}
+
+/**
+ * The HTML view's text back to the editor's HTML: the code between markers
+ * becomes HTML blocks again (one over MAX_HTML_BLOCK_LENGTH, or empty,
+ * goes), and the rest is left for cleanHtml.
+ */
+export function htmlFromSource(source: string): string {
+ return source.replace(HTML_BLOCK_MARKED, (_, code: string) =>
+ code.trim() && code.length <= MAX_HTML_BLOCK_LENGTH ? htmlBlockElement(code) : '',
+ );
+}
+
+/**
+ * The page as HTML to publish, what Copy HTML puts on the clipboard: cleaned
+ * as a draft stores it, one block per line, each HTML block as its own code
+ * and the schema JSON-LD as its script.
+ */
+export function publishableHtml(html: string): string {
+ const clean = cleanHtml(html, { keepImages: true, keepHtmlBlocks: true }).html;
+ return expandHtmlBlocks(lineByBlock(clean), (code) => code);
}
diff --git a/web/src/lib/content/html-allowlist.test.ts b/web/src/lib/content/html-allowlist.test.ts
index 650dd49b4..38449f739 100644
--- a/web/src/lib/content/html-allowlist.test.ts
+++ b/web/src/lib/content/html-allowlist.test.ts
@@ -1,10 +1,13 @@
import { describe, expect, it } from 'vitest';
import {
cleanHref,
- extractJsonLd,
+ decodeHtmlBlock,
+ encodeHtmlBlock,
+ extractKeptBlocks,
+ htmlBlockElement,
jsonLdType,
normalizeJsonLd,
- restoreJsonLd,
+ restoreKeptBlocks,
} from './html-allowlist';
describe('cleanHref', () => {
@@ -45,39 +48,53 @@ describe('normalizeJsonLd', () => {
});
});
-describe('extractJsonLd and restoreJsonLd', () => {
+describe('extractKeptBlocks and restoreKeptBlocks', () => {
it('keeps valid schema blocks through cleaning, as script elements of our own', () => {
- const { html, blocks } = extractJsonLd(
+ const { html, kept } = extractKeptBlocks(
'<p>a</p><script type="application/ld+json" onload="x()">{"@type":"Product"}</script>' +
"<script type='application/ld+json'>not json</script><script>alert(1)</script>",
+ { htmlBlocks: false },
);
- expect(blocks).toEqual(['{"@type":"Product"}']);
- expect(html).toBe('<p>a</p><jsonld-slot data-i="0"></jsonld-slot><script>alert(1)</script>');
- expect(restoreJsonLd('<p>a</p><jsonld-slot data-i="0"></jsonld-slot>', blocks)).toBe(
+ expect(kept).toEqual(['<script type="application/ld+json">{"@type":"Product"}</script>']);
+ expect(html).toBe('<p>a</p><kept-slot data-i="0"></kept-slot><script>alert(1)</script>');
+ expect(restoreKeptBlocks('<p>a</p><kept-slot data-i="0"></kept-slot>', kept)).toBe(
'<p>a</p><script type="application/ld+json">{"@type":"Product"}</script>',
);
});
+ it('keeps HTML blocks only where they’re allowed, and only ones that decode and fit', () => {
+ const block = htmlBlockElement('<iframe src="https://youtube.com/embed/x"></iframe>');
+ const tooLong = htmlBlockElement('x'.repeat(50_001));
+ const input = `<p>a</p>${block}<div data-html-block="!!"></div>${tooLong}`;
+ expect(extractKeptBlocks(input, { htmlBlocks: true })).toEqual({
+ html: '<p>a</p><kept-slot data-i="0"></kept-slot><div data-html-block="!!"></div>',
+ kept: [block],
+ });
+ expect(extractKeptBlocks(input, { htmlBlocks: false }).kept).toEqual([]);
+ });
+
it('reads tags in any case, and skips other tags and plain scripts', () => {
- const { html, blocks } = extractJsonLd(
+ const { html, kept } = extractKeptBlocks(
'<SCRIPT TYPE="Application/LD+JSON">{"@type":"Product"}</SCRIPT>' +
'<scripts type="application/ld+json">{}</scripts>' +
'<script>var s = "<script type=application/ld+json>{}";</script><p>b</p>',
+ { htmlBlocks: false },
);
- expect(blocks).toEqual(['{"@type":"Product"}']);
+ expect(kept).toEqual(['<script type="application/ld+json">{"@type":"Product"}</script>']);
expect(html).toBe(
- '<jsonld-slot data-i="0"></jsonld-slot><scripts type="application/ld+json">{}</scripts>' +
+ '<kept-slot data-i="0"></kept-slot><scripts type="application/ld+json">{}</scripts>' +
'<script>var s = "<script type=application/ld+json>{}";</script><p>b</p>',
);
});
it('finds a block after letters whose lower case is longer, as Turkish İ', () => {
- const { html, blocks } = extractJsonLd(
+ const { html, kept } = extractKeptBlocks(
'<h1>İstanbul İçin İyi Kahve</h1><script type="application/ld+json">{"@type":"Product"}</script><p>Sonra.</p>',
+ { htmlBlocks: false },
);
- expect(blocks).toEqual(['{"@type":"Product"}']);
+ expect(kept).toEqual(['<script type="application/ld+json">{"@type":"Product"}</script>']);
expect(html).toBe(
- '<h1>İstanbul İçin İyi Kahve</h1><jsonld-slot data-i="0"></jsonld-slot><p>Sonra.</p>',
+ '<h1>İstanbul İçin İyi Kahve</h1><kept-slot data-i="0"></kept-slot><p>Sonra.</p>',
);
});
@@ -87,14 +104,28 @@ describe('extractJsonLd and restoreJsonLd', () => {
for (const input of [unclosed, unended]) {
expect(input.length).toBeGreaterThan(450_000);
const started = performance.now();
- const { html, blocks } = extractJsonLd(input);
+ const { html, kept } = extractKeptBlocks(input, { htmlBlocks: true });
expect(performance.now() - started).toBeLessThan(500);
- expect(blocks).toEqual([]);
+ expect(kept).toEqual([]);
expect(html).toBe(input);
}
});
it('fills a slot nobody kept with nothing', () => {
- expect(restoreJsonLd('<jsonld-slot data-i="7"></jsonld-slot><p>a</p>', [])).toBe('<p>a</p>');
+ expect(restoreKeptBlocks('<kept-slot data-i="7"></kept-slot><p>a</p>', [])).toBe('<p>a</p>');
+ });
+});
+
+describe('HTML block code', () => {
+ it('round-trips any code, Unicode included, as base64 that holds no markup', () => {
+ const code = '<script>alert("ğüş")</script>\n<style>p{color:red}</style> &amp; "quotes"';
+ const encoded = encodeHtmlBlock(code);
+ expect(encoded).toMatch(/^[A-Za-z0-9+/]+=*$/);
+ expect(decodeHtmlBlock(encoded)).toBe(code);
+ });
+
+ it('refuses what isn’t base64, or isn’t UTF-8', () => {
+ expect(decodeHtmlBlock('<script>')).toBeNull();
+ expect(decodeHtmlBlock(btoa('\xff\xfe'))).toBeNull();
});
});
diff --git a/web/src/lib/content/html-allowlist.ts b/web/src/lib/content/html-allowlist.ts
index 77c4584bb..18daa10fe 100644
--- a/web/src/lib/content/html-allowlist.ts
+++ b/web/src/lib/content/html-allowlist.ts
@@ -150,35 +150,79 @@ export function jsonLdScripts(html: string): { start: number; end: number; text:
return found;
}
-/** Where a kept JSON-LD block waits while the rest of the HTML is cleaned. */
-export const JSON_LD_SLOT = 'jsonld-slot';
-const SLOT_PATTERN = new RegExp(`<${JSON_LD_SLOT} data-i="(\\d+)"></${JSON_LD_SLOT}>`, 'g');
+/** The most an HTML block holds, in characters of code. */
+export const MAX_HTML_BLOCK_LENGTH = 50_000;
/**
- * Takes the schema JSON-LD blocks out of the HTML before it is cleaned, as
- * slots, so the cleaner, which removes every script, keeps them. A block
- * that isn't valid JSON is dropped with its script.
+ * An HTML block's code as a draft stores it: UTF-8 in base64, in a data
+ * attribute of an empty element. Nothing in the code can act as markup there,
+ * and no cleaner has a reason to touch it.
*/
-export function extractJsonLd(html: string): { html: string; blocks: string[] } {
- const blocks: string[] = [];
- let out = '';
+export function encodeHtmlBlock(code: string): string {
+ let binary = '';
+ for (const byte of new TextEncoder().encode(code)) binary += String.fromCharCode(byte);
+ return btoa(binary);
+}
+
+/** An HTML block's code back from its stored form; null when it isn't one. */
+export function decodeHtmlBlock(encoded: string): string | null {
+ if (!/^[A-Za-z0-9+/]*={0,2}$/.test(encoded)) return null;
+ try {
+ const bytes = Uint8Array.from(atob(encoded), (c) => c.charCodeAt(0));
+ const code = new TextDecoder('utf-8', { fatal: true }).decode(bytes);
+ return code.length <= MAX_HTML_BLOCK_LENGTH ? code : null;
+ } catch {
+ return null;
+ }
+}
+
+/** An HTML block in a draft's HTML. */
+export function htmlBlockElement(code: string): string {
+ return `<div data-html-block="${encodeHtmlBlock(code)}"></div>`;
+}
+
+export const HTML_BLOCK_ELEMENT = /<div data-html-block="([A-Za-z0-9+/=]*)"\s*>\s*<\/div>/g;
+
+/** Where a kept block (JSON-LD, an HTML block) waits while the rest is cleaned. */
+export const KEPT_SLOT = 'kept-slot';
+const SLOT_PATTERN = new RegExp(`<${KEPT_SLOT} data-i="(\\d+)"></${KEPT_SLOT}>`, 'g');
+
+/**
+ * Takes out what the cleaners must not touch, as slots, before the rest of
+ * the HTML is cleaned:
+ *
+ * * schema JSON-LD, written again from its JSON (one that isn't valid
+ * JSON is dropped with its script);
+ * * HTML blocks, when `htmlBlocks` allows them (a paste from elsewhere
+ * doesn't), each checked to decode and to fit MAX_HTML_BLOCK_LENGTH.
+ *
+ * `kept` holds each one as it goes back: our own element, with only its data.
+ */
+export function extractKeptBlocks(
+ html: string,
+ { htmlBlocks }: { htmlBlocks: boolean },
+): { html: string; kept: string[] } {
+ const kept: string[] = [];
+ const slot = (element: string) => {
+ kept.push(element);
+ return `<${KEPT_SLOT} data-i="${kept.length - 1}"></${KEPT_SLOT}>`;
+ };
+ let withJsonLd = '';
let last = 0;
for (const script of jsonLdScripts(html)) {
- out += html.slice(last, script.start);
+ withJsonLd += html.slice(last, script.start);
const json = normalizeJsonLd(script.text.trim());
- if (json) {
- blocks.push(json);
- out += `<${JSON_LD_SLOT} data-i="${blocks.length - 1}"></${JSON_LD_SLOT}>`;
- }
+ if (json) withJsonLd += slot(`<script type="application/ld+json">${json}</script>`);
last = script.end;
}
- return { html: out + html.slice(last), blocks };
+ const out = (withJsonLd + html.slice(last)).replace(HTML_BLOCK_ELEMENT, (_, encoded: string) => {
+ const code = htmlBlocks ? decodeHtmlBlock(encoded) : null;
+ return code ? slot(htmlBlockElement(code)) : '';
+ });
+ return { html: out, kept };
}
-/** Puts the kept blocks back as script elements of our own, with only their JSON. */
-export function restoreJsonLd(html: string, blocks: string[]): string {
- return html.replace(SLOT_PATTERN, (_, i: string) => {
- const json = blocks[Number(i)];
- return json ? `<script type="application/ld+json">${json}</script>` : '';
- });
+/** Puts the kept blocks back where their slots are; a slot nobody kept goes. */
+export function restoreKeptBlocks(html: string, kept: string[]): string {
+ return html.replace(SLOT_PATTERN, (_, i: string) => kept[Number(i)] ?? '');
}
diff --git a/web/src/lib/content/sanitize-draft-html.test.ts b/web/src/lib/content/sanitize-draft-html.test.ts
index bd8520b98..d84856fdc 100644
--- a/web/src/lib/content/sanitize-draft-html.test.ts
+++ b/web/src/lib/content/sanitize-draft-html.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, it } from 'vitest';
+import { htmlBlockElement } from './html-allowlist';
import { sanitizeDraftHtml } from './sanitize-draft-html';
/**
@@ -47,6 +48,16 @@ describe('sanitizeDraftHtml', () => {
);
});
+ it('keeps HTML blocks as their encoded code, and drops ones that aren’t', () => {
+ const block = htmlBlockElement('<script src="https://widget.example/x.js"></script>');
+ expect(
+ sanitizeDraftHtml(
+ `<p>a</p>${block}<div data-html-block="&lt;script&gt;"></div>` +
+ `${htmlBlockElement('x'.repeat(50_001))}<div data-html-block="" onclick="x()"></div>`,
+ ),
+ ).toBe(`<p>a</p>${block}`);
+ });
+
it('keeps the schema and the text after it on a page with Turkish İ before it', () => {
const html =
'<h1>İzmir İçin İpuçları</h1><script type="application/ld+json">{"@type":"FAQPage"}</script><p>After the schema.</p>';
diff --git a/web/src/lib/content/sanitize-draft-html.ts b/web/src/lib/content/sanitize-draft-html.ts
index 0936c60a2..113f6d972 100644
--- a/web/src/lib/content/sanitize-draft-html.ts
+++ b/web/src/lib/content/sanitize-draft-html.ts
@@ -3,10 +3,10 @@ import {
ALLOWED_ATTRIBUTES,
ALLOWED_TAGS,
DEMOTED_HEADINGS,
- JSON_LD_SLOT,
+ KEPT_SLOT,
cleanHref,
- extractJsonLd,
- restoreJsonLd,
+ extractKeptBlocks,
+ restoreKeptBlocks,
} from './html-allowlist';
/**
@@ -15,18 +15,19 @@ import {
* editor sends clean HTML already; this keeps a save made without it to the
* same allow-list, so the save action never stores a script, an event
* handler or a javascript: link. Schema JSON-LD stays, written again from
- * its JSON.
+ * its JSON, and HTML blocks stay as their encoded code, checked to decode
+ * and fit.
*
* Stored HTML is still untrusted: a member can also write
* content_drafts.body_html straight through PostgREST (the update policy in
* 00204), past this check. Whatever reads it parses it through the editor's
- * schema or cleans it again; nothing renders or hands it on raw.
+ * schema or cleans it again before showing it or handing it on.
*/
export function sanitizeDraftHtml(html: string): string {
- const { html: withSlots, blocks } = extractJsonLd(html);
+ const { html: withSlots, kept } = extractKeptBlocks(html, { htmlBlocks: true });
const clean = sanitizeHtml(withSlots, {
- allowedTags: [...ALLOWED_TAGS, JSON_LD_SLOT],
- allowedAttributes: { ...ALLOWED_ATTRIBUTES, [JSON_LD_SLOT]: ['data-i'] },
+ allowedTags: [...ALLOWED_TAGS, KEPT_SLOT],
+ allowedAttributes: { ...ALLOWED_ATTRIBUTES, [KEPT_SLOT]: ['data-i'] },
allowedSchemes: ['http', 'https', 'mailto'],
allowedSchemesByTag: { img: ['http', 'https'] },
allowProtocolRelative: false,
@@ -41,5 +42,5 @@ export function sanitizeDraftHtml(html: string): string {
// An image whose address was refused is no image at all.
exclusiveFilter: (frame) => frame.tag === 'img' && !frame.attribs.src,
});
- return restoreJsonLd(clean, blocks);
+ return restoreKeptBlocks(clean, kept);
}