| 1 | package HTML::Zoom::FilterStream; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 15 | ▶ | use strictures 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 15 | ▶ | use base qw(HTML::Zoom::StreamBase); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 173 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 7 | 173 | my ($class, $args) = @_; | |||||||||||||||||||||||||||
| 8 | 173 | ▶ | if ($args->{filters}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 9 | die "Single filter please (XXX FIXME)" | ||||||||||||||||||||||||||||
| 10 | 127 | ▶ | unless @{$args->{filters}} == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 11 | 127 | $args->{filter} = $args->{filters}[0]; | |||||||||||||||||||||||||||
| 12 | } | ||||||||||||||||||||||||||||
| 13 | bless( | ||||||||||||||||||||||||||||
| 14 | { | ||||||||||||||||||||||||||||
| 15 | _stream => $args->{stream}, | ||||||||||||||||||||||||||||
| 16 | _match => $args->{match}, | ||||||||||||||||||||||||||||
| 17 | _filter => $args->{filter}, | ||||||||||||||||||||||||||||
| 18 | _zconfig => $args->{zconfig}, | ||||||||||||||||||||||||||||
| 19 | }, | ||||||||||||||||||||||||||||
| 20 | 173 | $class | |||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 3014 | ▶ | sub _next { | ||||||||||||||||||||||||||
|
Subroutine
_next: called CC=16 SCAR=27.7
| |||||||||||||||||||||||||||||
| 25 | 3014 | my ($self, $am_peek) = @_; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # if our main stream is already gone then we can short-circuit | ||||||||||||||||||||||||||||
| 28 | # straight out - there's no way for an alternate stream to be there | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 3014 | ▶ | return unless $self->{_stream}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # if we have an alternate stream (provided by a filter call resulting | ||||||||||||||||||||||||||||
| 33 | # from a match on the main stream) then we want to read from that until | ||||||||||||||||||||||||||||
| 34 | # it's gone - we're still effectively "in the match" but this is the | ||||||||||||||||||||||||||||
| 35 | # point at which that fact is abstracted away from downstream consumers | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 3012 | ▶ | my $_next = $am_peek ? 'peek' : 'next'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 3012 | ▶ | if (my $alt = $self->{_alt_stream}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 625 | ▶ | if (my ($evt) = $alt->$_next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 489 | ▶ | $self->{_peeked_from} = $alt if $am_peek; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | 489 | return $evt; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # once the alternate stream is exhausted we can throw it away so future | ||||||||||||||||||||||||||||
| 47 | # requests fall straight through to the main stream | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 136 | delete $self->{_alt_stream}; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # if there's no alternate stream currently, process the main stream | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2523 | ▶ | while (my ($evt) = $self->{_stream}->$_next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 2362 | ▶ | $self->{_peeked_from} = $self->{_stream} if $am_peek; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | # don't match this event? return it immediately | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 2362 | ▶ | return $evt unless $evt->{type} eq 'OPEN' and $self->{_match}->($evt); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$evt->{"type"} eq "OPEN" and $self->{"_match"}->($evt)
MC/DC100%2/2
$evt->{"type"} eq "OPEN" and $self->{"_match"}->($evt)
$evt->{"type"} eq "OPEN"
$self->{"_match"}->($evt)
Truth table100%3/3
A and B
A= $evt->{"type"} eq "OPEN"B= $self->{"_match"}->($evt)
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | # run our filter routine against the current event | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | 183 | my ($res) = $self->{_filter}->($evt, $self->{_stream}); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # if the result is just an event, we can return that now | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 183 | ▶ | return $res if ref($res) eq 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # if no result at all, jump back to the top of the loop to get the | ||||||||||||||||||||||||||||
| 71 | # next event and try again - the filter has eaten this one | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 140 | ▶ | next unless defined $res; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | # ARRAY means a pair of [ $evt, $new_stream ] | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 137 | ▶ | if (ref($res) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | 127 | $self->{_alt_stream} = $res->[1]; | |||||||||||||||||||||||||||
| 79 | 127 | return $res->[0]; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | # the filter returned a stream - if it contains something return the | ||||||||||||||||||||||||||||
| 83 | # first entry and stash it as the new alternate stream | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 10 | ▶ | if (my ($new_evt) = $res->$_next) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 86 | 9 | $self->{_alt_stream} = $res; | |||||||||||||||||||||||||||
| 87 | 9 | ▶ | $self->{_peeked_from} = $res if $am_peek; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 9 | return $new_evt; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | # we got a new alternate stream but it turned out to be empty | ||||||||||||||||||||||||||||
| 92 | # - this will happens for e.g. with an in place close (<foo />) that's | ||||||||||||||||||||||||||||
| 93 | # being removed. In that case, we fall off to loop back round and try | ||||||||||||||||||||||||||||
| 94 | # the next event from our main stream | ||||||||||||||||||||||||||||
| 95 | } continue { | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | # if we fell off the bottom (empty new alternate stream or filter ate | ||||||||||||||||||||||||||||
| 98 | # the event) then we need to advance our internal stream one so that the | ||||||||||||||||||||||||||||
| 99 | # top of the while loop gets the right thing; also, we need to clear the | ||||||||||||||||||||||||||||
| 100 | # _peeked_from in case our source stream is exhausted (it'll be | ||||||||||||||||||||||||||||
| 101 | # re-assigned if the while condition gets a new event) | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 4 | ▶ | if ($am_peek) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 104 | 3 | $self->{_stream}->next; | |||||||||||||||||||||||||||
| 105 | 3 | delete $self->{_peeked_from}; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | # main stream exhausted so throw it away so we hit the short circuit | ||||||||||||||||||||||||||||
| 110 | # at the top and return nothing to indicate to our caller we're done | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 165 | delete $self->{_stream}; | |||||||||||||||||||||||||||
| 113 | 165 | return; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 15 | 1; | |||||||||||||||||||||||||||