Make WordPress Core

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#59400 closed enhancement (fixed)

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

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

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.


3 years 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
3 years 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:


3 years ago
#3

Rebased.

#4 @Bernhard Reiter
3 years ago

  • Keywords commit added
  • Milestone Awaiting Review → 6.4

#5 @Bernhard Reiter
3 years ago

  • Owner set to Bernhard Reiter
  • Resolution → fixed
  • Status new → 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.