From f7fd3684f2be3633746153b1301e26e3231b69e1 Mon Sep 17 00:00:00 2001 From: Mario Santos <34552881+SantosGuillamot@users.noreply.github.com> Date: Thu, 5 Sep 2024 16:14:31 +0200 Subject: [PATCH] Block Bindings: Fix empty strings placeholders in post meta bindings (#65089) * Contemplate empty strings as truthy * Add e2e test for empty values Co-authored-by: zaguiini Co-authored-by: SantosGuillamot --- packages/e2e-tests/plugins/block-bindings.php | 10 ++++++ packages/editor/src/bindings/post-meta.js | 2 +- .../editor/various/block-bindings.spec.js | 32 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/packages/e2e-tests/plugins/block-bindings.php b/packages/e2e-tests/plugins/block-bindings.php index b2eb9d797610d..143feb240ac2e 100644 --- a/packages/e2e-tests/plugins/block-bindings.php +++ b/packages/e2e-tests/plugins/block-bindings.php @@ -41,6 +41,16 @@ function gutenberg_test_block_bindings_registration() { 'default' => '#url-custom-field', ) ); + register_meta( + 'post', + 'empty_field', + array( + 'show_in_rest' => true, + 'type' => 'string', + 'single' => true, + 'default' => '', + ) + ); register_meta( 'post', '_protected_field', diff --git a/packages/editor/src/bindings/post-meta.js b/packages/editor/src/bindings/post-meta.js index 4de8396d4c13b..7618ba6c36023 100644 --- a/packages/editor/src/bindings/post-meta.js +++ b/packages/editor/src/bindings/post-meta.js @@ -22,7 +22,7 @@ export default { for ( const [ attributeName, source ] of Object.entries( bindings ) ) { // Use the key if the value is not set. newValues[ attributeName ] = - meta?.[ source.args.key ] || source.args.key; + meta?.[ source.args.key ] ?? source.args.key; } return newValues; }, diff --git a/test/e2e/specs/editor/various/block-bindings.spec.js b/test/e2e/specs/editor/various/block-bindings.spec.js index 2799315a10b22..6e36b6ad5dd33 100644 --- a/test/e2e/specs/editor/various/block-bindings.spec.js +++ b/test/e2e/specs/editor/various/block-bindings.spec.js @@ -1278,6 +1278,38 @@ test.describe( 'Block bindings', () => { ).toHaveText( 'fallback value' ); } ); + test( 'should show the prompt placeholder in field with empty value', async ( { + editor, + } ) => { + await editor.insertBlock( { + name: 'core/paragraph', + attributes: { + content: 'paragraph default content', + metadata: { + bindings: { + content: { + source: 'core/post-meta', + args: { key: 'empty_field' }, + }, + }, + }, + }, + } ); + + const paragraphBlock = editor.canvas.getByRole( 'document', { + // Aria-label is changed for empty paragraphs. + name: 'Add empty_field', + } ); + + await expect( paragraphBlock ).toBeEmpty(); + + const placeholder = paragraphBlock.locator( 'span' ); + await expect( placeholder ).toHaveAttribute( + 'data-rich-text-placeholder', + 'Add empty_field' + ); + } ); + test( 'should not show the value of a protected meta field', async ( { editor, } ) => {