Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 19bea7f
Author: Jon Surrell <[email protected]>
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 <[email protected]>
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 <[email protected]>
Date:   Tue Jan 30 16:55:03 2024 +0100

    Update ticket

commit e0668b6
Author: Jon Surrell <[email protected]>
Date:   Tue Jan 30 16:51:02 2024 +0100

    Add failing test

commit 0deab0a
Author: Jon Surrell <[email protected]>
Date:   Tue Jan 30 15:53:48 2024 +0100

    Use $p variable for processor like other tests
  • Loading branch information
sirreal committed Jan 31, 2024
1 parent e053a62 commit 93996ba
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ public function next_token() {
$found_a_token = parent::next_token();

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

return $found_a_token;
Expand Down Expand Up @@ -513,7 +513,7 @@ public function step( $node_to_process = self::PROCESS_NEXT_NODE ) {
return false;
}

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

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

/**
* Indicates that the current HTML token should be processed without advancing the parser.
*
* @since 6.5.0
*
* @var string
*/
const PROCESS_CURRENT_NODE = 'process-current-node';

/**
* Indicates that the parser encountered unsupported markup and has bailed.
*
Expand Down
53 changes: 53 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,59 @@ public function test_cannot_nest_void_tags( $tag_name ) {
);
}

/**
* Ensure non-nesting tags do not nest when processing tokens.
*
* @ticket 60382
*
* @dataProvider data_void_tags
*
* @param string $tag_name Name of void tag under test.
*/
public function test_cannot_nest_void_tags_next_token( $tag_name ) {
$processor = WP_HTML_Processor::create_fragment( "<{$tag_name}><div>" );

/*
* This HTML represents the same as the following HTML,
* assuming that it were provided `<img>` as the tag:
*
* <html>
* <body>
* <img>
* <div></div>
* </body>
* </html>
*/

$found_tag = $processor->next_token();

if ( WP_HTML_Processor::ERROR_UNSUPPORTED === $processor->get_last_error() ) {
$this->markTestSkipped( "Tag {$tag_name} is not supported." );
}

$this->assertTrue(
$found_tag,
"Could not find first {$tag_name}."
);

$this->assertSame(
array( 'HTML', 'BODY', $tag_name ),
$processor->get_breadcrumbs(),
'Found incorrect nesting of first element.'
);

$this->assertTrue(
$processor->next_token(),
'Should have found the DIV as the second tag.'
);

$this->assertSame(
array( 'HTML', 'BODY', 'DIV' ),
$processor->get_breadcrumbs(),
"DIV should have been a sibling of the {$tag_name}."
);
}

/**
* Data provider.
*
Expand Down

0 comments on commit 93996ba

Please sign in to comment.