@@ -233,6 +233,31 @@ final class OD_HTML_Tag_Processor extends WP_HTML_Tag_Processor {
233233 */
234234 private $ reached_end_of_document = false ;
235235
236+ /**
237+ * Count for the number of times that the cursor was moved.
238+ *
239+ * The use of this has been replaced with {@see self::$cursor_at_bookmark}.
240+ *
241+ * @since 0.6.0
242+ * @var non-negative-int
243+ * @see self::next_token()
244+ */
245+ private $ cursor_move_count = 0 ;
246+
247+ /**
248+ * The bookmark that the cursor is currently known to be at.
249+ *
250+ * This is used as a backport for the WP 6.8 fix in {@link https://core.trac.wordpress.org/ticket/62085} which
251+ * no-ops seek() calls in which the cursor is already at the provided bookmark.
252+ *
253+ * @since n.e.x.t
254+ * @var string|null
255+ * @see self::next_token()
256+ * @see self::seek()
257+ * @see self::set_bookmark()
258+ */
259+ private $ cursor_at_bookmark = null ;
260+
236261 /**
237262 * Finds the next tag.
238263 *
@@ -310,6 +335,8 @@ public function expects_closer( ?string $tag_name = null ): bool {
310335 public function next_token (): bool {
311336 $ this ->current_stored_xpath = null ; // Clear cache.
312337 $ this ->current_xpath = null ; // Clear cache.
338+ $ this ->cursor_at_bookmark = null ;
339+ ++$ this ->cursor_move_count ;
313340 if ( ! parent ::next_token () ) {
314341 $ this ->open_stack_tags = array ();
315342 $ this ->open_stack_attributes = array ();
@@ -408,6 +435,20 @@ public function next_token(): bool {
408435 return true ;
409436 }
410437
438+ /**
439+ * Gets the number of times the cursor has moved.
440+ *
441+ * @since 0.6.0
442+ * @deprecated n.e.x.t The use of this has been replaced with {@see self::$cursor_at_bookmark}.
443+ * @codeCoverageIgnore
444+ *
445+ * @return non-negative-int Count of times the cursor has moved.
446+ */
447+ public function get_cursor_move_count (): int {
448+ _deprecated_function ( __METHOD__ , 'optimization-detective n.e.x.t ' );
449+ return $ this ->cursor_move_count ;
450+ }
451+
411452 /**
412453 * Updates or creates a new attribute on the currently matched tag with the passed value.
413454 *
@@ -485,8 +526,14 @@ public function get_current_depth(): int {
485526 * @return bool Whether the internal cursor was successfully moved to the bookmark's location.
486527 */
487528 public function seek ( $ bookmark_name ): bool {
529+ // This is only needed prior to WP 6.8 per <https://core.trac.wordpress.org/ticket/62085>.
530+ if ( $ bookmark_name === $ this ->cursor_at_bookmark ) {
531+ return true ;
532+ }
533+
488534 $ result = parent ::seek ( $ bookmark_name );
489535 if ( $ result ) {
536+ $ this ->cursor_at_bookmark = $ bookmark_name ;
490537 $ this ->open_stack_tags = $ this ->bookmarked_open_stacks [ $ bookmark_name ]['tags ' ];
491538 $ this ->open_stack_attributes = $ this ->bookmarked_open_stacks [ $ bookmark_name ]['attributes ' ];
492539 $ this ->open_stack_indices = $ this ->bookmarked_open_stacks [ $ bookmark_name ]['indices ' ];
@@ -506,6 +553,8 @@ public function seek( $bookmark_name ): bool {
506553 public function set_bookmark ( $ name ): bool {
507554 $ result = parent ::set_bookmark ( $ name );
508555 if ( $ result ) {
556+ $ this ->cursor_at_bookmark = $ name ; // Only needed prior to WP 6.8 per <https://core.trac.wordpress.org/ticket/62085>.
557+
509558 $ this ->bookmarked_open_stacks [ $ name ] = array (
510559 'tags ' => $ this ->open_stack_tags ,
511560 'attributes ' => $ this ->open_stack_attributes ,
@@ -534,6 +583,10 @@ public function release_bookmark( $name ): bool {
534583 return false ;
535584 }
536585 unset( $ this ->bookmarked_open_stacks [ $ name ] );
586+ if ( $ this ->cursor_at_bookmark === $ name ) {
587+ // Only needed prior to WP 6.8 per <https://core.trac.wordpress.org/ticket/62085>.
588+ $ this ->cursor_at_bookmark = null ;
589+ }
537590 return parent ::release_bookmark ( $ name );
538591 }
539592
0 commit comments