1 package HTML::Zoom::FilterBuilder;
4 use warnings FATAL => 'all';
5 use base qw(HTML::Zoom::SubObject);
6 use HTML::Zoom::CodeStream;
8 sub _stream_from_code {
9 shift->_zconfig->stream_utils->stream_from_code(@_)
12 sub _stream_from_array {
13 shift->_zconfig->stream_utils->stream_from_array(@_)
16 sub _stream_from_proto {
17 shift->_zconfig->stream_utils->stream_from_proto(@_)
21 shift->_zconfig->stream_utils->stream_concat(@_)
25 my ($self, $args) = @_;
26 my ($name, $value) = @{$args}{qw(name value)};
28 my $a = (my $evt = $_[0])->{attrs};
29 my $e = exists $a->{$name};
30 +{ %$evt, raw => undef, raw_attrs => undef,
31 attrs => { %$a, $name => $value },
32 ($e # add to name list if not present
34 : (attr_names => [ @{$evt->{attr_names}}, $name ]))
40 my ($self, $args) = @_;
41 my ($name, $value) = @{$args}{qw(name value)};
43 my $a = (my $evt = $_[0])->{attrs};
44 my $e = exists $a->{$name};
45 +{ %$evt, raw => undef, raw_attrs => undef,
48 $name => join(' ', ($e ? $a->{$name} : ()), $value)
50 ($e # add to name list if not present
52 : (attr_names => [ @{$evt->{attr_names}}, $name ]))
57 sub remove_attribute {
58 my ($self, $args) = @_;
59 my $name = $args->{name};
61 my $a = (my $evt = $_[0])->{attrs};
62 return $evt unless exists $a->{$name};
63 $a = { %$a }; delete $a->{$name};
64 +{ %$evt, raw => undef, raw_attrs => undef,
66 attr_names => [ grep $_ ne $name, @{$evt->{attr_names}} ]
72 my ($self, $options) = @_;
73 my ($into, $passthrough, $content, $filter) =
74 @{$options}{qw(into passthrough content filter)};
76 my ($evt, $stream) = @_;
77 # We wipe the contents of @$into here so that other actions depending
78 # on this (such as a repeater) can be invoked multiple times easily.
79 # I -suspect- it's better for that state reset to be managed here; if it
80 # ever becomes painful the decision should be revisited
82 @$into = $content ? () : ($evt);
84 if ($evt->{is_in_place_close}) {
85 return $evt if $passthrough || $content;
88 my $name = $evt->{name};
90 my $_next = $content ? 'peek' : 'next';
91 $stream = do { local $_ = $stream; $filter->($stream) } if $filter;
92 my $collector = $self->_stream_from_code(sub {
93 return unless $stream;
94 while (my ($evt) = $stream->$_next) {
95 $depth++ if ($evt->{type} eq 'OPEN');
96 $depth-- if ($evt->{type} eq 'CLOSE');
100 push(@$into, $evt) if $into;
101 return $evt if $passthrough;
104 push(@$into, $evt) if $into;
105 $stream->next if $content;
106 return $evt if $passthrough;
108 die "Never saw closing </${name}> before end of source";
110 return ($passthrough||$content) ? [ $evt, $collector ] : $collector;
114 sub collect_content {
115 my ($self, $options) = @_;
116 $self->collect({ %{$options||{}}, content => 1 })
120 my ($self, $events) = @_;
121 sub { return $self->_stream_from_array(@$events, $_[0]) };
125 my ($self, $events) = @_;
126 my $coll_proto = $self->collect({ passthrough => 1 });
129 my $emit = $self->_stream_from_array(@$events);
130 my $coll = &$coll_proto;
131 return ref($coll) eq 'HASH' # single event, no collect
133 : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
137 sub prepend_content {
138 my ($self, $events) = @_;
141 if ($evt->{is_in_place_close}) {
142 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
143 return [ $evt, $self->_stream_from_array(
144 @$events, { type => 'CLOSE', name => $evt->{name} }
147 return $self->_stream_from_array($evt, @$events);
152 my ($self, $events) = @_;
153 my $coll_proto = $self->collect({ passthrough => 1, content => 1 });
156 if ($evt->{is_in_place_close}) {
157 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
158 return [ $evt, $self->_stream_from_array(
159 @$events, { type => 'CLOSE', name => $evt->{name} }
162 my $coll = &$coll_proto;
163 my $emit = $self->_stream_from_array(@$events);
164 return [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
169 my ($self, $replace_with, $options) = @_;
170 my $coll_proto = $self->collect($options);
172 my ($evt, $stream) = @_;
173 my $emit = $self->_stream_from_proto($replace_with);
174 my $coll = &$coll_proto;
175 # For a straightforward replace operation we can, in fact, do the emit
176 # -before- the collect, and my first cut did so. However in order to
177 # use the captured content in generating the new content, we need
178 # the collect stage to happen first - and it seems highly unlikely
179 # that in normal operation the collect phase will take long enough
180 # for the difference to be noticeable
183 ? (ref $coll eq 'ARRAY'
184 ? [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ]
185 : $self->_stream_concat($coll, $emit)
192 sub replace_content {
193 my ($self, $replace_with, $options) = @_;
194 $self->replace($replace_with, { %{$options||{}}, content => 1 })
198 my ($self, $repeat_for, $options) = @_;
199 $options->{into} = \my @into;
201 my $repeat_between = delete $options->{repeat_between};
202 if ($repeat_between) {
203 $options->{filter} = sub {
204 $_->select($repeat_between)->collect({ into => \@between })
208 my $s = $self->_stream_from_proto($repeat_for);
209 # We have to test $repeat_between not @between here because
210 # at the point we're constructing our return stream @between
211 # hasn't been populated yet - but we can test @between in the
212 # map routine because it has been by then and that saves us doing
213 # the extra stream construction if we don't need it.
214 if ($repeat_between) {
216 local $_ = $self->_stream_from_array(@into);
217 (@between && $s->peek)
218 ? $self->_stream_concat(
219 $_[0]->($_), $self->_stream_from_array(@between)
226 local $_ = $self->_stream_from_array(@into);
232 $self->replace($repeater, $options);
236 my ($self, $repeat_for, $options) = @_;
237 $self->repeat($repeat_for, { %{$options||{}}, content => 1 })