Skip to content

Commit

Permalink
Update to call $this->next_token() in seek() and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmsnell committed Jan 16, 2024
1 parent 3d8b20e commit 4959837
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
17 changes: 17 additions & 0 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,23 @@ public function next_tag( $query = null ) {
return false;
}

/**
* Steps through the HTML document and stop at the next token, if any.
*
* Currently only supports stepping through tags.
*
* @return bool
*/
public function next_token() {
$found_a_token = parent::next_token();

if ( '#tag' === $this->get_token_type() ) {
$this->step( self::REPROCESS_CURRENT_NODE );
}

return $found_a_token;
}

/**
* Indicates if the currently-matched tag matches the given breadcrumbs.
*
Expand Down
4 changes: 2 additions & 2 deletions src/wp-includes/html-api/class-wp-html-tag-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ class WP_HTML_Tag_Processor {
*
* @var string
*/
private $parser_state = self::STATE_READY;
protected $parser_state = self::STATE_READY;

/**
* How many bytes from the original HTML document have been read and parsed.
Expand Down Expand Up @@ -2294,7 +2294,7 @@ public function seek( $bookmark_name ) {

// Point this tag processor before the sought tag opener and consume it.
$this->bytes_already_parsed = $this->bookmarks[ $bookmark_name ]->start;
return $this->next_tag( array( 'tag_closers' => 'visit' ) );
return $this->next_token();
}

/**
Expand Down
32 changes: 16 additions & 16 deletions tests/phpunit/tests/html-api/wpHtmlTagProcessor-token-scanning.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Tests_HtmlApi_WpHtmlProcessor_Token_Scanning extends WP_UnitTestCase {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_completes_empty_document() {
$processor = WP_HTML_Processor::create_fragment( '' );
$processor = new WP_HTML_Tag_Processor( '' );

$this->assertFalse(
$processor->next_token(),
Expand All @@ -40,7 +40,7 @@ public function test_completes_empty_document() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_text_node() {
$processor = WP_HTML_Processor::create_fragment( 'Hello, World!' );
$processor = new WP_HTML_Tag_Processor( 'Hello, World!' );
$processor->next_token();

$this->assertSame(
Expand All @@ -66,7 +66,7 @@ public function test_basic_assertion_text_node() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_element() {
$processor = WP_HTML_Processor::create_fragment( '<div id="test" inert>Hello, World!</div>' );
$processor = new WP_HTML_Tag_Processor( '<div id="test" inert>Hello, World!</div>' );
$processor->next_token();

$this->assertSame(
Expand Down Expand Up @@ -111,7 +111,7 @@ public function test_basic_assertion_element() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_script_element() {
$processor = WP_HTML_Processor::create_fragment( '<script type="module">console.log( "Hello, World!" );</script>' );
$processor = new WP_HTML_Tag_Processor( '<script type="module">console.log( "Hello, World!" );</script>' );
$processor->next_token();

$this->assertSame(
Expand Down Expand Up @@ -151,7 +151,7 @@ public function test_basic_assertion_script_element() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_textarea_element() {
$processor = WP_HTML_Processor::create_fragment(
$processor = new WP_HTML_Tag_Processor(
<<<HTML
<textarea rows=30 cols="80">
Is <HTML> &gt; XHTML?
Expand Down Expand Up @@ -204,7 +204,7 @@ public function test_basic_assertion_textarea_element() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_title_element() {
$processor = WP_HTML_Processor::create_fragment(
$processor = new WP_HTML_Tag_Processor(
<<<HTML
<title class="multi-line-title">
Is <HTML> &gt; XHTML?
Expand Down Expand Up @@ -254,7 +254,7 @@ public function test_basic_assertion_title_element() {
* @param string $tag_name The name of the RAWTEXT tag to test.
*/
public function test_basic_assertion_rawtext_elements( $tag_name ) {
$processor = WP_HTML_Processor::create_fragment(
$processor = new WP_HTML_Tag_Processor(
<<<HTML
<{$tag_name} class="multi-line-title">
Is <HTML> &gt; XHTML?
Expand Down Expand Up @@ -315,7 +315,7 @@ public function data_rawtext_elements() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_cdata_section() {
$processor = WP_HTML_Processor::create_fragment( '<![CDATA[this is a comment]]>' );
$processor = new WP_HTML_Tag_Processor( '<![CDATA[this is a comment]]>' );
$processor->next_token();

$this->assertSame(
Expand Down Expand Up @@ -351,7 +351,7 @@ public function test_basic_assertion_cdata_section() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_abruptly_closed_cdata_section() {
$processor = WP_HTML_Processor::create_fragment( '<![CDATA[this is > a comment]]>' );
$processor = new WP_HTML_Tag_Processor( '<![CDATA[this is > a comment]]>' );
$processor->next_token();

$this->assertSame(
Expand Down Expand Up @@ -401,7 +401,7 @@ public function test_basic_assertion_abruptly_closed_cdata_section() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_processing_instruction() {
$processor = WP_HTML_Processor::create_fragment( '<?wp-bit {"just": "kidding"}?>' );
$processor = new WP_HTML_Tag_Processor( '<?wp-bit {"just": "kidding"}?>' );
$processor->next_token();

$this->assertSame(
Expand Down Expand Up @@ -443,7 +443,7 @@ public function test_basic_assertion_processing_instruction() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_abruptly_closed_processing_instruction() {
$processor = WP_HTML_Processor::create_fragment( '<?version=">=5.3.6"?>' );
$processor = new WP_HTML_Tag_Processor( '<?version=">=5.3.6"?>' );
$processor->next_token();

$this->assertSame(
Expand Down Expand Up @@ -498,7 +498,7 @@ public function test_basic_assertion_abruptly_closed_processing_instruction() {
* @param string $text Contains the appropriate modifiable text.
*/
public function test_basic_assertion_common_comments( $html, $text ) {
$processor = WP_HTML_Processor::create_fragment( $html );
$processor = new WP_HTML_Tag_Processor( $html );
$processor->next_token();

$this->assertSame(
Expand Down Expand Up @@ -557,7 +557,7 @@ public function data_common_comments() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_html_comment() {
$processor = WP_HTML_Processor::create_fragment( '<!-- wp:paragraph -->' );
$processor = new WP_HTML_Tag_Processor( '<!-- wp:paragraph -->' );
$processor->next_token();

$this->assertSame(
Expand Down Expand Up @@ -599,7 +599,7 @@ public function test_basic_assertion_html_comment() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_doctype() {
$processor = WP_HTML_Processor::create_fragment( '<!DOCTYPE html>' );
$processor = new WP_HTML_Tag_Processor( '<!DOCTYPE html>' );
$processor->next_token();

$this->assertSame(
Expand Down Expand Up @@ -641,7 +641,7 @@ public function test_basic_assertion_doctype() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_presumptuous_tag() {
$processor = WP_HTML_Processor::create_fragment( '</>' );
$processor = new WP_HTML_Tag_Processor( '</>' );
$processor->next_token();

$this->assertSame(
Expand Down Expand Up @@ -683,7 +683,7 @@ public function test_basic_assertion_presumptuous_tag() {
* @covers WP_HTML_Tag_Processor::next_token
*/
public function test_basic_assertion_funky_comment() {
$processor = WP_HTML_Processor::create_fragment( '</%url>' );
$processor = new WP_HTML_Tag_Processor( '</%url>' );
$processor->next_token();

$this->assertSame(
Expand Down

0 comments on commit 4959837

Please sign in to comment.