Skip to content

Commit 96975a2

Browse files
committed
Fix test, adjust HTML Processor.
1 parent 4fcdb14 commit 96975a2

2 files changed

Lines changed: 36 additions & 27 deletions

File tree

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

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ public function get_last_error() {
361361
public function next_tag( $query = null ) {
362362
if ( null === $query ) {
363363
while ( $this->step() ) {
364-
if ( ! $this->is_tag_closer() ) {
364+
if ( '#tag' === $this->get_token_type() && ! $this->is_tag_closer() ) {
365365
return true;
366366
}
367367
}
@@ -384,7 +384,7 @@ public function next_tag( $query = null ) {
384384

385385
if ( ! ( array_key_exists( 'breadcrumbs', $query ) && is_array( $query['breadcrumbs'] ) ) ) {
386386
while ( $this->step() ) {
387-
if ( ! $this->is_tag_closer() ) {
387+
if ( '#tag' === $this->get_token_type() && ! $this->is_tag_closer() ) {
388388
return true;
389389
}
390390
}
@@ -428,13 +428,7 @@ public function next_tag( $query = null ) {
428428
* @return bool
429429
*/
430430
public function next_token() {
431-
$found_a_token = parent::next_token();
432-
433-
if ( '#tag' === $this->get_token_type() ) {
434-
$this->step( self::PROCESS_CURRENT_NODE );
435-
}
436-
437-
return $found_a_token;
431+
return $this->step();
438432
}
439433

440434
/**
@@ -535,13 +529,25 @@ public function step( $node_to_process = self::PROCESS_NEXT_NODE ) {
535529
}
536530

537531
if ( self::PROCESS_NEXT_NODE === $node_to_process ) {
538-
while ( parent::next_token() && '#tag' !== $this->get_token_type() ) {
539-
continue;
532+
/*
533+
* Currently tag and text nodes must be processed. Text nodes may
534+
* trigger active format reconstruction, otherwise they could be
535+
* skipped until the HTML Processor supports visiting text nodes.
536+
*/
537+
while ( parent::next_token() ) {
538+
$token_type = $this->get_token_type();
539+
540+
if ( '#tag' === $token_type || '#text' === $token_type ) {
541+
break;
542+
}
540543
}
541544
}
542545

543546
// Finish stepping when there are no more tokens in the document.
544-
if ( null === $this->get_tag() ) {
547+
if (
548+
WP_HTML_Tag_Processor::STATE_COMPLETE === $this->parser_state ||
549+
WP_HTML_Tag_Processor::STATE_INCOMPLETE_INPUT === $this->parser_state
550+
) {
545551
return false;
546552
}
547553

@@ -1194,10 +1200,6 @@ private function step_in_body() {
11941200
* @return string|false Name of created bookmark, or false if unable to create.
11951201
*/
11961202
private function bookmark_tag() {
1197-
if ( ! $this->get_tag() ) {
1198-
return false;
1199-
}
1200-
12011203
if ( ! parent::set_bookmark( ++$this->bookmark_counter ) ) {
12021204
$this->last_error = self::ERROR_EXCEEDED_MAX_BOOKMARKS;
12031205
throw new Exception( 'could not allocate bookmark' );

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

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ class Tests_HtmlApi_WpHtmlSupportRequiredActiveFormatReconstruction extends WP_U
2020
* @ticket 60455
2121
*/
2222
public function test_reconstructs_active_formats_on_text_nodes() {
23-
$processor = WP_HTML_Processor::create_fragment( '<p><b>One<span><p>Two<an-element>' );
23+
$processor = WP_HTML_Processor::create_fragment( '<p><b>One<p><source>Two<source>' );
2424

25-
$processor->next_tag( 'span' );
26-
$this->assertSame(
27-
array( 'HTML', 'BODY', 'P', 'B', 'SPAN' ),
28-
$processor->get_breadcrumbs(),
29-
'Should have identified the stack of open elements for the first text node.'
25+
// The SOURCE element doesn't trigger reconstruction, and this test asserts that.
26+
$this->assertTrue(
27+
$processor->next_tag( 'SOURCE' ),
28+
'Should have found the first custom element.'
3029
);
3130

32-
$this->assertTrue(
33-
$processor->next_tag( 'p' ),
34-
'Should have found second P element.'
31+
$this->assertSame(
32+
array( 'HTML', 'BODY', 'P', 'SOURCE' ),
33+
$processor->get_breadcrumbs(),
34+
'Should have closed formatting element at first P element.'
3535
);
3636

3737
/*
@@ -45,11 +45,18 @@ public function test_reconstructs_active_formats_on_text_nodes() {
4545
* To ensure that this test properly works once that support is expanded,
4646
* it's written to verify both circumstances. Once support is added, this
4747
* can be simplified to only contain the first clause of the conditional.
48+
*
49+
* The use of the SOURCE element is important here because most elements
50+
* will also trigger reconstruction, which would conflate the test results
51+
* with the text node triggering reconstruction. The SOURCE element won't
52+
* do this, making it neutral. Therefore, the implicitly-closed B element
53+
* will only be reconstructed by the text node.
4854
*/
4955

50-
if ( $processor->next_tag( 'AN-ELEMENT' ) ) {
56+
if ( $processor->next_tag( 'SOURCE' ) ) {
57+
echo "\e[32mSOURCE\e[m\n";
5158
$this->assertSame(
52-
array( 'HTML', 'BODY', 'P', 'B', 'AN-ELEMENT' ),
59+
array( 'HTML', 'BODY', 'P', 'B', 'SOURCE' ),
5360
$processor->get_breadcrumbs(),
5461
'Should have reconstructed the implicitly-closed B element.'
5562
);

0 commit comments

Comments
 (0)