Skip to content

Commit

Permalink
HTML API: Store current token reference in HTML Processor state.
Browse files Browse the repository at this point in the history
The `$current_token` reference has been stored in the HTML Processor itself, but I suggested to move it into the externalized state so that it can be stored and replaced.

In this patch the reference is moved to that state variable and it should become more possible to save and load state, to resume execution after pausing.

Props dmsnell.
Fixes #59268.
Built from https://develop.svn.wordpress.org/trunk@56558


git-svn-id: http://core.svn.wordpress.org/trunk@56070 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
Bernhard Reiter authored and Bernhard Reiter committed Sep 12, 2023
1 parent b847b9c commit 251a9c7
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
9 changes: 9 additions & 0 deletions wp-includes/html-api/class-wp-html-processor-state.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ class WP_HTML_Processor_State {
*/
public $active_formatting_elements = null;

/**
* Refers to the currently-matched tag, if any.
*
* @since 6.4.0
*
* @var WP_HTML_Token|null
*/
public $current_token = null;

/**
* Tree construction insertion mode.
*
Expand Down
35 changes: 14 additions & 21 deletions wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,15 +164,6 @@ class WP_HTML_Processor extends WP_HTML_Tag_Processor {
*/
private $bookmark_counter = 0;

/**
* Refers to the currently-matched tag, if any.
*
* @since 6.4.0
*
* @var WP_HTML_Token|null
*/
private $current_token = null;

/**
* Stores an explanation for why something failed, if it did.
*
Expand Down Expand Up @@ -451,7 +442,7 @@ public function step( $node_to_process = self::PROCESS_NEXT_NODE ) {
return false;
}

$this->current_token = new WP_HTML_Token(
$this->state->current_token = new WP_HTML_Token(
$this->bookmark_tag(),
$this->get_tag(),
$this->is_tag_closer(),
Expand Down Expand Up @@ -538,7 +529,7 @@ private function step_in_body() {
}

$this->reconstruct_active_formatting_elements();
$this->insert_html_element( $this->current_token );
$this->insert_html_element( $this->state->current_token );
$this->state->frameset_ok = false;

return true;
Expand All @@ -558,7 +549,7 @@ private function step_in_body() {
$this->close_a_p_element();
}

$this->insert_html_element( $this->current_token );
$this->insert_html_element( $this->state->current_token );
return true;

/*
Expand Down Expand Up @@ -590,7 +581,7 @@ private function step_in_body() {
*/
case '-P':
if ( ! $this->state->stack_of_open_elements->has_p_in_button_scope() ) {
$this->insert_html_element( $this->current_token );
$this->insert_html_element( $this->state->current_token );
}

$this->close_a_p_element();
Expand All @@ -612,8 +603,8 @@ private function step_in_body() {
}

$this->reconstruct_active_formatting_elements();
$this->insert_html_element( $this->current_token );
$this->state->active_formatting_elements->push( $this->current_token );
$this->insert_html_element( $this->state->current_token );
$this->state->active_formatting_elements->push( $this->state->current_token );
return true;

/*
Expand All @@ -633,8 +624,8 @@ private function step_in_body() {
case '+TT':
case '+U':
$this->reconstruct_active_formatting_elements();
$this->insert_html_element( $this->current_token );
$this->state->active_formatting_elements->push( $this->current_token );
$this->insert_html_element( $this->state->current_token );
$this->state->active_formatting_elements->push( $this->state->current_token );
return true;

/*
Expand Down Expand Up @@ -662,15 +653,15 @@ private function step_in_body() {
*/
case '+IMG':
$this->reconstruct_active_formatting_elements();
$this->insert_html_element( $this->current_token );
$this->insert_html_element( $this->state->current_token );
return true;

/*
* > Any other start tag
*/
case '+SPAN':
$this->reconstruct_active_formatting_elements();
$this->insert_html_element( $this->current_token );
$this->insert_html_element( $this->state->current_token );
return true;

/*
Expand Down Expand Up @@ -796,15 +787,17 @@ public function release_bookmark( $bookmark_name ) {
*/
public function seek( $bookmark_name ) {
$actual_bookmark_name = "_{$bookmark_name}";
$processor_started_at = $this->current_token ? $this->bookmarks[ $this->current_token->bookmark_name ]->start : 0;
$processor_started_at = $this->state->current_token
? $this->bookmarks[ $this->state->current_token->bookmark_name ]->start
: 0;
$bookmark_starts_at = $this->bookmarks[ $actual_bookmark_name ]->start;
$direction = $bookmark_starts_at > $processor_started_at ? 'forward' : 'backward';

switch ( $direction ) {
case 'forward':
// When moving forwards, re-parse the document until reaching the same location as the original bookmark.
while ( $this->step() ) {
if ( $bookmark_starts_at === $this->bookmarks[ $this->current_token->bookmark_name ]->start ) {
if ( $bookmark_starts_at === $this->bookmarks[ $this->state->current_token->bookmark_name ]->start ) {
return true;
}
}
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.4-alpha-56557';
$wp_version = '6.4-alpha-56558';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit 251a9c7

Please sign in to comment.