Skip to content

Fix pasting text with line breaks into a code block#905

Open
jorgemanrubia wants to merge 2 commits intomainfrom
fix-paste-line-breaks-in-code-block
Open

Fix pasting text with line breaks into a code block#905
jorgemanrubia wants to merge 2 commits intomainfrom
fix-paste-line-breaks-in-code-block

Conversation

@jorgemanrubia
Copy link
Member

Summary

  • Pasting plain text containing line breaks into a code block caused text after the first break to escape into a new paragraph outside the code block
  • The clipboard handler previously bailed out entirely when inside a code block (return false), deferring to Lexical's default paste handler which creates paragraph nodes that break out of the code node
  • Now handles code block paste explicitly: splits plain text on newlines and inserts TextNode/LineBreakNode pairs directly into the code block, keeping all pasted lines inside

Fizzy card #4979

When pasting plain text containing line breaks into a code block, the
text after the first line break would escape the code block and land in
a new paragraph. This happened because the clipboard handler bailed out
entirely when inside a code block, letting Lexical's default paste
handler create paragraph nodes that broke out of the code node.

Instead of deferring to Lexical, handle code block paste explicitly by
splitting the plain text on newlines and inserting TextNode/LineBreakNode
pairs directly. This keeps all pasted lines inside the code block.
Copilot AI review requested due to automatic review settings March 20, 2026 17:58
@jorgemanrubia jorgemanrubia self-assigned this Mar 20, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a paste-handling gap where pasting multi-line plain text into a Lexical code block could break out into a sibling paragraph by explicitly inserting TextNode + LineBreakNode sequences into the active code block.

Changes:

  • Add explicit “paste into code block” path in the clipboard handler (inserts line breaks as LineBreakNodes).
  • Add a Playwright regression test ensuring multi-line paste remains contained within the code block.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
test/browser/tests/paste/paste.test.js Adds a regression test for multi-line plain-text paste inside a code block.
src/editor/clipboard.js Implements custom code-block paste handling to keep newline-separated text within the code node.

Tip

If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +21 to +25
if (this.#isPastingIntoCodeBlock()) {
this.#pasteIntoCodeBlock(clipboardData)
event.preventDefault()
return true
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

paste() unconditionally calls preventDefault() and returns true when inside a code block, even if #pasteIntoCodeBlock() ends up doing nothing (e.g., clipboard has no text/plain). This can swallow pastes like files/HTML and result in no-op paste behavior in code blocks. Consider having #pasteIntoCodeBlock() return a boolean (handled vs not), and only preventDefault()/return true when it actually inserts nodes; otherwise fall through to the existing rich text / file handling.

Copilot uses AI. Check for mistakes.
Comment on lines +82 to +88
const lines = text.split(/\n/)
const nodes = []

for (let i = 0; i < lines.length; i++) {
if (i > 0) nodes.push($createLineBreakNode())
if (lines[i]) nodes.push($createTextNode(lines[i]))
}
Copy link

Copilot AI Mar 20, 2026

Choose a reason for hiding this comment

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

Splitting pasted text with text.split(/\n/) will leave stray \r characters in the inserted text when the clipboard uses Windows newlines (\r\n). Normalizing first (e.g., split on /\r?\n/ or replace \r\n with \n) will prevent carriage returns from appearing in code blocks.

Copilot uses AI. Check for mistakes.
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.

2 participants