From ad75ad5df1d88c6765af26e2a70c1898ea0f1e07 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Wed, 18 Sep 2024 13:52:22 -0700 Subject: [PATCH 1/2] HTML API: Stop counting noop seek operations against the max seek count. Co-authored-by: Weston Ruter --- src/wp-includes/html-api/class-wp-html-tag-processor.php | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/wp-includes/html-api/class-wp-html-tag-processor.php b/src/wp-includes/html-api/class-wp-html-tag-processor.php index 233d47eb8da95..3e4d0ddd7aa04 100644 --- a/src/wp-includes/html-api/class-wp-html-tag-processor.php +++ b/src/wp-includes/html-api/class-wp-html-tag-processor.php @@ -2548,6 +2548,15 @@ public function seek( $bookmark_name ): bool { return false; } + $existing_bookmark = $this->bookmarks[ $bookmark_name ]; + + if ( + $this->token_starts_at === $existing_bookmark->start && + $this->token_length === $existing_bookmark->length + ) { + return true; + } + if ( ++$this->seek_count > static::MAX_SEEK_OPS ) { _doing_it_wrong( __METHOD__, From a430966d31bde5b5e2a4be32710f04d2d67af862 Mon Sep 17 00:00:00 2001 From: Dennis Snell Date: Thu, 19 Sep 2024 11:41:54 -0700 Subject: [PATCH 2/2] Verify behavior of new seeking shortcut. --- .../html-api/wpHtmlTagProcessor-bookmark.php | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php b/tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php index ad0cf68906245..c5ea4c38f95a3 100644 --- a/tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php +++ b/tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php @@ -435,16 +435,49 @@ public function test_limits_the_number_of_bookmarks() { public function test_limits_the_number_of_seek_calls() { $processor = new WP_HTML_Tag_Processor( '
  • One
  • Two
  • Three
' ); $processor->next_tag( 'li' ); - $processor->set_bookmark( 'bookmark' ); - - for ( $i = 0; $i < WP_HTML_Tag_Processor::MAX_SEEK_OPS; $i++ ) { - $this->assertTrue( $processor->seek( 'bookmark' ), 'Could not seek to the "bookmark"' ); + $processor->set_bookmark( 'ping' ); + $processor->next_tag( 'li' ); + $processor->set_bookmark( 'pong' ); + + for ( $i = 0; $i < WP_HTML_Tag_Processor::MAX_SEEK_OPS; $i += 2 ) { + $this->assertTrue( + $processor->seek( 'ping' ), + 'Could not seek to the "ping": check test setup.' + ); + + $this->assertTrue( + $processor->seek( 'pong' ), + 'Could not seek to the "pong": check test setup.' + ); } $this->setExpectedIncorrectUsage( 'WP_HTML_Tag_Processor::seek' ); $this->assertFalse( $processor->seek( 'bookmark' ), "$i-th seek() to the bookmark succeeded, even though it should exceed the allowed limit" ); } + /** + * @ticket {TICKET_NUMBER} + * + * @covers WP_HTML_Tag_Processor::seek + */ + public function test_skips_counting_noop_seek_calls() { + $processor = new WP_HTML_Tag_Processor( '
  • One
  • Two
  • Three
' ); + $processor->next_tag( 'li' ); + $processor->set_bookmark( 'here' ); + + for ( $i = 0; $i < WP_HTML_Tag_Processor::MAX_SEEK_OPS; $i++ ) { + $this->assertTrue( + $processor->seek( 'here' ), + 'Could not seek to the "here": check test setup.' + ); + } + + $this->assertTrue( + $processor->seek( 'here' ), + 'Should never fail to seek if the seek is pointing at the current location.' + ); + } + /** * Ensures that it's possible to seek to an earlier location in a document even * after reaching the end of a document, when most functionality shuts down.