Skip to content

Commit

Permalink
Block Bindings: Fix empty strings placeholders in post meta bindings (#…
Browse files Browse the repository at this point in the history
…65089)

* Contemplate empty strings as truthy

* Add e2e test for empty values

Co-authored-by: zaguiini <zaguiini@git.wordpress.org>
Co-authored-by: SantosGuillamot <santosguillamot@git.wordpress.org>
  • Loading branch information
3 people authored Sep 5, 2024
1 parent f4cad96 commit f7fd368
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
10 changes: 10 additions & 0 deletions packages/e2e-tests/plugins/block-bindings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion packages/editor/src/bindings/post-meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
},
Expand Down
32 changes: 32 additions & 0 deletions test/e2e/specs/editor/various/block-bindings.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
} ) => {
Expand Down

0 comments on commit f7fd368

Please sign in to comment.