Skip to content

Block Editor: Include resolved bound content when copying blocks - #84428

Open
salvatorecorsi wants to merge 14 commits into
WordPress:trunkfrom
salvatorecorsi:fix/copy-bound-block-content
Open

salvatorecorsi wants to merge 14 commits into
WordPress:trunkfrom
salvatorecorsi:fix/copy-bound-block-content

Conversation

@salvatorecorsi

@salvatorecorsi salvatorecorsi commented Oct 9, 2026 •

Copy link
Copy Markdown
Contributor

What?

See #84362. Related to #84389, which handles fully selected bound RichText separately.

Include available client-resolved binding values when copying blocks from the canvas, List View and the options menu. Preserve the binding definitions and saved fallbacks when an attribute cannot be resolved. Server-only editor placeholders must not become saved content.

Why?

Block serialization uses stored attributes, so a bound paragraph can look populated while its copy is empty. Treating all bound attributes as one snapshot also loses a resolved Button label when its URL is unavailable. An asynchronous HTML clipboard write can strip the comments carrying block metadata, which is especially harmful after Cut removes the original.

Copying an individual bound List Item needs its parent wrapper for normal paste, while Paste styles needs the selected item's styles.

How?

Record computed attributes from eligible mounted block edits in the existing per-editor refs context and apply them to copies of the selected tree. Only client sources with getValues contribute snapshots, and available values are applied attribute by attribute. Missing values retain their saved fallback. Stale, ambiguous, preview and Query Loop snapshots remain excluded.

Canvas, List View and menu copies use the same payload builder. The menu first writes HTML and visible plain text through a synchronous copy event. If that event is unavailable, it writes the full serialized markup as text so the block comments survive. Cut removes the source only after that write succeeds. Unbound menu copying and Copy styles retain their existing transport.

A clipboard-only prefix identifies a synthetic parent wrapper. Normal paste consumes the prefix and keeps the wrapper; Paste styles consumes it and reads the selected children instead. Copying an actual List still uses the List's styles. Existing Windows fragment and Chromium metadata normalization is shared before reading the prefix.

Testing Instructions

  1. Bind a paragraph's content to a REST-visible post-meta value, for example Bound <strong>text</strong>, while its saved content is empty. Add a normal paragraph and an empty paste target.
  2. Copy the bound block from the canvas, List View and its options menu. With the normal copy event available, an external text destination receives Bound text and a rich destination keeps its formatting.
  3. Paste into Gutenberg and inspect the saved fallback and metadata.bindings. The original block and the source value should be unchanged.
  4. Repeat with mixed selections, reversed selections and partial RichText endpoints.
  5. Use testing/server-only-source with saved fallback content. Copy must preserve the fallback rather than save the editor's placeholder.
  6. Bind both Button text and URL, returning only text. Its copied label must survive, with any saved URL fallback preserved.
  7. Force the menu's copy event to fail, then Cut and Paste. The original must remain while the asynchronous text write is pending; after success, the pasted block must retain its binding, content and valid serialization.
  8. Copy a large bound List Item inside a small ordered List. Normal paste should keep the ordered wrapper, and Paste styles should use large. Copying the List itself should still use small.

Testing Instructions for Keyboard

Copy with Ctrl+C/Cmd+C and paste with Ctrl+V/Cmd+V. Repeat from List View and extend the selection with Shift+ArrowDown. The options-menu fallback was also checked with a real Cut followed by native Ctrl+V in the post editor.

Current validation — 22 November 2026

Published head 2e8faea22fb4b4d2dc1727da92dd5ce9b68d6903 addresses the four requested follow-ups.

  • The unchanged candidate tree passed 98 scoped JavaScript test executions in 10 files on 21 November, including the prior clipboard controls. The new fallback, partially resolved Button, Cut and selected-item-style regressions were reproduced before their corrections.
  • Full production build and full repository typecheck passed on that tree, along with scoped JavaScript lint, PR changelog checks, test conventions and documentation generation/cleanliness checks. The pre-existing registry dependency warning remains.
  • On 22 November, a fresh Chromium 158 check used the actual built checkout in the native post editor. With the copy event forced to fail, a real clipboard text write was held pending: the source stayed present until completion, then native Paste preserved the binding, exact content and block validity. Three served asset hashes matched the checkout before and after. The owned draft, session, preferences, mount and server state were restored and the full preference-key presence/owner/hash remained unchanged.
  • Normal pre-commit hooks passed without changing the validated tree.

The Chromium check uses a controlled promise gate around an actual write; it does not measure clipboard latency. The platform payload cases in unit tests are modeled inputs. No local Safari/WebKit, Docker E2E, full repository functional suite or full published-dependency audit is claimed for this follow-up. Hosted CI and the required type label remain separate from these local results.

Could a maintainer add the [Type] Bug label? I do not have permission to apply upstream labels.

Use of AI Tools

OpenAI Codex assisted with investigation, implementation, regression tests, automated source review and validation, including the focused native-editor Chromium adapter. Maintainer review is requested.

@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label Oct 9, 2026
@github-actions

github-actions Bot commented Oct 9, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

👋 Welcome

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @salvatorecorsi! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: salvatorecorsi <salvatorecorsi@git.wordpress.org>
Co-authored-by: ciampo <mciampini@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

🏷️ Labels

This pull request needs exactly one label indicating its type, and has 0.

  • Required: any label starting with [Type].
  • Found: none.

Read more about Type labels in Gutenberg. If you cannot add labels yourself, a reviewer can do it for you.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Oct 9, 2026
@github-actions
github-actions Bot requested review from aduth and ciampo November 16, 2026 10:39

@ciampo ciampo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't have the full context on the block bindings work, but I took a look from the menu and clipboard side and left a few comments inline. Please don't count me as the main reviewer for this one.

( Window & typeof globalThis ) | null;
if ( view?.navigator.clipboard?.write && view.ClipboardItem ) {
try {
await view.navigator.clipboard.write( [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

In WebKit 27.2, this write succeeded, but the paste event contained the visible text and had lost the block comments carrying metadata.bindings. The copy-event fallback preserved both in the same check. Could we use a write path that preserves the binding and cover copying from the menu and pasting back into Gutenberg in WebKit?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The hosted WebKit test now passes when copying from Options and pasting back into Gutenberg. Could we also force the copy event to fail and check that Cut followed by Paste keeps the binding? Cut removes the original as soon as the async write reports success.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I replaced the async HTML fallback with a serialized text write, because a successful HTML write can still strip the binding comments. In the native Chromium post editor, I forced the copy event to fail and held the real text-write promise: Cut kept the source until success, then Paste retained its binding and content.

'text/html': new view.Blob( [ content.html ], {
type: 'text/html',
} ),
'text/plain': new view.Blob( [ content.plainText ], {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copying a styled bound paragraph from Options used to work with Paste styles. usePasteStyles reads only text/plain, which now contains just the visible text, so it reports that it couldn't find any styles. Could we have Paste styles use the serialized block when it's available and cover that pair of commands?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The Chromium test now copies a bound paragraph from Options and applies its font size to the target paragraph. That covers the case I raised here.

: getClipboardBlocksContent(
getBlocksByClientId( clientIds ) as Block[],
registry,
attributesForCopy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copying a bound List Item from an ordered list produces a bare <li> here. Could the rich menu copy honor requiresWrapperOnCopy so it retains the parent <ol> the same way keyboard copy does?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The ordered list now pastes with its wrapper. I found one side effect for Paste styles. With a large bound List Item inside a small List, copying the item applies small to the target. Could we keep the wrapper for regular paste and use the selected item's styles for Paste styles?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Paste styles now reads the selected item’s styles while normal paste keeps the ordered-list wrapper. A clipboard-only marker distinguishes a synthetic wrapper from an actually selected List and is removed before insertion, so copying the List itself still uses its own styles.

registry,
attributesForCopy
),
() => serialize( getBlocksByClientId( clientIds ) as Block[] ),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For an unbound selection, getClipboardBlocksContent has already serialized these blocks. Could we reuse that markup for the text-only write so Copy and Cut only serialize the selection once?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Unbound Copy and Cut now reuse the serialized markup for the text write. That addresses my comment here.

@salvatorecorsi

Copy link
Copy Markdown
Contributor Author

Thanks for checking these clipboard paths. The update in 53cf427a tries the synchronous copy event first so the block comments can travel with the visible text. Paste styles reads the serialized HTML when available and still accepts the existing serialized text format. Menu copy keeps the ordered-list wrapper for a bound item, and unbound Copy and Cut reuse the same serialization.

I verified the menu copy, pasting back with the binding, and Paste styles in the actual post editor in Chromium. The menu round trip is also in the WebKit Playwright project. My Windows WebKit 27.2 build did not return the rich clipboard payload even in a minimal copy-event control, so the Safari round trip still needs confirmation.

ciampo
ciampo previously requested changes Nov 21, 2026
.filter(
( [ attributeName, binding ] ) =>
bindableAttributes?.includes( attributeName ) &&
registeredSources[ binding?.source as string ]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

With testing/server-only-source, the paragraph stores "paragraph default content" and the editor displays "Server Source". Copying the block now makes "Server Source" the pasted paragraph's saved content. Could we preserve the original fallback when the editor only has a placeholder? The existing server-only fixture would cover this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks :D Server-only sources now stay out of the editor snapshot, because their displayed text is a placeholder rather than a value we can save. Copy retains the paragraph’s saved fallback; the regression covers a source without getValues.

if (
attributes !== block.attributes ||
! boundAttributeNames.length ||
boundAttributeNames.some(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If a Button binds both text and url, and there's no saved URL, a source that returns only the text fails this check. Copy can then produce an empty button even though its label is visible. Could we keep each resolved attribute and use the saved fallback for the missing one?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

The snapshot now keeps resolved attributes individually, so a missing URL no longer drops the Button’s resolved label. Any saved URL fallback is preserved too.

( Window & typeof globalThis ) | null;
if ( view?.navigator.clipboard?.write && view.ClipboardItem ) {
try {
await view.navigator.clipboard.write( [

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The hosted WebKit test now passes when copying from Options and pasting back into Gutenberg. Could we also force the copy event to fail and check that Cut followed by Paste keeps the binding? Cut removes the original as soon as the async write reports success.

: getClipboardBlocksContent(
getBlocksByClientId( clientIds ) as Block[],
registry,
attributesForCopy

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The ordered list now pastes with its wrapper. I found one side effect for Paste styles. With a large bound List Item inside a small List, copying the item applies small to the target. Could we keep the wrapper for regular paste and use the selected item's styles for Paste styles?

'text/html': new view.Blob( [ content.html ], {
type: 'text/html',
} ),
'text/plain': new view.Blob( [ content.plainText ], {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The Chromium test now copies a bound paragraph from Options and applies its font size to the target paragraph. That covers the case I raised here.

registry,
attributesForCopy
),
() => serialize( getBlocksByClientId( clientIds ) as Block[] ),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Unbound Copy and Cut now reuse the serialized markup for the text write. That addresses my comment here.

@ciampo
ciampo dismissed their stale review November 21, 2026 00:29

The inline feedback is advisory.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Block editor /packages/block-editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants