Make WordPress Core


Ignore:
Timestamp:
06/25/2026 01:15:16 PM (3 months ago)
Author:
jonsurrell
Message:

HTML API: Move "any other end tag" handling to a separate method.

Extract the _in body_ insertion mode's "any other end tag" steps into their own method so they can be invoked directly, such as from the adoption agency algorithm.

Developed in https://github.com/WordPress/wordpress-develop/pull/12267.

Props jonsurrell, dmsnell.
See #65383.

File:
1 edited

Legend:

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

    r62542 r62563  
    32583258                         * > Any other end tag
    32593259                         */
    3260 
    3261                         /*
    3262                          * Find the corresponding tag opener in the stack of open elements, if
    3263                          * it exists before reaching a special element, which provides a kind
    3264                          * of boundary in the stack. For example, a `</custom-tag>` should not
    3265                          * close anything beyond its containing `P` or `DIV` element.
    3266                          */
    3267                         foreach ( $this->state->stack_of_open_elements->walk_up() as $node ) {
    3268                                 if ( 'html' === $node->namespace && $token_name === $node->node_name ) {
    3269                                         break;
    3270                                 }
    3271 
    3272                                 if ( self::is_special( $node ) ) {
    3273                                         // This is a parse error, ignore the token.
    3274                                         return $this->step();
    3275                                 }
     3260                        return $this->in_body_any_other_end_tag();
     3261                }
     3262
     3263                $this->bail( 'Should not have been able to reach end of IN BODY processing. Check HTML API code.' );
     3264                // This unnecessary return prevents tools from inaccurately reporting type errors.
     3265                return false;
     3266        }
     3267
     3268        /**
     3269         * Applies the "any other end tag" parsing instructions for the IN BODY insertion mode.
     3270         *
     3271         * @since 7.1.0
     3272         * @ignore
     3273         *
     3274         * @throws WP_HTML_Unsupported_Exception When encountering unsupported HTML input.
     3275         *
     3276         * @see https://html.spec.whatwg.org/#parsing-main-inbody
     3277         * @see WP_HTML_Processor::step_in_body
     3278         *
     3279         * @return bool Whether an element was found.
     3280         */
     3281        private function in_body_any_other_end_tag(): bool {
     3282                $token_name = $this->get_token_name();
     3283
     3284                /*
     3285                 * Find the corresponding tag opener in the stack of open elements, if
     3286                 * it exists before reaching a special element, which provides a kind
     3287                 * of boundary in the stack. For example, a `</custom-tag>` should not
     3288                 * close anything beyond its containing `P` or `DIV` element.
     3289                 */
     3290                foreach ( $this->state->stack_of_open_elements->walk_up() as $node ) {
     3291                        if ( 'html' === $node->namespace && $token_name === $node->node_name ) {
     3292                                break;
    32763293                        }
    32773294
    3278                         $this->generate_implied_end_tags( $token_name );
    3279                         if ( $node !== $this->state->stack_of_open_elements->current_node() ) {
    3280                                 // @todo Record parse error: this error doesn't impact parsing.
     3295                        if ( self::is_special( $node ) ) {
     3296                                // This is a parse error, ignore the token.
     3297                                return $this->step();
    32813298                        }
    3282 
    3283                         foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) {
    3284                                 $this->state->stack_of_open_elements->pop();
    3285                                 if ( $node === $item ) {
    3286                                         return true;
    3287                                 }
     3299                }
     3300
     3301                $this->generate_implied_end_tags( $token_name );
     3302                if ( $node !== $this->state->stack_of_open_elements->current_node() ) {
     3303                        // @todo Record parse error: this error doesn't impact parsing.
     3304                }
     3305
     3306                foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) {
     3307                        $this->state->stack_of_open_elements->pop();
     3308                        if ( $node === $item ) {
     3309                                return true;
    32883310                        }
    32893311                }
    32903312
    3291                 $this->bail( 'Should not have been able to reach end of IN BODY processing. Check HTML API code.' );
     3313                $this->bail( 'Should not have been able to reach end of "any other end tag" IN BODY processing. Check HTML API code.' );
    32923314                // This unnecessary return prevents tools from inaccurately reporting type errors.
    32933315                return false;
Note: See TracChangeset for help on using the changeset viewer.