Make WordPress Core

Changes between Version 2 and Version 3 of Ticket #63020


Ignore:
Timestamp:
02/26/2025 07:48:17 PM (16 months ago)
Author:
westonruter
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #63020 – Description

    v2 v3  
    1010
    1111All this to say, I suggest that in addition to `get_breadcrumbs()` that there be a way to get more information from the open stack of tags, including the attributes for each tag and the node index for each.
     12
     13In other words, it's currently possible to construct an XPath like `/HTML/BODY` as follows:
     14
     15{{{#!php
     16<?php
     17$xpath = array_map(
     18        function ( string $breadcrumb ): string {
     19                return "/$breadcrumb";
     20        },
     21        $processor->get_breadcrumbs()
     22);
     23}}}
     24
     25But I'm proposing something like `get_element_breadcrumbs()` which would return objects for each open tag on the stack instead of just the tag name. So then you could construct a full unambiguous XPath:
     26
     27{{{#!php
     28<?php
     29$xpath = array_map(
     30        function ( WP_Element $breadcrumb ): string {
     31                $expression = '/*';
     32                $expression .= sprintf( '[self::*]', $breadcrumb->get_tag() );
     33                foreach ( array( 'id', 'role', 'class' ) as $attribute_name ) {
     34                        $attribute = $breadcrumb->get_attribute( $attribute_name );
     35                        if ( is_string( $attribute ) ) {
     36                                $expression .= sprintf( '[@%s="%s"]', $breadcrumb->get_tag(), addcslashes( $attribute, '\\"' ) );
     37                                break;
     38                        }
     39                }
     40                return $expression;
     41        },
     42        $processor->get_element_breadcrumbs()
     43);
     44}}}