-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Lightbox: Re-use existing Tag Processor instance. #55281
Conversation
ab3c0d5
to
fdb0b2f
Compare
@@ -22,7 +22,7 @@ function render_block_core_image( $attributes, $content, $block ) { | |||
|
|||
$processor = new WP_HTML_Tag_Processor( $content ); | |||
|
|||
if ( ! $processor->next_tag( 'img' ) || null === $processor->get_attribute( 'src' ) ) { | |||
if ( ! $processor->next_tag( 'IMG' ) || null === $processor->get_attribute( 'src' ) ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this isn't as important, but I carried it over from my other patch since I've tried to standardize tag names in the HTML API in uppercase, only because that's the form of the tag name that the API hands out
fdb0b2f
to
51d86dd
Compare
Flaky tests detected in 9d56eb8. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6520770211
|
51d86dd
to
9d56eb8
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general this looks all right and the majority of the refactors make it easier to read. Re-using the Tag Processor obviously makes sense.
However, I'm pretty concerned that the need to manually reset each property after calling seek()
makes it much harder to update this file in the future. If I want to add a $processor->addClass('newClass')
call anywhere in this file in the future, I would also have to find the right place to add $processor->removeClass('newClass')
. This is not an acceptable burden on future developers 😅
I would be much happier to merge this, it if there were a built in-method in the Tag Processor that we could call to reset the internal state and we could use it in this PR, like I mention below in #55281 (comment)
// Only process if there's an IMG element after this FIGURE. It should be inside it. | ||
if ( ! $processor->next_tag( 'IMG' ) ) { | ||
return $block_content; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure about this assumption. Other elements are also valid children of the <figure>
, e.g. there might be a <figcaption>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will still fall on an IMG, but I'm noting that assumes that the image appears later in the document than the FIGURE, and is also assuming that an image is inside the figure.
in the HTML Processor we'd express this in the following way:
if ( ! $processor->next_tag( array( 'breadcrumbs' => array( 'FIGURE', 'IMG' ) ) ) ) {
return $block_content;
}
I can update the comment to make this clearer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
expanded the comment in 0c0f5bc
* | ||
* In order to reuse the existing Tag Processor, changes made before | ||
* need to be undone before setting the new changes here. | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. This is beyond the scope of this PR of course, but are there any plans to add something like a $processor->reset()
method or maybe a $processor->seek( '<tag-name>', $reset = true )
to the Tag Processor so that there's no need to reset each property manually?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think it will make sense to include a $reset_changes
mode to the tag processor because of the complexity and runtime costs that could introduce. I have explored a chunked mode that could be used here instead, but in this particular case I think most solutions will be a bit messy.
your concern is real though, and I share it. it's a bit nasty having to reset. I did run some initial profiling but it's hard to get a good handle on the runtime impact without a more typical representative page, something I didn't have time to do last week.
Companion work in WordPress/wordpress-develop#5428 Resolves #55123 This patch refactors the use of the HTML API in image lightbox rendering. It replaces creating multiple Tag Processor instances with the use of a single instance that rewinds and jumps around to perform multiple jobs on a single instance. Also incorporated is a fix for bailing out when no images are in a given image block's output HTML.
9d56eb8
to
7b3ec58
Compare
Closing this now as I think it does not apply anymore and the PR would require a significant rewrite because of the changes in the Feel free to re-open if you plan to work on it in the future! 🙂 |
Status
Companion work in WordPress/wordpress-develop#5428
Resolves #55123
What?
This patch refactors the use of the HTML API in image lightbox rendering. It replaces creating multiple Tag Processor instances with the use of a single instance that rewinds and jumps around to perform multiple jobs on a single instance.
Also incorporated is a fix for bailing out when no images are in a given image block's output HTML.
Why?
The use of the HTML API in the current Lightbox rendering code is a bit confusing and it's inefficient (pending measurements). Being official code in Core/Gutenberg it sets an example for other developers wanting to learn how to use the HTML API. This refactor exists to ensure that we set a strong example of various uses of the Tag Processor and how to balance performance and usability needs.
How?
Reuses the existing Tag Processor instance and relies on bookmarks and seeking to make successive changes.
Testing Instructions
Review the code changes, test posts with "Expand on click" enabled for an image, for multiple images, for no images. Ensure that the behaviors here and in
trunk
match.