Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Bindings / Pattern Overrides: Prevent normal attribute updates when a __default binding exists #62471

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 ];
Copy link
Contributor

@SantosGuillamot SantosGuillamot Jun 11, 2024

Choose a reason for hiding this comment

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

Instead of just checking if it has the __default attribute, maybe we can check that it is core/pattern-overrides as we are doing here? If we do that, we might want to change the name of the variable.

If we decide to support __default in any source in the future, I am not sure if skipping setAttributes for any attribute would be okay for all of them. It seems specific to pattern overrides to me.

Additionally, adding the __default property pointing to any other source, currently not supported, would skip setAttributes as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I've made that change 👍

Ultimately I'm not sure if this PR is the right fix, but it's a small change, and I can't think of many other options.

const bindings = useMemo(
() =>
replacePatternOverrideDefaultBindings(
Expand Down Expand Up @@ -213,7 +215,10 @@ export const withBlockBindingSupport = createHigherOrderComponent(
}
}

if ( Object.keys( keptAttributes ).length ) {
if (
! hasDefaultBinding &&
Object.keys( keptAttributes ).length
) {
setAttributes( keptAttributes );
}
} );
Expand All @@ -226,6 +231,7 @@ export const withBlockBindingSupport = createHigherOrderComponent(
context,
setAttributes,
sources,
hasDefaultBinding,
]
);

Expand Down
53 changes: 53 additions & 0 deletions test/e2e/specs/editor/various/pattern-overrides.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<!-- wp:image {"metadata":{"name":"${ imageName }","bindings":{"__default":{"source":"core/pattern-overrides"}}}} -->
<figure class="wp-block-image"><img alt=""/></figure>
<!-- /wp:image -->`,
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,
Expand Down
Loading