Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML API: Store current token reference in HTML Processor state. #5127

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/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 src/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