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
45 changes: 45 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,4 +334,49 @@ public static function data_unsupported_special_in_body_tags() {
'XMP' => array( 'XMP' ),
);
}

/**
* Ensures that the HTML Processor properly reports the depth of a given element.
*
* @ticket 61255
*
* @dataProvider data_html_with_target_element_and_depth_in_body
*
* @param string $html_with_target_element HTML containing element with `target` class.
* @param int $depth_at_element Depth into document at target node.
*/
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 );

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.'
);

$this->assertSame(
$depth_at_element,
$processor->get_current_depth(),
'HTML Processor reported the wrong depth at the matched element.'
);
}

/**
* Data provider.
*
* @return array[].
*/
public static function data_html_with_target_element_and_depth_in_body() {
return array(
'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