Make WordPress Core


Ignore:
Timestamp:
07/04/2024 11:14:44 PM (10 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.

File:
1 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     *
Note: See TracChangeset for help on using the changeset viewer.