Add support for [attr~="value"] selector (attribute contains word)
[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 my $stub = '<div class="waargh"></div>';
18
19 # el#id
20 is( HTML::Zoom->from_html('<div id="yo"></div>'.$stub)
21    ->select('div#yo')
22       ->replace_content('grg')
23    ->to_html,
24    '<div id="yo">grg</div>'.$stub,
25    'E#id works' );
26
27 # el.class1
28 is( HTML::Zoom->from_html('<div class="yo"></div>'.$stub)
29    ->select('div.yo')
30       ->replace_content('grg')
31    ->to_html,
32    '<div class="yo">grg</div>'.$stub,
33    'E.class works' );
34
35 # el[attr]
36 is( HTML::Zoom->from_html('<div frew="yo"></div>'.$stub)
37    ->select('div[frew]')
38       ->replace_content('grg')
39    ->to_html,
40    '<div frew="yo">grg</div>'.$stub,
41    'E[attr] works' );
42
43 # el[attr="foo"]
44 is( HTML::Zoom->from_html('<div frew="yo"></div>'.$stub)
45    ->select('div[frew="yo"]')
46       ->replace_content('grg')
47    ->to_html,
48    '<div frew="yo">grg</div>'.$stub,
49    'E[attr="val"] works' );
50
51 # el[attr=foo]
52 is( HTML::Zoom->from_html('<div frew="yo"></div>'.$stub)
53     ->select('div[frew=yo]')
54     ->replace_content('grg')
55     ->to_html,
56     '<div frew="yo">grg</div>'.$stub,
57     'E[attr=val] works' );
58  
59
60 # el[attr*="foo"]
61 is( HTML::Zoom->from_html('<div f="frew goog"></div>'.$stub)
62    ->select('div[f*="oo"]')
63       ->replace_content('grg')
64    ->to_html,
65    '<div f="frew goog">grg</div>'.$stub,
66    'E[attr*="val"] works' );
67
68 # el[attr^="foo"]
69 is( HTML::Zoom->from_html('<div f="foobar"></div>'.$stub)
70    ->select('div[f^="foo"]')
71       ->replace_content('grg')
72    ->to_html,
73    '<div f="foobar">grg</div>'.$stub,
74    'E[attr^="val"] works' );
75
76 # el[attr$="foo"]
77 is( HTML::Zoom->from_html('<div f="foobar"></div>'.$stub)
78    ->select('div[f$="bar"]')
79       ->replace_content('grg')
80    ->to_html,
81    '<div f="foobar">grg</div>'.$stub,
82    'E[attr$="val"] works' );
83
84 # el[attr*="foo"]
85 is( HTML::Zoom->from_html('<div f="foo bar"></div>'.$stub)
86    ->select('div[f*="bar"]')
87       ->replace_content('grg')
88    ->to_html,
89    '<div f="foo bar">grg</div>'.$stub,
90    'E[attr*="val"] works' );
91
92 # el[attr~="foo"]
93 is( HTML::Zoom->from_html('<div frew="foo bar baz"></div>'.$stub)
94    ->select('div[frew~="bar"]')
95       ->replace_content('grg')
96    ->to_html,
97    '<div frew="foo bar baz">grg</div>'.$stub,
98    'E[attr~="val"] works' );
99
100 # [attr=bar]
101 ok( check_select( '[prop=moo]'), '[attr=bar]' );
102
103 # el[attr=bar],[prop=foo]
104 is( check_select('span[class=career],[prop=moo]'), 2,
105     'Multiple selectors: el[attr=bar],[attr=foo]');
106
107
108 # selector parse error test:
109 eval{
110     HTML::Zoom->from_html('<span att="bar"></span>')
111       ->select('[att=bar')
112       ->replace_content('cats')
113           ->to_html;
114 };
115 like( $@, qr/Error parsing dispatch specification/,
116       'Malformed attribute selector ([att=bar) results in a helpful error' );
117
118 =pod
119
120 # sel1 sel2
121 is( HTML::Zoom->from_html('<table><tr></tr><tr></tr></table>')
122    ->select('table tr')
123       ->replace_content(\'<td></td>')
124    ->to_html,
125    '<table><tr><td></td></tr><tr><td></td></tr></table>',
126    'sel1 sel2 works' );
127
128
129 # sel1 sel2 sel3
130 is( HTML::Zoom->from_html('<table><tr><td></td></tr><tr><td></td></tr></table>')
131    ->select('table tr td')
132       ->replace_content('frew')
133    ->to_html,
134    '<table><tr><td>frew</td></tr><tr><td>frew</td></tr></table>',
135    'sel1 sel2 sel3 works' );
136
137
138
139 =cut
140
141 done_testing;
142
143
144 sub check_select{
145     # less crude?:
146     my $output = HTML::Zoom
147     ->from_html($tmpl)
148     ->select(shift)->replace("the monkey")->to_html;
149     my $count = 0;
150     while ( $output =~ /the monkey/g ){
151         $count++;
152     }
153     return $count;
154 }