Skip to content

Commit 83ae6b7

Browse files
HTML API: Fix finding bookmarks set on closing tag WP_HTML_Tag_Processor.
Setting a bookmark on a tag should set its "start" position before the opening "<", e.g.: {{{ <div> Testing a <b>Bookmark</b> ----------------^ }}} The previous calculation assumed this is always one byte to the left from `$tag_name_starts_at`. However, in a closing tag that index points to a solidus symbol "/": {{{ <div> Testing a <b>Bookmark</b> ----------------------------^ }}} The bookmark should therefore start two bytes before the tag name: {{{ <div> Testing a <b>Bookmark</b> ---------------------------^ }}} This changeset achieves this by: * Using the correct starting index for closing tag bookmarks. * Adding `array( 'tag_closers' => 'visit' )` in `WP_HTML_Tag_Processor::seek()`. Follow-up to [55203]. Props zieladam, dmsnell, flixos90. Fixes #57787. See #57575. git-svn-id: https://develop.svn.wordpress.org/trunk@55407 602fd350-edb4-49c9-b593-d223f7449a82
1 parent b075479 commit 83ae6b7

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,7 +722,7 @@ public function set_bookmark( $name ) {
722722
}
723723

724724
$this->bookmarks[ $name ] = new WP_HTML_Span(
725-
$this->tag_name_starts_at - 1,
725+
$this->tag_name_starts_at - ( $this->is_closing_tag ? 2 : 1 ),
726726
$this->tag_ends_at
727727
);
728728

@@ -1504,7 +1504,7 @@ public function seek( $bookmark_name ) {
15041504
$this->bytes_already_parsed = $this->bookmarks[ $bookmark_name ]->start;
15051505
$this->bytes_already_copied = $this->bytes_already_parsed;
15061506
$this->output_buffer = substr( $this->html, 0, $this->bytes_already_copied );
1507-
return $this->next_tag();
1507+
return $this->next_tag( array( 'tag_closers' => 'visit' ) );
15081508
}
15091509

15101510
/**

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,28 @@ public function test_seek() {
6363
);
6464
}
6565

66+
/**
67+
* @ticket 57787
68+
*
69+
* @covers WP_HTML_Tag_Processor::seek
70+
*/
71+
public function test_seeks_to_tag_closer_bookmark() {
72+
$p = new WP_HTML_Tag_Processor( '<div>First</div><span>Second</span>' );
73+
$p->next_tag( array( 'tag_closers' => 'visit' ) );
74+
$p->set_bookmark( 'first' );
75+
$p->next_tag( array( 'tag_closers' => 'visit' ) );
76+
$p->set_bookmark( 'second' );
77+
78+
$p->seek( 'first' );
79+
$p->seek( 'second' );
80+
81+
$this->assertSame(
82+
'DIV',
83+
$p->get_tag(),
84+
'Did not seek to the intended bookmark location'
85+
);
86+
}
87+
6688
/**
6789
* WP_HTML_Tag_Processor used to test for the diffs affecting
6890
* the adjusted bookmark position while simultaneously adjusting

0 commit comments

Comments
 (0)