use strict; use warnings FATAL => 'all'; use HTML::Zoom; use Test::More; ok my $zoom = HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(< Hi!

Test

Some Stuff

More Stuff

Even More Stuff

  1. First
  2. Stuff A
  3. Stuff B
  4. Stuff C
  5. Last

Even More stuff

Some Stuff

Sub Item

HTML ok ! eval { $zoom->non_method('.first-param' => 'First!'); 1}, 'Properly die on bad method'; ok my $title = sub { my ($z, $content) = @_; $z = $z->replace_content(title =>$content); return $z; }; ok my $dwim = $zoom ->$title('Hello NYC') ->replace_content('.first-para' => 'First!') ->replace_content('.last-para' => 'Last!') ->add_to_attribute(p => class => 'para') ->prepend_content('.first-item' => [{type=>'TEXT', raw=>'FIRST: '}]) ->prepend_content('.last-item' => [{type=>'TEXT', raw=>'LAST: '}]) ->replace_content('title' => 'Hello World') ->repeat_content( '.items' => [ map { my $v = $_; sub { $_->replace_content('.first' => $v) ->replace_content('.second' => $v); }, } (111,222) ] ) ->to_html; like( HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[Hi!]) ->$title('Hello NYC') ->to_html, qr/Hello NYC/, 'Got correct title(custom subref)' ); like( HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[

Hi!

]) ->replace_content(p=>'Ask not what your country can do for you...') ->to_html, qr/Ask not what your country can do for you\.\.\./, 'Got correct from replace_content' ); like( HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[

Hi!

]) ->add_to_attribute('p', {name => 'class', value => 'para'}) ->to_html, qr/first para/, 'Got correct from add_to_attribute' ); like( HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[

Hi!

]) ->add_to_attribute('p', class => 'para') ->to_html, qr/first para/, 'Got correct from add_to_attribute' ); like( HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[

Hi!

]) ->set_attribute('p', class => 'para') ->to_html, qr/class="para"/, 'Got correct from set_attribute' ); like( HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[

Hi!

]) ->set_attribute('p', {name => 'class', value => 'para'}) ->to_html, qr/class="para"/, 'Got correct from set_attribute' ); is( HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[

Hi!

]) ->add_before(p=>[{type=>'TEXT', raw=>'added before'}]) ->to_html, 'added before

Hi!

', 'Got correct from add_before' ); is( HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[

Hi!

]) ->add_after(p=>[{type=>'TEXT', raw=>'added after'}]) ->to_html, '

Hi!

added after', 'Got correct from add_after' ); is( HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[

Hi!

]) ->prepend_content(p=>[{type=>'TEXT', raw=>'prepend_content'}]) ->to_html, '

prepend_contentHi!

', 'Got correct from prepend_content' ); is( HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[

Hi!

]) ->append_content(p=>[{type=>'TEXT', raw=>'append_content'}]) ->to_html, '

Hi!append_content

', 'Got correct from append_content' ); { my @body; ok my $body = HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[
My Stuff Is Here
]) ->collect_content(div => { into => \@body, passthrough => 1}) ->to_html, 'Collected Content'; is $body, "
My Stuff Is Here
", "Got expected"; is( HTML::Zoom->from_events(\@body)->to_html, 'My Stuff Is Here', 'Collected the right stuff', ); } like( HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[]) ->repeat_content( '.items' => [ map { my $v = $_; sub { $_->replace_content('.first' => $v) ->replace_content('.second' => $v); }, } (111,222) ] ) ->to_html, qr[], 'Got correct list' ); { ok my $z = HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[
Life, whatever

Hi!

]) ->add_before(p=>'added before'); is $z->to_html, '
Life, whatever
added before

Hi!

', 'Got correct from add_before'; } { ok my $z = HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[
Life, whatever

Hi!

]) ->add_after(p=>'added after'); is $z->to_html, '
Life, whatever

Hi!

added after', 'Got correct from add_after'; } { ok my $z = HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[
Life, whatever

Hi!

]) ->append_content(p=>'appended'); is $z->to_html, '
Life, whatever

Hi!appended

', 'Got correct from append_content'; } { ok my $z = HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[
Life, whatever

Hi!

]) ->prepend_content(p=>'prepended'); is $z->to_html, '
Life, whatever

prependedHi!

', 'Got correct from prepend_content'; } { ok my $z = HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[]) ->select('ul') ->repeat_content( [ sub { $_->select('li')->replace_content('Real Life1') }, sub { $_->select('li')->replace_content('Real Life2') }, sub { $_->select('li')->replace_content('Real Life3') }, ], ) ->to_html; is $z, '', 'Got correct from repeat_content'; } { ok my $z = HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[]) ->select('ul') ->repeat_content([ map { my $i = $_; +sub {$_->select('li')->replace_content("Real Life$i")} } (1,2,3) ]) ->to_html; is $z, '', 'Got correct from repeat_content'; } use HTML::Zoom::CodeStream; sub code_stream (&) { my $code = shift; return sub { HTML::Zoom::CodeStream->new({ code => $code, }); } } { my @list = qw(foo bar baz); ok my $z = HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[]) ->select('ul') ->repeat_content(code_stream { if (my $name = shift @list) { return sub { $_->select('li')->replace_content($name) }; } else { return } }) ->to_html; is $z, '', 'Got correct from repeat_content'; } { my @list = qw(foo bar baz); ok my $z = HTML::Zoom->new( { zconfig => { parser => 'HTML::Zoom::Parser::HTML::Parser' } } ) ->from_html(q[]) ->select('ul') ->repeat_content(sub { HTML::Zoom::CodeStream->new({ code => sub { if (my $name = shift @list) { return sub { $_->select('li')->replace_content($name) }; } else { return } } }); }) ->to_html; is $z, '', 'Got correct from repeat_content'; } done_testing;