mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-06-28 22:30:04 +00:00
HTML API: Add matches_breadcrumbs() method for better querying.
Inside a `next_tag()` loop it can be challenging to use breadcrumbs because they are only exposed inside the call to `next_tag()` via the `$query` arg. In this patch a new method, `matches_breadcrumbs()`, is exposed which allows for querying within the `next_tag()` loop for more complicated queries. This method exposes a wildcard `*` operator to allow matching ''any HTML tag'' that the currently-matched tag is a child or descendant of. Props dmsnell, westonruter, mukesh27. Fixes #59400. git-svn-id: https://develop.svn.wordpress.org/trunk@56702 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -352,6 +352,64 @@ class Tests_HtmlApi_WpHtmlProcessorBreadcrumbs extends WP_UnitTestCase {
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 59400
|
||||
*
|
||||
* @dataProvider data_html_with_breadcrumbs_of_various_specificity
|
||||
*
|
||||
* @param string $html_with_target_node HTML with a node containing a "target" attribute.
|
||||
* @param string[] $breadcrumbs Breadcrumbs to test at the target node.
|
||||
* @param bool $should_match Whether the target node should match the breadcrumbs.
|
||||
*/
|
||||
public function test_reports_if_tag_matches_breadcrumbs_of_various_specificity( $html_with_target_node, $breadcrumbs, $should_match ) {
|
||||
$processor = WP_HTML_Processor::createFragment( $html_with_target_node );
|
||||
while ( $processor->next_tag() && null === $processor->get_attribute( 'target' ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$matches = $processor->matches_breadcrumbs( $breadcrumbs );
|
||||
$path = implode( ', ', $breadcrumbs );
|
||||
if ( $should_match ) {
|
||||
$this->assertTrue( $matches, "HTML tag {$processor->get_tag()} should have matched breadcrumbs but didn't: {$path}." );
|
||||
} else {
|
||||
$this->assertFalse( $matches, "HTML tag {$processor->get_tag()} should not have matched breadcrumbs but did: {$path}." );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Data provider.
|
||||
*
|
||||
* @return array[].
|
||||
*/
|
||||
public function data_html_with_breadcrumbs_of_various_specificity() {
|
||||
return array(
|
||||
// Test with void elements.
|
||||
'Inner IMG' => array( '<div><span><figure><img target></figure></span></div>', array( 'span', 'figure', 'img' ), true ),
|
||||
'Inner IMG wildcard' => array( '<div><span><figure><img target></figure></span></div>', array( 'span', '*', 'img' ), true ),
|
||||
'Inner IMG no wildcard' => array( '<div><span><figure><img target></figure></span></div>', array( 'span', 'img' ), false ),
|
||||
'Full specification' => array( '<div><span><figure><img target></figure></span></div>', array( 'html', 'body', 'div', 'span', 'figure', 'img' ), true ),
|
||||
'Invalid Full specification' => array( '<div><span><figure><img target></figure></span></div>', array( 'html', 'div', 'span', 'figure', 'img' ), false ),
|
||||
|
||||
// Test also with non-void elements that open and close.
|
||||
'Inner P' => array( '<div><span><figure><p target></figure></span></div>', array( 'span', 'figure', 'p' ), true ),
|
||||
'Inner P wildcard' => array( '<div><span><figure><p target></figure></span></div>', array( 'span', '*', 'p' ), true ),
|
||||
'Inner P no wildcard' => array( '<div><span><figure><p target></figure></span></div>', array( 'span', 'p' ), false ),
|
||||
'Full specification (P)' => array( '<div><span><figure><p target></figure></span></div>', array( 'html', 'body', 'div', 'span', 'figure', 'p' ), true ),
|
||||
'Invalid Full specification (P)' => array( '<div><span><figure><p target></figure></span></div>', array( 'html', 'div', 'span', 'figure', 'p' ), false ),
|
||||
|
||||
// Ensure that matches aren't on tag closers.
|
||||
'Inner P' => array( '<div><span><figure></p target></figure></span></div>', array( 'span', 'figure', 'p' ), false ),
|
||||
'Inner P wildcard' => array( '<div><span><figure></p target></figure></span></div>', array( 'span', '*', 'p' ), false ),
|
||||
'Inner P no wildcard' => array( '<div><span><figure></p target></figure></span></div>', array( 'span', 'p' ), false ),
|
||||
'Full specification (P)' => array( '<div><span><figure></p target></figure></span></div>', array( 'html', 'body', 'div', 'span', 'figure', 'p' ), false ),
|
||||
'Invalid Full specification (P)' => array( '<div><span><figure></p target></figure></span></div>', array( 'html', 'div', 'span', 'figure', 'p' ), false ),
|
||||
|
||||
// Test wildcard behaviors.
|
||||
'Single wildcard element' => array( '<figure><code><div><p><span><img target></span></p></div></code></figure>', array( '*' ), true ),
|
||||
'Child of wildcard element' => array( '<figure><code><div><p><span><img target></span></p></div></code></figure>', array( 'SPAN', '*' ), true ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensures that the ability to set attributes isn't broken by the HTML Processor.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user