HTML API: Fix finding RCData and Script tag closers.

Fixes finding the following tag closers `</script>`, `</textarea>`, and `</title>` in `WP_HTML_Tag_Processor`.

Follow-up to [55407], [55203].

Props zieladam, dmsnell, hellofromTonya.
Fixes #57852.
See #57575.

git-svn-id: https://develop.svn.wordpress.org/trunk@55469 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork
2023-03-06 18:53:38 +00:00
parent 65c7df0344
commit 455b1e857e
2 changed files with 47 additions and 5 deletions

View File

@@ -775,7 +775,8 @@ class WP_HTML_Tag_Processor {
return false;
}
$at += 2;
$closer_potentially_starts_at = $at;
$at += 2;
/*
* Find a case-insensitive match to the tag name.
@@ -818,7 +819,7 @@ class WP_HTML_Tag_Processor {
}
if ( '>' === $html[ $at ] || '/' === $html[ $at ] ) {
++$this->bytes_already_parsed;
$this->bytes_already_parsed = $closer_potentially_starts_at;
return true;
}
}
@@ -887,7 +888,8 @@ class WP_HTML_Tag_Processor {
}
if ( '/' === $html[ $at ] ) {
$is_closing = true;
$closer_potentially_starts_at = $at - 1;
$is_closing = true;
++$at;
} else {
$is_closing = false;
@@ -938,7 +940,7 @@ class WP_HTML_Tag_Processor {
}
if ( $is_closing ) {
$this->bytes_already_parsed = $at;
$this->bytes_already_parsed = $closer_potentially_starts_at;
if ( $this->bytes_already_parsed >= $doc_length ) {
return false;
}
@@ -948,7 +950,7 @@ class WP_HTML_Tag_Processor {
}
if ( '>' === $html[ $this->bytes_already_parsed ] ) {
++$this->bytes_already_parsed;
$this->bytes_already_parsed = $closer_potentially_starts_at;
return true;
}
}

View File

@@ -430,6 +430,7 @@ class Tests_HtmlApi_wpHtmlTagProcessor extends WP_UnitTestCase {
/**
* @ticket 56299
* @ticket 57852
*
* @covers WP_HTML_Tag_Processor::next_tag
* @covers WP_HTML_Tag_Processor::is_tag_closer
@@ -459,6 +460,45 @@ class Tests_HtmlApi_wpHtmlTagProcessor extends WP_UnitTestCase {
'Did not stop at desired tag closer'
);
$this->assertTrue( $p->is_tag_closer(), 'Indicated a tag closer is a tag opener' );
$p = new WP_HTML_Tag_Processor( '<div>' );
$this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), "Did not find a tag opener when tag_closers was set to 'visit'" );
$this->assertFalse( $p->next_tag( array( 'tag_closers' => 'visit' ) ), "Found a closer where there wasn't one" );
}
/**
* @ticket 57852
*
* @covers WP_HTML_Tag_Processor::next_tag
* @covers WP_HTML_Tag_Processor::is_tag_closer
*/
public function test_next_tag_should_stop_on_rcdata_and_script_tag_closers_when_requested() {
$p = new WP_HTML_Tag_Processor( '<script>abc</script>' );
$p->next_tag();
$this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </script> tag closer' );
$this->assertTrue( $p->is_tag_closer(), 'Indicated a <script> tag opener is a tag closer' );
$p = new WP_HTML_Tag_Processor( 'abc</script>' );
$this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </script> tag closer when there was no tag opener' );
$p = new WP_HTML_Tag_Processor( '<textarea>abc</textarea>' );
$p->next_tag();
$this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </textarea> tag closer' );
$this->assertTrue( $p->is_tag_closer(), 'Indicated a <textarea> tag opener is a tag closer' );
$p = new WP_HTML_Tag_Processor( 'abc</textarea>' );
$this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </textarea> tag closer when there was no tag opener' );
$p = new WP_HTML_Tag_Processor( '<title>abc</title>' );
$p->next_tag();
$this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </title> tag closer' );
$this->assertTrue( $p->is_tag_closer(), 'Indicated a <title> tag opener is a tag closer' );
$p = new WP_HTML_Tag_Processor( 'abc</title>' );
$this->assertTrue( $p->next_tag( array( 'tag_closers' => 'visit' ) ), 'Did not find the </title> tag closer when there was no tag opener' );
}
/**