fixed add/before pre/app pend content stuff, added docs and test, also docs and test...
[catagits/HTML-Zoom.git] / t / dwim-autoload.t
index 76a89a7..b43f89b 100644 (file)
@@ -23,34 +23,224 @@ ok my $zoom = HTML::Zoom->from_html(<<HTML);
         <li class="last-item odd">Last</li>
       </ol>
       <ul class="items">
-        <li>space</li>
+        <li class="first">space</li>
+        <li class="second">space</li>
       </ul>
       <p class="body-para">Even More stuff</p>
       <p class="last-para">Some Stuff</p>
     </div>
+    <div id="blocks">
+      <h2 class="h2-item">Sub Item</h2>
+    </div>
+    <ul id="people">
+        <li class="name">name</li>
+        <li class="email">email</li>
+    </ul>
+    <ul id="people2">
+        <li class="name">name</li>
+        <li class="email">email</li>
+    </ul>
+    <ul id="object">
+        <li class="aa">AA</li>
+        <li class="bb">BB</li>
+    </ul>
     <p id="footer">Copyright 2222</p>
   </body>
 </html>
 HTML
 
-ok ! eval { $zoom->ressplace_content('.first-param' => 'First!'); 1},
+ok ! eval { $zoom->non_method('.first-param' => 'First!'); 1},
   'Properly die on bad method';
 
-ok my $html = $zoom
+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!')
-  ->prepend_content(
-    '.first-item' => [{type=>'TEXT', raw=>'FIRST: '}],
-    '.last-item' => [{type=>'TEXT', raw=>'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)
+    ]
   )
-  ->fill('.odd' => 'IS ODD')
-  ->fill('.items' => [
-        sub { $_->select('li')->replace_content("XXX")},
-        sub { $_->select('li')->replace_content("YYY")},
-        sub { $_->select('li')->replace_content("ZZZ", "000")},
-  ])
   ->to_html;
 
-warn $html;
+##warn  $dwim;
+
+like(
+  HTML::Zoom
+    ->from_html(q[<title>Hi!</title>])
+    ->$title('Hello NYC')
+    ->to_html,
+  qr/Hello NYC/,
+  'Got correct title(custom subref)'
+);
+
+like(
+  HTML::Zoom
+    ->from_html(q[<p>Hi!</p>])
+    ->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
+    ->from_html(q[<p class="first">Hi!</p>])
+    ->add_to_attribute('p', {name => 'class', value => 'para'})
+    ->to_html,
+  qr/first para/,
+  'Got correct from add_to_attribute'
+);
+
+like(
+  HTML::Zoom
+    ->from_html(q[<p class="first">Hi!</p>])
+    ->add_to_attribute('p', class => 'para')
+    ->to_html,
+  qr/first para/,
+  'Got correct from add_to_attribute'
+);
+
+like(
+  HTML::Zoom
+    ->from_html(q[<p class="first">Hi!</p>])
+    ->set_attribute('p', class => 'para')
+    ->to_html,
+  qr/class="para"/,
+  'Got correct from set_attribute'
+);
+
+like(
+  HTML::Zoom
+    ->from_html(q[<p class="first">Hi!</p>])
+    ->set_attribute('p', {name => 'class', value => 'para'})
+    ->to_html,
+  qr/class="para"/,
+  'Got correct from set_attribute'
+);
+
+is(
+  HTML::Zoom
+    ->from_html(q[<p class="first">Hi!</p>])
+    ->add_before(p=>[{type=>'TEXT', raw=>'added before'}])
+    ->to_html,
+  'added before<p class="first">Hi!</p>',
+  'Got correct from add_before'
+);
+
+is(
+  HTML::Zoom
+    ->from_html(q[<p class="first">Hi!</p>])
+    ->add_after(p=>[{type=>'TEXT', raw=>'added after'}])
+    ->to_html,
+  '<p class="first">Hi!</p>added after',
+  'Got correct from add_after'
+);
+
+is(
+  HTML::Zoom
+    ->from_html(q[<p class="first">Hi!</p>])
+    ->prepend_content(p=>[{type=>'TEXT', raw=>'prepend_content'}])
+    ->to_html,
+  '<p class="first">prepend_contentHi!</p>',
+  'Got correct from prepend_content'
+);
+
+is(
+  HTML::Zoom
+    ->from_html(q[<p class="first">Hi!</p>])
+    ->append_content(p=>[{type=>'TEXT', raw=>'append_content'}])
+    ->to_html,
+  '<p class="first">Hi!append_content</p>',
+  'Got correct from append_content'
+);
+
+{
+    my @body;
+    ok my $body = HTML::Zoom
+      ->from_html(q[<div>My Stuff Is Here</div>])
+      ->collect_content(div => { into => \@body, passthrough => 1})
+      ->to_html, 'Collected Content';
+
+    is $body, "<div>My Stuff Is Here</div>", "Got expected";
+
+    is(
+      HTML::Zoom->from_events(\@body)->to_html,
+      'My Stuff Is Here',
+      'Collected the right stuff',
+    );
+}
+
+like(
+  HTML::Zoom
+    ->from_html(q[<ul class="items"><li class="first">first</li><li class="second">second</li></ul>])
+    ->repeat_content(
+      '.items' => [ 
+        map { 
+          my $v = $_;
+          sub {
+            $_->replace_content('.first' => $v)
+              ->replace_content('.second' => $v);
+          },
+        } (111,222)
+      ]
+    )
+    ->to_html,
+  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>],
+  'Got correct list'
+);
+
+{
+  ok my $z = HTML::Zoom
+    ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
+    ->add_before(p=>'added before');
+
+  is $z->to_html, '<div>Life, whatever</div>added before<p class="first">Hi!</p>',
+    'Got correct from add_before';
+}
+
+{
+  ok my $z = HTML::Zoom
+    ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
+    ->add_after(p=>'added after');
+
+  is $z->to_html, '<div>Life, whatever</div><p class="first">Hi!</p>added after',
+    'Got correct from add_after';
+}
+
+{
+  ok my $z = HTML::Zoom
+    ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
+    ->append_content(p=>'appended');
+
+  is $z->to_html, '<div>Life, whatever</div><p class="first">Hi!appended</p>',
+    'Got correct from append_content';
+}
+
+{
+  ok my $z = HTML::Zoom
+    ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>])
+    ->prepend_content(p=>'prepended');
+    
+
+  is $z->to_html, '<div>Life, whatever</div><p class="first">prependedHi!</p>',
+    'Got correct from prepend_content';
+}
 
 done_testing;