@@ -26,10 +26,17 @@ public function test_select_miss() {
2626 *
2727 * @dataProvider data_selectors
2828 */
29- public function test_select ( string $ html , string $ selector ) {
29+ public function test_select ( string $ html , string $ selector, int $ match_count ) {
3030 $ processor = new WP_HTML_Tag_Processor ( $ html );
31- $ this ->assertTrue ( $ processor ->select ( $ selector ) );
32- $ this ->assertTrue ( $ processor ->get_attribute ( 'match ' ) );
31+ $ count = 0 ;
32+ foreach ( $ processor ->select_all ( $ selector ) as $ _ ) {
33+ $ this ->assertTrue (
34+ $ processor ->get_attribute ( 'match ' ),
35+ "Matched unexpected tag {$ processor ->get_tag ()}"
36+ );
37+ ++$ count ;
38+ }
39+ $ this ->assertSame ( $ match_count , $ count , 'Did not match expected number of tags. ' );
3340 }
3441
3542 /**
@@ -39,58 +46,44 @@ public function test_select( string $html, string $selector ) {
3946 */
4047 public static function data_selectors (): array {
4148 return array (
42- 'simple type ' => array ( '<p><div match> ' , 'div ' ),
43- 'any type ' => array ( '<div match> ' , '* ' ),
44- 'simple class ' => array ( '<div><div class="x" match> ' , '.x ' ),
45- 'simple id ' => array ( '<div><div id="x" match> ' , '#x ' ),
46- 'boolean attribute ' => array ( '<div tta><div att match> ' , '[att] ' ),
47- 'boolean attribute with string match ' => array ( '<div tta><div att match> ' , '[att=""] ' ),
49+ 'simple type ' => array ( '<div match><div match><span> ' , 'div ' , 2 ),
50+ 'any type ' => array ( '<div match><span match> ' , '* ' , 2 ),
51+ 'simple class ' => array ( '<div><div class="x" match><span><span class="x" match> ' , '.x ' , 2 ),
52+ 'simple id ' => array ( '<div><div id="x" match><span id="x" match> ' , '#x ' , 2 ),
4853
49- 'attribute value ' => array ( '<div att="v"><div att="val" match> ' , '[att=val] ' ),
50- 'attribute quoted value ' => array ( '<div att><div att="::" match> ' , '[att="::"] ' ),
51- 'attribute case insensitive ' => array ( '<div att="value"><div att="val" match> ' , '[att="VAL"i] ' ),
52- 'attribute case sensitive mod ' => array ( '<div att="VAL"><div att="val" match> ' , '[att="val"s] ' ),
54+ 'attribute presence ' => array ( '<div tta><div att match><span att="foo" match> ' , '[att] ' , 2 ),
55+ 'attribute empty string match ' => array ( '<div tta><div att match><span match att=> ' , '[att=""] ' , 2 ),
56+ 'attribute value ' => array ( '<div att="v"><div att="val" match><p match att=val> ' , '[att=val] ' , 2 ),
57+ 'attribute quoted value ' => array ( '<div att><div att="::" match><p att= \':: \' match> ' , '[att="::"] ' , 2 ),
58+ 'attribute case insensitive ' => array ( '<div att="value"><div att="val" match><p match att=VaL> ' , '[att="VAL"i] ' , 2 ),
59+ 'attribute case sensitive mod ' => array ( '<div att="VAL"><div att="val" match><p att=val match> ' , '[att="val"s] ' , 2 ),
5360
54- 'attribute one of ' => array ( '<div att="a B c"><div att="a b c" match> ' , '[att~="b"] ' ),
55- 'attribute one of insensitive ' => array ( '<div att="a c"><div att="a B c" match> ' , '[att~="b"i] ' ),
56- 'attribute one of mod sensitive ' => array ( '<div att="a B c"><div att="a b c" match> ' , '[att~="b"s] ' ),
57- 'attribute one of whitespace cases ' => array ( "<div att=' a'><div att=' \na \ta b ' match> " , '[att~="b"] ' ),
61+ 'attribute one of ' => array ( '<div att="a B c"><div att="a b c" match><p att="b" match><p att="abc"><p att="abcdef b " match> ' , '[att~="b"] ' , 3 ),
62+ 'attribute one of insensitive ' => array ( '<div att="a c"><div att="a B c" match> ' , '[att~="b"i] ' , 1 ),
63+ 'attribute one of mod sensitive ' => array ( '<div att="a B c"><div att="a b c" match> ' , '[att~="b"s] ' , 1 ),
64+ 'attribute one of whitespace cases ' => array ( "<div att=' a'><div att=' \na \ta b ' match> " , '[att~="b"] ' , 1 ),
5865
59- 'attribute with-hyphen (no hyphen) ' => array ( '<p att="special_not"><p att="special" match> ' , '[att|="special"] ' ),
60- 'attribute with-hyphen (hyphen prefix) ' => array ( '<p att="special_not"><p att="special-yeah" match> ' , '[att|="special"] ' ),
61- 'attribute with-hyphen insensitive ' => array ( '<p att="special_not"><p att="SPECIAL" match> ' , '[att|="special"i] ' ),
62- 'attribute with-hyphen sensitive mod ' => array ( '<p att="spec"><p att="special" match> ' , '[att|="special"s] ' ),
66+ 'attribute with-hyphen ' => array ( '<p att="special_not"><p att="special" match><p att="special-great" match> ' , '[att|="special"] ' , 2 ),
67+ 'attribute with-hyphen insensitive ' => array ( '<p att="special_not"><p att="SPECIAL" match><p att="SPECIAL-great" match> ' , '[att|="special" i] ' , 2 ),
68+ 'attribute with-hyphen sensitive mod ' => array ( '<p att="SPECIAL"><p att="special" match> ' , '[att|="special"s] ' , 1 ),
6369
64- 'attribute prefixed ' => array ( '<p att="fix "><p att="prefix" match> ' , '[att^="p"] ' ),
65- 'attribute prefixed insensitive ' => array ( '<p att="fix "><p att="Prefix" match> ' , '[att^="p"i] ' ),
66- 'attribute prefixed sensitive mod ' => array ( '<p att="Prefix"><p att="prefix" match> ' , '[att^="p"s] ' ),
70+ 'attribute prefixed ' => array ( '<p att="notprefix "><p att="prefix" match><p att="perfect" match> ' , '[att^="p"] ' , 2 ),
71+ 'attribute prefixed insensitive ' => array ( '<p att="notprefix "><p att="Prefix" match> ' , '[att^="p"i] ' , 1 ),
72+ 'attribute prefixed sensitive mod ' => array ( '<p att="Prefix"><p att="prefix" match> ' , '[att^="p"s] ' , 1 ),
6773
68- 'attribute suffixed ' => array ( '<p att="suffix_"><p att="suffix" match> ' , '[att$="x"] ' ),
69- 'attribute suffixed insensitive ' => array ( '<p att="suffix_"><p att="suffiX" match> ' , '[att$="x"i] ' ),
70- 'attribute suffixed sensitive mod ' => array ( '<p att="suffiX"><p att="suffix" match> ' , '[att$="x"s] ' ),
74+ 'attribute suffixed ' => array ( '<p att="suffix_"><p att="suffix" match><p att="brilliant…x" match> ' , '[att$="x"] ' , 2 ),
75+ 'attribute suffixed insensitive ' => array ( '<p att="suffix_"><p att="suffiX" match> ' , '[att$="x"i] ' , 1 ),
76+ 'attribute suffixed sensitive mod ' => array ( '<p att="suffiX"><p att="suffix" match> ' , '[att$="x"s] ' , 1 ),
7177
72- 'attribute contains ' => array ( '<p att="abcyz"><p att="abcxyz" match> ' , '[att*="x"] ' ),
73- 'attribute contains insensitive ' => array ( '<p att="abcyz"><p att="abcXyz" match> ' , '[att*="x"i] ' ),
74- 'attribute contains sensitive mod ' => array ( '<p att="abcXyz"><p att="abcxyz" match> ' , '[att*="x"s] ' ),
78+ 'attribute contains ' => array ( '<p att="abcyz"><p att="abcxyz" match><p att="x" match> ' , '[att*="x"] ' , 2 ),
79+ 'attribute contains insensitive ' => array ( '<p att="abcyz"><p att="abcXyz" match> ' , '[att*="x"i] ' , 1 ),
80+ 'attribute contains sensitive mod ' => array ( '<p att="abcXyz"><p att="abcxyz" match> ' , '[att*="x"s] ' , 1 ),
7581
76- 'list ' => array ( '<div><p match> ' , 'a, p ' ),
77- 'compound ' => array ( '<div att><section att="bar" match> ' , 'section [att="bar"]' ),
82+ 'list ' => array ( '<div><p match><a match><span> ' , 'a, p, .class, #id, [att] ' , 2 ),
83+ 'compound ' => array ( '<div att><custom-el att="bar" fruit="APPLE BANANA" match> ' , 'custom-el [att="bar"][ fruit ~= "banana" i] ' , 1 ),
7884 );
7985 }
8086
81- /**
82- * @ticket TBD
83- */
84- public function test_select_all () {
85- $ processor = new WP_HTML_Tag_Processor ( '<div match><p class="x" match><svg><rect match/></svg><i id="y" match></i> ' );
86- $ count = 0 ;
87- foreach ( $ processor ->select_all ( 'div, .x, rect, #y ' ) as $ _ ) {
88- ++$ count ;
89- $ this ->assertTrue ( $ processor ->get_attribute ( 'match ' ) );
90- }
91- $ this ->assertSame ( 4 , $ count );
92- }
93-
9487 /**
9588 * @ticket TBD
9689 *
0 commit comments