Skip to content

Commit c73d272

Browse files
committed
Add test, fix list item scope logic
1 parent a0be379 commit c73d272

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

src/wp-includes/html-api/class-wp-html-open-elements.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public function has_element_in_specific_scope( $tag_name, $termination_list ) {
129129
}
130130

131131
if ( in_array( $node->node_name, $termination_list, true ) ) {
132-
return true;
132+
return false;
133133
}
134134
}
135135

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

+11
Original file line numberDiff line numberDiff line change
@@ -821,10 +821,21 @@ private function step_in_body() {
821821
case '-DT':
822822
case '-LI':
823823
if (
824+
/*
825+
* An end tag whose tag name is "li":
826+
* If the stack of open elements does not have an li element in list item scope,
827+
* then this is a parse error; ignore the token.
828+
*/
824829
(
825830
'LI' === $tag_name &&
826831
! $this->state->stack_of_open_elements->has_element_in_list_item_scope( 'LI' )
827832
) ||
833+
/*
834+
* An end tag whose tag name is one of: "dd", "dt":
835+
* If the stack of open elements does not have an element in scope that is an
836+
* HTML element with the same tag name as that of the token, then this is a
837+
* parse error; ignore the token.
838+
*/
828839
(
829840
'LI' !== $tag_name &&
830841
! $this->state->stack_of_open_elements->has_element_in_scope( $tag_name )

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

+22
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,26 @@ public function test_in_body_dt_in_dt_closes_p_in_button_scope() {
323323
'DT should have left the outer P open, but closed it.'
324324
);
325325
}
326+
327+
public function test_unexpected_li_close_tag_is_properly_contained() {
328+
$processor = WP_HTML_Processor::create_fragment( '<ul><li><ul></li><li target>a</li></ul></li></ul>' );
329+
330+
while (
331+
null === $processor->get_attribute( 'target' ) &&
332+
$processor->next_tag()
333+
) {
334+
continue;
335+
}
336+
337+
$this->assertTrue(
338+
$processor->get_attribute( 'target' ),
339+
'Failed to find target node.'
340+
);
341+
342+
$this->assertSame(
343+
array( 'HTML', 'BODY', 'UL', 'LI', 'UL', 'LI' ),
344+
$processor->get_breadcrumbs(),
345+
'Unexpected LI close tag should have left its containing UL open, but closed it.'
346+
);
347+
}
326348
}

0 commit comments

Comments
 (0)