Make WordPress Core

Changeset 58676


Ignore:
Timestamp:
07/04/2024 11:14:44 PM (9 months ago)
Author:
dmsnell
Message:

HTML API: Add current_node_is() helper method to stack of open elements.

As part of work to add more spec support to the HTML API, this new method
will make it easier to implement the logic when in the SELECT and TABLE
insertion modes.

Developed in https://github.com/WordPress/wordpress-develop/pull/6968
Discussed in https://core.trac.wordpress.org/ticket/51576

Props dmsnell, jonsurrell.
See #61576.

Location:
trunk/src/wp-includes/html-api
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/html-api/class-wp-html-open-elements.php

    r58588 r58676  
    146146
    147147    /**
     148     * Indicates if the current node is of a given type or name.
     149     *
     150     * It's possible to pass either a node type or a node name to this function.
     151     * In the case there is no current element it will always return `false`.
     152     *
     153     * Example:
     154     *
     155     *     // Is the current node a text node?
     156     *     $stack->current_node_is( '#text' );
     157     *
     158     *     // Is the current node a DIV element?
     159     *     $stack->current_node_is( 'DIV' );
     160     *
     161     *     // Is the current node any element/tag?
     162     *     $stack->current_node_is( '#tag' );
     163     *
     164     * @see WP_HTML_Tag_Processor::get_token_type
     165     * @see WP_HTML_Tag_Processor::get_token_name
     166     *
     167     * @since 6.7.0
     168     *
     169     * @access private
     170     *
     171     * @param string $identity Check if the current node has this name or type (depending on what is provided).
     172     * @return bool Whether there is a current element that matches the given identity, whether a token name or type.
     173     */
     174    public function current_node_is( string $identity ): bool {
     175        $current_node = end( $this->stack );
     176        if ( false === $current_node ) {
     177            return false;
     178        }
     179
     180        $current_node_name = $current_node->node_name;
     181
     182        return (
     183            $current_node_name === $identity ||
     184            ( '#doctype' === $identity && 'html' === $current_node_name ) ||
     185            ( '#tag' === $identity && ctype_upper( $current_node_name ) )
     186        );
     187    }
     188
     189    /**
    148190     * Returns whether an element is in a specific scope.
    149191     *
  • trunk/src/wp-includes/html-api/class-wp-html-processor.php

    r58656 r58676  
    10301030
    10311031                $this->generate_implied_end_tags();
    1032                 if ( $this->state->stack_of_open_elements->current_node()->node_name !== $token_name ) {
     1032                if ( ! $this->state->stack_of_open_elements->current_node_is( $token_name ) ) {
    10331033                    // @todo Record parse error: this error doesn't impact parsing.
    10341034                }
     
    10951095                $this->generate_implied_end_tags();
    10961096
    1097                 if ( $this->state->stack_of_open_elements->current_node()->node_name !== $token_name ) {
     1097                if ( ! $this->state->stack_of_open_elements->current_node_is( $token_name ) ) {
    10981098                    // @todo Record parse error: this error doesn't impact parsing.
    10991099                }
     
    11211121                    $node_name = $is_li ? 'LI' : $node->node_name;
    11221122                    $this->generate_implied_end_tags( $node_name );
    1123                     if ( $node_name !== $this->state->stack_of_open_elements->current_node()->node_name ) {
     1123                    if ( ! $this->state->stack_of_open_elements->current_node_is( $node_name ) ) {
    11241124                        // @todo Indicate a parse error once it's possible. This error does not impact the logic here.
    11251125                    }
     
    11981198                $this->generate_implied_end_tags( $token_name );
    11991199
    1200                 if ( $token_name !== $this->state->stack_of_open_elements->current_node()->node_name ) {
     1200                if ( ! $this->state->stack_of_open_elements->current_node_is( $token_name ) ) {
    12011201                    // @todo Indicate a parse error once it's possible. This error does not impact the logic here.
    12021202                }
Note: See TracChangeset for help on using the changeset viewer.