2 use warnings FATAL => 'all';
7 ok my $zoom = HTML::Zoom->from_html(<<HTML);
12 <body id="content-area">
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>
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>
26 <li class="first">space</li>
27 <li class="second">space</li>
29 <p class="body-para">Even More stuff</p>
30 <p class="last-para">Some Stuff</p>
33 <h2 class="h2-item">Sub Item</h2>
36 <li class="name">name</li>
37 <li class="email">email</li>
40 <li class="name">name</li>
41 <li class="email">email</li>
44 <li class="aa">AA</li>
45 <li class="bb">BB</li>
47 <p id="footer">Copyright 2222</p>
52 ok ! eval { $zoom->non_method('.first-param' => 'First!'); 1},
53 'Properly die on bad method';
56 my ($z, $content) = @_;
57 $z = $z->replace_content(title =>$content);
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')
74 $_->replace_content('.first' => $v)
75 ->replace_content('.second' => $v);
84 ->from_html(q[<title>Hi!</title>])
88 'Got correct title(custom subref)'
93 ->from_html(q[<p>Hi!</p>])
94 ->replace_content(p=>'Ask not what your country can do for you...')
96 qr/Ask not what your country can do for you\.\.\./,
97 'Got correct from replace_content'
102 ->from_html(q[<p class="first">Hi!</p>])
103 ->add_to_attribute('p', class => 'para')
106 'Got correct from add_to_attribute'
111 ->from_html(q[<p class="first">Hi!</p>])
112 ->set_attribute('p', class => 'para')
115 'Got correct from set_attribute'
120 ->from_html(q[<p class="first">Hi!</p>])
121 ->add_before(p=>[{type=>'TEXT', raw=>'added before'}])
123 'added before<p class="first">Hi!</p>',
124 'Got correct from add_before'
129 ->from_html(q[<p class="first">Hi!</p>])
130 ->add_after(p=>[{type=>'TEXT', raw=>'added after'}])
132 '<p class="first">Hi!</p>added after',
133 'Got correct from add_after'
138 ->from_html(q[<p class="first">Hi!</p>])
139 ->prepend_content(p=>[{type=>'TEXT', raw=>'prepend_content'}])
141 '<p class="first">prepend_contentHi!</p>',
142 'Got correct from prepend_content'
147 ->from_html(q[<p class="first">Hi!</p>])
148 ->append_content(p=>[{type=>'TEXT', raw=>'append_content'}])
150 '<p class="first">Hi!append_content</p>',
151 'Got correct from append_content'
156 ok my $body = HTML::Zoom
157 ->from_html(q[<div>My Stuff Is Here</div>])
158 ->collect_content(div => { into => \@body, passthrough => 1})
159 ->to_html, 'Collected Content';
161 is $body, "<div>My Stuff Is Here</div>", "Got expected";
164 HTML::Zoom->from_events(\@body)->to_html,
166 'Collected the right stuff',
172 ->from_html(q[<ul class="items"><li class="first">first</li><li class="second">second</li></ul>])
178 $_->replace_content('.first' => $v)
179 ->replace_content('.second' => $v);
185 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>],
190 ok my $z = HTML::Zoom
191 ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
192 ->add_before(p=>'added before');
194 is $z->to_html, '<div>Life, whatever</div>added before<p class="first">Hi!</p>',
195 'Got correct from add_before';
199 ok my $z = HTML::Zoom
200 ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
201 ->add_after(p=>'added after');
203 is $z->to_html, '<div>Life, whatever</div><p class="first">Hi!</p>added after',
204 'Got correct from add_after';
208 ok my $z = HTML::Zoom
209 ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
210 ->append_content(p=>'appended');
212 is $z->to_html, '<div>Life, whatever</div><p class="first">Hi!appended</p>',
213 'Got correct from append_content';
217 ok my $z = HTML::Zoom
218 ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
219 ->prepend_content(p=>'prepended');
222 is $z->to_html, '<div>Life, whatever</div><p class="first">prependedHi!</p>',
223 'Got correct from prepend_content';
227 ok my $z = HTML::Zoom
228 ->from_html(q[<ul><li>Test</li></ul>])
232 sub { $_->select('li')->replace_content('Real Life1') },
233 sub { $_->select('li')->replace_content('Real Life2') },
234 sub { $_->select('li')->replace_content('Real Life3') },
239 is $z, '<ul><li>Real Life1</li><li>Real Life2</li><li>Real Life3</li></ul>',
240 'Got correct from repeat_content';
244 ok my $z = HTML::Zoom
245 ->from_html(q[<ul><li>Test</li></ul>])
248 map { my $i = $_; +sub {$_->select('li')->replace_content("Real Life$i")} } (1,2,3)
252 is $z, '<ul><li>Real Life1</li><li>Real Life2</li><li>Real Life3</li></ul>',
253 'Got correct from repeat_content';
257 use HTML::Zoom::CodeStream;
258 sub code_stream (&) {
261 HTML::Zoom::CodeStream->new({
268 my @list = qw(foo bar baz);
269 ok my $z = HTML::Zoom
270 ->from_html(q[<ul><li>Test</li></ul>])
272 ->repeat_content(code_stream {
273 if (my $name = shift @list) {
274 return sub { $_->select('li')->replace_content($name) };
281 is $z, '<ul><li>foo</li><li>bar</li><li>baz</li></ul>',
282 'Got correct from repeat_content';
286 my @list = qw(foo bar baz);
287 ok my $z = HTML::Zoom
288 ->from_html(q[<ul><li>Test</li></ul>])
290 ->repeat_content(sub {
291 HTML::Zoom::CodeStream->new({
293 if (my $name = shift @list) {
294 return sub { $_->select('li')->replace_content($name) };
303 is $z, '<ul><li>foo</li><li>bar</li><li>baz</li></ul>',
304 'Got correct from repeat_content';
308 ok my $dwim = HTML::Zoom
309 ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>])
314 is $dwim, '<ul><li class="foo">foo</li><li class="bar">bar</li></ul>',
315 'Hashref selectors (via replace_content)';
319 ok my $dwim = HTML::Zoom
320 ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>])
322 'li.foo' => [ class => 'baz' ],
323 'li.bar' => [ class => 'qux' ],
325 is $dwim, '<ul><li class="baz"></li><li class="qux"></li></ul>',
326 'Hashref selectors (via set_attribute)';
330 ok my $dwim = HTML::Zoom
331 ->from_html(q[<ul><li class="foo"></li><li class="bar"></li></ul>])
332 ->select('ul')->collect({
336 'li.foo' => [ class => 'baz' ],
337 'li.bar' => [ class => 'qux' ],
341 is $dwim, '<ul><li class="baz"></li><li class="qux"></li></ul>',
342 'Hashref selectors on codestream';
346 ok my $dwim = HTML::Zoom
347 ->from_html(q[<ul><li class="foo" name="bar"></li><li class="bar"></li></ul>])
348 ->select('ul')->collect({
352 'li.foo' => [{ class => 'baz', name => 'moo', }],
353 'li.bar' => [ class => 'qux' ],
357 is $dwim, '<ul><li class="baz" name="moo"></li><li class="qux"></li></ul>',
358 'Hashref selectors with hashref attributes on codestream';