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 Library: Refactor core blocks to use HTML Tag Processor #43178

Merged
merged 5 commits into from
Feb 21, 2023

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Aug 12, 2022

What?

The full proposal for the new HTM Tag Processor API is available at https://make.wordpress.org/core/2022/08/19/a-new-system-for-simply-and-reliably-updating-html-attributes/.

Why?

This branch presents the usage of the new HTML Tag Processor API introduced by @adamziel and @dmsnell in #42485.

How?

Updated core blocks:

  • Cover
  • Image
  • Site Logo
  • Social Link (code style changes only)

Testing Instructions

All updated core blocks should work as before.

@gziolo gziolo changed the base branch from trunk to add/html-tokenizer-2 August 12, 2022 10:16
@gziolo gziolo added [Package] Block library /packages/block-library [Type] Code Quality Issues or PRs that relate to code quality labels Aug 12, 2022
packages/block-library/src/gallery/index.php Outdated Show resolved Hide resolved
packages/block-library/src/image/index.php Outdated Show resolved Hide resolved
packages/block-library/src/site-logo/index.php Outdated Show resolved Hide resolved
@gziolo gziolo added the [Status] Blocked Used to indicate that a current effort isn't able to move forward label Aug 25, 2022
@gziolo gziolo self-assigned this Sep 9, 2022
Base automatically changed from add/html-tokenizer-2 to trunk September 23, 2022 06:36
@gziolo gziolo removed the [Status] Blocked Used to indicate that a current effort isn't able to move forward label Sep 27, 2022
@gziolo gziolo marked this pull request as ready for review September 27, 2022 08:10
@gziolo gziolo changed the title Block Library: Refactor core blocks to use HTML Walker Block Library: Refactor core blocks to use HTML Tag Processor Sep 27, 2022
@gziolo
Copy link
Member Author

gziolo commented Sep 27, 2022

I rebase this branch with trunk. Refactored code to use the new class name WP_HTML_Tag_Processor. One of the core blocks Post Featured Image got refactored and it no longer is necessary to use Tag Processor there.

I wrapped all values passed to set_attribute with esc_attr to present how this would look in practice if we leave it up to developers.

@adamziel
Copy link
Contributor

adamziel commented Sep 27, 2022

Esc_attr shouldn’t be needed thanks to #44447. Let’s remove it to avoid escaping the value twice.

@gziolo
Copy link
Member Author

gziolo commented Sep 27, 2022

Esc_attr shouldn’t be needed thanks to #44447. Let’s remove it to avoid escaping the value twice.

Nice, it still might be necessary to use some sanitization in some cases so we need to have a good documentation that covers all possible scenarios.

@gziolo
Copy link
Member Author

gziolo commented Sep 28, 2022

@adamziel and @dmsnell, I think this PR depends on how #44447 ends up handling escaping attributes. It would be great if you could bring it to the finish line. 1983b8f removes esc_attr usage, so if we need to add it back, we only need to remove this commit.

);
$processor = new WP_HTML_Tag_Processor( $content );
$processor->next_tag();
$processor->set_attribute( 'style', $styles );
Copy link
Member

Choose a reason for hiding this comment

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

would $tags or $tag_stream be more useful names here? I think we discussed this at one point. seeing $processor here reads to me as if things are more complicated than they are.

$tag_stream = new WP_HTML_Tag_Processor( $content );
$tag_stream->next_tag();
$tag_stream->set_attribute( 'style', $styles );
$content = (string) $tag_stream;

not that I care how people name the instance of this class, but given that it's core code I recognize that we're setting the examples and how we do it will influence how people use it; I want our examples to be super clear. (not saying $tag_stream is universally clearer, just raising the point to discuss)

Copy link
Member Author

Choose a reason for hiding this comment

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

Good thinking. It's one of the reasons I wanted to have this PR opened early on so we can exercise this API in the codebase.

We still have some time to apply changes to the API or how we will promote usage. From the reader's perspective, I might prefer:

$tag_stream = new WP_HTML_Tag_Stream( $content );
$tag_stream->next();
$tag_stream->set_attribute( 'style', $styles );
$content = (string) $tag_stream;

I changed the name of the class to align with the variable. I also renamed next_tag to avoid repeating the word tag twice.

$tags is shorter, so it might be more desirable. It also feels similar to taxonomy tags, but I don't think they will be used too often together.

Copy link
Member

@dmsnell dmsnell Sep 28, 2022

Choose a reason for hiding this comment

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

I also renamed next_tag to avoid repeating the word tag twice.

double-check with @adamziel. that seems like a much bigger change since it impacts the API and not only the application-side. if someone does create $p = new WP_HTML_Tag_Processor(); then they'll have $p->next()

in my original proposal I used ->find() as the query function but @adamziel had feelings about how ->next_tag() communicated forward progression through the document.

I could be fine with next(), given that we now have WP_HTML_Tag_Processor indicating what "next" is, but it does feel slightly sparser in the self-documenting way (and at the same time, repeating "tag" here doesn't feel like any real inconvenience)

Copy link
Contributor

@adamziel adamziel Sep 29, 2022

Choose a reason for hiding this comment

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

It feels weird to say $tag_stream = new WP_HTML_Tag_Processor(); – I think I'd rather say something like $tags = new WP_HTML_Tag_Processor() or $tags = new WP_HTML_Tag_Stream(). The latter doesn't communicate the processing capabilities, though, so I'd go with $tags = new WP_HTML_Tag_Processor().

If we use $tags as the variable name, it makes sense to say ->next(), but if we don't, then I'd rather say ->next_tag(). Since we can only suggest these conventions but not enforce them, I feel like ->next_tag() will be more useful for the larger audience, even if a tiny bit more annoying to type.

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm inclined to leave it as is for now. The following part doesn't look ideal:

$tags->set_attribute( 'foo', 'boo' );
$content = $tags->get_updated_html();

if ( 'home' === $processor->get_attribute( 'rel' ) ) {
$processor->set_attribute( 'aria-label', __( '(Home link, opens in a new tab)' ) );
$processor->set_attribute( 'target', $attributes['linkTarget'] );
}
Copy link
Member

Choose a reason for hiding this comment

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

places like this make me appreciate the new interface.

Copy link
Member Author

Choose a reason for hiding this comment

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

We are in full agreement here 💯

Copy link
Contributor

Choose a reason for hiding this comment

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

💯 indeed

@dmsnell
Copy link
Member

dmsnell commented Feb 1, 2023

If we want these in WordPress 6.2 we should hurry and merge them, otherwise I don't think it matters if we merge sooner or later.

@gziolo gziolo force-pushed the update/html-walker-blocks branch 2 times, most recently from 5597a30 to c3a6012 Compare February 3, 2023 10:16
@gziolo
Copy link
Member Author

gziolo commented Feb 3, 2023

I refreshed this PR to use the latest API. It doesn't matter if we include it in WordPress 6.2. Actually, it's probably better to test it in the plugin first.

Copy link
Contributor

@adamziel adamziel left a comment

Choose a reason for hiding this comment

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

Still looks great to me @gziolo, feel free to merge or postpone as you see fit.

@github-actions
Copy link

Flaky tests detected in 9bb0a03.
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/4231586927
📝 Reported issues:

@gziolo
Copy link
Member Author

gziolo commented Feb 21, 2023

Testing

Cover block

✅ Minimum height style applied correctly

Screenshot 2023-02-21 at 12 17 56

Image block

✅ Aria label and target applied correctly

Screenshot 2023-02-21 at 12 48 51

Social Icon

✅ Still properly handles rel and target attributes

Screenshot 2023-02-21 at 12 53 43

@adamziel
Copy link
Contributor

Still looking good 👍

@gziolo gziolo merged commit 773f9c9 into trunk Feb 21, 2023
@gziolo gziolo deleted the update/html-walker-blocks branch February 21, 2023 11:54
@github-actions github-actions bot added this to the Gutenberg 15.3 milestone Feb 21, 2023
@carolinan
Copy link
Contributor

I'd like to know if hurry was the only reason the regex for removing the link from the site logo was left as is?
That regex was what was flagged as incorrect and used as one of the reasons for including the tag processor in the first place :).
If that is the only reason, that is not a problem! No complaints. I am only asking to learn if there was another reason or if I can continue with replacing the regex.

@gziolo
Copy link
Member Author

gziolo commented Feb 23, 2023

I'd like to know if hurry was the only reason the regex for removing the link from the site logo was left as is?

HTML Tag Processor doesn't support removing HTML tags as of today, it only allows operations on HTML attributes.

@carolinan
Copy link
Contributor

Aha! Thank you @gziolo. We need to wait then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Package] Block library /packages/block-library [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

5 participants