Make WordPress Core


Ignore:
Timestamp:
10/23/2024 10:03:23 PM (7 months ago)
Author:
westonruter
Message:

HTML API: Fix extensibility of WP_HTML_Processor::next_token().

Break out logic from the next_token() method into a private method which may call itself recursively. This allows for subclasses to override the next_token() method and be assured that each call to next_token() corresponds with the consumption of one single token. This also parallels how WP_HTML_Tag_Processor::next_token() wraps a private base_class_next_token() method.

Props westonruter, jonsurrell.
Fixes #62269.

File:
1 edited

Legend:

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

    r59248 r59285  
    605605
    606606    /**
    607      * Ensures internal accounting is maintained for HTML semantic rules while
    608      * the underlying Tag Processor class is seeking to a bookmark.
     607     * Finds the next token in the HTML document.
    609608     *
    610609     * This doesn't currently have a way to represent non-tags and doesn't process
     
    613612     *
    614613     * @since 6.5.0 Added for internal support; do not use.
     614     * @since 6.7.1 Refactored so subclasses may extend.
     615     *
     616     * @return bool Whether a token was parsed.
     617     */
     618    public function next_token(): bool {
     619        return $this->_next_token();
     620    }
     621
     622    /**
     623     * Ensures internal accounting is maintained for HTML semantic rules while
     624     * the underlying Tag Processor class is seeking to a bookmark.
     625     *
     626     * This doesn't currently have a way to represent non-tags and doesn't process
     627     * semantic rules for text nodes. For access to the raw tokens consider using
     628     * WP_HTML_Tag_Processor instead.
     629     *
     630     * @since 6.7.1 Added for internal support; do not use.
    615631     *
    616632     * @access private
     
    618634     * @return bool
    619635     */
    620     public function next_token(): bool {
     636    private function _next_token(): bool {
    621637        $this->current_element = null;
    622638
     
    636652         */
    637653        if ( empty( $this->element_queue ) && $this->step() ) {
    638             return $this->next_token();
     654            return $this->_next_token();
    639655        }
    640656
     
    647663            }
    648664
    649             return empty( $this->element_queue ) ? false : $this->next_token();
     665            return empty( $this->element_queue ) ? false : $this->_next_token();
    650666        }
    651667
     
    658674         */
    659675        if ( 'root-node' === $this->current_element->token->bookmark_name ) {
    660             return $this->next_token();
     676            return $this->_next_token();
    661677        }
    662678
     
    670686        // Avoid sending close events for elements which don't expect a closing.
    671687        if ( $is_pop && ! $this->expects_closer( $this->current_element->token ) ) {
    672             return $this->next_token();
     688            return $this->_next_token();
    673689        }
    674690
Note: See TracChangeset for help on using the changeset viewer.