Skip to content

Commit c20224b

Browse files
author
dmsnell
committed
HTML API: Only stop on full matches for requested tag name.
An optimization pass on the HTML API left a bug in the `matches()` method, whereby it would falsely detect a tag name match if the found tag were a lexical subset of the requested tag. This occurred because of the use of `substr_compare()` without checking that the outer lengths matched. This patch resolves the bug by adding the length check. Developed in #7189 Discussed in https://core.trac.wordpress.org/ticket/61545 Follow-up to [58613]. Props dmsnell, westonruter. See #61545. git-svn-id: https://develop.svn.wordpress.org/trunk@58893 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 7bf4947 commit c20224b

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4009,7 +4009,13 @@ private function matches(): bool {
40094009
}
40104010

40114011
// Does the tag name match the requested tag name in a case-insensitive manner?
4012-
if ( isset( $this->sought_tag_name ) && 0 !== substr_compare( $this->html, $this->sought_tag_name, $this->tag_name_starts_at, $this->tag_name_length, true ) ) {
4012+
if (
4013+
isset( $this->sought_tag_name ) &&
4014+
(
4015+
strlen( $this->sought_tag_name ) !== $this->tag_name_length ||
4016+
0 !== substr_compare( $this->html, $this->sought_tag_name, $this->tag_name_starts_at, $this->tag_name_length, true )
4017+
)
4018+
) {
40134019
return false;
40144020
}
40154021

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,31 @@ public function test_next_tag_should_return_false_for_a_non_existing_tag() {
601601
$this->assertFalse( $processor->next_tag( 'p' ), 'Querying a non-existing tag did not return false' );
602602
}
603603

604+
/**
605+
* @ticket 61545
606+
*/
607+
public function test_next_tag_should_not_match_on_substrings_of_a_requested_tag() {
608+
$processor = new WP_HTML_Tag_Processor( '<p><pic><picture>' );
609+
610+
$this->assertTrue(
611+
$processor->next_tag( 'PICTURE' ),
612+
'Failed to find a tag when requested: check test setup.'
613+
);
614+
615+
$this->assertSame(
616+
'PICTURE',
617+
$processor->get_tag(),
618+
'Should have skipped past substring tag matches, directly finding the PICTURE element.'
619+
);
620+
621+
$processor = new WP_HTML_Tag_Processor( '<p><pic>' );
622+
623+
$this->assertFalse(
624+
$processor->next_tag( 'PICTURE' ),
625+
"Should not have found any PICTURE element, but found '{$processor->get_token_name()}' instead."
626+
);
627+
}
628+
604629
/**
605630
* @ticket 59209
606631
*

0 commit comments

Comments
 (0)