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.

Built from https://develop.svn.wordpress.org/trunk@59285


git-svn-id: http://core.svn.wordpress.org/trunk@58677 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Weston Ruter 2024-10-23 22:05:17 +00:00
parent d885fc7216
commit 204d60314b
2 changed files with 23 additions and 7 deletions

View File

@ -603,6 +603,22 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
return false; return false;
} }
/**
* Finds the next token in the HTML document.
*
* This doesn't currently have a way to represent non-tags and doesn't process
* semantic rules for text nodes. For access to the raw tokens consider using
* WP_HTML_Tag_Processor instead.
*
* @since 6.5.0 Added for internal support; do not use.
* @since 6.7.1 Refactored so subclasses may extend.
*
* @return bool Whether a token was parsed.
*/
public function next_token(): bool {
return $this->_next_token();
}
/** /**
* Ensures internal accounting is maintained for HTML semantic rules while * Ensures internal accounting is maintained for HTML semantic rules while
* the underlying Tag Processor class is seeking to a bookmark. * the underlying Tag Processor class is seeking to a bookmark.
@ -611,13 +627,13 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
* semantic rules for text nodes. For access to the raw tokens consider using * semantic rules for text nodes. For access to the raw tokens consider using
* WP_HTML_Tag_Processor instead. * WP_HTML_Tag_Processor instead.
* *
* @since 6.5.0 Added for internal support; do not use. * @since 6.7.1 Added for internal support; do not use.
* *
* @access private * @access private
* *
* @return bool * @return bool
*/ */
public function next_token(): bool { private function _next_token(): bool {
$this->current_element = null; $this->current_element = null;
if ( isset( $this->last_error ) ) { if ( isset( $this->last_error ) ) {
@ -635,7 +651,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
* tokens works in the meantime and isn't obviously wrong. * tokens works in the meantime and isn't obviously wrong.
*/ */
if ( empty( $this->element_queue ) && $this->step() ) { if ( empty( $this->element_queue ) && $this->step() ) {
return $this->next_token(); return $this->_next_token();
} }
// Process the next event on the queue. // Process the next event on the queue.
@ -646,7 +662,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
continue; continue;
} }
return empty( $this->element_queue ) ? false : $this->next_token(); return empty( $this->element_queue ) ? false : $this->_next_token();
} }
$is_pop = WP_HTML_Stack_Event::POP === $this->current_element->operation; $is_pop = WP_HTML_Stack_Event::POP === $this->current_element->operation;
@ -657,7 +673,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
* the breadcrumbs. * the breadcrumbs.
*/ */
if ( 'root-node' === $this->current_element->token->bookmark_name ) { if ( 'root-node' === $this->current_element->token->bookmark_name ) {
return $this->next_token(); return $this->_next_token();
} }
// Adjust the breadcrumbs for this event. // Adjust the breadcrumbs for this event.
@ -669,7 +685,7 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
// Avoid sending close events for elements which don't expect a closing. // Avoid sending close events for elements which don't expect a closing.
if ( $is_pop && ! $this->expects_closer( $this->current_element->token ) ) { if ( $is_pop && ! $this->expects_closer( $this->current_element->token ) ) {
return $this->next_token(); return $this->_next_token();
} }
return true; return true;

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.8-alpha-59284'; $wp_version = '6.8-alpha-59285';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.