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