Skip to content

Commit 8cc8eb5

Browse files
committed
HTML API: Stop counting no-op seek operations against the max seek count.
This allows `seek()` to be freely called when the current cursor at the provided bookmark. Props dmsnell, jonsurrell, westonruter. Fixes #62085. git-svn-id: https://develop.svn.wordpress.org/trunk@59812 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 64fd288 commit 8cc8eb5

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

src/wp-includes/html-api/class-wp-html-tag-processor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2551,6 +2551,15 @@ public function seek( $bookmark_name ): bool {
25512551
return false;
25522552
}
25532553

2554+
$existing_bookmark = $this->bookmarks[ $bookmark_name ];
2555+
2556+
if (
2557+
$this->token_starts_at === $existing_bookmark->start &&
2558+
$this->token_length === $existing_bookmark->length
2559+
) {
2560+
return true;
2561+
}
2562+
25542563
if ( ++$this->seek_count > static::MAX_SEEK_OPS ) {
25552564
_doing_it_wrong(
25562565
__METHOD__,

tests/phpunit/tests/html-api/wpHtmlTagProcessor-bookmark.php

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -435,16 +435,49 @@ public function test_limits_the_number_of_bookmarks() {
435435
public function test_limits_the_number_of_seek_calls() {
436436
$processor = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );
437437
$processor->next_tag( 'li' );
438-
$processor->set_bookmark( 'bookmark' );
439-
440-
for ( $i = 0; $i < WP_HTML_Tag_Processor::MAX_SEEK_OPS; $i++ ) {
441-
$this->assertTrue( $processor->seek( 'bookmark' ), 'Could not seek to the "bookmark"' );
438+
$processor->set_bookmark( 'ping' );
439+
$processor->next_tag( 'li' );
440+
$processor->set_bookmark( 'pong' );
441+
442+
for ( $i = 0; $i < WP_HTML_Tag_Processor::MAX_SEEK_OPS; $i += 2 ) {
443+
$this->assertTrue(
444+
$processor->seek( 'ping' ),
445+
'Could not seek to the "ping": check test setup.'
446+
);
447+
448+
$this->assertTrue(
449+
$processor->seek( 'pong' ),
450+
'Could not seek to the "pong": check test setup.'
451+
);
442452
}
443453

444454
$this->setExpectedIncorrectUsage( 'WP_HTML_Tag_Processor::seek' );
445455
$this->assertFalse( $processor->seek( 'bookmark' ), "$i-th seek() to the bookmark succeeded, even though it should exceed the allowed limit" );
446456
}
447457

458+
/**
459+
* @ticket 62085
460+
*
461+
* @covers WP_HTML_Tag_Processor::seek
462+
*/
463+
public function test_skips_counting_noop_seek_calls() {
464+
$processor = new WP_HTML_Tag_Processor( '<ul><li>One</li><li>Two</li><li>Three</li></ul>' );
465+
$processor->next_tag( 'li' );
466+
$processor->set_bookmark( 'here' );
467+
468+
for ( $i = 0; $i < WP_HTML_Tag_Processor::MAX_SEEK_OPS; $i++ ) {
469+
$this->assertTrue(
470+
$processor->seek( 'here' ),
471+
'Could not seek to the "here": check test setup.'
472+
);
473+
}
474+
475+
$this->assertTrue(
476+
$processor->seek( 'here' ),
477+
'Should never fail to seek if the seek is pointing at the current location.'
478+
);
479+
}
480+
448481
/**
449482
* Ensures that it's possible to seek to an earlier location in a document even
450483
* after reaching the end of a document, when most functionality shuts down.

0 commit comments

Comments
 (0)