Skip to content

fix(core): keep parsed HTML out of the active document - #3393

Draft
YousefED wants to merge 7 commits into
mainfrom
fix/html-parse-detached-document
Draft

YousefED wants to merge 7 commits into
mainfrom
fix/html-parse-detached-document

Conversation

@YousefED

@YousefED YousefED commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

The HTML parser of @blocknote/core reads HTML in a document without a browsing context. There, an element cannot load a resource or run an event handler. In four places, the code put content of that HTML into the active document of the page, where both can occur. Thus HTML from a paste, or given to tryParseHTMLToBlocks, pasteHTML or pasteMarkdown, could run script in the page of the editor.

Each fix is one commit. The element that gets the content is now in a document without a browsing context.

  • The content of a list item (getListItemContent).
  • The children of a details element (getDetailsContent): toggle list items and toggle headings.
  • Content after a nested list in a list item (nestedListsToBlockNoteStructure). This place only started an image request, in WebKit and Firefox.
  • The HTML that the markdown export reads (htmlToMarkdown). This place started a second request for each video and audio block on a copy, a block drag and a markdown export.

The last two commits make the pattern one rule, and the result of a parse stays the same. All DOM work on content from outside is in detachedDoc(), one shared document without a browsing context (an internal helper, with the rule in its JSDoc): the code parses a string there, makes more elements there, and copies a node that it gets with detachedDoc().importNode. Thus a parse helper does not depend on the document of the node that its caller gives it.

Testing

  • Each fix has a test that fails without the fix. The new tests are browser tests with a spy: the parse must make no element in the active document. They do not wait for a request or for an event handler.
  • Browser tests of @blocknote/core in Chromium, Firefox and WebKit: 2,228 passed (main: 2,213). The browser integration tests (2,343) and the Node tests pass as on main.
  • Each effect was measured in Chromium, Firefox and WebKit, with the DOM steps of the code in a blank page.

Notes

  • A relative image URL in a list item or a details element stays as the HTML has it. Before, it became an absolute URL with the URL of the page. Six parse snapshots change for this reason.
  • The markdown output does not change.
  • Lint: one local Oxlint rule (blocknote-dom/no-active-document, in scripts/lint/detached-doc.mjs) enforces the rule of util/detachedDoc.ts for the source of @blocknote/core. It has three checks: no cloneNode and no createHTMLDocument in each file; no global document and no ownerDocument in the files that only handle content from outside (parsers, paste handlers, markdown export, parse helpers); and the same in each parse function, which is a function with a name that starts with parse or contains Parse. Seven correct uses have an exception with its reason. vite.config.ts only turns the rule on.

Summary by CodeRabbit

  • Bug Fixes
    • HTML import and conversion now process external content in an isolated document rather than the active page, preventing parsed elements from being temporarily added to the page.
    • Preserved content handling for nested lists, images, videos, and expandable sections, including content following nested lists.
    • Improved consistency when importing content that includes images inside list items or expandable sections.

@vercel

vercel Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
blocknote Ready Ready Preview Nov 24, 2026 12:38pm UTC
blocknote-website Ready Ready Preview Nov 24, 2026 12:38pm UTC

Request Review

@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The change adds a cached detached HTML document and uses it in clipboard normalization, Markdown conversion, nested-list processing, list and details content extraction, and paragraph merging. Tests check conversion and parsing results. Lint rules restrict selected DOM operations in core code.

Changes: Detached HTML processing

Layer / File(s) Summary
Shared detached document and node import
packages/core/src/util/detachedDoc.ts, packages/core/src/schema/blocks/createSpec.ts, packages/core/src/util/detachedDoc.test.ts
A cached detached HTML document is added. Paragraph merging imports nodes through it. A test checks parsing results and resulting block-content node types.
Clipboard and Markdown conversion
packages/core/src/api/clipboard/fromClipboard/normalizeOfficeHTML.ts, packages/core/src/api/exporters/markdown/htmlToMarkdown.ts, packages/core/src/api/exporters/markdown/htmlToMarkdown.test.ts
Clipboard normalization and HTML-to-Markdown conversion create parsing elements through the detached document. A test checks Markdown output and active-document element creation.
Nested-list transformation
packages/core/src/api/parsers/html/util/nestedLists.ts, packages/core/src/api/parsers/html/util/nestedLists.test.ts
nestedListsToBlockNoteStructure accepts an HTML string and transforms a detached element. A test checks that content after a nested list appears in a separate list item.
List and details content extraction
packages/core/src/blocks/ListItem/getListItemContent.ts, packages/core/src/blocks/ListItem/getListItemContent.test.ts, packages/core/src/blocks/getDetailsContent.ts, packages/core/src/blocks/getDetailsContent.test.ts
List-item and details content extraction import nodes into the detached document. Tests check parsed image output and active-document element creation.
DOM operation lint rules
vite.config.ts, packages/core/src/api/exporters/html/externalHTMLExporter.ts, packages/core/src/api/parsers/html/util/normalizeWhitespace.ts, packages/core/src/extensions/SideMenu/dragging.ts
Core lint rules restrict selected DOM operations. Suppressions mark existing fragment cloning, tree walkers, and drag-preview cloning.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Suggested reviewers: nperez0111, matthewlipski

Merge Risk: Minimal · up to 2871c · assessed changes only

Path filters excluded changed files. This rating applies only to the assessed changes.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "fix(core): keep parsed HTML out of the active document" matches the main change. The PR moves HTML parsing in @blocknote/core to a detached document (detachedDoc()) so parsed content cannot…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description follows the template's main sections: Summary, Rationale-like reasoning, Changes, Testing, and Notes. It explains the security problem (HTML parsed in the active document can run scrip…
Full details: Title check

Explanation

The title "fix(core): keep parsed HTML out of the active document" matches the main change. The PR moves HTML parsing in @blocknote/core to a detached document (detachedDoc()) so parsed content cannot run scripts or load resources in the page. The title is specific, uses a conventional commit prefix, and names the core package.

Full details: Description check

Explanation

The description follows the template's main sections: Summary, Rationale-like reasoning, Changes, Testing, and Notes. It explains the security problem (HTML parsed in the active document can run script or load resources), lists the four affected code paths, and describes the new detachedDoc() rule and the lint enforcement. The Testing section gives test results across Chromium, Firefox, and WebKit. Several template sections are missing or only implicit: there is no explicit Rationale heading, no Impact heading (the Notes cover the snapshot changes for relative image URLs and state that markdown output is unchanged), no Screenshots/Video section (not applicable for this change), no Checklist, and no Additional Notes heading. The description does not state whether documentation was updated. Still, the core content is present and the omissions are mostly non-critical.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit imports leaves with care
Into a quiet document there
Lists and pictures find their place
Markdown keeps its measured pace
The parser hops along its way
And saves a carrot for the day 🌿

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 33505936-b475-4eb1-8791-77993e10e13a
Commits

Reviewing files that changed from the base of the PR and between acc4b68 and 2871c70.

Files selected for processing: 9

  • packages/core/src/api/exporters/html/externalHTMLExporter.ts
  • packages/core/src/api/exporters/markdown/htmlToMarkdown.test.ts
  • packages/core/src/api/parsers/html/util/nestedLists.test.ts
  • packages/core/src/api/parsers/html/util/normalizeWhitespace.ts
  • packages/core/src/blocks/ListItem/getListItemContent.test.ts
  • packages/core/src/blocks/getDetailsContent.test.ts
  • packages/core/src/extensions/SideMenu/dragging.ts
  • packages/core/src/util/detachedDoc.ts
  • vite.config.ts
Files skipped from review as they are similar to previous changes: 1

  • packages/core/src/util/detachedDoc.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review.

@pkg-pr-new

pkg-pr-new Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@blocknote/ariakit

npm i https://pkg.pr.new/@blocknote/ariakit@3393

@blocknote/code-block

npm i https://pkg.pr.new/@blocknote/code-block@3393

@blocknote/core

npm i https://pkg.pr.new/@blocknote/core@3393

@blocknote/diagram-block

npm i https://pkg.pr.new/@blocknote/diagram-block@3393

@blocknote/mantine

npm i https://pkg.pr.new/@blocknote/mantine@3393

@blocknote/math-block

npm i https://pkg.pr.new/@blocknote/math-block@3393

@blocknote/react

npm i https://pkg.pr.new/@blocknote/react@3393

@blocknote/server-util

npm i https://pkg.pr.new/@blocknote/server-util@3393

@blocknote/shadcn

npm i https://pkg.pr.new/@blocknote/shadcn@3393

@blocknote/xl-ai

npm i https://pkg.pr.new/@blocknote/xl-ai@3393

@blocknote/xl-docx-exporter

npm i https://pkg.pr.new/@blocknote/xl-docx-exporter@3393

@blocknote/xl-email-exporter

npm i https://pkg.pr.new/@blocknote/xl-email-exporter@3393

@blocknote/xl-multi-column

npm i https://pkg.pr.new/@blocknote/xl-multi-column@3393

@blocknote/xl-odt-exporter

npm i https://pkg.pr.new/@blocknote/xl-odt-exporter@3393

@blocknote/xl-pdf-exporter

npm i https://pkg.pr.new/@blocknote/xl-pdf-exporter@3393

@blocknote/xl-typst-exporter

npm i https://pkg.pr.new/@blocknote/xl-typst-exporter@3393

commit: ac056a2

@github-actions

github-actions Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
PR Preview Action v1.8.2

QR code for preview link

🚀 View preview at
https://TypeCellOS.github.io/BlockNote/pr-preview/pr-3393/

Built to branch gh-pages at 2026-11-24 12:46 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

@nperez0111 nperez0111 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like enforcing all the callsites, let's go one step further into a lint rule


// Cloned so merging doesn't modify the element being parsed.
const clone = (node as HTMLElement).cloneNode(true) as HTMLElement;
// Not `cloneNode`: see the rule in `util/detachedDoc.ts`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add a lint rule for this?

* HTML or markdown string, the HTML of an export). It has no browsing context:
* content in it cannot load a resource or run an event handler.
*
* Do all DOM work on such content in this document, never in the global

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's do a lint rule for this stuff

// | Where | Not allowed |
// | ---------------------------------------- | ----------------------------------- |
// | 1. each file | `.cloneNode`, `.createHTMLDocument` |
// | 2. a file that only handles such content | global `document`, `.ownerDocument` |

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nperez0111 this is a bit tricky. We can have a lint rule that prvents rule 1 on every file. But we can't block all access to document or ownerDocument, because in some cases (e.g.: render() or toExternalHTML this is valid). We only want to prevent it on code that parses outside content.

Rules 2 and 3 try to prevent that (by blocking document for specific files and for any file inside a parse* function; but maybe we should drop rule 2 and 3 altogether as we already know it can't be 100% accurate (it would miss other filenames or function names). wdyt?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we've been bitten by the whole accessing document thing a couple of times (relative to shadow-dom & iframe usage), so I actually would not be opposed to a blanket rule against touching document & then suppressing the rule on the line for the things we know that need them. This would sort of encourage us to be more intentional about when to use document & give a little friction which would be good to second guess it's usage if we can do it another way.

So maybe just apply the rule everywhere & suppress where needed

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

check. even if it's like 30 places?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

render & toExternalHTML & parse can be rewritten (which I think is actually more valid). For example:

render (block, editor) {
 document.createElement('h1')
 // vs.
 editor.domElement.ownerDocument.createElement('h1')
}
// we should make that less verbose

parse (el) {
 el.querySelector()
 el.ownerDocument.createElement()
}

I think it is more accurate since it will always be in the correct "realm" if done like this since they will be relative to their inputs rather than having to pull in globals. But maybe this is too pedantic? I'll leave it to you. I would prefer if we could remove certain classes of errors (since it can be hard to know when it is important for shadow-dom or not, and we don't really have that many tests for shadow-dom).

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure your exact example holds up (for example editor.domElement doesn't exist in headless editors), so it would require more investigation. I agree it would be great to better standardize how we access document safely. Atm though I think it'd be at least a day of work to come up with a good pattern and fix 135 spots (my 30 places earlier was too low).

I'd propose to get this PR in as it fixes some current potential security issues. I would drop lint rule 2 and 3 for now and create an engineering task to investigate all uses of document

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alright sure, fine with deferring

Content from outside the editor must not get into the active document.
The code had two forms for this: `detachedDoc()` in `nestedLists.ts`,
and a new `createHTMLDocument` for each call in `normalizeOfficeHTML.ts`.

`detachedDoc()` moved to `util/detachedDoc.ts`, and the two files use
it. `normalizeOfficeHTML.ts` makes all its elements with it: the tree
that it changes is in that document.

The JSDoc of `detachedDoc` states the rule for content from outside. No
behavior changes: the documents are of the same kind, and a caller only
makes elements with the shared document.
The HTML parser reads HTML in a document without a browsing context, so
that an element of the HTML cannot load a resource or run an event
handler. `getListItemContent` made its work `div` with the active
`document` and put clones of the children of the `li` element into it.
Thus these clones were in the active document.

The work `div` is now an element of `detachedDoc()`, and each copy is
made with `detachedDoc().importNode`. A clone is in the document of its
source, and the caller controls that document.

One result of this: the URL of an image in a list item is the URL as the
HTML has it. It was the URL after a resolve with the URL of the page.
Five parse snapshots had that form.
… document

The HTML parser reads HTML in a document without a browsing context, so
that an element of the HTML cannot load a resource or run an event
handler. `getDetailsContent` made its work `div` with the active
`document` and put clones of the children of the `details` element into
it. Thus these clones were in the active document. This is the same
fault as in `getListItemContent`, for a toggle list item and for a
toggle heading.

The work `div` is now an element of `detachedDoc()`, and each copy
(also the copy of the `summary` element) is made with
`detachedDoc().importNode`.

One result of this: the URL of an image in a `details` element is the
URL as the HTML has it. It was the URL after a resolve with the URL of
the page. One parse snapshot had that form.
…ment

Before the parse, `nestedListsToBlockNoteStructure` changes the list
structure of the HTML. The HTML is in a document without a browsing
context, so that an element of the HTML cannot load a resource or run an
event handler. For content that comes after a nested list in a list
item, the function made an `li` with the active `document`, put the
content into it, and then put the `li` into the HTML. Thus the content
was in the active document for a moment. An image in that content
started its request at that time: an `img` element in WebKit, an SVG
`image` element and an image `input` element in Firefox. No event
handler ran.

The `li` is now an element of `detachedDoc()`.

The two `div` elements of `createGroups` did not have this effect,
because they go into the HTML before they get content. They are now
also elements of `detachedDoc()`, so that the file has one rule.

That rule is only correct for a tree that is in `detachedDoc()`. Thus
`nestedListsToBlockNoteStructure` now takes only the HTML string, and
parses it itself. No caller gave it an element.
…ment

`htmlToMarkdown` parses the HTML of the external HTML exporter and makes
markdown from its elements. It set the HTML as the `innerHTML` of a
`div` of the active `document`. In the active document, an element of
the HTML can load a resource and run an event handler. Thus each `video`
and each `audio` element of the export started a second request, on a
copy, on a drag of a block and on a markdown export. `cleanHTMLToMarkdown`
is an export of the package: with HTML that does not come from the
exporter, an element could also run its event handler.

The `div` is now an element of `detachedDoc()`, as in the HTML parser.
The markdown is the same.
A parse rule does not control the document of the node that it gets:
the caller does. The content parser of a block spec made its work copy
with `cloneNode`, and a clone is in the document of its source. For a
node of the active document, the clone loads its resource and runs its
event handler one more time.

The content parser now makes the copy with
`detachedDoc().importNode(node, true)`, as `getListItemContent` and
`getDetailsContent` do. The result of a parse is the same.

The test gives the parse rules HTML that is in the active document: they
make no element and no clone in that document.
The rule of `util/detachedDoc.ts` was only in its JSDoc. Now the lint
enforces it for the source files of `@blocknote/core`, with one rule of
a local Oxlint plugin: `blocknote-dom/no-active-document` in
`scripts/lint/detached-doc.mjs`. The rule has three checks:

- In each file: no `cloneNode` and no `createHTMLDocument`. A parse
  rule gets a node from outside, and a parse rule can be in each file.
- In the files that only handle content from outside (the parsers, the
  paste handlers, the markdown export, the parse helpers of the
  blocks): also no global `document` and no `ownerDocument`. Other
  files render, and make elements in the active document on purpose.
- In a parse function of each file (a function with a name that starts
  with `parse` or contains `Parse`, and the functions in it): also no
  global `document` and no `ownerDocument`.

Oxlint has no built-in rule that a configuration can limit to a
function, and an override replaces the options of a built-in rule. Thus
the whole policy is in the plugin: `vite.config.ts` has only the plugin
entry and one override that turns the rule on. The explanation is in the
header of the plugin and in `scripts/lint/README.md`. The plugin has a
test for `node --test`.

Seven lines are correct and have an exception with its reason: the one
`createHTMLDocument` of `detachedDoc()`, the copies of the editor's own
DOM for the drag preview and for the HTML export, and two TreeWalkers
(they only read).

This branch was successfully deployed

2 active deployments
Preview – blocknote-website — ac056a24 Deployed Nov 24, 2026 by vercel[bot]
Preview – blocknote — ac056a24 Deployed Nov 24, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants