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(@_)
24 sub _flatten_stream_of_streams {
25 shift->_zconfig->stream_utils->flatten_stream_of_streams(@_)
30 my ($name, $value) = $self->_parse_attribute_args(@_);
32 my $a = (my $evt = $_[0])->{attrs};
33 my $e = exists $a->{$name};
34 +{ %$evt, raw => undef, raw_attrs => undef,
35 attrs => { %$a, $name => $value },
36 ($e # add to name list if not present
38 : (attr_names => [ @{$evt->{attr_names}}, $name ]))
43 sub _parse_attribute_args {
45 # allow ->add_attribute(name => 'value')
46 # or ->add_attribute({ name => 'name', value => 'value' })
47 my ($name, $value) = @_ > 1 ? @_ : @{$_[0]}{qw(name value)};
48 return ($name, $self->_zconfig->parser->html_escape($value));
53 my ($name, $value) = $self->_parse_attribute_args(@_);
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 = (ref($args) eq 'HASH') ? $args->{name} : $args;
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, $content, $filter, $flush_before) =
86 @{$options}{qw(into passthrough content filter flush_before)};
88 my ($evt, $stream) = @_;
89 # We wipe the contents of @$into here so that other actions depending
90 # on this (such as a repeater) can be invoked multiple times easily.
91 # I -suspect- it's better for that state reset to be managed here; if it
92 # ever becomes painful the decision should be revisited
94 @$into = $content ? () : ($evt);
96 if ($evt->{is_in_place_close}) {
97 return $evt if $passthrough || $content;
100 my $name = $evt->{name};
102 my $_next = $content ? 'peek' : 'next';
103 $stream = do { local $_ = $stream; $filter->($stream) } if $filter;
104 my $collector = $self->_stream_from_code(sub {
105 return unless $stream;
106 while (my ($evt) = $stream->$_next) {
107 $depth++ if ($evt->{type} eq 'OPEN');
108 $depth-- if ($evt->{type} eq 'CLOSE');
112 push(@$into, $evt) if $into;
113 return $evt if $passthrough;
116 push(@$into, $evt) if $into;
117 $stream->next if $content;
118 return $evt if $passthrough;
120 die "Never saw closing </${name}> before end of source";
123 if ($passthrough||$content) {
124 $evt = { %$evt, flush => 1 };
126 $evt = { type => 'EMPTY', flush => 1 };
129 return ($passthrough||$content||$flush_before)
130 ? [ $evt, $collector ]
135 sub collect_content {
136 my ($self, $options) = @_;
137 $self->collect({ %{$options||{}}, content => 1 })
141 my ($self, $events) = @_;
142 sub { return $self->_stream_from_array(@$events, $_[0]) };
146 my ($self, $events) = @_;
147 my $coll_proto = $self->collect({ passthrough => 1 });
150 my $emit = $self->_stream_from_array(@$events);
151 my $coll = &$coll_proto;
152 return ref($coll) eq 'HASH' # single event, no collect
154 : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
158 sub prepend_content {
159 my ($self, $events) = @_;
162 if ($evt->{is_in_place_close}) {
163 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
164 return [ $evt, $self->_stream_from_array(
165 @$events, { type => 'CLOSE', name => $evt->{name} }
168 return $self->_stream_from_array($evt, @$events);
173 my ($self, $events) = @_;
174 my $coll_proto = $self->collect({ passthrough => 1, content => 1 });
177 if ($evt->{is_in_place_close}) {
178 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
179 return [ $evt, $self->_stream_from_array(
180 @$events, { type => 'CLOSE', name => $evt->{name} }
183 my $coll = &$coll_proto;
184 my $emit = $self->_stream_from_array(@$events);
185 return [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
190 my ($self, $replace_with, $options) = @_;
191 my $coll_proto = $self->collect($options);
193 my ($evt, $stream) = @_;
194 my $emit = $self->_stream_from_proto($replace_with);
195 my $coll = &$coll_proto;
196 # For a straightforward replace operation we can, in fact, do the emit
197 # -before- the collect, and my first cut did so. However in order to
198 # use the captured content in generating the new content, we need
199 # the collect stage to happen first - and it seems highly unlikely
200 # that in normal operation the collect phase will take long enough
201 # for the difference to be noticeable
204 ? (ref $coll eq 'ARRAY'
205 ? [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ]
206 : $self->_stream_concat($coll, $emit)
213 sub replace_content {
214 my ($self, $replace_with, $options) = @_;
215 $self->replace($replace_with, { %{$options||{}}, content => 1 })
219 my ($self, $repeat_for, $options) = @_;
220 $options->{into} = \my @into;
222 my $repeat_between = delete $options->{repeat_between};
223 if ($repeat_between) {
224 $options->{filter} = sub {
225 $_->select($repeat_between)->collect({ into => \@between })
229 my $s = $self->_stream_from_proto($repeat_for);
230 # We have to test $repeat_between not @between here because
231 # at the point we're constructing our return stream @between
232 # hasn't been populated yet - but we can test @between in the
233 # map routine because it has been by then and that saves us doing
234 # the extra stream construction if we don't need it.
235 $self->_flatten_stream_of_streams(do {
236 if ($repeat_between) {
238 local $_ = $self->_stream_from_array(@into);
239 (@between && $s->peek)
240 ? $self->_stream_concat(
241 $_[0]->($_), $self->_stream_from_array(@between)
247 local $_ = $self->_stream_from_array(@into);
253 $self->replace($repeater, $options);
257 my ($self, $repeat_for, $options) = @_;
258 $self->repeat($repeat_for, { %{$options||{}}, content => 1 })