]> iEval git - html-element-library.git/blobdiff - lib/HTML/Element/Library.pm
Add itercb, fid, fclass methods
[html-element-library.git] / lib / HTML / Element / Library.pm
index d4d82b8f54c489a4d38b30f62d7835772478d7df..928707cca6b2d891b37a675aecf69fe98cf0df09 100644 (file)
@@ -2,7 +2,7 @@ package HTML::Element::Library;
 use strict;
 use warnings;
 
-our $VERSION = '5.120100';
+our $VERSION = '5.210000';
 our $DEBUG = 0;
 
 use Array::Group ':all';
@@ -246,6 +246,19 @@ sub HTML::Element::iter {
        $p->replace_with(@item);
 }
 
+sub HTML::Element::itercb {
+       my ($self, $data, $code) = @_;
+       my $orig = $self;
+       my $prev = $orig;
+       for my $el (@$data) {
+               my $current = $orig->clone;
+               $code->($el, $current);
+               $prev->postinsert($current);
+               $prev = $current;
+       }
+       $orig->detach;
+}
+
 sub HTML::Element::iter2 { ## no critic (RequireArgUnpacking)
        my $tree = shift;
 
@@ -681,6 +694,9 @@ sub HTML::TreeBuilder::parse_string {
        HTML::TreeBuilder->parse($string);
 }
 
+sub HTML::Element::fid    { shift->look_down(id    => $_[0]) }
+sub HTML::Element::fclass { shift->look_down(class => qr/\b$_[0]\b/s) }
+
 1;
 __END__
 
@@ -697,13 +713,25 @@ HTML::Element::Library - HTML::Element convenience functions
 
 =head1 DESCRIPTION
 
-This method provides API calls for common actions on trees when using
-L<HTML::Tree>.
+HTML:::Element::Library provides extra methods for HTML::Element.
 
 =head1 METHODS
 
-The test suite contains examples of each of these methods in a file
-C<t/$method.t>
+=head2 Aliases
+
+These are short aliases for common operations:
+
+=over
+
+=item I<$el>->B<fid>(I<$id>)
+
+Finds an element given its id. Equivalent to C<< $el->look_down(id => $id) >>.
+
+=item I<$el>->B<fclass>(I<$class>)
+
+Finds one or more elements given one of their classes. Equivalent to C<< $el->look_down(class => qr/\b$class\b/s) >>
+
+=back
 
 =head2 Positional Querying Methods
 
@@ -887,7 +915,7 @@ we have a hashref whose keys are the same:
   my %data = (people_id => 888, phone => '444-4444', password => 'dont-you-dare-render');
 
 Then a single API call allows us to populate the HTML while excluding
-those ones we dont:
+those ones we don't:
 
   $tree->hashmap(smap => \%data, ['password']);
 
@@ -1191,6 +1219,78 @@ instead of:
 and there is no good answer. The latter would be more concise and it
 is what I should have done.
 
+=head3 Unrolling an array via a single sample element and a callback (<ul> container)
+
+This is a more advanced version of the previous method. Instead of
+cloning the sample element several times and calling
+C<replace_content> on the clone with the array element, a custom
+callback is called with the clone and array element.
+
+Here is the example from before.
+
+ <strong>Here are the things I need from the store:</strong>
+ <ul>
+   <li class="store_items">Sample item</li>
+ </ul>
+
+Code:
+
+  sub cb {
+    my ($data, $li) = @_;
+    $li->replace_content($data);
+  }
+
+  my $li = $tree->look_down(class => 'store_items');
+  my @items = qw(bread butter vodka);
+  $li->itercb(\@items, \&cb);
+
+Output is as before:
+
+ <html>
+  <head></head>
+  <body>Here are the things I need from the store:
+    <ul>
+      <li class="store_items">bread</li>
+      <li class="store_items">butter</li>
+      <li class="store_items">vodka</li>
+    </ul>
+  </body>
+ </html>
+
+Here is a more complex example (unrolling a table). HTML:
+
+  <table><thead><th>First Name<th>Last Name<th>Option</thead>
+  <tbody>
+  <tr><td class="first">First<td class="last">Last<td class="option">1
+  </tbody></table>
+
+Code:
+
+  sub tr_cb {
+    my ($data, $tr) = @_;
+    $tr->look_down(class => 'first')->replace_content($data->{first});
+    $tr->look_down(class => 'last')->replace_content($data->{last});
+    $tr->look_down(class => 'option')->replace_content($data->{option});
+  }
+
+  my @data = (
+    {first => 'Foo', last => 'Bar', option => 2},
+    {first => 'Bar', last => 'Bar', option => 3},
+    {first => 'Baz', last => 'Bar', option => 4},
+  );
+
+  my $tr = $tree->find('table')->find('tbody')->find('tr');
+  $tr->itercb(\@data, \&tr_cb);
+
+Produces:
+
+  <table><thead><th>First Name<th>Last Name<th>Option</thead>
+  <tbody>
+  <tr><td class="first">Foo<td class="last">Bar<td class="option">2
+  <tr><td class="first">Bar<td class="last">Bar<td class="option">3
+  <tr><td class="first">Baz<td class="last">Bar<td class="option">4
+  </tbody></table>
+
 =head3 Unrolling an array via n sample elements (<dl> container)
 
 C<iter()> was fine for awhile, but some things (e.g. definition lists)
@@ -1739,7 +1839,7 @@ L<HTML::Element>.
 A loltree is an arrayref consisting of arrayrefs which is used by C<<
 new_from__lol >> in L<HTML::Element> to produce HTML trees. The CPAN
 distro L<XML::Element::Tolol> creates such XML trees by parsing XML
-files, analagous to L<XML::Toolkit>. The purpose of the functions in
+files, analogous to L<XML::Toolkit>. The purpose of the functions in
 this section is to allow you manipulate a loltree programmatically.
 
 These could not be methods because if you bless a loltree, then
@@ -1867,7 +1967,7 @@ development of newchild.
 
 =head1 COPYRIGHT AND LICENSE
 
-Coypright (C) 2014 by Marius Gavrilescu
+Coypright (C) 2014-2015 by Marius Gavrilescu
 
 Copyright (C) 2004-2012 by Terrence Brannon
 
This page took 0.023449 seconds and 4 git commands to generate.