Skip to content

Conversation

@sirreal
Copy link
Member

@sirreal sirreal commented Nov 8, 2024

Trac ticket: https://core.trac.wordpress.org/ticket/62427

The HTML API ::next_tag method accepts a tag_name query. The tag name matching should be case-insensitive. Tag names are generally UPPERCASE in the processors, a lower or mixed case name doesn't make much sense.

// This is OK
$processor = WP_HTML_Processor::create_fragment( '<DIV>' );
assert( $processor->next_tag( 'div' ) );

// This is OK
$processor = WP_HTML_Processor::create_fragment( '<DIV>' );
assert( $processor->next_tag( array( 'breadcrumbs' => array( 'div' ) ) ) );

// This does not work but should
$processor = WP_HTML_Processor::create_fragment( '<DIV>' );
assert( $processor->next_tag( array( 'tag_name' => 'div' ) ) ); // AssertionError!

This is also supported in the tag processor case-insensitively:

$processor = new WP_HTML_Tag_Processor( '<section><DIV>' );
assert( $processor->next_tag( array( 'tag_name' => 'div' ) ) );
$processor->get_tag(); // "DIV"

This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

@github-actions
Copy link

github-actions bot commented Nov 8, 2024

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

@sirreal sirreal changed the title HTML API: next_tag case insensitive tag_name query HTML API: Use case insensitive next_tag tag_name comparison Nov 14, 2024
@sirreal sirreal marked this pull request as ready for review November 14, 2024 14:57
@github-actions
Copy link

github-actions bot commented Nov 14, 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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props jonsurrell, dmsnell.

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

@dmsnell
Copy link
Member

dmsnell commented Nov 14, 2024

Did we originally do this intentionally ASCII case-sensitively, and if so, why?

@sirreal
Copy link
Member Author

sirreal commented Nov 14, 2024

As far as I can tell this was an oversight. I can't think of a reason to match this case-sensitively.

I suspect this was not noticed because the shorthand exists for the next tag with a tag name in the HTML Processor: next_tag( 'div' ).

Under the hood that uses next_tag( array( 'breadcrumbs' => array( 'div' ) ) ):

if ( is_string( $query ) ) {
$query = array( 'breadcrumbs' => array( $query ) );
}

And that is handled correctly by the matches_breadcrumbs function:

$crumb = strtoupper( current( $breadcrumbs ) );

(I had included a change in this patch to change the shorthand implementation to use tag_name instead of breadcrumbs, but removed that as out-of-scope for a bugfix.)

@michalczaplinski michalczaplinski self-requested a review November 14, 2024 23:39
Copy link
Member

@dmsnell dmsnell left a comment

Choose a reason for hiding this comment

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

@sirreal your archaeology fits the picture well. The tag_name query in the array form was added late, and because we were using the breadcrumb trick for the short form, it makes sense to me that this was indeed an oversight. We can’t reuse the breadcrumb trick in the array form since breadcrumbs could also be there.

Glad to have that investigation because it seemed so weird to me that we’d change the rules.

@michalczaplinski
Copy link
Contributor

Committed to core in https://core.trac.wordpress.org/changeset/59422

@sirreal sirreal deleted the html-api/next-tag-case-insensitive-tag-name-match branch November 19, 2024 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants