Make WordPress Core

Opened 19 months ago

Closed 19 months ago

Last modified 19 months ago

#59400 closed enhancement (fixed)

HTML API: Add matches_breadcrumbs() to HTML Processor for better querying

Reported by: dmsnell's profile dmsnell Owned by: bernhard-reiter's profile Bernhard Reiter
Milestone: 6.4 Priority: normal
Severity: normal Version: 6.4
Component: HTML API Keywords: has-patch has-unit-tests commit
Focuses: Cc:

Description

Inside a next_tag() loop it can be challenging to use breadcrumbs because they are only exposed inside the call to next_tag() via the $query arg.

<?php
while ( $processor->next_tag( array( 'breadcrumbs' => array( 'figure', 'img' ) ) ) ) {
        
}

In this patch a new method, matches_breadcrumbs() is exposed which allows for querying within the next_tag() loop for more complicated queries.

<?php
while ( $processor->next_tag() ) {
        if ($processor->matches_breadcrumbs( array( 'figure', 'img' ) ) ) {
                
        }
}

This method exposes a wildcard * operator to allow matching any HTML tag that the currently-matched tag is a child or descendant of. It also exposes a ** operator to allow matching from zero to any number of non-specific tags in the stack of open elements between the current breadcrumb and the next.

<?php
while ( $processor->next_tag() ) {
        // Detect A descendants of NAV elements.
        if ( $processor->matches_breadcrumbs( array( 'nav', '**', a' ) ) ) {
                
        }
}

Change History (6)

This ticket was mentioned in PR #5243 on WordPress/wordpress-develop by @dmsnell.


19 months ago
#1

Trac ticket: #59400

Inside a next_tag() loop it can be challenging to use breadcrumbs because they are only exposed inside the call to next_tag() via the $query arg.

In this patch a new method, matches_breadcrumbs() is exposed which allows for querying within the next_tag() loop for more complicated queries.

This method exposes a wildcard * operator to allow matching _any HTML tag_ that the currently-matched tag is a child or descendant of.

Trac ticket:

#2 @dmsnell
19 months ago

Probably I won't add the ** because it not only is much more complicated than * but it also opens up the opportunity for some gnarly performance cases, whereas I think it has to incorporate backtracking to make sure it works the way people expect.

@Bernhard Reiter commented on PR #5243:


19 months ago
#3

Rebased.

#4 @Bernhard Reiter
19 months ago

  • Keywords commit added
  • Milestone changed from Awaiting Review to 6.4

#5 @Bernhard Reiter
19 months ago

  • Owner set to Bernhard Reiter
  • Resolution set to fixed
  • Status changed from new to closed

In 56702:

HTML API: Add matches_breadcrumbs() method for better querying.

Inside a next_tag() loop it can be challenging to use breadcrumbs because they are only exposed inside the call to next_tag() via the $query arg.

In this patch a new method, matches_breadcrumbs(), is exposed which allows for querying within the next_tag() loop for more complicated queries.

This method exposes a wildcard * operator to allow matching any HTML tag that the currently-matched tag is a child or descendant of.

Props dmsnell, westonruter, mukesh27.
Fixes #59400.

Note: See TracTickets for help on using tickets.