Repository navigation
Conversation
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
lexical
@lexical/a11y
@lexical/clipboard
@lexical/code
@lexical/code-core
@lexical/code-prism
@lexical/code-shiki
@lexical/compiler
@lexical/devtools-core
@lexical/dragon
@lexical/eslint-plugin
@lexical/extension
@lexical/fast-check
@lexical/file
@lexical/hashtag
@lexical/headless
@lexical/history
@lexical/html
@lexical/internal
@lexical/link
@lexical/list
@lexical/mark
@lexical/markdown
@lexical/mdast
@lexical/offset
@lexical/overflow
@lexical/plain-text
@lexical/react
@lexical/rich-text
@lexical/selection
@lexical/table
@lexical/tailwind
@lexical/text
@lexical/utils
@lexical/y
@lexical/yjs
commit: |
There was a problem hiding this comment.
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
$generateNodesFromDOMnow modifies anyParentNodeit is given, not only a parsedDocument. 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: prewrapper and becomes code content for pretty-printed HTML. Dropping those text nodes while wrapping avoids it. - The branch now conflicts with
main, andmain's CONTRIBUTING.md ("Changesets") asks for a changeset on user-facing fixes. That means an'@lexical/html': patchentry starting[fix] …, with aVerify: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.
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>
dfe6344 to
b281d12
Compare
|
Thanks for the review. Both fixed in the latest push, which also resolves the conflict with
Added a changeset. — Ved Thakar |
There was a problem hiding this comment.
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:
- 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 registereddivconversion will claim the wrapper. - Changeset
Verify:(.changeset/html-import-safari-vscode-paste.md): CONTRIBUTING.md ("Changesets") asks a fix that changes observable output to add aVerify: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.
- JSDoc (
packages/lexical-html/src/index.ts:143-147):$generateNodesFromDOMnow rewrites theDocumentorParentNodeit is given (it inserts a wrapper and removesfont-family/white-spacefrom the lines). One sentence there would warn callers who keep using that DOM afterwards. - Test plan: the description lists 8 tests with 5 failing on
main, but the file now has 9 and 6 of them fail onmain. 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.
| dom: Document | ParentNode, | ||
| ): LexicalNode[] { | ||
| $inlineStylesFromStyleSheetsDOM(dom); | ||
| wrapMonospacePreSiblingRunsDOM(dom); |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
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$generateNodesFromDOMpath,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$generateNodesFromDOMright after$inlineStylesFromStyleSheetsDOM. It wraps each run of two or more sibling elements that meet all of these conditions, in a<div>with the samefont-family/white-space, i.e. Chrome's shape:white-space: pre*<div>/<br>with at least one<div>Single divs, runs of only
<br>s, and content inside an existing monospace-pre element are left alone. The DOMImportExtension path isn't changed, becauseCodeImportExtensionalready handles this shape there.Notes / behaviour worth knowing:
CodeNoderegistered, a paragraph containing"\n"between paragraphs$generateNodesFromDOMthe right place for the preprocessing, or would you rather have it in@lexical/code's import, e.g. a parent-aware conversion?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:<br>s isn't wrappeddev-examples/dom-import/src/fixtures/vscode-safari.htmlbecomes one code block. Its trailing body-level<br class="Apple-interchange-newline">still imports as alinebreak: it's outside any block, so the legacyLineBreakNodefilter 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 onmain; the other 3 are controls).CodeBlock.spec.mjsand all ofCopyAndPaste/htmlpass.pnpm run test-unit: all 10,080 tests passtsc,pnpm run flow(0 errors), and eslint/prettier on the changed files: cleanAuthor: Ved Thakar (@Vedthakar)
🤖 Generated with Claude Code