Skip to content

Commit

Permalink
Add test, fix list item scope logic
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Dec 22, 2023
1 parent a0be379 commit c73d272
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/wp-includes/html-api/class-wp-html-open-elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function has_element_in_specific_scope( $tag_name, $termination_list ) {
}

if ( in_array( $node->node_name, $termination_list, true ) ) {
return true;
return false;
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/wp-includes/html-api/class-wp-html-processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -821,10 +821,21 @@ private function step_in_body() {
case '-DT':
case '-LI':
if (
/*
* An end tag whose tag name is "li":
* If the stack of open elements does not have an li element in list item scope,
* then this is a parse error; ignore the token.
*/
(
'LI' === $tag_name &&
! $this->state->stack_of_open_elements->has_element_in_list_item_scope( 'LI' )
) ||
/*
* An end tag whose tag name is one of: "dd", "dt":
* If the stack of open elements does not have an element in scope that is an
* HTML element with the same tag name as that of the token, then this is a
* parse error; ignore the token.
*/
(
'LI' !== $tag_name &&
! $this->state->stack_of_open_elements->has_element_in_scope( $tag_name )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,26 @@ public function test_in_body_dt_in_dt_closes_p_in_button_scope() {
'DT should have left the outer P open, but closed it.'
);
}

public function test_unexpected_li_close_tag_is_properly_contained() {
$processor = WP_HTML_Processor::create_fragment( '<ul><li><ul></li><li target>a</li></ul></li></ul>' );

while (
null === $processor->get_attribute( 'target' ) &&
$processor->next_tag()
) {
continue;
}

$this->assertTrue(
$processor->get_attribute( 'target' ),
'Failed to find target node.'
);

$this->assertSame(
array( 'HTML', 'BODY', 'UL', 'LI', 'UL', 'LI' ),
$processor->get_breadcrumbs(),
'Unexpected LI close tag should have left its containing UL open, but closed it.'
);
}
}

0 comments on commit c73d272

Please sign in to comment.