From 86539d340500c352ce14c026593b31be2185a034 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 11 Jun 2024 15:34:01 +0800 Subject: [PATCH 1/3] Try preventing normal attribute updates when a __default binding exists --- .../block-editor/src/hooks/use-bindings-attributes.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index 334c751bc01b0..32cdd15f19e7d 100644 --- a/packages/block-editor/src/hooks/use-bindings-attributes.js +++ b/packages/block-editor/src/hooks/use-bindings-attributes.js @@ -97,6 +97,8 @@ export const withBlockBindingSupport = createHigherOrderComponent( unlock( select( blocksStore ) ).getAllBlockBindingsSources() ); const { name, clientId, context } = props; + const hasDefaultBinding = + props.attributes.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ]; const bindings = useMemo( () => replacePatternOverrideDefaultBindings( @@ -213,7 +215,10 @@ export const withBlockBindingSupport = createHigherOrderComponent( } } - if ( Object.keys( keptAttributes ).length ) { + if ( + ! hasDefaultBinding && + Object.keys( keptAttributes ).length + ) { setAttributes( keptAttributes ); } } ); @@ -226,6 +231,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( context, setAttributes, sources, + hasDefaultBinding, ] ); From 95999a97132560b801e9e18d28d33a87ff313e4e Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 11 Jun 2024 16:17:59 +0800 Subject: [PATCH 2/3] Add an e2e test --- .../editor/various/pattern-overrides.spec.js | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/test/e2e/specs/editor/various/pattern-overrides.spec.js b/test/e2e/specs/editor/various/pattern-overrides.spec.js index f4648a03efe95..3587296a6b2a0 100644 --- a/test/e2e/specs/editor/various/pattern-overrides.spec.js +++ b/test/e2e/specs/editor/various/pattern-overrides.spec.js @@ -742,6 +742,59 @@ test.describe( 'Pattern Overrides', () => { ).toBeHidden(); } ); + test( 'overridden images should not have unsupported attributes set', async ( { + admin, + requestUtils, + editor, + } ) => { + const imageName = 'Editable image'; + const TEST_IMAGE_FILE_PATH = path.resolve( + __dirname, + '../../../assets/10x10_e2e_test_image_z9T8jK.png' + ); + const { id } = await requestUtils.createBlock( { + title: 'Pattern', + content: ` +
+`, + status: 'publish', + } ); + + await admin.createNewPost(); + + await editor.insertBlock( { + name: 'core/block', + attributes: { ref: id }, + } ); + + const imageBlock = editor.canvas.getByRole( 'document', { + name: 'Block: Image', + } ); + await editor.selectBlocks( imageBlock ); + await imageBlock + .getByTestId( 'form-file-upload-input' ) + .setInputFiles( TEST_IMAGE_FILE_PATH ); + await expect( imageBlock.getByRole( 'img' ) ).toHaveCount( 1 ); + await expect( imageBlock.getByRole( 'img' ) ).toHaveAttribute( + 'src', + /\/wp-content\/uploads\// + ); + + // Because the image is an inner block of a controlled pattern block, + // `getBlocks` has to be called using the pattern block's client id. + const patternBlock = editor.canvas.getByRole( 'document', { + name: 'Block: Pattern', + } ); + const patternClientId = await patternBlock.getAttribute( 'data-block' ); + const patternInnerBlocks = await editor.getBlocks( { + clientId: patternClientId, + } ); + + // Link is an unsupported attribute, so should be undefined, even though + // the image block tries to set its attribute. + expect( patternInnerBlocks[ 0 ].attributes.link ).toBe( undefined ); + } ); + test( 'blocks with the same name should be synced', async ( { page, admin, From f54d88ca25f6c5321e9284da88dad21090493e11 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Wed, 12 Jun 2024 14:55:09 +0800 Subject: [PATCH 3/3] Only skip normal attribute updates for pattern overrides --- .../src/hooks/use-bindings-attributes.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/block-editor/src/hooks/use-bindings-attributes.js b/packages/block-editor/src/hooks/use-bindings-attributes.js index 32cdd15f19e7d..7bd5df05d31eb 100644 --- a/packages/block-editor/src/hooks/use-bindings-attributes.js +++ b/packages/block-editor/src/hooks/use-bindings-attributes.js @@ -97,8 +97,9 @@ export const withBlockBindingSupport = createHigherOrderComponent( unlock( select( blocksStore ) ).getAllBlockBindingsSources() ); const { name, clientId, context } = props; - const hasDefaultBinding = - props.attributes.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ]; + const hasPatternOverridesDefaultBinding = + props.attributes.metadata?.bindings?.[ DEFAULT_ATTRIBUTE ] + ?.source === 'core/pattern-overrides'; const bindings = useMemo( () => replacePatternOverrideDefaultBindings( @@ -215,8 +216,11 @@ export const withBlockBindingSupport = createHigherOrderComponent( } } + // Only apply normal attribute updates to blocks + // that have partial bindings. Currently this is + // only skipped for pattern overrides sources. if ( - ! hasDefaultBinding && + ! hasPatternOverridesDefaultBinding && Object.keys( keptAttributes ).length ) { setAttributes( keptAttributes ); @@ -231,7 +235,7 @@ export const withBlockBindingSupport = createHigherOrderComponent( context, setAttributes, sources, - hasDefaultBinding, + hasPatternOverridesDefaultBinding, ] );