Skip to content

fix(bindx-editor): trim block-edge whitespace when deserializing pasted HTML#61

Merged
matej21 merged 2 commits into
mainfrom
bug/html-paste-prepends-leading-space
Jul 2, 2026
Merged

fix(bindx-editor): trim block-edge whitespace when deserializing pasted HTML#61
matej21 merged 2 commits into
mainfrom
bug/html-paste-prepends-leading-space

Conversation

@MalaRuze

@MalaRuze MalaRuze commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #60.

Problem

Pasting HTML into a BlockEditor prepends (and appends) a stray space whenever the source markup is indented / pretty-printed. HtmlDeserializer.deserializeTextNode collapses a leading newline + indentation into a single space (text.replace(/[ \t]*(?:\r?\n[ \t]*)+/g, ' ')) but nothing trims that space at the block boundary, so <p>\n\t\tSome text\n</p> deserializes to " Some text " instead of "Some text". Users pasting from rendered pages get a leading space on essentially every paste.

Fix

deserializeBlocks is the block-content boundary — it is called both at the top level (from withPaste) and for each block's children (from every block plugin's next, e.g. paragraph / heading / list-item). After assembling a block's inline texts run there, trim leading whitespace from the first text leaf and trailing whitespace from the last, descending through inline wrappers (anchors, etc.) to reach the edge leaf. This matches how a browser renders the same markup under white-space: normal. Interior whitespace between words is untouched.

Tests

Added tests/unit/editor/repro-paste-leading-space.test.ts:

  • pasting <p>\n\t\tSome text\n</p> yields Some text (no leading/trailing space)
  • interior whitespace still collapses: <p>\n\tfoo\n\t\tbar\n</p>foo bar

The first commit adds the test (red against main); the second adds the fix (green). Full tests/unit/editor/ suite passes and bun run build is clean.

Notes / scope

Trimming is applied to the inline-texts result of deserializeBlocks. Loose text nodes that sit as siblings of block elements and get wrapped into a default element (the containsBlock branch of processNodeListPaste) are out of scope here — happy to extend if maintainers prefer a single normalization pass covering that case too.

Reported downstream by a real end user pasting into a rich-text field; a temporary workaround (wrapping editor.insertFragment to trim the pasted fragment's edges) is shipped in the reporting project and will be removed once this lands.

@matej21 matej21 merged commit 3c2fd0d into main Jul 2, 2026
3 checks passed
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.

HtmlDeserializer prepends leading space when pasting indented block HTML

2 participants