X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FHTML%2FElement%2FLibrary.pm;fp=lib%2FHTML%2FElement%2FLibrary.pm;h=928707cca6b2d891b37a675aecf69fe98cf0df09;hb=dbfa87a3950d2074807fee15619ae6c2a6cf8a67;hp=2ebc43e8ce4b4ea88e7b2386d36a9adb6da915e9;hpb=e0dfa7be65c0072eff275132235091d71b40c4d7;p=html-element-library.git diff --git a/lib/HTML/Element/Library.pm b/lib/HTML/Element/Library.pm index 2ebc43e..928707c 100644 --- a/lib/HTML/Element/Library.pm +++ b/lib/HTML/Element/Library.pm @@ -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__ @@ -701,6 +717,22 @@ HTML:::Element::Library provides extra methods for HTML::Element. =head1 METHODS +=head2 Aliases + +These are short aliases for common operations: + +=over + +=item I<$el>->B(I<$id>) + +Finds an element given its id. Equivalent to C<< $el->look_down(id => $id) >>. + +=item I<$el>->B(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 =head3 $elem->siblings @@ -1187,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 (