diff --git a/packages/block-editor/CHANGELOG.md b/packages/block-editor/CHANGELOG.md
index 0886eb592ee4ff..a833a7b0229bd9 100644
--- a/packages/block-editor/CHANGELOG.md
+++ b/packages/block-editor/CHANGELOG.md
@@ -22,6 +22,7 @@
- Writing flow: Bring back the editor header and block toolbar that iOS slides off screen when the on-screen keyboard opens, once the canvas stops scrolling ([#87316](https://github.com/WordPress/gutenberg/pull/87316)).
- Block states: Undo a text gradient set in Global Styles, for the block type or an applied block style variation, when a state or breakpoint sets a box clip or text color. A breakpoint pseudo-state now also undoes one set by the default pseudo-state ([#86245](https://github.com/WordPress/gutenberg/pull/86245)).
- List View: Arrow Left moves to the parent row when the parent is outside the render window ([#87266](https://github.com/WordPress/gutenberg/pull/87266)).
+- Restrict the bound-text clipboard exception to supported attributes and resolve pattern override default bindings ([#84389](https://github.com/WordPress/gutenberg/pull/84389)).
### Internal
diff --git a/packages/block-editor/src/components/writing-flow/test/use-clipboard-handler.jsdom.test.ts b/packages/block-editor/src/components/writing-flow/test/use-clipboard-handler.jsdom.test.ts
new file mode 100644
index 00000000000000..2ea0c24752ca6b
--- /dev/null
+++ b/packages/block-editor/src/components/writing-flow/test/use-clipboard-handler.jsdom.test.ts
@@ -0,0 +1,453 @@
+import {
+ afterAll,
+ afterEach,
+ beforeAll,
+ describe,
+ expect,
+ it,
+ vi,
+} from 'vitest';
+import {
+ act,
+ fireEvent,
+ render,
+ renderHook,
+ screen,
+} from '@testing-library/react';
+import { createElement, RawHTML } from '@wordpress/element';
+import type { PropsWithChildren } from 'react';
+import { createRegistry, RegistryProvider, useSelect } from '@wordpress/data';
+import {
+ createBlock,
+ getBlockTypes,
+ registerBlockType,
+ store as blocksStore,
+ unregisterBlockType,
+} from '@wordpress/blocks';
+import type { Block } from '@wordpress/blocks';
+import { store as noticesStore } from '@wordpress/notices';
+import { create, store as richTextStore } from '@wordpress/rich-text';
+import type { RichTextData } from '@wordpress/rich-text';
+import { useRichText } from '../../../../../rich-text/src/hook';
+import { addField } from '../../../../../rich-text/src/hook/event-listeners';
+import { store as blockEditorStore } from '../../../store';
+import useClipboardHandler from '../use-clipboard-handler';
+
+describe( 'useClipboardHandler', () => {
+ let cleanups: Array< () => void > | undefined;
+
+ beforeAll( () => {
+ for ( const [ name, tagName ] of [
+ [ 'core/paragraph', 'p' ],
+ [ 'core/heading', 'h3' ],
+ ] ) {
+ registerBlockType< {
+ content: string | RichTextData;
+ level?: number;
+ metadata?: Record< string, unknown >;
+ } >( name, {
+ apiVersion: 3,
+ title: name,
+ category: 'text',
+ attributes: {
+ content: {
+ type: 'rich-text',
+ source: 'rich-text',
+ selector: tagName,
+ },
+ level: { type: 'number' },
+ metadata: { type: 'object' },
+ },
+ save: ( { attributes } ) =>
+ createElement(
+ tagName,
+ null,
+ createElement( RawHTML, {
+ children: attributes.content.toString(),
+ } )
+ ),
+ } );
+ }
+ } );
+
+ afterAll( () => {
+ getBlockTypes().forEach( ( { name } ) => unregisterBlockType( name ) );
+ } );
+
+ afterEach( () => {
+ cleanups?.forEach( ( cleanup ) => cleanup() );
+ document.body.ownerDocument
+ .defaultView!.getSelection()!
+ .removeAllRanges();
+ } );
+
+ function mountClipboard(
+ blocks: Block[],
+ { collapsed = false, supportedAttributes = [ 'content' ] } = {}
+ ) {
+ const registry = createRegistry();
+ registry.register( blocksStore );
+ registry.register( blockEditorStore );
+ registry.register( noticesStore );
+ registry.dispatch( blocksStore ).addBlockTypes( getBlockTypes() );
+ const actions = registry.dispatch( blockEditorStore );
+ actions.resetBlocks( blocks );
+ actions.updateSettings( {
+ __experimentalBlockBindingsSupportedAttributes: {
+ 'core/paragraph': supportedAttributes,
+ },
+ } );
+
+ const [ block ] = blocks;
+ const canvas = document.createElement( 'div' );
+ const editable = document.createElement( 'p' );
+ editable.id = `block-${ block.clientId }`;
+ editable.className = 'block-editor-block-list__block';
+ editable.contentEditable = 'true';
+ editable.tabIndex = 0;
+ editable.innerHTML = 'Bound text';
+ Object.defineProperty( editable, 'isContentEditable', { value: true } );
+ canvas.append( editable );
+ document.body.append( canvas );
+ editable.focus();
+
+ const selection = editable.ownerDocument.defaultView!.getSelection()!;
+ selection.selectAllChildren( editable );
+ if ( collapsed ) {
+ selection.collapseToStart();
+ }
+ if ( blocks.length > 1 ) {
+ actions.multiSelect( block.clientId, blocks.at( -1 )!.clientId );
+ } else {
+ actions.selectionChange(
+ block.clientId,
+ 'content',
+ 0,
+ collapsed ? 0 : editable.textContent!.length
+ );
+ }
+
+ const { result, unmount } = renderHook( useClipboardHandler, {
+ wrapper: ( { children }: PropsWithChildren ) =>
+ createElement( RegistryProvider, {
+ value: registry,
+ children,
+ } ),
+ } );
+ result.current( canvas );
+ const record = {
+ current: create( {
+ element: editable,
+ range: selection.getRangeAt( 0 ),
+ } ),
+ };
+ const handleChange = vi.fn();
+ const unsubscribe = addField( editable, {
+ current: {
+ record,
+ handleChange,
+ applyRecord: vi.fn(),
+ createRecord: () =>
+ create( {
+ element: editable,
+ range: selection.getRangeAt( 0 ),
+ } ),
+ onSelectionChange: vi.fn(),
+ forceRender: vi.fn(),
+ },
+ } );
+ cleanups = [
+ unsubscribe,
+ () => {
+ result.current( null );
+ },
+ unmount,
+ () => canvas.remove(),
+ ];
+
+ return {
+ registry,
+ handleChange,
+ copy( type: 'copy' | 'cut' = 'copy' ) {
+ const clipboard = new Map< string, string >();
+ const event = new Event( type, {
+ bubbles: true,
+ cancelable: true,
+ } ) as ClipboardEvent;
+ Object.defineProperty( event, 'clipboardData', {
+ value: {
+ getData: ( format: string ) =>
+ clipboard.get( format ) ?? '',
+ setData: ( format: string, content: string ) =>
+ clipboard.set( format, content ),
+ },
+ } );
+ editable.dispatchEvent( event );
+ return {
+ plainText: event.clipboardData!.getData( 'text/plain' ),
+ html: event.clipboardData!.getData( 'text/html' ),
+ };
+ },
+ };
+ }
+
+ it.each( [
+ { content: { source: 'core/post-meta', args: { key: 'movie_field' } } },
+ { content: { source: 'testing/custom-source' } },
+ { __default: { source: 'core/pattern-overrides' } },
+ ] )(
+ 'copies resolved text and formatting for bound text (%j)',
+ ( bindings ) => {
+ const block = createBlock( 'core/paragraph', {
+ content: '',
+ metadata: { bindings },
+ } );
+ const { copy, registry } = mountClipboard( [ block ] );
+
+ expect( copy() ).toEqual( {
+ plainText: 'Bound text',
+ html: 'Bound text',
+ } );
+ const attributes = registry
+ .select( blockEditorStore )
+ .getBlockAttributes( block.clientId );
+ expect( String( attributes!.content ) ).toBe( '' );
+ expect( attributes!.metadata!.bindings ).toEqual( bindings );
+ }
+ );
+
+ it( 'copies bound text after RichText synchronizes the DOM selection', () => {
+ const block = createBlock( 'core/paragraph', {
+ content: '',
+ metadata: {
+ bindings: {
+ content: { source: 'core/post-meta' },
+ },
+ },
+ } );
+ const registry = createRegistry();
+ registry.register( blocksStore );
+ registry.register( blockEditorStore );
+ registry.register( noticesStore );
+ registry.register( richTextStore );
+ registry.dispatch( blocksStore ).addBlockTypes( getBlockTypes() );
+ const actions = registry.dispatch( blockEditorStore );
+ actions.resetBlocks( [ block ] );
+ actions.updateSettings( {
+ __experimentalBlockBindingsSupportedAttributes: {
+ 'core/paragraph': [ 'content' ],
+ },
+ } );
+ actions.selectBlock( block.clientId );
+
+ function BoundClipboard() {
+ const clipboardRef = useClipboardHandler();
+ const selection = useSelect( ( select ) => {
+ const { getSelectionStart, getSelectionEnd } =
+ select( blockEditorStore );
+ const start = getSelectionStart();
+ const end = getSelectionEnd();
+ const isSelected = start.attributeKey === 'content';
+ return {
+ isSelected,
+ start: isSelected ? start.offset : undefined,
+ end: isSelected ? end.offset : undefined,
+ };
+ }, [] );
+ const { ref } = useRichText( {
+ value: 'Bound text',
+ selectionStart: selection.start,
+ selectionEnd: selection.end,
+ __unstableIsSelected: selection.isSelected,
+ onChange: vi.fn(),
+ onSelectionChange( start, end ) {
+ actions.selectionChange(
+ block.clientId,
+ 'content',
+ start,
+ end
+ );
+ },
+ } );
+ return createElement(
+ 'div',
+ {
+ ref: clipboardRef,
+ role: 'textbox',
+ 'aria-label': 'Editor canvas',
+ contentEditable: true,
+ suppressContentEditableWarning: true,
+ tabIndex: 0,
+ },
+ createElement( 'p', {
+ id: `block-${ block.clientId }`,
+ role: 'textbox',
+ 'aria-label': 'Bound content',
+ className: 'block-editor-block-list__block',
+ contentEditable: true,
+ 'data-wp-block-attribute-key': 'content',
+ ref,
+ } )
+ );
+ }
+
+ const { unmount } = render(
+ createElement( RegistryProvider, {
+ value: registry,
+ children: createElement( BoundClipboard ),
+ } )
+ );
+ cleanups = [ unmount ];
+ const canvas = screen.getByRole( 'textbox', { name: 'Editor canvas' } );
+ const editable = screen.getByRole( 'textbox', {
+ name: 'Bound content',
+ } );
+ canvas.contentEditable = 'true';
+ editable.contentEditable = 'true';
+ Object.defineProperty( editable, 'isContentEditable', { value: true } );
+ act( () => {
+ canvas.focus();
+ editable.ownerDocument
+ .defaultView!.getSelection()!
+ .selectAllChildren( editable );
+ } );
+
+ expect(
+ registry.select( blockEditorStore ).getSelectionStart().attributeKey
+ ).toBeUndefined();
+ const clipboard = new Map< string, string >();
+ const event = new Event( 'copy', {
+ bubbles: true,
+ cancelable: true,
+ } ) as ClipboardEvent;
+ Object.defineProperty( event, 'clipboardData', {
+ value: {
+ getData: ( format: string ) => clipboard.get( format ) ?? '',
+ setData: ( format: string, content: string ) =>
+ clipboard.set( format, content ),
+ },
+ } );
+ fireEvent( canvas, event );
+
+ expect( event.clipboardData!.getData( 'text/plain' ) ).toBe(
+ 'Bound text'
+ );
+ expect( event.clipboardData!.getData( 'text/html' ) ).toBe(
+ 'Bound text'
+ );
+ expect(
+ registry.select( blockEditorStore ).getSelectionStart().attributeKey
+ ).toBe( 'content' );
+ } );
+
+ it.each( [
+ { anchor: { source: 'core/post-meta', args: { key: 'movie_field' } } },
+ { __default: { source: 'testing/custom-source' } },
+ ] )(
+ 'copies the whole block when the selected attribute is not bound (%j)',
+ ( bindings ) => {
+ const block = createBlock( 'core/paragraph', {
+ content: 'Bound text',
+ metadata: { bindings },
+ } );
+ const { copy } = mountClipboard( [ block ] );
+ const copiedContent = copy();
+
+ expect( copiedContent.plainText ).toBe( 'Bound text' );
+ expect( copiedContent.html ).toContain( '