Commit | Line | Data |
94a3ddd9 |
1 | use strict; |
2 | use warnings FATAL => 'all'; |
3 | |
4 | use HTML::Zoom; |
5 | use Test::More; |
6 | |
7 | ok my $zoom = HTML::Zoom->from_html(<<HTML); |
8 | <html> |
9 | <head> |
10 | <title>Hi!</title> |
11 | </head> |
12 | <body id="content-area"> |
13 | <h1>Test</h1> |
14 | <div> |
15 | <p class="first-para">Some Stuff</p> |
16 | <p class="body-para">More Stuff</p> |
17 | <p class="body-para">Even More Stuff</p> |
18 | <ol> |
19 | <li class="first-item odd">First</li> |
20 | <li class="body-items even">Stuff A</li> |
21 | <li class="body-items odd">Stuff B</li> |
22 | <li class="body-items even">Stuff C</li> |
23 | <li class="last-item odd">Last</li> |
24 | </ol> |
25 | <ul class="items"> |
26 | <li class="first">space</li> |
27 | <li class="second">space</li> |
28 | </ul> |
29 | <p class="body-para">Even More stuff</p> |
30 | <p class="last-para">Some Stuff</p> |
31 | </div> |
32 | <div id="blocks"> |
33 | <h2 class="h2-item">Sub Item</h2> |
34 | </div> |
35 | <ul id="people"> |
36 | <li class="name">name</li> |
37 | <li class="email">email</li> |
38 | </ul> |
39 | <ul id="people2"> |
40 | <li class="name">name</li> |
41 | <li class="email">email</li> |
42 | </ul> |
43 | <ul id="object"> |
44 | <li class="aa">AA</li> |
45 | <li class="bb">BB</li> |
46 | </ul> |
47 | <p id="footer">Copyright 2222</p> |
48 | </body> |
49 | </html> |
50 | HTML |
51 | |
52 | ok ! eval { $zoom->non_method('.first-param' => 'First!'); 1}, |
53 | 'Properly die on bad method'; |
54 | |
55 | ok my $title = sub { |
56 | my ($z, $content) = @_; |
57 | $z = $z->replace_content(title =>$content); |
58 | return $z; |
59 | }; |
60 | |
61 | ok my $dwim = $zoom |
62 | ->$title('Hello NYC') |
63 | ->replace_content('.first-para' => 'First!') |
64 | ->replace_content('.last-para' => 'Last!') |
65 | ->add_to_attribute(p => class => 'para') |
66 | ->prepend_content('.first-item' => [{type=>'TEXT', raw=>'FIRST: '}]) |
67 | ->prepend_content('.last-item' => [{type=>'TEXT', raw=>'LAST: '}]) |
68 | ->replace_content('title' => 'Hello World') |
69 | ->repeat_content( |
70 | '.items' => [ |
71 | map { |
72 | my $v = $_; |
73 | sub { |
74 | $_->replace_content('.first' => $v) |
75 | ->replace_content('.second' => $v); |
76 | }, |
77 | } (111,222) |
78 | ] |
79 | ) |
80 | ->to_html; |
81 | |
82 | like( |
83 | HTML::Zoom |
84 | ->from_html(q[<title>Hi!</title>]) |
85 | ->$title('Hello NYC') |
86 | ->to_html, |
87 | qr/Hello NYC/, |
88 | 'Got correct title(custom subref)' |
89 | ); |
90 | |
91 | like( |
92 | HTML::Zoom |
93 | ->from_html(q[<p>Hi!</p>]) |
94 | ->replace_content(p=>'Ask not what your country can do for you...') |
95 | ->to_html, |
96 | qr/Ask not what your country can do for you\.\.\./, |
97 | 'Got correct from replace_content' |
98 | ); |
99 | |
100 | like( |
101 | HTML::Zoom |
102 | ->from_html(q[<p class="first">Hi!</p>]) |
94a3ddd9 |
103 | ->add_to_attribute('p', class => 'para') |
104 | ->to_html, |
105 | qr/first para/, |
106 | 'Got correct from add_to_attribute' |
107 | ); |
108 | |
109 | like( |
110 | HTML::Zoom |
111 | ->from_html(q[<p class="first">Hi!</p>]) |
112 | ->set_attribute('p', class => 'para') |
113 | ->to_html, |
114 | qr/class="para"/, |
115 | 'Got correct from set_attribute' |
116 | ); |
117 | |
b2d87676 |
118 | like( |
119 | HTML::Zoom |
120 | ->from_html(q[<p class="first">Hi!</p>]) |
121 | ->set_attribute('p.first' => { class => 'para' }) |
122 | ->to_html, |
123 | qr/class="para"/, |
124 | 'Got correct from set_attribute (via hashref)' |
125 | ); |
126 | |
94a3ddd9 |
127 | is( |
128 | HTML::Zoom |
129 | ->from_html(q[<p class="first">Hi!</p>]) |
130 | ->add_before(p=>[{type=>'TEXT', raw=>'added before'}]) |
131 | ->to_html, |
132 | 'added before<p class="first">Hi!</p>', |
133 | 'Got correct from add_before' |
134 | ); |
135 | |
136 | is( |
137 | HTML::Zoom |
138 | ->from_html(q[<p class="first">Hi!</p>]) |
139 | ->add_after(p=>[{type=>'TEXT', raw=>'added after'}]) |
140 | ->to_html, |
141 | '<p class="first">Hi!</p>added after', |
142 | 'Got correct from add_after' |
143 | ); |
144 | |
145 | is( |
146 | HTML::Zoom |
147 | ->from_html(q[<p class="first">Hi!</p>]) |
148 | ->prepend_content(p=>[{type=>'TEXT', raw=>'prepend_content'}]) |
149 | ->to_html, |
150 | '<p class="first">prepend_contentHi!</p>', |
151 | 'Got correct from prepend_content' |
152 | ); |
153 | |
154 | is( |
155 | HTML::Zoom |
156 | ->from_html(q[<p class="first">Hi!</p>]) |
157 | ->append_content(p=>[{type=>'TEXT', raw=>'append_content'}]) |
158 | ->to_html, |
159 | '<p class="first">Hi!append_content</p>', |
160 | 'Got correct from append_content' |
161 | ); |
162 | |
163 | { |
164 | my @body; |
165 | ok my $body = HTML::Zoom |
166 | ->from_html(q[<div>My Stuff Is Here</div>]) |
167 | ->collect_content(div => { into => \@body, passthrough => 1}) |
168 | ->to_html, 'Collected Content'; |
169 | |
170 | is $body, "<div>My Stuff Is Here</div>", "Got expected"; |
171 | |
172 | is( |
173 | HTML::Zoom->from_events(\@body)->to_html, |
174 | 'My Stuff Is Here', |
175 | 'Collected the right stuff', |
176 | ); |
177 | } |
178 | |
179 | like( |
180 | HTML::Zoom |
181 | ->from_html(q[<ul class="items"><li class="first">first</li><li class="second">second</li></ul>]) |
182 | ->repeat_content( |
183 | '.items' => [ |
184 | map { |
185 | my $v = $_; |
186 | sub { |
187 | $_->replace_content('.first' => $v) |
188 | ->replace_content('.second' => $v); |
189 | }, |
190 | } (111,222) |
191 | ] |
192 | ) |
193 | ->to_html, |
194 | qr[<ul class="items"><li class="first">111</li><li class="second">111</li><li class="first">222</li><li class="second">222</li></ul>], |
195 | 'Got correct list' |
196 | ); |
197 | |
198 | { |
199 | ok my $z = HTML::Zoom |
200 | ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>]) |
201 | ->add_before(p=>'added before'); |
202 | |
203 | is $z->to_html, '<div>Life, whatever</div>added before<p class="first">Hi!</p>', |
204 | 'Got correct from add_before'; |
205 | } |
206 | |
207 | { |
208 | ok my $z = HTML::Zoom |
209 | ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>]) |
210 | ->add_after(p=>'added after'); |
211 | |
212 | is $z->to_html, '<div>Life, whatever</div><p class="first">Hi!</p>added after', |
213 | 'Got correct from add_after'; |
214 | } |
215 | |
216 | { |
217 | ok my $z = HTML::Zoom |
218 | ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>]) |
219 | ->append_content(p=>'appended'); |
220 | |
221 | is $z->to_html, '<div>Life, whatever</div><p class="first">Hi!appended</p>', |
222 | 'Got correct from append_content'; |
223 | } |
224 | |
225 | { |
226 | ok my $z = HTML::Zoom |
227 | ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>]) |
228 | ->prepend_content(p=>'prepended'); |
229 | |
230 | |
231 | is $z->to_html, '<div>Life, whatever</div><p class="first">prependedHi!</p>', |
232 | 'Got correct from prepend_content'; |
233 | } |
234 | |
235 | { |
236 | ok my $z = HTML::Zoom |
237 | ->from_html(q[<ul><li>Test</li></ul>]) |
238 | ->select('ul') |
239 | ->repeat_content( |
240 | [ |
241 | sub { $_->select('li')->replace_content('Real Life1') }, |
242 | sub { $_->select('li')->replace_content('Real Life2') }, |
243 | sub { $_->select('li')->replace_content('Real Life3') }, |
244 | ], |
245 | ) |
246 | ->to_html; |
247 | |
248 | is $z, '<ul><li>Real Life1</li><li>Real Life2</li><li>Real Life3</li></ul>', |
249 | 'Got correct from repeat_content'; |
250 | } |
251 | |
252 | { |
253 | ok my $z = HTML::Zoom |
254 | ->from_html(q[<ul><li>Test</li></ul>]) |
255 | ->select('ul') |
256 | ->repeat_content([ |
257 | map { my $i = $_; +sub {$_->select('li')->replace_content("Real Life$i")} } (1,2,3) |
258 | ]) |
259 | ->to_html; |
260 | |
261 | is $z, '<ul><li>Real Life1</li><li>Real Life2</li><li>Real Life3</li></ul>', |
262 | 'Got correct from repeat_content'; |
263 | } |
264 | |
265 | |
266 | use HTML::Zoom::CodeStream; |
267 | sub code_stream (&) { |
268 | my $code = shift; |
269 | return sub { |
270 | HTML::Zoom::CodeStream->new({ |
271 | code => $code, |
272 | }); |
273 | } |
274 | } |
275 | |
276 | { |
277 | my @list = qw(foo bar baz); |
278 | ok my $z = HTML::Zoom |
279 | ->from_html(q[<ul><li>Test</li></ul>]) |
280 | ->select('ul') |
281 | ->repeat_content(code_stream { |
282 | if (my $name = shift @list) { |
283 | return sub { $_->select('li')->replace_content($name) }; |
284 | } else { |
285 | return |
286 | } |
287 | }) |
288 | ->to_html; |
289 | |
290 | is $z, '<ul><li>foo</li><li>bar</li><li>baz</li></ul>', |
291 | 'Got correct from repeat_content'; |
292 | } |
293 | |
294 | { |
295 | my @list = qw(foo bar baz); |
296 | ok my $z = HTML::Zoom |
297 | ->from_html(q[<ul><li>Test</li></ul>]) |
298 | ->select('ul') |
299 | ->repeat_content(sub { |
300 | HTML::Zoom::CodeStream->new({ |
301 | code => sub { |
302 | if (my $name = shift @list) { |
303 | return sub { $_->select('li')->replace_content($name) }; |
304 | } else { |
305 | return |
306 | } |
307 | } |
308 | }); |
309 | }) |
310 | ->to_html; |
311 | |
312 | is $z, '<ul><li>foo</li><li>bar</li><li>baz</li></ul>', |
313 | 'Got correct from repeat_content'; |
314 | } |
315 | |
6af04df5 |
316 | { |
317 | ok my $dwim = HTML::Zoom |
318 | ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>]) |
319 | ->replace_content({ |
320 | 'li.foo' => ['foo'], |
321 | 'li.bar' => ['bar'], |
322 | })->to_html; |
323 | is $dwim, '<ul><li class="foo">foo</li><li class="bar">bar</li></ul>', |
701459a4 |
324 | 'Hashref selectors (via replace_content)'; |
efc6c054 |
325 | } |
326 | |
327 | { |
328 | ok my $dwim = HTML::Zoom |
329 | ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>]) |
330 | ->set_attribute({ |
331 | 'li.foo' => [ class => 'baz' ], |
332 | 'li.bar' => [ class => 'qux' ], |
333 | })->to_html; |
334 | is $dwim, '<ul><li class="baz"></li><li class="qux"></li></ul>', |
701459a4 |
335 | 'Hashref selectors (via set_attribute)'; |
336 | } |
337 | |
338 | { |
339 | ok my $dwim = HTML::Zoom |
340 | ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>]) |
341 | ->select('ul')->collect({ |
342 | passthrough => 1, |
343 | filter => sub { |
344 | $_->set_attribute({ |
345 | 'li.foo' => [ class => 'baz' ], |
346 | 'li.bar' => [ class => 'qux' ], |
347 | }); |
348 | } |
349 | })->to_html; |
350 | is $dwim, '<ul><li class="baz"></li><li class="qux"></li></ul>', |
351 | 'Hashref selectors on codestream'; |
6af04df5 |
352 | } |
353 | |
bce61371 |
354 | { |
355 | ok my $dwim = HTML::Zoom |
356 | ->from_html(q[<ul><li class="foo" name="bar"></li><li class="bar"></li></ul>]) |
357 | ->select('ul')->collect({ |
358 | passthrough => 1, |
359 | filter => sub { |
360 | $_->set_attribute({ |
361 | 'li.foo' => [{ class => 'baz', name => 'moo', }], |
362 | 'li.bar' => [ class => 'qux' ], |
363 | }); |
364 | } |
365 | })->to_html; |
366 | is $dwim, '<ul><li class="baz" name="moo"></li><li class="qux"></li></ul>', |
367 | 'Hashref selectors with hashref attributes on codestream'; |
368 | } |
369 | |
94a3ddd9 |
370 | done_testing; |