From: Matt S Trout Date: Wed, 17 Feb 2010 21:32:53 +0000 (+0000) Subject: move collect method to a more sensible place in FilterBuilder X-Git-Tag: release_0.009004~85 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FHTML-Zoom.git;a=commitdiff_plain;h=76cecb10183d6e452588c98648dfa226a27b8906 move collect method to a more sensible place in FilterBuilder --- diff --git a/lib/HTML/Zoom/FilterBuilder.pm b/lib/HTML/Zoom/FilterBuilder.pm index bccc856..dc03958 100644 --- a/lib/HTML/Zoom/FilterBuilder.pm +++ b/lib/HTML/Zoom/FilterBuilder.pm @@ -80,6 +80,41 @@ sub remove_attribute { }; } +sub collect { + my ($self, $options) = @_; + my ($into, $passthrough, $inside) = @{$options}{qw(into passthrough inside)}; + sub { + my ($evt, $stream) = @_; + push(@$into, $evt) if $into && !$inside; + if ($evt->{is_in_place_close}) { + return $evt if $passthrough || $inside; + return; + } + my $name = $evt->{name}; + my $depth = 1; + my $_next = $inside ? 'peek' : 'next'; + my $collector = $self->_stream_from_code(sub { + return unless $stream; + while (my ($evt) = $stream->$_next) { + $depth++ if ($evt->{type} eq 'OPEN'); + $depth-- if ($evt->{type} eq 'CLOSE'); + unless ($depth) { + undef $stream; + return if $inside; + push(@$into, $evt) if $into; + return $evt if $passthrough; + return; + } + push(@$into, $evt) if $into; + $stream->next if $inside; + return $evt if $passthrough; + } + die "Never saw closing before end of source"; + }); + return ($passthrough||$inside) ? [ $evt, $collector ] : $collector; + }; +} + sub add_before { my ($self, $events) = @_; sub { return $self->_stream_from_array(@$events, $_[0]) }; @@ -144,39 +179,4 @@ sub replace { }; } -sub collect { - my ($self, $options) = @_; - my ($into, $passthrough, $inside) = @{$options}{qw(into passthrough inside)}; - sub { - my ($evt, $stream) = @_; - push(@$into, $evt) if $into && !$inside; - if ($evt->{is_in_place_close}) { - return $evt if $passthrough || $inside; - return; - } - my $name = $evt->{name}; - my $depth = 1; - my $_next = $inside ? 'peek' : 'next'; - my $collector = $self->_stream_from_code(sub { - return unless $stream; - while (my ($evt) = $stream->$_next) { - $depth++ if ($evt->{type} eq 'OPEN'); - $depth-- if ($evt->{type} eq 'CLOSE'); - unless ($depth) { - undef $stream; - return if $inside; - push(@$into, $evt) if $into; - return $evt if $passthrough; - return; - } - push(@$into, $evt) if $into; - $stream->next if $inside; - return $evt if $passthrough; - } - die "Never saw closing before end of source"; - }); - return ($passthrough||$inside) ? [ $evt, $collector ] : $collector; - }; -} - 1;