Skip to content

Commit

Permalink
Tag Processor: Add bookmark invalidation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Jan 30, 2023
1 parent b1d0a55 commit 6895965
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions lib/experimental/html/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ private function apply_attributes_updates() {
$this->updated_bytes = $diff->end;
}

foreach ( $this->bookmarks as $bookmark ) {
foreach ( $this->bookmarks as $bookmark_name => $bookmark ) {
/*
* As we loop through $this->lexical_updates, we keep comparing
* $bookmark->start and $bookmark->end to $diff->start. We can't
Expand All @@ -1289,24 +1289,37 @@ private function apply_attributes_updates() {
$tail_delta = 0;

foreach ( $this->lexical_updates as $diff ) {
$update_head = $bookmark->start >= $diff->start;
$update_tail = $bookmark->end >= $diff->start;
$bookmark_start_is_after_diff_start = $bookmark->start >= $diff->start;
$bookmark_end_is_after_diff_end = $bookmark->end >= $diff->start;

if ( $bookmark_start_is_after_diff_start ) {
$bookmark_end_is_before_diff_end = $bookmark->end < $diff->end;
if ( $bookmark_end_is_before_diff_end ) {
// The bookmark is fully contained within the diff. We need to invalidate it.
$this->release_bookmark( $bookmark_name );
}
}

if ( ! $update_head && ! $update_tail ) {
if ( ! $bookmark_start_is_after_diff_start && ! $bookmark_end_is_after_diff_end ) {
break;
}

$delta = strlen( $diff->text ) - ( $diff->end - $diff->start );

if ( $update_head ) {
if ( $bookmark_start_is_after_diff_start ) {
$head_delta += $delta;
}

if ( $update_tail ) {
if ( $bookmark_end_is_after_diff_end ) {
$tail_delta += $delta;
}
}

// Did we end up invalidating the bookmark?
if ( ! isset( $this->bookmarks[ $bookmark_name ] ) ) {
continue;
}

$bookmark->start += $head_delta;
$bookmark->end += $tail_delta;
}
Expand Down

0 comments on commit 6895965

Please sign in to comment.