Skip to content

Commit f7fd368

Browse files
Block Bindings: Fix empty strings placeholders in post meta bindings (WordPress#65089)
* Contemplate empty strings as truthy * Add e2e test for empty values Co-authored-by: zaguiini <[email protected]> Co-authored-by: SantosGuillamot <[email protected]>
1 parent f4cad96 commit f7fd368

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

packages/e2e-tests/plugins/block-bindings.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ function gutenberg_test_block_bindings_registration() {
4141
'default' => '#url-custom-field',
4242
)
4343
);
44+
register_meta(
45+
'post',
46+
'empty_field',
47+
array(
48+
'show_in_rest' => true,
49+
'type' => 'string',
50+
'single' => true,
51+
'default' => '',
52+
)
53+
);
4454
register_meta(
4555
'post',
4656
'_protected_field',

packages/editor/src/bindings/post-meta.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export default {
2222
for ( const [ attributeName, source ] of Object.entries( bindings ) ) {
2323
// Use the key if the value is not set.
2424
newValues[ attributeName ] =
25-
meta?.[ source.args.key ] || source.args.key;
25+
meta?.[ source.args.key ] ?? source.args.key;
2626
}
2727
return newValues;
2828
},

test/e2e/specs/editor/various/block-bindings.spec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,6 +1278,38 @@ test.describe( 'Block bindings', () => {
12781278
).toHaveText( 'fallback value' );
12791279
} );
12801280

1281+
test( 'should show the prompt placeholder in field with empty value', async ( {
1282+
editor,
1283+
} ) => {
1284+
await editor.insertBlock( {
1285+
name: 'core/paragraph',
1286+
attributes: {
1287+
content: 'paragraph default content',
1288+
metadata: {
1289+
bindings: {
1290+
content: {
1291+
source: 'core/post-meta',
1292+
args: { key: 'empty_field' },
1293+
},
1294+
},
1295+
},
1296+
},
1297+
} );
1298+
1299+
const paragraphBlock = editor.canvas.getByRole( 'document', {
1300+
// Aria-label is changed for empty paragraphs.
1301+
name: 'Add empty_field',
1302+
} );
1303+
1304+
await expect( paragraphBlock ).toBeEmpty();
1305+
1306+
const placeholder = paragraphBlock.locator( 'span' );
1307+
await expect( placeholder ).toHaveAttribute(
1308+
'data-rich-text-placeholder',
1309+
'Add empty_field'
1310+
);
1311+
} );
1312+
12811313
test( 'should not show the value of a protected meta field', async ( {
12821314
editor,
12831315
} ) => {

0 commit comments

Comments
 (0)