Skip to content

Commit 6895965

Browse files
committed
Tag Processor: Add bookmark invalidation logic
1 parent b1d0a55 commit 6895965

1 file changed

Lines changed: 19 additions & 6 deletions

File tree

lib/experimental/html/class-wp-html-tag-processor.php

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,7 @@ private function apply_attributes_updates() {
12781278
$this->updated_bytes = $diff->end;
12791279
}
12801280

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

12911291
foreach ( $this->lexical_updates as $diff ) {
1292-
$update_head = $bookmark->start >= $diff->start;
1293-
$update_tail = $bookmark->end >= $diff->start;
1292+
$bookmark_start_is_after_diff_start = $bookmark->start >= $diff->start;
1293+
$bookmark_end_is_after_diff_end = $bookmark->end >= $diff->start;
1294+
1295+
if ( $bookmark_start_is_after_diff_start ) {
1296+
$bookmark_end_is_before_diff_end = $bookmark->end < $diff->end;
1297+
if ( $bookmark_end_is_before_diff_end ) {
1298+
// The bookmark is fully contained within the diff. We need to invalidate it.
1299+
$this->release_bookmark( $bookmark_name );
1300+
}
1301+
}
12941302

1295-
if ( ! $update_head && ! $update_tail ) {
1303+
if ( ! $bookmark_start_is_after_diff_start && ! $bookmark_end_is_after_diff_end ) {
12961304
break;
12971305
}
12981306

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

1301-
if ( $update_head ) {
1309+
if ( $bookmark_start_is_after_diff_start ) {
13021310
$head_delta += $delta;
13031311
}
13041312

1305-
if ( $update_tail ) {
1313+
if ( $bookmark_end_is_after_diff_end ) {
13061314
$tail_delta += $delta;
13071315
}
13081316
}
13091317

1318+
// Did we end up invalidating the bookmark?
1319+
if ( ! isset( $this->bookmarks[ $bookmark_name ] ) ) {
1320+
continue;
1321+
}
1322+
13101323
$bookmark->start += $head_delta;
13111324
$bookmark->end += $tail_delta;
13121325
}

0 commit comments

Comments
 (0)