Skip to content

Commit

Permalink
Add more tests verifying that the H1-H6 elements close opened heading…
Browse files Browse the repository at this point in the history
… elements.
  • Loading branch information
dmsnell committed Nov 15, 2023
1 parent a8a2e0e commit 6a2ae42
Showing 1 changed file with 75 additions and 3 deletions.
78 changes: 75 additions & 3 deletions tests/phpunit/tests/html-api/wpHtmlProcessorSemanticRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,23 @@ public function test_in_body_button_with_button_in_scope_as_ancestor() {
* @param string $tag_name Name of H1 - H6 element under test.
*/
public function test_heading_element_closes_open_p_tag( $tag_name ) {
$p = WP_HTML_Processor::create_fragment( "<p>Open<{$tag_name}>Closed P</{$tag_name}></p>" );
$processor = WP_HTML_Processor::create_fragment(
"<p>Open<{$tag_name}>Closed P</{$tag_name}><img></p>"
);

$processor->next_tag( $tag_name );
$this->assertSame(
array( 'HTML', 'BODY', $tag_name ),
$processor->get_breadcrumbs(),
"Expected {$tag_name} to be a direct child of the BODY, having closed the open P element."
);

$p->next_tag( $tag_name );
$this->assertSame( array( 'HTML', 'BODY', $tag_name ), $p->get_breadcrumbs() );
$processor->next_tag( 'IMG' );
$this->assertSame(
array( 'HTML', 'BODY', 'IMG' ),
$processor->get_breadcrumbs(),
'Expected IMG to be a direct child of BODY, having closed the open P element.'
);
}

/**
Expand All @@ -157,6 +170,65 @@ public function data_heading_elements() {
);
}

/**
* Verifies that H1 through H6 elements close an open H1 through H6 element.
*
* @dataProvider data_heading_combinations
*
* @param string $first_heading H1 - H6 element appearing (unclosed) before the second.
* @param string $second_heading H1 - H6 element appearing after the first.
*/
public function test_heading_element_closes_other_heading_elements( $first_heading, $second_heading ) {
$processor = WP_HTML_Processor::create_fragment(
"<div><{$first_heading} first> then <{$second_heading} second> and end </{$second_heading}><img></{$first_heading}></div>"
);

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

$this->assertTrue(
$processor->get_attribute( 'second' ),
"Failed to find expected {$second_heading} tag."
);

$this->assertSame(
array( 'HTML', 'BODY', 'DIV', $second_heading ),
$processor->get_breadcrumbs(),
"Expected {$second_heading} to be a direct child of the DIV, having closed the open {$first_heading} element."
);

$processor->next_tag( 'IMG' );
$this->assertSame(
array( 'HTML', 'BODY', 'DIV', 'IMG' ),
$processor->get_breadcrumbs(),
"Expected IMG to be a direct child of DIV, having closed the open {$first_heading} element."
);
}

/**
* Data provider.
*
* @return array[]
*/
public function data_heading_combinations() {
$headings = array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' );

$combinations = array();

// Create all unique pairs of H1 - H6 elements.
for ( $first = 0; $first < count( $headings ); $first++ ) {
for ( $second = $first; $second < count( $headings ); $second++ ) {
$first_tag = $headings[ $first ];
$second_tag = $headings[ $second ];

$combinations[ "{$first_tag} then {$second_tag}" ] = array( $first_tag, $second_tag );
}
}

return $combinations;
}

/**
* Verifies that when "in body" and encountering "any other end tag"
* that the HTML processor ignores the end tag if there's a special
Expand Down

0 comments on commit 6a2ae42

Please sign in to comment.