Skip to content

Commit 93996ba

Browse files
committed
Squashed commit of the following: commit 19bea7f Author: Jon Surrell <sirreal@users.noreply.github.com> Date: Wed Jan 31 10:26:06 2024 +0100 Revert "Use $p variable for processor like other tests" This reverts commit 0deab0a. commit 31d71de Author: Dennis Snell <dennis.snell@automattic.com> Date: Tue Jan 30 15:34:23 2024 -0700 Ensure void and self-closing elements pop from stack when advancing. Previously, the logic to pop void and self-closing elements from the stack of open elements only ran when stepping into the next node in a document. With the introduction of `next_token()` there appeared a new way to reprocesses the current token, so this logic would be skipped when calling `next_token()` _into_ a void or self-closing element, leaving it on the stack. In this patch the logic runs whenever the processor is not reprocessing the current token. A new class constant communicates that `step()` should treat the current token as if it arrived there itself, that is, to process it with the normal rules but without advancing the parser. commit 7890b53 Author: Jon Surrell <sirreal@users.noreply.github.com> Date: Tue Jan 30 16:55:03 2024 +0100 Update ticket commit e0668b6 Author: Jon Surrell <sirreal@users.noreply.github.com> Date: Tue Jan 30 16:51:02 2024 +0100 Add failing test commit 0deab0a Author: Jon Surrell <sirreal@users.noreply.github.com> Date: Tue Jan 30 15:53:48 2024 +0100 Use $p variable for processor like other tests
1 parent e053a62 commit 93996ba

2 files changed

Lines changed: 66 additions & 2 deletions

File tree

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)