Commit | Line | Data |
3de6bc44 |
1 | use strict; |
2 | #use warnings FATAL => 'all'; |
3 | use Test::More; |
4 | |
5 | use HTML::Zoom; |
6 | |
682fa876 |
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 | |
27ed8809 |
17 | my $stub = '<div class="waargh"></div>'; |
18 | |
3de6bc44 |
19 | # el#id |
27ed8809 |
20 | is( 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 |
28 | is( 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 |
36 | is( 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 |
44 | is( 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 |
52 | is( 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' ); |
58 | |
59 | |
3de6bc44 |
60 | # el[attr*="foo"] |
27ed8809 |
61 | is( HTML::Zoom->from_html('<div f="frew goog"></div>'.$stub) |
3de6bc44 |
62 | ->select('div[f*="oo"]') |
63 | ->replace_content('grg') |
64 | ->to_html, |
27ed8809 |
65 | '<div f="frew goog">grg</div>'.$stub, |
3de6bc44 |
66 | 'E[attr*="val"] works' ); |
67 | |
68 | # el[attr^="foo"] |
27ed8809 |
69 | is( HTML::Zoom->from_html('<div f="foobar"></div>'.$stub) |
3de6bc44 |
70 | ->select('div[f^="foo"]') |
71 | ->replace_content('grg') |
72 | ->to_html, |
27ed8809 |
73 | '<div f="foobar">grg</div>'.$stub, |
3de6bc44 |
74 | 'E[attr^="val"] works' ); |
75 | |
76 | # el[attr$="foo"] |
27ed8809 |
77 | is( HTML::Zoom->from_html('<div f="foobar"></div>'.$stub) |
3de6bc44 |
78 | ->select('div[f$="bar"]') |
79 | ->replace_content('grg') |
80 | ->to_html, |
27ed8809 |
81 | '<div f="foobar">grg</div>'.$stub, |
3de6bc44 |
82 | 'E[attr$="val"] works' ); |
83 | |
84 | # el[attr*="foo"] |
27ed8809 |
85 | is( HTML::Zoom->from_html('<div f="foo bar"></div>'.$stub) |
3de6bc44 |
86 | ->select('div[f*="bar"]') |
87 | ->replace_content('grg') |
88 | ->to_html, |
27ed8809 |
89 | '<div f="foo bar">grg</div>'.$stub, |
3de6bc44 |
90 | 'E[attr*="val"] works' ); |
91 | |
682fa876 |
92 | # [attr=bar] |
93 | ok( check_select( '[prop=moo]'), '[attr=bar]' ); |
94 | |
95 | # el[attr=bar],[prop=foo] |
96 | is( check_select('span[class=career],[prop=moo]'), 2, |
97 | 'Multiple selectors: el[attr=bar],[attr=foo]'); |
98 | |
c9448e21 |
99 | TODO:{ |
100 | local $TODO = 'Fix selector error messages'; |
101 | # selector parse error test: |
102 | eval{ |
103 | HTML::Zoom->from_html('<span att="bar"></span>') |
104 | ->select('[att=bar') |
105 | ->replace_content('cats') |
106 | ->to_html; |
107 | }; |
108 | like( $@, qr/Error parsing dispatch specification/, |
109 | 'Malformed attribute selector results in a helpful error' ); |
110 | }; |
111 | |
27ed8809 |
112 | =pod |
682fa876 |
113 | |
3de6bc44 |
114 | # sel1 sel2 |
115 | is( HTML::Zoom->from_html('<table><tr></tr><tr></tr></table>') |
116 | ->select('table tr') |
117 | ->replace_content(\'<td></td>') |
118 | ->to_html, |
119 | '<table><tr><td></td></tr><tr><td></td></tr></table>', |
120 | 'sel1 sel2 works' ); |
121 | |
122 | |
123 | # sel1 sel2 sel3 |
124 | is( HTML::Zoom->from_html('<table><tr><td></td></tr><tr><td></td></tr></table>') |
125 | ->select('table tr td') |
126 | ->replace_content('frew') |
127 | ->to_html, |
128 | '<table><tr><td>frew</td></tr><tr><td>frew</td></tr></table>', |
129 | 'sel1 sel2 sel3 works' ); |
130 | |
c9448e21 |
131 | |
132 | |
27ed8809 |
133 | =cut |
134 | |
3de6bc44 |
135 | done_testing; |
682fa876 |
136 | |
137 | |
138 | sub check_select{ |
139 | # less crude?: |
140 | my $output = HTML::Zoom |
141 | ->from_html($tmpl) |
142 | ->select(shift)->replace("the monkey")->to_html; |
143 | my $count = 0; |
eacf665f |
144 | while ( $output =~ /the monkey/g ){ |
682fa876 |
145 | $count++; |
146 | } |
147 | return $count; |
148 | } |