Skip to content

Commit c4fb5ff

Browse files
committed
Update to call $this->next_token() in seek() and fix tests
1 parent 730cdc3 commit c4fb5ff

3 files changed

Lines changed: 35 additions & 18 deletions

File tree

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,23 @@ public function next_tag( $query = null ) {
413413
return false;
414414
}
415415

416+
/**
417+
* Steps through the HTML document and stop at the next token, if any.
418+
*
419+
* Currently only supports stepping through tags.
420+
*
421+
* @return bool
422+
*/
423+
public function next_token() {
424+
$found_a_token = parent::next_token();
425+
426+
if ( '#tag' === $this->get_token_type() ) {
427+
$this->step( self::REPROCESS_CURRENT_NODE );
428+
}
429+
430+
return $found_a_token;
431+
}
432+
416433
/**
417434
* Indicates if the currently-matched tag matches the given breadcrumbs.
418435
*

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
@@ -515,7 +515,7 @@ class WP_HTML_Tag_Processor {
515515
*
516516
* @var string
517517
*/
518-
private $parser_state = self::STATE_READY;
518+
protected $parser_state = self::STATE_READY;
519519

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

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

23002300
/**

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

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class Tests_HtmlApi_WpHtmlProcessor_Token_Scanning extends WP_UnitTestCase {
2222
* @covers WP_HTML_Tag_Processor::next_token
2323
*/
2424
public function test_completes_empty_document() {
25-
$processor = WP_HTML_Processor::create_fragment( '' );
25+
$processor = new WP_HTML_Tag_Processor( '' );
2626

2727
$this->assertFalse(
2828
$processor->next_token(),
@@ -40,7 +40,7 @@ public function test_completes_empty_document() {
4040
* @covers WP_HTML_Tag_Processor::next_token
4141
*/
4242
public function test_basic_assertion_text_node() {
43-
$processor = WP_HTML_Processor::create_fragment( 'Hello, World!' );
43+
$processor = new WP_HTML_Tag_Processor( 'Hello, World!' );
4444
$processor->next_token();
4545

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

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

117117
$this->assertSame(
@@ -151,7 +151,7 @@ public function test_basic_assertion_script_element() {
151151
* @covers WP_HTML_Tag_Processor::next_token
152152
*/
153153
public function test_basic_assertion_textarea_element() {
154-
$processor = WP_HTML_Processor::create_fragment(
154+
$processor = new WP_HTML_Tag_Processor(
155155
<<<HTML
156156
<textarea rows=30 cols="80">
157157
Is <HTML> &gt; XHTML?
@@ -204,7 +204,7 @@ public function test_basic_assertion_textarea_element() {
204204
* @covers WP_HTML_Tag_Processor::next_token
205205
*/
206206
public function test_basic_assertion_title_element() {
207-
$processor = WP_HTML_Processor::create_fragment(
207+
$processor = new WP_HTML_Tag_Processor(
208208
<<<HTML
209209
<title class="multi-line-title">
210210
Is <HTML> &gt; XHTML?
@@ -254,7 +254,7 @@ public function test_basic_assertion_title_element() {
254254
* @param string $tag_name The name of the RAWTEXT tag to test.
255255
*/
256256
public function test_basic_assertion_rawtext_elements( $tag_name ) {
257-
$processor = WP_HTML_Processor::create_fragment(
257+
$processor = new WP_HTML_Tag_Processor(
258258
<<<HTML
259259
<{$tag_name} class="multi-line-title">
260260
Is <HTML> &gt; XHTML?
@@ -315,7 +315,7 @@ public function data_rawtext_elements() {
315315
* @covers WP_HTML_Tag_Processor::next_token
316316
*/
317317
public function test_basic_assertion_cdata_section() {
318-
$processor = WP_HTML_Processor::create_fragment( '<![CDATA[this is a comment]]>' );
318+
$processor = new WP_HTML_Tag_Processor( '<![CDATA[this is a comment]]>' );
319319
$processor->next_token();
320320

321321
$this->assertSame(
@@ -351,7 +351,7 @@ public function test_basic_assertion_cdata_section() {
351351
* @covers WP_HTML_Tag_Processor::next_token
352352
*/
353353
public function test_basic_assertion_abruptly_closed_cdata_section() {
354-
$processor = WP_HTML_Processor::create_fragment( '<![CDATA[this is > a comment]]>' );
354+
$processor = new WP_HTML_Tag_Processor( '<![CDATA[this is > a comment]]>' );
355355
$processor->next_token();
356356

357357
$this->assertSame(
@@ -401,7 +401,7 @@ public function test_basic_assertion_abruptly_closed_cdata_section() {
401401
* @covers WP_HTML_Tag_Processor::next_token
402402
*/
403403
public function test_basic_assertion_processing_instruction() {
404-
$processor = WP_HTML_Processor::create_fragment( '<?wp-bit {"just": "kidding"}?>' );
404+
$processor = new WP_HTML_Tag_Processor( '<?wp-bit {"just": "kidding"}?>' );
405405
$processor->next_token();
406406

407407
$this->assertSame(
@@ -443,7 +443,7 @@ public function test_basic_assertion_processing_instruction() {
443443
* @covers WP_HTML_Tag_Processor::next_token
444444
*/
445445
public function test_basic_assertion_abruptly_closed_processing_instruction() {
446-
$processor = WP_HTML_Processor::create_fragment( '<?version=">=5.3.6"?>' );
446+
$processor = new WP_HTML_Tag_Processor( '<?version=">=5.3.6"?>' );
447447
$processor->next_token();
448448

449449
$this->assertSame(
@@ -498,7 +498,7 @@ public function test_basic_assertion_abruptly_closed_processing_instruction() {
498498
* @param string $text Contains the appropriate modifiable text.
499499
*/
500500
public function test_basic_assertion_common_comments( $html, $text ) {
501-
$processor = WP_HTML_Processor::create_fragment( $html );
501+
$processor = new WP_HTML_Tag_Processor( $html );
502502
$processor->next_token();
503503

504504
$this->assertSame(
@@ -557,7 +557,7 @@ public function data_common_comments() {
557557
* @covers WP_HTML_Tag_Processor::next_token
558558
*/
559559
public function test_basic_assertion_html_comment() {
560-
$processor = WP_HTML_Processor::create_fragment( '<!-- wp:paragraph -->' );
560+
$processor = new WP_HTML_Tag_Processor( '<!-- wp:paragraph -->' );
561561
$processor->next_token();
562562

563563
$this->assertSame(
@@ -599,7 +599,7 @@ public function test_basic_assertion_html_comment() {
599599
* @covers WP_HTML_Tag_Processor::next_token
600600
*/
601601
public function test_basic_assertion_doctype() {
602-
$processor = WP_HTML_Processor::create_fragment( '<!DOCTYPE html>' );
602+
$processor = new WP_HTML_Tag_Processor( '<!DOCTYPE html>' );
603603
$processor->next_token();
604604

605605
$this->assertSame(
@@ -641,7 +641,7 @@ public function test_basic_assertion_doctype() {
641641
* @covers WP_HTML_Tag_Processor::next_token
642642
*/
643643
public function test_basic_assertion_presumptuous_tag() {
644-
$processor = WP_HTML_Processor::create_fragment( '</>' );
644+
$processor = new WP_HTML_Tag_Processor( '</>' );
645645
$processor->next_token();
646646

647647
$this->assertSame(
@@ -683,7 +683,7 @@ public function test_basic_assertion_presumptuous_tag() {
683683
* @covers WP_HTML_Tag_Processor::next_token
684684
*/
685685
public function test_basic_assertion_funky_comment() {
686-
$processor = WP_HTML_Processor::create_fragment( '</%url>' );
686+
$processor = new WP_HTML_Tag_Processor( '</%url>' );
687687
$processor->next_token();
688688

689689
$this->assertSame(

0 commit comments

Comments
 (0)