| | 12 | |
| | 13 | In 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 | |
| | 25 | But 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 | }}} |