Skip to content

Commit 070e927

Browse files
committed
Add html processor select test suite
1 parent 3d7ccd6 commit 070e927

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/**
3+
* Unit tests covering WP_HTML_Processor functionality.
4+
*
5+
* @package WordPress
6+
* @subpackage HTML-API
7+
*
8+
* @since TBD
9+
*
10+
* @group html-api
11+
*
12+
* @coversDefaultClass WP_HTML_Processor
13+
*/
14+
class Tests_HtmlApi_WpHtmlProcessor_Select extends WP_UnitTestCase {
15+
/**
16+
* @ticket TBD
17+
*/
18+
public function test_select_miss() {
19+
$processor = WP_HTML_Processor::create_full_parser( '<span>' );
20+
$this->assertFalse( $processor->select( 'div' ) );
21+
}
22+
23+
/**
24+
* @ticket TBD
25+
*
26+
* @dataProvider data_selectors
27+
*/
28+
public function test_select( string $html, string $selector ) {
29+
$processor = WP_HTML_Processor::create_full_parser( $html );
30+
$this->assertTrue( $processor->select( $selector ) );
31+
$this->assertTrue( $processor->get_attribute( 'match' ) );
32+
}
33+
34+
/**
35+
* Data provider.
36+
*
37+
* @return array
38+
*/
39+
public static function data_selectors(): array {
40+
return array(
41+
'simple type' => array( '<div match>', 'div' ),
42+
'any type' => array( '<html match>', '*' ),
43+
'simple class' => array( '<div class="x" match>', '.x' ),
44+
'simple id' => array( '<div id="x" match>', '#x' ),
45+
'simple attribute' => array( '<div att match>', '[att]' ),
46+
'attribute value' => array( '<div att="val" match>', '[att=val]' ),
47+
'attribute quoted value' => array( '<div att="::" match>', '[att="::"]' ),
48+
'complex any descendant' => array( '<section><div match>', 'section *' ),
49+
'complex any child' => array( '<section><div match>', 'section > *' ),
50+
51+
'list' => array( '<div><p match>', 'a, p' ),
52+
'compound' => array( '<div att><section att="foo bar" match>', 'section[att~="bar"]' ),
53+
);
54+
}
55+
56+
/**
57+
* @ticket TBD
58+
*/
59+
public function test_select_all() {
60+
$processor = WP_HTML_Processor::create_full_parser( '<div match><p class="x" match><svg><rect match/></svg><i id="y" match></i>' );
61+
$count = 0;
62+
foreach ( $processor->select_all( 'div, .x, svg>rect, #y' ) as $_ ) {
63+
++$count;
64+
$this->assertTrue( $processor->get_attribute( 'match' ) );
65+
}
66+
$this->assertSame( 4, $count );
67+
}
68+
}

0 commit comments

Comments
 (0)