Skip to content
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
122 changes: 48 additions & 74 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,23 +317,6 @@ class WP_HTML_Tag_Processor {
*/
private $stop_on_tag_closers;

/**
* Holds updated HTML as updates are applied.
*
* Updates and unmodified portions of the input document are
* appended to this value as they are applied. It will hold
* a copy of the updated document up until the point of the
* latest applied update. The fully-updated HTML document
* will comprise this value plus the part of the input document
* which follows that latest update.
*
* @see $bytes_already_copied
*
* @since 6.2.0
* @var string
*/
private $output_buffer = '';

/**
* How many bytes from the original HTML document have been read and parsed.
*
Expand All @@ -346,23 +329,6 @@ class WP_HTML_Tag_Processor {
*/
private $bytes_already_parsed = 0;

/**
* How many bytes from the input HTML document have already been
* copied into the output buffer.
*
* Lexical updates are enqueued and processed in batches. Prior
* to any given update in the input document, there might exist
* a span of HTML unaffected by any changes. This span ought to
* be copied verbatim into the output buffer before applying the
* following update. This value will point to the starting byte
* offset in the input document where that unaffected span of
* HTML starts.
*
* @since 6.2.0
* @var int
*/
private $bytes_already_copied = 0;

/**
* Byte offset in input document where current tag name starts.
*
Expand Down Expand Up @@ -1303,8 +1269,7 @@ private function skip_whitespace() {
* @return void
*/
private function after_tag() {
$this->class_name_updates_to_attributes_updates();
$this->apply_attributes_updates();
$this->get_updated_html();
$this->tag_name_starts_at = null;
$this->tag_name_length = null;
$this->tag_ends_at = null;
Expand Down Expand Up @@ -1460,15 +1425,19 @@ private function class_name_updates_to_attributes_updates() {
* Applies attribute updates to HTML document.
*
* @since 6.2.0
* @since 6.2.1 Accumulates shift for internal cursor and passed pointer.
* @since 6.3.0 Invalidate any bookmarks whose targets are overwritten.
*
* @return void
* @param int $shift_this_point Accumulate and return shift for this position.
* @return int How many bytes the given pointer moved in response to the updates.
*/
private function apply_attributes_updates() {
private function apply_attributes_updates( $shift_this_point = 0 ) {
if ( ! count( $this->lexical_updates ) ) {
return;
return 0;
}

$accumulated_shift_for_given_point = 0;

/*
* Attribute updates can be enqueued in any order but updates
* to the document must occur in lexical order; that is, each
Expand All @@ -1481,12 +1450,28 @@ private function apply_attributes_updates() {
*/
usort( $this->lexical_updates, array( self::class, 'sort_start_ascending' ) );

$bytes_already_copied = 0;
$output_buffer = '';
foreach ( $this->lexical_updates as $diff ) {
$this->output_buffer .= substr( $this->html, $this->bytes_already_copied, $diff->start - $this->bytes_already_copied );
$this->output_buffer .= $diff->text;
$this->bytes_already_copied = $diff->end;
$shift = strlen( $diff->text ) - ( $diff->end - $diff->start );

// Adjust the cursor position by however much an update affects it.
if ( $diff->start <= $this->bytes_already_parsed ) {
$this->bytes_already_parsed += $shift;
}

// Accumulate shift of the given pointer within this function call.
if ( $diff->start <= $shift_this_point ) {
$accumulated_shift_for_given_point += $shift;
}

$output_buffer .= substr( $this->html, $bytes_already_copied, $diff->start - $bytes_already_copied );
$output_buffer .= $diff->text;
$bytes_already_copied = $diff->end;
}

$this->html = $output_buffer . substr( $this->html, $bytes_already_copied );

/*
* Adjust bookmark locations to account for how the text
* replacements adjust offsets in the input document.
Expand Down Expand Up @@ -1527,6 +1512,8 @@ private function apply_attributes_updates() {
}

$this->lexical_updates = array();

return $accumulated_shift_for_given_point;
}

/**
Expand Down Expand Up @@ -1576,8 +1563,6 @@ public function seek( $bookmark_name ) {

// Point this tag processor before the sought tag opener and consume it.
$this->bytes_already_parsed = $this->bookmarks[ $bookmark_name ]->start;
$this->bytes_already_copied = $this->bytes_already_parsed;
$this->output_buffer = substr( $this->html, 0, $this->bytes_already_copied );
return $this->next_tag( array( 'tag_closers' => 'visit' ) );
}

Expand Down Expand Up @@ -2122,6 +2107,7 @@ public function __toString() {
* Returns the string representation of the HTML Tag Processor.
*
* @since 6.2.0
* @since 6.2.1 Shifts the internal cursor corresponding to the applied updates.
*
* @return string The processed HTML.
*/
Expand All @@ -2132,46 +2118,24 @@ public function get_updated_html() {
* When there is nothing more to update and nothing has already been
* updated, return the original document and avoid a string copy.
*/
if ( $requires_no_updating && 0 === $this->bytes_already_copied ) {
if ( $requires_no_updating ) {
return $this->html;
}

/*
* If there are no updates left to apply, but some have already
* been applied, then finish by copying the rest of the input
* to the end of the updated document and return.
* Keep track of the position right before the current tag. This will
* be necessary for reparsing the current tag after updating the HTML.
*/
if ( $requires_no_updating && $this->bytes_already_copied > 0 ) {
$this->html = $this->output_buffer . substr( $this->html, $this->bytes_already_copied );
$this->bytes_already_copied = strlen( $this->output_buffer );
return $this->output_buffer . substr( $this->html, $this->bytes_already_copied );
}

// Apply the updates, rewind to before the current tag, and reparse the attributes.
$content_up_to_opened_tag_name = $this->output_buffer . substr(
$this->html,
$this->bytes_already_copied,
$this->tag_name_starts_at + $this->tag_name_length - $this->bytes_already_copied
);
$before_current_tag = $this->tag_name_starts_at - 1;

/*
* 1. Apply the edits by flushing them to the output buffer and updating the copied byte count.
*
* Note: `apply_attributes_updates()` modifies `$this->output_buffer`.
* 1. Apply the enqueued edits and update all the pointers to reflect those changes.
*/
$this->class_name_updates_to_attributes_updates();
$this->apply_attributes_updates();
$before_current_tag += $this->apply_attributes_updates( $before_current_tag );

/*
* 2. Replace the original HTML with the now-updated HTML so that it's possible to
* seek to a previous location and have a consistent view of the updated document.
*/
$this->html = $this->output_buffer . substr( $this->html, $this->bytes_already_copied );
$this->output_buffer = $content_up_to_opened_tag_name;
$this->bytes_already_copied = strlen( $this->output_buffer );

/*
* 3. Point this tag processor at the original tag opener and consume it
* 2. Rewind to before the current tag and reparse to get updated attributes.
*
* At this point the internal cursor points to the end of the tag name.
* Rewind before the tag name starts so that it's as if the cursor didn't
Expand All @@ -2183,9 +2147,19 @@ public function get_updated_html() {
* ^ | back up by the length of the tag name plus the opening <
* \<-/ back up by strlen("em") + 1 ==> 3
*/
$this->bytes_already_parsed = strlen( $content_up_to_opened_tag_name ) - $this->tag_name_length - 1;

// Store existing state so it can be restored after reparsing.
$previous_parsed_byte_count = $this->bytes_already_parsed;
$previous_query = $this->last_query;

// Reparse attributes.
$this->bytes_already_parsed = $before_current_tag;
$this->next_tag();

// Restore previous state.
$this->bytes_already_parsed = $previous_parsed_byte_count;
$this->parse_query( $previous_query );

return $this->html;
}

Expand Down
36 changes: 31 additions & 5 deletions tests/phpunit/tests/html-api/wpHtmlTagProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,32 @@ public function test_next_tag_should_stop_on_rcdata_and_script_tag_closers_when_
$this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </title> tag closer when there was no tag opener' );
}

/**
* Verifies that updates to a document before calls to `get_updated_html()` don't
* lead to the Tag Processor jumping to the wrong tag after the updates.
*
* @ticket 58179
*
* @covers WP_HTML_Tag_Processor::get_updated_html
*/
public function test_internal_pointer_returns_to_original_spot_after_inserting_content_before_cursor() {
$tags = new WP_HTML_Tag_Processor( '<div>outside</div><section><div><img>inside</div></section>' );

$tags->next_tag();
$tags->add_class( 'foo' );
$tags->next_tag( 'section' );

// Return to this spot after moving ahead.
$tags->set_bookmark( 'here' );

// Move ahead.
$tags->next_tag( 'img' );
$tags->seek( 'here' );
$this->assertSame( '<div class="foo">outside</div><section><div><img>inside</div></section>', $tags->get_updated_html() );
$this->assertSame( 'SECTION', $tags->get_tag() );
$this->assertFalse( $tags->is_tag_closer() );
}

/**
* @ticket 56299
*
Expand Down Expand Up @@ -1522,7 +1548,7 @@ public function test_advanced_use_case() {
HTML;

$p = new WP_HTML_Tag_Processor( $input );
$this->assertTrue( $p->next_tag( 'div' ), 'Querying an existing tag did not return true' );
$this->assertTrue( $p->next_tag( 'div' ), 'Did not find first DIV tag in input.' );
$p->set_attribute( 'data-details', '{ "key": "value" }' );
$p->add_class( 'is-processed' );
$this->assertTrue(
Expand All @@ -1532,7 +1558,7 @@ public function test_advanced_use_case() {
'class_name' => 'BtnGroup',
)
),
'Querying an existing tag did not return true'
'Did not find the first BtnGroup DIV tag'
);
$p->remove_class( 'BtnGroup' );
$p->add_class( 'button-group' );
Expand All @@ -1544,7 +1570,7 @@ public function test_advanced_use_case() {
'class_name' => 'BtnGroup',
)
),
'Querying an existing tag did not return true'
'Did not find the second BtnGroup DIV tag'
);
$p->remove_class( 'BtnGroup' );
$p->add_class( 'button-group' );
Expand All @@ -1557,10 +1583,10 @@ public function test_advanced_use_case() {
'match_offset' => 3,
)
),
'Querying an existing tag did not return true'
'Did not find third BUTTON tag with "btn" CSS class'
);
$p->remove_attribute( 'class' );
$this->assertFalse( $p->next_tag( 'non-existent' ), 'Querying a non-existing tag did not return false' );
$this->assertFalse( $p->next_tag( 'non-existent' ), "Found a {$p->get_tag()} tag when none should have been found." );
$p->set_attribute( 'class', 'test' );
$this->assertSame( $expected_output, $p->get_updated_html(), 'Calling get_updated_html after updating the attributes did not return the expected HTML' );
}
Expand Down