@@ -234,6 +234,31 @@ final class OD_HTML_Tag_Processor extends WP_HTML_Tag_Processor {
234234 */
235235 private $ reached_end_of_document = false ;
236236
237+ /**
238+ * Count for the number of times that the cursor was moved.
239+ *
240+ * The use of this has been replaced with {@see self::$cursor_at_bookmark}.
241+ *
242+ * @since 0.6.0
243+ * @var non-negative-int
244+ * @see self::next_token()
245+ */
246+ private $ cursor_move_count = 0 ;
247+
248+ /**
249+ * The bookmark that the cursor is currently known to be at.
250+ *
251+ * This is used as a backport for the WP 6.8 fix in {@link https://core.trac.wordpress.org/ticket/62085} which
252+ * no-ops seek() calls in which the cursor is already at the provided bookmark.
253+ *
254+ * @since n.e.x.t
255+ * @var string|null
256+ * @see self::next_token()
257+ * @see self::seek()
258+ * @see self::set_bookmark()
259+ */
260+ private $ cursor_at_bookmark = null ;
261+
237262 /**
238263 * Finds the next tag.
239264 *
@@ -311,6 +336,8 @@ public function expects_closer( ?string $tag_name = null ): bool {
311336 public function next_token (): bool {
312337 $ this ->current_stored_xpath = null ; // Clear cache.
313338 $ this ->current_xpath = null ; // Clear cache.
339+ $ this ->cursor_at_bookmark = null ;
340+ ++$ this ->cursor_move_count ;
314341 if ( ! parent ::next_token () ) {
315342 $ this ->open_stack_tags = array ();
316343 $ this ->open_stack_attributes = array ();
@@ -409,6 +436,20 @@ public function next_token(): bool {
409436 return true ;
410437 }
411438
439+ /**
440+ * Gets the number of times the cursor has moved.
441+ *
442+ * @since 0.6.0
443+ * @deprecated n.e.x.t The use of this has been replaced with {@see self::$cursor_at_bookmark}.
444+ * @codeCoverageIgnore
445+ *
446+ * @return non-negative-int Count of times the cursor has moved.
447+ */
448+ public function get_cursor_move_count (): int {
449+ _deprecated_function ( __METHOD__ , 'optimization-detective n.e.x.t ' );
450+ return $ this ->cursor_move_count ;
451+ }
452+
412453 /**
413454 * Updates or creates a new attribute on the currently matched tag with the passed value.
414455 *
@@ -486,8 +527,14 @@ public function get_current_depth(): int {
486527 * @return bool Whether the internal cursor was successfully moved to the bookmark's location.
487528 */
488529 public function seek ( $ bookmark_name ): bool {
530+ // This is only needed prior to WP 6.8 per <https://core.trac.wordpress.org/ticket/62085>.
531+ if ( $ bookmark_name === $ this ->cursor_at_bookmark ) {
532+ return true ;
533+ }
534+
489535 $ result = parent ::seek ( $ bookmark_name );
490536 if ( $ result ) {
537+ $ this ->cursor_at_bookmark = $ bookmark_name ;
491538 $ this ->open_stack_tags = $ this ->bookmarked_open_stacks [ $ bookmark_name ]['tags ' ];
492539 $ this ->open_stack_attributes = $ this ->bookmarked_open_stacks [ $ bookmark_name ]['attributes ' ];
493540 $ this ->open_stack_indices = $ this ->bookmarked_open_stacks [ $ bookmark_name ]['indices ' ];
@@ -507,6 +554,8 @@ public function seek( $bookmark_name ): bool {
507554 public function set_bookmark ( $ name ): bool {
508555 $ result = parent ::set_bookmark ( $ name );
509556 if ( $ result ) {
557+ $ this ->cursor_at_bookmark = $ name ; // Only needed prior to WP 6.8 per <https://core.trac.wordpress.org/ticket/62085>.
558+
510559 $ this ->bookmarked_open_stacks [ $ name ] = array (
511560 'tags ' => $ this ->open_stack_tags ,
512561 'attributes ' => $ this ->open_stack_attributes ,
@@ -535,6 +584,10 @@ public function release_bookmark( $name ): bool {
535584 return false ;
536585 }
537586 unset( $ this ->bookmarked_open_stacks [ $ name ] );
587+ if ( $ this ->cursor_at_bookmark === $ name ) {
588+ // Only needed prior to WP 6.8 per <https://core.trac.wordpress.org/ticket/62085>.
589+ $ this ->cursor_at_bookmark = null ;
590+ }
538591 return parent ::release_bookmark ( $ name );
539592 }
540593
0 commit comments