Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTML API: Add method to report depth of currently-matched element. #6589

Closed
wants to merge 11 commits into from
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 @@ -1263,6 +1263,17 @@ public function get_tag() {
}
}

/**
* Return how deep the currently-matched element is in the HTML document.
*
* @since 6.6.0
*
* @return int
*/
public function get_current_depth() {
return $this->state->stack_of_open_elements->count();
}

/**
* Removes a bookmark that is no longer needed.
*
Expand Down
15 changes: 12 additions & 3 deletions tests/phpunit/tests/html-api/wpHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,15 @@ public static function data_unsupported_special_in_body_tags() {
public function test_reports_proper_element_depth_in_body( $html_with_target_element, $depth_at_element ) {
$processor = WP_HTML_Processor::create_fragment( $html_with_target_element );

$this->assertTrue(
$processor->next_tag( array( 'class_name' => 'target' ) ),
while ( $processor->next_tag() ) {
if ( $processor->has_class( 'target' ) ) {
break;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why this change?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought the other form was broken, causing the test to fail. That's the only reason. I may be mistaken, feel free to revert.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops, this is a known bug I forgot about. we claim to support class_name but don't

}
}

$this->assertSame(
'#tag',
$processor->get_token_type(),
'Failed to find target element: check test data provider.'
);

Expand All @@ -367,7 +374,9 @@ public function test_reports_proper_element_depth_in_body( $html_with_target_ele
*/
public static function data_html_with_target_element_and_depth_in_body() {
return array(
'Basic layout and formatting stack' => array( '<div><span><p><b><em class=target>', 7 ),
'Single element' => array( '<div class="target">', 3 ),
'Basic layout and formatting stack' => array( '<div><span><p><b><em class="target">', 7 ),
'Adjacent elements' => array( '<div><span></span><span class="target"></div>', 4 ),
);
}
}
Loading