pre-filter for collect/repeat/replace
Matt S Trout [Thu, 18 Feb 2010 01:51:33 +0000 (01:51 +0000)]
lib/HTML/Zoom/FilterBuilder.pm
t/actions.t

index e9c67d8..0659cca 100644 (file)
@@ -84,7 +84,8 @@ sub remove_attribute {
 
 sub collect {
   my ($self, $options) = @_;
-  my ($into, $passthrough, $content) = @{$options}{qw(into passthrough content)};
+  my ($into, $passthrough, $content, $filter) =
+    @{$options}{qw(into passthrough content filter)};
   sub {
     my ($evt, $stream) = @_;
     # We wipe the contents of @$into here so that other actions depending
@@ -101,6 +102,7 @@ sub collect {
     my $name = $evt->{name};
     my $depth = 1;
     my $_next = $content ? 'peek' : 'next';
+    $stream = $filter->($stream) if $filter;
     my $collector = $self->_stream_from_code(sub {
       return unless $stream;
       while (my ($evt) = $stream->$_next) {
index d9b883a..790962f 100644 (file)
@@ -262,4 +262,43 @@ is(
   'repeat_content ok'
 );
 
+is(
+  run_for {
+    my @between;
+    $_->repeat_content(
+      [
+        sub {
+          HTML::Zoom::CodeStream->from_array(
+            (filter
+              filter($_ => '.name' => $r_content->('mst'))
+              => '.career' => $r_content->('Chainsaw Wielder')),
+            HTML::Zoom::CodeStream->from_array(@between)
+          )->flatten
+        },
+        sub {
+          filter
+            filter($_ => '.name' => $r_content->('mdk'))
+            => '.career' => $r_content->('Adminion')
+        },
+      ],
+      { filter => sub {
+          filter $_[0] => 'hr' => sub { $_->collect({ into => \@between }) }
+        }
+      }
+    )
+  },
+  q{<body>
+  <div class="main">
+    <span class="hilight name">mst</span>
+    <span class="career">Chainsaw Wielder</span>
+    <hr />
+    <span class="hilight name">mdk</span>
+    <span class="career">Adminion</span>
+    
+  </div>
+</body>
+},
+  'repeat_content with filter ok'
+);
+
 done_testing;