1 package HTML::Zoom::FilterBuilder;
6 use warnings FATAL => 'all';
7 use HTML::Zoom::CodeStream;
9 sub new { bless({}, shift) }
11 sub _stream_from_code {
12 HTML::Zoom::CodeStream->new({ code => $_[1] })
15 sub _stream_from_array {
17 HTML::Zoom::CodeStream->from_array(@_)
23 my $cur_stream = shift(@streams) or die "No streams passed";
24 HTML::Zoom::CodeStream->new({
26 return unless $cur_stream;
28 until (($evt) = $cur_stream->next) {
29 return unless $cur_stream = shift(@streams);
37 my ($self, $args) = @_;
38 my ($name, $value) = @{$args}{qw(name value)};
40 my $a = (my $evt = $_[0])->{attrs};
41 my $e = exists $a->{$name};
42 +{ %$evt, raw => undef, raw_attrs => undef,
43 attrs => { %$a, $name => $value },
44 ($e # add to name list if not present
46 : (attr_names => [ @{$evt->{attr_names}}, $name ]))
52 my ($self, $args) = @_;
53 my ($name, $value) = @{$args}{qw(name value)};
55 my $a = (my $evt = $_[0])->{attrs};
56 my $e = exists $a->{$name};
57 +{ %$evt, raw => undef, raw_attrs => undef,
60 $name => join(' ', ($e ? $a->{$name} : ()), $value)
62 ($e # add to name list if not present
64 : (attr_names => [ @{$evt->{attr_names}}, $name ]))
69 sub remove_attribute {
70 my ($self, $args) = @_;
71 my $name = $args->{name};
73 my $a = (my $evt = $_[0])->{attrs};
74 return $evt unless exists $a->{$name};
75 $a = { %$a }; delete $a->{$name};
76 +{ %$evt, raw => undef, raw_attrs => undef,
78 attr_names => [ grep $_ ne $name, @{$evt->{attr_names}} ]
84 my ($self, $options) = @_;
85 my ($into, $passthrough, $inside) = @{$options}{qw(into passthrough inside)};
87 my ($evt, $stream) = @_;
88 push(@$into, $evt) if $into && !$inside;
89 if ($evt->{is_in_place_close}) {
90 return $evt if $passthrough || $inside;
93 my $name = $evt->{name};
95 my $_next = $inside ? 'peek' : 'next';
96 my $collector = $self->_stream_from_code(sub {
97 return unless $stream;
98 while (my ($evt) = $stream->$_next) {
99 $depth++ if ($evt->{type} eq 'OPEN');
100 $depth-- if ($evt->{type} eq 'CLOSE');
104 push(@$into, $evt) if $into;
105 return $evt if $passthrough;
108 push(@$into, $evt) if $into;
109 $stream->next if $inside;
110 return $evt if $passthrough;
112 die "Never saw closing </${name}> before end of source";
114 return ($passthrough||$inside) ? [ $evt, $collector ] : $collector;
119 my ($self, $events) = @_;
120 sub { return $self->_stream_from_array(@$events, $_[0]) };
124 my ($self, $events) = @_;
127 my $emit = $self->_stream_from_array(@$events);
128 my $coll = $self->collect({ passthrough => 1 })->(@_);
129 return ref($coll) eq 'HASH' # single event, no collect
131 : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
136 my ($self, $events) = @_;
139 if ($evt->{is_in_place_close}) {
140 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
141 return [ $evt, $self->_stream_from_array(
142 @$events, { type => 'CLOSE', name => $evt->{name} }
145 return $self->_stream_from_array($evt, @$events);
150 my ($self, $events) = @_;
153 if ($evt->{is_in_place_close}) {
154 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
155 return [ $evt, $self->_stream_from_array(
156 @$events, { type => 'CLOSE', name => $evt->{name} }
159 my $coll = $self->collect({ passthrough => 1, inside => 1 })->(@_);
160 my $emit = $self->_stream_from_array(@$events);
161 return [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
166 my ($self, $events, $options) = @_;
168 my ($evt, $stream) = @_;
169 my $emit = $self->_stream_from_array(@$events);
170 my $coll = $self->collect($options)->(@_);
173 ? (ref $coll eq 'ARRAY'
174 ? [ $coll->[0], $self->_stream_concat($emit, $coll->[1]) ]
175 : $self->_stream_concat($emit, $coll)