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: Simplify the HTML replacement logic until the HTML API is ready #61236

Merged

Conversation

SantosGuillamot
Copy link
Contributor

What?

Change the block bindings HTML replacement logic to use a hardcoded list of selectors based on the block and a regex to substitute the inner content.

Why?

The existing logic is too complex because the HTML API doesn't support CSS selectors yet and it doesn't have an official way to modify the inner HTML. This makes it difficult to understand, iterate on it and add new functionalities.

As it will be solved in the future once the HTML API is ready, I believe it is better to keep a simple hardcoded version that is easier to understand and iterate on.

How?

  • Change the selector based on the supported blocks.
  • Using a regex, replace the inner content.

Testing Instructions

  1. Register a custom field in your site to test it. You can use a code snippet like this:
register_meta(
	'post',
	'text_custom_field',
	array(
		'show_in_rest' => true,
		'single'       => true,
		'type'         => 'string',
		'default'	   => 'https://wpmovies.dev/wp-content/uploads/2023/04/goncharov-poster-original-1-682x1024.jpeg',
	)
);
  1. Go to a page and insert a paragraph block.
  2. Go to the Code editor and connect the paragraph to the custom field by adding the metadata bindings property. It should look something like this:
<!-- wp:paragraph {"metadata":{"bindings":{"content":{"source":"core/post-meta","args":{"key":"text_custom_field"}}}}} -->
<p>Original content</p>
<!-- /wp:paragraph -->
  1. Save the page and check that in the frontend, the text defined in the custom field is used.
  2. Repeat the process for heading, and button blocks.

Copy link

github-actions bot commented Apr 30, 2024

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: SantosGuillamot <santosguillamot@git.wordpress.org>
Co-authored-by: talldan <talldanwp@git.wordpress.org>
Co-authored-by: cbravobernal <cbravobernal@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copy link

Flaky tests detected in a2d9f08.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/8894082376
📝 Reported issues:

$block_reader->release_bookmark( 'iterate-selectors' );
return $block_content;
if ( 'core/button' === $block_name && 'text' === $attribute_name ) {
$selector = 'a';
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a button block can also be a <button> element depending on the value of the tagName attribute.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right it seems supported according to the attributes. Although I checked how that attribute works and I couldn't find a way to use a <button> instead of a link tag.

I added this commit to cover that possibility.

@SantosGuillamot SantosGuillamot force-pushed the update/simplify-block-bindings-replacement-logic branch from 57c2ce1 to 3473e82 Compare May 1, 2024 10:37
Copy link
Contributor

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

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

Works as expected.

@cbravobernal cbravobernal merged commit 2021ed8 into trunk May 7, 2024
62 checks passed
@cbravobernal cbravobernal deleted the update/simplify-block-bindings-replacement-logic branch May 7, 2024 16:58
@github-actions github-actions bot added this to the Gutenberg 18.4 milestone May 7, 2024
@cbravobernal
Copy link
Contributor

Should we update it in wordpress-develop too?

@SantosGuillamot
Copy link
Contributor Author

Should we update it in wordpress-develop too?

Yes, I plan to create a backport PR, but I wanted to wait to confirm this was the right approach. I'll do that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block bindings [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants