Skip to content

[lexical-html] Bug Fix: Import Safari's VS Code paste as one code block - #9460

Open
Vedthakar wants to merge 2 commits into
meta:mainfrom
Vedthakar:fix/5569-safari-vscode-paste
Open

Vedthakar wants to merge 2 commits into
meta:mainfrom
Vedthakar:fix/5569-safari-vscode-paste

Conversation

@Vedthakar

Copy link
Copy Markdown
Contributor

Description

This solves issue #5569.

Safari copies code from VS Code differently from Chrome. Chrome wraps the line <div>s in an outer <div style="font-family: …monospace; white-space: pre">. Safari omits that wrapper and puts the styles on each sibling line <div> (empty lines are <div><br></div>). On the legacy $generateNodesFromDOM path, CodeNode's monospace-div conversion then fires once per line, so Safari pastes one code block per line.

Following @etrepum's suggestion on the issue, this adds a DOM preprocessing step, wrapMonospacePreSiblingRunsDOM (packages/lexical-html/src/import/wrapMonospacePreRuns.ts). It runs in $generateNodesFromDOM right after $inlineStylesFromStyleSheetsDOM. It wraps each run of two or more sibling elements that meet all of these conditions, in a <div> with the same font-family / white-space, i.e. Chrome's shape:

  • each element has a monospace font and white-space: pre*
  • the run is all <div>/<br> with at least one <div>
  • only blank text sits between the elements

Single divs, runs of only <br>s, and content inside an existing monospace-pre element are left alone. The DOMImportExtension path isn't changed, because CodeImportExtension already handles this shape there.

Notes / behaviour worth knowing:

  • After the fix, Safari's result equals Chrome's apart from Chrome's leading newline. That includes two existing quirks of the legacy import, which this PR doesn't change:
    • an extra blank line between line divs
    • without CodeNode registered, a paragraph containing "\n" between paragraphs
  • Questions for review:
    • Is $generateNodesFromDOM the right place for the preprocessing, or would you rather have it in @lexical/code's import, e.g. a parent-aware conversion?
    • Should it be exported?

Closes #5569

Test plan

New packages/lexical-code-core/src/__tests__/unit/LegacyVscodePasteImport.test.ts (jsdom, using the Safari and Chrome HTML from the issue), 8 tests:

  • Safari: the sibling line divs become one code block
  • Safari gives the same code block as Chrome, apart from the leading newline
  • Chrome is unchanged
  • two runs separated by other content stay two code blocks
  • a single div isn't wrapped
  • a run of only <br>s isn't wrapped
  • a line containing a tab keeps the tab inside the one code block
  • the unprettified real clipboard fixture dev-examples/dom-import/src/fixtures/vscode-safari.html becomes one code block. Its trailing body-level <br class="Apple-interchange-newline"> still imports as a linebreak: it's outside any block, so the legacy LineBreakNode filter keeps it. That's pre-existing and unrelated to this fix, so the test asserts it explicitly.

Before

On main (8 tests): 5 failed / 3 passed. The Safari, multi-run, tab and fixture tests fail, because Safari's paste gives one code block per line.

After

On current main (tested together with my other open fixes):

  • LegacyVscodePasteImport.test.ts: 8 passed (5 fail on main; the other 3 are controls).
  • Chromium e2e: CodeBlock.spec.mjs and all of CopyAndPaste/html pass.
  • pnpm run test-unit: all 10,080 tests pass
  • tsc, pnpm run flow (0 errors), and eslint/prettier on the changed files: clean

Author: Ved Thakar (@Vedthakar)

🤖 Generated with Claude Code

@vercel

vercel Bot commented Oct 25, 2026 •

Copy link
Copy Markdown

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

Project Deployment Actions Updated
lexical Ready Ready Preview Nov 16, 2026 11:48pm UTC
lexical-playground Ready Ready Preview Nov 16, 2026 11:48pm UTC

Request Review

@pkg-pr-new

pkg-pr-new Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
lexical

npm i https://pkg.pr.new/lexical@9460

@lexical/a11y

npm i https://pkg.pr.new/@lexical/a11y@9460

@lexical/clipboard

npm i https://pkg.pr.new/@lexical/clipboard@9460

@lexical/code

npm i https://pkg.pr.new/@lexical/code@9460

@lexical/code-core

npm i https://pkg.pr.new/@lexical/code-core@9460

@lexical/code-prism

npm i https://pkg.pr.new/@lexical/code-prism@9460

@lexical/code-shiki

npm i https://pkg.pr.new/@lexical/code-shiki@9460

@lexical/compiler

npm i https://pkg.pr.new/@lexical/compiler@9460

@lexical/devtools-core

npm i https://pkg.pr.new/@lexical/devtools-core@9460

@lexical/dragon

npm i https://pkg.pr.new/@lexical/dragon@9460

@lexical/eslint-plugin

npm i https://pkg.pr.new/@lexical/eslint-plugin@9460

@lexical/extension

npm i https://pkg.pr.new/@lexical/extension@9460

@lexical/fast-check

npm i https://pkg.pr.new/@lexical/fast-check@9460

@lexical/file

npm i https://pkg.pr.new/@lexical/file@9460

@lexical/hashtag

npm i https://pkg.pr.new/@lexical/hashtag@9460

@lexical/headless

npm i https://pkg.pr.new/@lexical/headless@9460

@lexical/history

npm i https://pkg.pr.new/@lexical/history@9460

@lexical/html

npm i https://pkg.pr.new/@lexical/html@9460

@lexical/internal

npm i https://pkg.pr.new/@lexical/internal@9460

@lexical/link

npm i https://pkg.pr.new/@lexical/link@9460

@lexical/list

npm i https://pkg.pr.new/@lexical/list@9460

@lexical/mark

npm i https://pkg.pr.new/@lexical/mark@9460

@lexical/markdown

npm i https://pkg.pr.new/@lexical/markdown@9460

@lexical/mdast

npm i https://pkg.pr.new/@lexical/mdast@9460

@lexical/offset

npm i https://pkg.pr.new/@lexical/offset@9460

@lexical/overflow

npm i https://pkg.pr.new/@lexical/overflow@9460

@lexical/plain-text

npm i https://pkg.pr.new/@lexical/plain-text@9460

@lexical/react

npm i https://pkg.pr.new/@lexical/react@9460

@lexical/rich-text

npm i https://pkg.pr.new/@lexical/rich-text@9460

@lexical/selection

npm i https://pkg.pr.new/@lexical/selection@9460

@lexical/table

npm i https://pkg.pr.new/@lexical/table@9460

@lexical/tailwind

npm i https://pkg.pr.new/@lexical/tailwind@9460

@lexical/text

npm i https://pkg.pr.new/@lexical/text@9460

@lexical/utils

npm i https://pkg.pr.new/@lexical/utils@9460

@lexical/y

npm i https://pkg.pr.new/@lexical/y@9460

@lexical/yjs

npm i https://pkg.pr.new/@lexical/yjs@9460

commit: b281d12

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks for picking up #5569 and for testing against the real Safari clipboard fixture. Two problems need fixing before this can land.

1. Each line still becomes its own CodeNode, now nested inside the block (packages/lexical-html/src/import/wrapMonospacePreRuns.ts, lines 84-91)

The line <div>s you move into the wrapper keep their own font-family: …monospace. CodeNode's legacy div conversion ($convertDivElement / isCodeElement in packages/lexical-code-core/src/CodeNode.ts) turns every monospace div into a CodeNode. With the issue's Safari HTML the result is code > [code, linebreak, code], not Chrome's flat CodeNode. That tree survives insertion, renders as <code> inside <code>, exports as <pre> inside <pre>, and is saved that way in JSON, although CodeNode declares children: 'inline'. Only an editor that also registers a highlighter, such as CodePrismExtension, flattens it.

Removing the two styles from each element as you move it fixes this: node.style.removeProperty('font-family'), and the same for white-space. The lines then convert the way Chrome's unstyled line divs do, and the Safari paste imports as one flat CodeNode whose children match Chrome's.

2. Node.TEXT_NODE throws outside a browser (same file, line 24)

Node.js has no global Node, and withDOM() from @lexical/headless/dom does not install one. $generateNodesFromDOM therefore now throws ReferenceError: Node is not defined for any HTML where a monospace white-space: pre element has a following sibling. That breaks both headless routes the docs describe: withDOM() in packages/lexical-website/docs/concepts/headless.md, and the jsdom example in packages/lexical-html/README.md. Both import this HTML fine on main today. AGENTS.md ("Shadow DOM and iframe realm safety") asks library code to use Lexical's DOM helpers rather than browser globals. isDOMTextNode from lexical makes this check without the global, as CodeImportExtension already does.

Tests

The new tests compare only getTextContent(), which is why (1) slipped through. The 'a\n\nb' expected by "two runs separated by other content stay two code blocks" is the double line break getTextContent() puts between nested block children. Chrome's wrapper around the same two lines gives 'a\nb', so the comment above that expectation also needs updating. Please assert the code block's children instead: no element children, or Safari's exportJSON() equal to Chrome's apart from the leading line break. A // @vitest-environment node case under withDOM() would cover (2). The AGENTS.md "Commits and Pull Requests" section also asks for the real Before/After command output to be pasted into the Test plan.

Optional, non-blocking

  • $generateNodesFromDOM now modifies any ParentNode it is given, not only a parsed Document. A note in its JSDoc, or running the preprocess only for documents, would avoid surprising callers that pass a live element.
  • Whitespace-only text between the run's elements moves into the white-space: pre wrapper and becomes code content for pretty-printed HTML. Dropping those text nodes while wrapping avoids it.
  • The branch now conflicts with main, and main's CONTRIBUTING.md ("Changesets") asks for a changeset on user-facing fixes. That means an '@lexical/html': patch entry starting [fix] …, with a Verify: paragraph.

Your questions

Putting the preprocess in $generateNodesFromDOM follows the placement suggested on the issue and mirrors inlineStylesFromStyleSheets. There is no need to export it: DOMImportExtension users already get CodeImportExtension's line-run rule.

Vedthakar and others added 2 commits November 16, 2026 18:45
Safari writes code copied from VS Code as a flat run of sibling line
<div>s and <br>s, each with the monospace `white-space: pre` style, so
the legacy importDOM converters made every line its own CodeNode.
$generateNodesFromDOM now wraps such a run in one styled <div>, the
shape Chrome writes, and moves the styles from the lines to the wrapper
so the result is one flat CodeNode. Blank text between the lines is
dropped, and the check for it uses isDOMTextNode so it also works where
`Node` is not a global (headless import).

Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-authored-by: Ved Thakar <111605341+Vedthakar@users.noreply.github.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@Vedthakar

Copy link
Copy Markdown
Contributor Author

Thanks for the review. Both fixed in the latest push, which also resolves the conflict with main:

  • Nested CodeNodes: font-family and white-space are removed from each line as it moves into the wrapper, so the Safari paste imports as one flat CodeNode.
  • Node.TEXT_NODE outside a browser: now isDOMTextNode. Added a test that runs the import with the Node global removed.
  • Tests: every imported code block is now checked to have no element children, and the two-runs case expects a\nb. That stricter test showed that blank text between the line divs became a line of its own inside the white-space: pre wrapper, so blank text between wrapped lines is now dropped.

Added a changeset.

— Ved Thakar

@rootnode-bot rootnode-bot Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks, this looks good. Safari's flat run of VS Code line <div>s now imports through $generateNodesFromDOM as one code block, the same shape a Chrome paste gives. The earlier points (the Node global, the nested CodeNodes, the blank line in the two-run test) are addressed in this revision, and the new tests cover the Safari shape, including the real clipboard fixture.

A few optional, non-blocking suggestions:

  1. Editors without CodeNode (packages/lexical-html/src/index.ts:153): the wrap runs on every legacy import. In an editor without CodeNode, each blank line of a Safari VS Code paste now becomes a paragraph holding a line break instead of an empty paragraph. That is what a Chrome paste already gives, and your description mentions it. If it is intended, saying so in the changeset helps people upgrading. Otherwise the wrap could be skipped when no registered div conversion will claim the wrapper.
  2. Changeset Verify: (.changeset/html-import-safari-vscode-paste.md): CONTRIBUTING.md ("Changesets") asks a fix that changes observable output to add a Verify: paragraph, for example:
    Verify: copy several lines from VS Code in Safari and paste them into an editor that registers CodeNode and uses the default clipboard import; they land as one code block. In an editor without CodeNode, each blank line now becomes a paragraph holding a line break, as a Chrome paste already does.
  3. JSDoc (packages/lexical-html/src/index.ts:143-147): $generateNodesFromDOM now rewrites the Document or ParentNode it is given (it inserts a wrapper and removes font-family/white-space from the lines). One sentence there would warn callers who keep using that DOM afterwards.
  4. Test plan: the description lists 8 tests with 5 failing on main, but the file now has 9 and 6 of them fail on main. AGENTS.md ("Commits and Pull Requests") asks for the pasted Before and After output of the command you ran.

On your two questions: keeping the step in $generateNodesFromDOM, next to $inlineStylesFromStyleSheetsDOM, fits the legacy importDOM model, where each conversion sees one element at a time. Leaving it unexported keeps it out of the public API that AGENTS.md's backwards-compatibility rule would then cover. A maintainer has the final say on both.

Automated review (maintainer link)

dom: Document | ParentNode,
): LexicalNode[] {
$inlineStylesFromStyleSheetsDOM(dom);
wrapMonospacePreSiblingRunsDOM(dom);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: this runs on every legacy import, including editors without CodeNode. There, the blank lines of a Safari VS Code paste now become paragraphs holding a line break instead of empty paragraphs (what a Chrome paste already gives). If that is intended, a line in the changeset's Verify: paragraph would tell people upgrading; otherwise the wrap could be skipped when no registered div conversion will claim the wrapper. A sentence in the JSDoc above would also help: the Document/ParentNode passed in is now rewritten in place.

'@lexical/html': patch
---

[fix] `$generateNodesFromDOM` imports code pasted from VS Code in Safari as one code block instead of one code block per line

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Optional: CONTRIBUTING.md ("Changesets") asks a fix that changes observable output to add a Verify: paragraph. For example:

Verify: copy several lines from VS Code in Safari and paste them into an editor that registers CodeNode and uses the default clipboard import; they land as one code block. In an editor without CodeNode, each blank line now becomes a paragraph holding a line break, as a Chrome paste already does.

This branch was successfully deployed

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Pasting code from VS Code in lexical generates multiple code blocks in Safari

1 participant