Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML API: Stop counting noop seek operations against the max seek count. #7399

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__,
Expand Down
41 changes: 37 additions & 4 deletions tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -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( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );
$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' );
Comment on lines +438 to +440
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏓


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( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );
$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.
Expand Down
Loading