Skip to content

Commit cdb218b

Browse files
committed
HTML API: Fix void tag nesting with next_token
When `next_token()` was introduced, it introduced a regression in the HTML Processor whereby void tags remain on the stack of open elements when they shouldn't. This led to invalid values returned from `get_breadcrumbs()`. The reason was that calling `next_token()` works through a different code path than the HTML Processor runs everything else. To solve this, its sub-classed `next_token()` called `step( self::REPROCESS_CURRENT_TOKEN )` so that the proper HTML accounting takes place. Unfortunately that same reprocessing code path skipped the step whereby void and self-closing elements are popped from the stack of open elements. In this patch, that step is run with a third mode for `step()`, which is the new `self::PROCESS_CURRENT_TOKEN`. This mode acts as if `self::PROCESS_NEXT_NODE` were called, except it doesn't advance the parser. Developed in #5975 Discussed in https://core.trac.wordpress.org/ticket/60382 Follow-up to [57348] Props dmsnell, jonsurrell Fixes #60382 git-svn-id: https://develop.svn.wordpress.org/trunk@57507 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4a2aa99 commit cdb218b

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public function next_token() {
431431
$found_a_token = parent::next_token();
432432

433433
if ( '#tag' === $this->get_token_type() ) {
434-
$this->step( self::REPROCESS_CURRENT_NODE );
434+
$this->step( self::PROCESS_CURRENT_NODE );
435435
}
436436

437437
return $found_a_token;
@@ -513,7 +513,7 @@ public function step( $node_to_process = self::PROCESS_NEXT_NODE ) {
513513
return false;
514514
}
515515

516-
if ( self::PROCESS_NEXT_NODE === $node_to_process ) {
516+
if ( self::REPROCESS_CURRENT_NODE !== $node_to_process ) {
517517
/*
518518
* Void elements still hop onto the stack of open elements even though
519519
* there's no corresponding closing tag. This is important for managing
@@ -532,7 +532,9 @@ public function step( $node_to_process = self::PROCESS_NEXT_NODE ) {
532532
if ( $top_node && self::is_void( $top_node->node_name ) ) {
533533
$this->state->stack_of_open_elements->pop();
534534
}
535+
}
535536

537+
if ( self::PROCESS_NEXT_NODE === $node_to_process ) {
536538
while ( parent::next_token() && '#tag' !== $this->get_token_type() ) {
537539
continue;
538540
}
@@ -1781,6 +1783,15 @@ public static function is_void( $tag_name ) {
17811783
*/
17821784
const REPROCESS_CURRENT_NODE = 'reprocess-current-node';
17831785

1786+
/**
1787+
* Indicates that the current HTML token should be processed without advancing the parser.
1788+
*
1789+
* @since 6.5.0
1790+
*
1791+
* @var string
1792+
*/
1793+
const PROCESS_CURRENT_NODE = 'process-current-node';
1794+
17841795
/**
17851796
* Indicates that the parser encountered unsupported markup and has bailed.
17861797
*

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

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,59 @@ public function test_cannot_nest_void_tags( $tag_name ) {
188188
);
189189
}
190190

191+
/**
192+
* Ensure non-nesting tags do not nest when processing tokens.
193+
*
194+
* @ticket 60382
195+
*
196+
* @dataProvider data_void_tags
197+
*
198+
* @param string $tag_name Name of void tag under test.
199+
*/
200+
public function test_cannot_nest_void_tags_next_token( $tag_name ) {
201+
$processor = WP_HTML_Processor::create_fragment( "<{$tag_name}><div>" );
202+
203+
/*
204+
* This HTML represents the same as the following HTML,
205+
* assuming that it were provided `<img>` as the tag:
206+
*
207+
* <html>
208+
* <body>
209+
* <img>
210+
* <div></div>
211+
* </body>
212+
* </html>
213+
*/
214+
215+
$found_tag = $processor->next_token();
216+
217+
if ( WP_HTML_Processor::ERROR_UNSUPPORTED === $processor->get_last_error() ) {
218+
$this->markTestSkipped( "Tag {$tag_name} is not supported." );
219+
}
220+
221+
$this->assertTrue(
222+
$found_tag,
223+
"Could not find first {$tag_name}."
224+
);
225+
226+
$this->assertSame(
227+
array( 'HTML', 'BODY', $tag_name ),
228+
$processor->get_breadcrumbs(),
229+
'Found incorrect nesting of first element.'
230+
);
231+
232+
$this->assertTrue(
233+
$processor->next_token(),
234+
'Should have found the DIV as the second tag.'
235+
);
236+
237+
$this->assertSame(
238+
array( 'HTML', 'BODY', 'DIV' ),
239+
$processor->get_breadcrumbs(),
240+
"DIV should have been a sibling of the {$tag_name}."
241+
);
242+
}
243+
191244
/**
192245
* Data provider.
193246
*

0 commit comments

Comments
 (0)