Back to Plate

Empty target fragment paste keeps first block wrapper

docs/solutions/logic-errors/2026-05-09-empty-target-fragment-paste-keeps-first-block-wrapper.md

53.0.62.7 KB
Original Source

Empty target fragment paste keeps first block wrapper

Problem

Lexical's native CopyAndPaste.spec.mjs includes a useful block-format row: copy two paragraphs, paste them into an empty quote, keep the first paragraph in the quote, and place the second paragraph after it.

Slate v2's single-empty-block fragment path replaced the target quote with the incoming paragraph fragment, losing the target wrapper.

Symptoms

  • The new DOM clipboard proof expected a block-quote followed by a paragraph.
  • The red run produced two paragraphs instead.
  • Existing single text-block replacement policy still needed to stay target-owned, so the fix could not blindly make source wrappers win.

Solution

Handle multi-text-block fragments before the generic block-fragment replacement inside getSingleEmptyBlockFragmentReplacement:

ts
if (
  fragment.length > 1 &&
  fragment.every((node) => isTextBlockElement(editor, node))
) {
  const [firstFragmentBlock, ...tailBlocks] = fragment as Element[]
  const children = [
    {
      ...onlyEditorNode,
      children: firstFragmentBlock.children.map(cloneDescendant),
    },
    ...tailBlocks.map(cloneDescendant),
  ] as Value

  return {
    children,
    previousChildren: editorChildren,
    selection: getFragmentEndSelection(children),
  }
}

Why This Works

The target empty text block owns the first pasted text block wrapper. After that first landing, remaining pasted blocks should keep their source shape and live after the target block.

This preserves the already-documented single text-block policy: when a single text block replaces a target text block, the target wrapper wins. The new rule only extends that policy to the first block of a multi-block fragment.

Prevention

  • For fragment insertion into formatted empty blocks, assert both first-block wrapper ownership and tail promotion.
  • Do not use source-wrapper preservation for single text-block fragments unless a future block-format paste owner explicitly changes the policy.
  • Keep native transport flake tags, product decorator rows, raw mobile, collaboration, and table-model claims out of package-only fragment proof.