Repository navigation
fix: render file links in full HTML exports #3355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,168 @@ | ||
| import { afterEach, expect, it, vi } from "vite-plus/test"; | ||
| import { userEvent } from "vite-plus/test/browser"; | ||
|
|
||
| import { BlockNoteEditor } from "./BlockNoteEditor.js"; | ||
| import { BlockNoteSchema } from "../blocks/BlockNoteSchema.js"; | ||
| import { defaultBlockSpecs } from "../blocks/defaultBlocks.js"; | ||
| import { createFileBlockConfig } from "../blocks/File/block.js"; | ||
| import { createBlockSpec } from "../schema/index.js"; | ||
|
|
||
| const editors: BlockNoteEditor[] = []; | ||
|
|
||
| afterEach(() => { | ||
| for (const editor of editors.splice(0)) { | ||
| editor._tiptapEditor.destroy(); | ||
| } | ||
| }); | ||
|
|
||
| function parse(html: string) { | ||
| const root = document.createElement("div"); | ||
| root.innerHTML = html; | ||
| return root; | ||
| } | ||
|
|
||
| it.each([ | ||
| ["https://example.com/report.pdf?x=1&y=2", "report.pdf", "Attachment"], | ||
| ["../files/report.pdf", "", ""], | ||
| ])( | ||
| "exports a navigable file without losing its source: %s", | ||
| async (url, name, caption) => { | ||
| const editor = BlockNoteEditor.create(); | ||
| editors.push(editor); | ||
| editor.replaceBlocks(editor.document, [ | ||
| { | ||
| id: "parent", | ||
| type: "paragraph", | ||
| children: [ | ||
| { | ||
| id: "attachment", | ||
| type: "file", | ||
| props: { url, name, caption }, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
| const blocks = editor.document; | ||
| const html = editor.blocksToFullHTML(); | ||
| const root = parse(html); | ||
| const link = root.querySelector("a.bn-file-link"); | ||
| expect(link?.getAttribute("href")).toBe(url); | ||
| expect( | ||
| link?.closest('[data-content-type="file"]')?.getAttribute("data-url"), | ||
| ).toBe(url); | ||
| expect(root.querySelector(".bn-file-name")?.textContent).toBe(name || url); | ||
| expect(editor.tryParseHTMLToBlocks(html)).toEqual(blocks); | ||
| // Applications can replace navigation URLs without changing stored sources. | ||
| link!.setAttribute("href", "https://cdn.example.com/resolved.pdf"); | ||
| expect(editor.tryParseHTMLToBlocks(root.innerHTML)).toEqual(blocks); | ||
|
|
||
| const host = document.createElement("div"); | ||
| document.body.appendChild(host); | ||
| try { | ||
| editor.mount(host); | ||
| expect(host.querySelector('[data-content-type="file"]')).not.toBeNull(); | ||
| expect( | ||
| host | ||
| .querySelector('[data-content-type="file"] a') | ||
| ?.getAttribute("href"), | ||
| ).toBe(url); | ||
| const navigation = vi.fn((event: MouseEvent) => { | ||
| const prevented = event.defaultPrevented; | ||
| // Observe the renderer's policy without actually leaving the test page. | ||
| event.preventDefault(); | ||
| return prevented; | ||
| }); | ||
| host.addEventListener("click", navigation); | ||
| const liveLink = host.querySelector<HTMLAnchorElement>("a.bn-file-link")!; | ||
| await userEvent.click(liveLink); | ||
| expect(navigation).toHaveLastReturnedWith(true); | ||
| expect(editor.getTextCursorPosition().block.id).toBe("attachment"); | ||
|
|
||
| editor.isEditable = false; | ||
| await userEvent.click(liveLink); | ||
| expect(navigation).toHaveLastReturnedWith(false); | ||
| navigation.mockClear(); | ||
| liveLink.focus(); | ||
| await userEvent.keyboard("{Enter}"); | ||
| expect(navigation).toHaveReturnedWith(false); | ||
| } finally { | ||
| editor.unmount(); | ||
| host.remove(); | ||
| } | ||
| }, | ||
| ); | ||
|
|
||
| it.each([ | ||
| "", | ||
| "javascript:alert(1)", | ||
| "java\nscript:alert(1)", | ||
| "data:text/html,test", | ||
| "storage://original", | ||
| ])("preserves the stored source without filtering schemes: %s", (url) => { | ||
| const editor = BlockNoteEditor.create(); | ||
| editors.push(editor); | ||
| editor.replaceBlocks(editor.document, [ | ||
| { id: "file", type: "file", props: { url } }, | ||
| { id: "safe", type: "file", props: { url: "https://example.com/safe" } }, | ||
| ]); | ||
| const blocks = editor.document; | ||
| const html = editor.blocksToFullHTML(); | ||
| const root = parse(html); | ||
| expect(root.querySelector('[data-id="file"] a')?.getAttribute("href")).toBe( | ||
| url || undefined, | ||
| ); | ||
| expect(root.querySelector('[data-id="safe"] a')?.getAttribute("href")).toBe( | ||
| "https://example.com/safe", | ||
| ); | ||
| expect(editor.tryParseHTMLToBlocks(html)).toEqual(blocks); | ||
| }); | ||
|
|
||
| it("does not invoke the resolver during synchronous export", () => { | ||
| const resolveFileUrl = vi.fn(async () => "https://cdn.example.com/private"); | ||
| const editor = BlockNoteEditor.create({ resolveFileUrl }); | ||
| editors.push(editor); | ||
| const html = editor.blocksToFullHTML([ | ||
| { type: "file", props: { url: "https://example.com/original" } }, | ||
| ]); | ||
| expect(parse(html).querySelector("a")?.getAttribute("href")).toBe( | ||
| "https://example.com/original", | ||
| ); | ||
| expect(resolveFileUrl).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("leaves replacement renderers alone and links media filename fallbacks", () => { | ||
| const resolveFileUrl = vi.fn(async () => "https://cdn.example.com/resolved"); | ||
| const customFile = createBlockSpec(createFileBlockConfig, { | ||
| render() { | ||
| const dom = document.createElement("div"); | ||
| dom.className = "bn-file-name-with-icon"; | ||
| dom.textContent = "Custom file UI"; | ||
| return { dom }; | ||
| }, | ||
| }); | ||
| const editor = BlockNoteEditor.create({ | ||
| schema: BlockNoteSchema.create({ | ||
| blockSpecs: { ...defaultBlockSpecs, file: customFile() }, | ||
| }), | ||
| resolveFileUrl, | ||
| }); | ||
| editors.push(editor); | ||
| const html = editor.blocksToFullHTML([ | ||
| { type: "file", props: { url: "https://example.com/custom.pdf" } }, | ||
| { | ||
| type: "image", | ||
| props: { url: "https://example.com/image.png", showPreview: false }, | ||
| }, | ||
| ]); | ||
| const root = parse(html); | ||
| expect(root.querySelector('[data-content-type="file"] a')).toBeNull(); | ||
| expect(root.querySelector("a.bn-file-link")?.getAttribute("href")).toBe( | ||
| "https://example.com/image.png", | ||
| ); | ||
| expect(html).toContain("Custom file UI"); | ||
| // The image renderer still performs its existing asynchronous preview work; | ||
| // the attachment exporter must not resolve the custom file or re-resolve media. | ||
| expect(resolveFileUrl).toHaveBeenCalledExactlyOnceWith( | ||
| "https://example.com/image.png", | ||
| ); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import { | ||
| BlockNoteEditor, | ||
| BlockNoteSchema, | ||
| defaultBlockSpecs, | ||
| } from "@blocknote/core"; | ||
| import { createRoot } from "react-dom/client"; | ||
| import { flushSync } from "react-dom"; | ||
| import { expect, it, vi } from "vite-plus/test"; | ||
| import { userEvent } from "vite-plus/test/browser"; | ||
|
|
||
| import { BlockNoteViewRaw } from "../../editor/BlockNoteView.js"; | ||
| import { ReactFileBlock } from "./block.js"; | ||
|
|
||
| it("renders React file links in the editor and exported HTML", async () => { | ||
| const editor = BlockNoteEditor.create({ | ||
| schema: BlockNoteSchema.create({ | ||
| blockSpecs: { ...defaultBlockSpecs, file: ReactFileBlock() }, | ||
| }), | ||
| initialContent: [ | ||
| { | ||
| id: "file", | ||
| type: "file", | ||
| props: { url: "https://example.com/report.pdf", caption: "Caption" }, | ||
| }, | ||
| ], | ||
| }); | ||
| const host = document.createElement("div"); | ||
| document.body.append(host); | ||
| const root = createRoot(host); | ||
| try { | ||
| flushSync(() => root.render(<BlockNoteViewRaw editor={editor} />)); | ||
| const blocks = editor.document; | ||
| const html = editor.blocksToFullHTML(); | ||
| const exported = document.createElement("div"); | ||
| exported.innerHTML = html; | ||
| expect(exported.querySelector("a.bn-file-link")?.getAttribute("href")).toBe( | ||
| "https://example.com/report.pdf", | ||
| ); | ||
| expect(exported.querySelector("figcaption")?.textContent).toBe("Caption"); | ||
| expect(editor.tryParseHTMLToBlocks(html)).toEqual(blocks); | ||
| expect( | ||
| host.querySelector('[data-content-type="file"] a')?.getAttribute("href"), | ||
| ).toBe("https://example.com/report.pdf"); | ||
| const navigation = vi.fn((event: MouseEvent) => { | ||
| const prevented = event.defaultPrevented; | ||
| event.preventDefault(); | ||
| return prevented; | ||
| }); | ||
| host.addEventListener("click", navigation); | ||
| const liveLink = host.querySelector<HTMLAnchorElement>("a.bn-file-link")!; | ||
| await userEvent.click(liveLink); | ||
| expect(navigation).toHaveLastReturnedWith(true); | ||
| expect(editor.getTextCursorPosition().block.id).toBe("file"); | ||
|
|
||
| flushSync(() => { | ||
| editor.isEditable = false; | ||
| }); | ||
| await userEvent.click(liveLink); | ||
| expect(navigation).toHaveLastReturnedWith(false); | ||
| navigation.mockClear(); | ||
| liveLink.focus(); | ||
| await userEvent.keyboard("{Enter}"); | ||
| expect(navigation).toHaveReturnedWith(false); | ||
| } finally { | ||
| root.unmount(); | ||
| editor._tiptapEditor.destroy(); | ||
| host.remove(); | ||
| } | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,25 @@ export const FileBlockWrapper = ( | |
| <> | ||
| {props.block.props.showPreview === false || !props.children ? ( | ||
| // Show file name and icon. | ||
| <FileNameWithIcon {...props} /> | ||
| <a | ||
| className="bn-file-link" | ||
| href={props.block.props.url} | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sanitize the file link The new anchor writes The package already sanitizes file URLs before navigation. Pass the base URL that
|
||
| onClick={(event) => { | ||
| if (props.editor.isEditable) { | ||
| event.preventDefault(); | ||
| } | ||
| }} | ||
| > | ||
| <FileNameWithIcon | ||
| block={{ | ||
| ...props.block, | ||
| props: { | ||
| ...props.block.props, | ||
| name: props.block.props.name || props.block.props.url, | ||
| }, | ||
| }} | ||
| /> | ||
| </a> | ||
| ) : ( | ||
| // Show preview. | ||
| props.children | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Validate the navigation URL before setting
href.A file with
url: "javascript:alert(document.domain)"now creates an executable link. Read-only clicks are not cancelled. Exported HTML also retains the link without the click handler. If CSP permits execution, activation runs script in the containing document. (html.spec.whatwg.org)Keep the original URL in
data-urlfor roundtripping. Validate the navigation scheme separately, including control-character variants, and omit unsafe links. Update the scheme-preservation test so it does not require executablehrefvalues.Based on learnings, “validate the URL scheme against an allowlist … before emitting it.”
Source: Learnings