Improved selector parsing.
[catagits/HTML-Zoom.git] / t / selectors.t
1 use strict;
2 #use warnings FATAL => 'all';
3 use Test::More;
4
5 use HTML::Zoom;
6
7 my $tmpl = <<END;
8 <body>
9   <div class="main">
10     <span prop='moo' class="hilight name">Bob</span>
11     <span class="career">Builder</span>
12     <hr />
13   </div>
14 </body>
15 END
16
17 # el#id
18 is( HTML::Zoom->from_html('<div id="yo"></div>')
19    ->select('div#yo')
20       ->replace_content('grg')
21    ->to_html,
22    '<div id="yo">grg</div>',
23    'E#id works' );
24
25 # el.class1
26 is( HTML::Zoom->from_html('<div class="yo"></div>')
27    ->select('div.yo')
28       ->replace_content('grg')
29    ->to_html,
30    '<div class="yo">grg</div>',
31    'E.class works' );
32
33 # el[attr]
34 is( HTML::Zoom->from_html('<div frew="yo"></div>')
35    ->select('div[frew]')
36       ->replace_content('grg')
37    ->to_html,
38    '<div frew="yo">grg</div>',
39    'E[attr] works' );
40
41 # el[attr="foo"]
42 is( HTML::Zoom->from_html('<div frew="yo"></div>')
43    ->select('div[frew="yo"]')
44       ->replace_content('grg')
45    ->to_html,
46    '<div frew="yo">grg</div>',
47    'E[attr="val"] works' );
48
49 # el[attr=foo]
50 is( HTML::Zoom->from_html('<div frew="yo"></div>')
51     ->select('div[frew=yo]')
52     ->replace_content('grg')
53     ->to_html,
54     '<div frew="yo">grg</div>',
55     'E[attr=val] works' );
56  
57
58 # el[attr*="foo"]
59 is( HTML::Zoom->from_html('<div f="frew goog"></div>')
60    ->select('div[f*="oo"]')
61       ->replace_content('grg')
62    ->to_html,
63    '<div f="frew goog">grg</div>',
64    'E[attr*="val"] works' );
65
66 # el[attr^="foo"]
67 is( HTML::Zoom->from_html('<div f="foobar"></div>')
68    ->select('div[f^="foo"]')
69       ->replace_content('grg')
70    ->to_html,
71    '<div f="foobar">grg</div>',
72    'E[attr^="val"] works' );
73
74 # el[attr$="foo"]
75 is( HTML::Zoom->from_html('<div f="foobar"></div>')
76    ->select('div[f$="bar"]')
77       ->replace_content('grg')
78    ->to_html,
79    '<div f="foobar">grg</div>',
80    'E[attr$="val"] works' );
81
82 # el[attr*="foo"]
83 is( HTML::Zoom->from_html('<div f="foo bar"></div>')
84    ->select('div[f*="bar"]')
85       ->replace_content('grg')
86    ->to_html,
87    '<div f="foo bar">grg</div>',
88    'E[attr*="val"] works' );
89
90 # [attr=bar]
91 ok( check_select( '[prop=moo]'), '[attr=bar]' );
92
93 # el[attr=bar],[prop=foo]
94 is( check_select('span[class=career],[prop=moo]'), 2,
95     'Multiple selectors: el[attr=bar],[attr=foo]');
96
97
98 # sel1 sel2
99 is( HTML::Zoom->from_html('<table><tr></tr><tr></tr></table>')
100    ->select('table tr')
101       ->replace_content(\'<td></td>')
102    ->to_html,
103    '<table><tr><td></td></tr><tr><td></td></tr></table>',
104    'sel1 sel2 works' );
105
106
107 # sel1 sel2 sel3
108 is( HTML::Zoom->from_html('<table><tr><td></td></tr><tr><td></td></tr></table>')
109    ->select('table tr td')
110       ->replace_content('frew')
111    ->to_html,
112    '<table><tr><td>frew</td></tr><tr><td>frew</td></tr></table>',
113    'sel1 sel2 sel3 works' );
114
115 done_testing;
116
117
118 sub check_select{
119     # less crude?:
120     my $output = HTML::Zoom
121     ->from_html($tmpl)
122     ->select(shift)->replace("the monkey")->to_html;
123     my $count = 0;
124     while ( $output =~ /\G?.*the monkey/gc ){
125         $count++;
126     }
127     return $count;
128 }