1 package HTML::Zoom::FilterBuilder;
4 use base qw(HTML::Zoom::SubObject);
5 use HTML::Zoom::CodeStream;
7 sub _stream_from_code {
8 shift->_zconfig->stream_utils->stream_from_code(@_)
11 sub _stream_from_array {
12 shift->_zconfig->stream_utils->stream_from_array(@_)
15 sub _stream_from_proto {
16 shift->_zconfig->stream_utils->stream_from_proto(@_)
20 shift->_zconfig->stream_utils->stream_concat(@_)
23 sub _flatten_stream_of_streams {
24 shift->_zconfig->stream_utils->flatten_stream_of_streams(@_)
27 sub set_attr { shift->set_attribute(@_); }
31 my $attr = $self->_parse_attribute_args(@_);
33 my $a = (my $evt = $_[0])->{attrs};
34 my @kadd = grep {!exists $a->{$_}} keys %$attr;
35 +{ %$evt, raw => undef, raw_attrs => undef,
36 attrs => { %$a, %$attr },
37 @kadd ? (attr_names => [ @{$evt->{attr_names}}, @kadd ]) : ()
42 sub _parse_attribute_args {
45 warn "WARNING: Long form arg (name => 'class', value => 'x') is deprecated. This may not do what you originally intended..."
46 if(@_ == 1 && $_[0]->{'name'} && $_[0]->{'value'});
48 my $opts = ref($_[0]) eq 'HASH' ? $_[0] : {$_[0] => $_[1]};
49 for (values %{$opts}) { $self->_zconfig->parser->html_escape($_); }
54 die "renamed to add_to_attribute. killing this entirely for 1.0";
57 sub add_class { shift->add_to_attribute('class',@_) }
59 sub remove_class { shift->remove_attribute('class',@_) }
61 sub set_class { shift->set_attribute('class',@_) }
63 sub set_id { shift->set_attribute('id',@_) }
65 sub add_to_attribute {
67 my $attr = $self->_parse_attribute_args(@_);
69 my $a = (my $evt = $_[0])->{attrs};
70 my @kadd = grep {!exists $a->{$_}} keys %$attr;
71 +{ %$evt, raw => undef, raw_attrs => undef,
74 map {$_ => join(' ', (exists $a->{$_} ? $a->{$_} : ()), $attr->{$_}) }
77 @kadd ? (attr_names => [ @{$evt->{attr_names}}, @kadd ]) : ()
82 sub remove_attribute {
83 my ($self, $args) = @_;
84 my $name = (ref($args) eq 'HASH') ? $args->{name} : $args;
86 my $a = (my $evt = $_[0])->{attrs};
87 return $evt unless exists $a->{$name};
88 $a = { %$a }; delete $a->{$name};
89 +{ %$evt, raw => undef, raw_attrs => undef,
91 attr_names => [ grep $_ ne $name, @{$evt->{attr_names}} ]
96 sub transform_attribute {
98 my ( $name, $code ) = @_ > 1 ? @_ : @{$_[0]}{qw(name code)};
102 my %a = %{ $evt->{attrs} };
103 my @names = @{ $evt->{attr_names} };
105 my $existed_before = exists $a{$name};
106 my $v = $code->( $a{$name} );
107 my $deleted = $existed_before && ! defined $v;
108 my $added = ! $existed_before && defined $v;
115 @names = grep $_ ne $name, @names;
119 +{ %$evt, raw => undef, raw_attrs => undef,
122 ? (attr_names => \@names )
129 my ($self, $options) = @_;
130 my ($into, $passthrough, $content, $filter, $flush_before) =
131 @{$options}{qw(into passthrough content filter flush_before)};
133 my ($evt, $stream) = @_;
134 # We wipe the contents of @$into here so that other actions depending
135 # on this (such as a repeater) can be invoked multiple times easily.
136 # I -suspect- it's better for that state reset to be managed here; if it
137 # ever becomes painful the decision should be revisited
139 @$into = $content ? () : ($evt);
141 if ($evt->{is_in_place_close}) {
142 return $evt if $passthrough || $content;
145 my $name = $evt->{name};
147 my $_next = $content ? 'peek' : 'next';
150 $stream = do { local $_ = $stream; $filter->($stream) };
153 local $_ = $self->_stream_concat(
154 $self->_stream_from_array($evt),
159 $evt = $stream->next;
162 my $collector = $self->_stream_from_code(sub {
163 return unless $stream;
164 while (my ($evt) = $stream->$_next) {
165 $depth++ if ($evt->{type} eq 'OPEN');
166 $depth-- if ($evt->{type} eq 'CLOSE');
170 push(@$into, $evt) if $into;
171 return $evt if $passthrough;
174 push(@$into, $evt) if $into;
175 $stream->next if $content;
176 return $evt if $passthrough;
178 die "Never saw closing </${name}> before end of source";
181 if ($passthrough||$content) {
182 $evt = { %$evt, flush => 1 };
184 $evt = { type => 'EMPTY', flush => 1 };
187 return ($passthrough||$content||$flush_before)
188 ? [ $evt, $collector ]
193 sub collect_content {
194 my ($self, $options) = @_;
195 $self->collect({ %{$options||{}}, content => 1 })
199 my ($self, $events) = @_;
200 my $coll_proto = $self->collect({ passthrough => 1 });
202 my $emit = $self->_stream_from_proto($events);
203 my $coll = &$coll_proto;
205 if(ref $coll eq 'ARRAY') {
206 my $firstbit = $self->_stream_from_proto([$coll->[0]]);
207 return $self->_stream_concat($emit, $firstbit, $coll->[1]);
208 } elsif(ref $coll eq 'HASH') {
209 return [$emit, $coll];
211 return $self->_stream_concat($emit, $coll);
213 } else { return $emit }
218 my ($self, $events) = @_;
219 my $coll_proto = $self->collect({ passthrough => 1 });
222 my $emit = $self->_stream_from_proto($events);
223 my $coll = &$coll_proto;
224 return ref($coll) eq 'HASH' # single event, no collect
226 : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
230 sub prepend_content {
231 my ($self, $events) = @_;
232 my $coll_proto = $self->collect({ passthrough => 1, content => 1 });
235 my $emit = $self->_stream_from_proto($events);
236 if ($evt->{is_in_place_close}) {
237 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
238 return [ $evt, $self->_stream_from_array(
239 $emit->next, { type => 'CLOSE', name => $evt->{name} }
242 my $coll = &$coll_proto;
243 return [ $coll->[0], $self->_stream_concat($emit, $coll->[1]) ];
248 my ($self, $events) = @_;
249 my $coll_proto = $self->collect({ passthrough => 1, content => 1 });
252 my $emit = $self->_stream_from_proto($events);
253 if ($evt->{is_in_place_close}) {
254 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
255 return [ $evt, $self->_stream_from_array(
256 $emit->next, { type => 'CLOSE', name => $evt->{name} }
259 my $coll = &$coll_proto;
260 return [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
265 my ($self, $replace_with, $options) = @_;
266 my $coll_proto = $self->collect($options);
268 my ($evt, $stream) = @_;
269 my $emit = $self->_stream_from_proto($replace_with);
270 my $coll = &$coll_proto;
271 # if we're replacing the contents of an in place close
272 # then we need to handle that here
273 if ($options->{content}
274 && ref($coll) eq 'HASH'
275 && $coll->{is_in_place_close}
277 my $close = $stream->next;
278 # shallow copy and nuke in place and raw (to force smart print)
279 $_ = { %$_ }, delete @{$_}{qw(is_in_place_close raw)} for ($coll, $close);
280 $emit = $self->_stream_concat(
282 $self->_stream_from_array($close),
285 # For a straightforward replace operation we can, in fact, do the emit
286 # -before- the collect, and my first cut did so. However in order to
287 # use the captured content in generating the new content, we need
288 # the collect stage to happen first - and it seems highly unlikely
289 # that in normal operation the collect phase will take long enough
290 # for the difference to be noticeable
293 ? (ref $coll eq 'ARRAY' # [ event, stream ]
294 ? [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ]
295 : (ref $coll eq 'HASH' # event or stream?
297 : $self->_stream_concat($coll, $emit))
304 sub replace_content {
305 my ($self, $replace_with, $options) = @_;
306 $self->replace($replace_with, { %{$options||{}}, content => 1 })
310 my ($self, $repeat_for, $options) = @_;
311 $options->{into} = \my @into;
313 my $repeat_between = delete $options->{repeat_between};
314 if ($repeat_between) {
315 $options->{filter} = sub {
316 $_->select($repeat_between)->collect({ into => \@between })
320 my $s = $self->_stream_from_proto($repeat_for);
321 # We have to test $repeat_between not @between here because
322 # at the point we're constructing our return stream @between
323 # hasn't been populated yet - but we can test @between in the
324 # map routine because it has been by then and that saves us doing
325 # the extra stream construction if we don't need it.
326 $self->_flatten_stream_of_streams(do {
327 if ($repeat_between) {
329 local $_ = $self->_stream_from_array(@into);
330 (@between && $s->peek)
331 ? $self->_stream_concat(
332 $_[0]->($_), $self->_stream_from_array(@between)
338 local $_ = $self->_stream_from_array(@into);
344 $self->replace($repeater, $options);
348 my ($self, $repeat_for, $options) = @_;
349 $self->repeat($repeat_for, { %{$options||{}}, content => 1 })
356 HTML::Zoom::FilterBuilder - Add Filters to a Stream
360 Create an L<HTML::Zoom> instance:
363 my $root = HTML::Zoom
367 <title>Default Title</title>
369 <body bad_attr='junk'>
375 Create a new attribute on the C<body> tag:
379 ->set_attribute(class=>'main');
381 Add a extra value to an existing attribute:
385 ->add_to_attribute(class=>'one-column');
387 Set the content of the C<title> tag:
391 ->replace_content('Hello World');
393 Set content from another L<HTML::Zoom> instance:
395 my $body = HTML::Zoom
399 <p id="p2">Is the Time</p>
405 ->replace_content($body);
407 Set an attribute on multiple matches:
411 ->set_attribute(class=>'para');
417 ->remove_attribute('bad_attr');
423 my $output = $root->to_html;
430 <title>Hello World</title>
432 <body class="main one-column"><div id="stuff">
433 <p class="para">Well Now</p>
434 <p id="p2" class="para">Is the Time</p>
442 is($output, $expect, 'Synopsis code works ok');
448 Given a L<HTML::Zoom> stream, provide methods to apply filters which
449 alter the content of that stream.
453 This class defines the following public API
457 Sets an attribute of a given name to a given value for all matching selections.
461 ->set_attribute(class=>'paragraph')
463 ->set_attribute({name=>'class', value=>'divider'});
465 Overrides existing values, if such exist. When multiple L</set_attribute>
466 calls are made against the same or overlapping selection sets, the final
469 =head2 add_to_attribute
471 Adds a value to an existing attribute, or creates one if the attribute does not
472 yet exist. You may call this method with either an Array or HashRef of Args.
476 ->set_attribute({class => 'paragraph', name => 'test'})
478 ->add_to_attribute(class=>'divider');
480 Attributes with more than one value will have a dividing space.
482 =head2 remove_attribute
484 Removes an attribute and all its values.
488 ->set_attribute(class=>'paragraph')
490 ->remove_attribute('class');
492 Removes attributes from the original stream or events already added.
494 =head2 transform_attribute
496 Transforms (or creates or deletes) an attribute by running the passed
497 coderef on it. If the coderef returns nothing, the attribute is
502 ->transform_attribute( href => sub {
503 ( my $a = shift ) =~ s/localhost/example.com/;
510 Collects and extracts results of L<HTML::Zoom/select>. It takes the following
511 optional common options as hash reference.
515 =item into [ARRAY REFERENCE]
517 Where to save collected events (selected elements).
519 $z1->select('#main-content')
520 ->collect({ into => \@body })
522 $z2->select('#main-content')
528 Run filter on collected elements (locally setting $_ to stream, and passing
529 stream as an argument to given code reference). Filtered stream would be
534 filter => sub { $_->select('.inner')->replace_content('bar!') },
538 It can be used to further filter selection. For example
542 filter => sub { $_->select('td') },
546 is equivalent to (not implemented yet) descendant selector combination, i.e.
550 =item passthrough [BOOLEAN]
552 Extract copy of elements; the stream is unchanged (it does not remove collected
553 elements). For example without 'passthrough'
555 HTML::Zoom->from_html('<foo><bar /></foo>')
557 ->collect({ content => 1 })
560 returns '<foo></foo>', while with C<passthrough> option
562 HTML::Zoom->from_html('<foo><bar /></foo>')
564 ->collect({ content => 1, passthough => 1 })
567 returns '<foo><bar /></foo>'.
569 =item content [BOOLEAN]
571 Collect content of the element, and not the element itself.
575 HTML::Zoom->from_html('<h1>Title</h1><p>foo</p>')
580 would return '<p>foo</p>', while
582 HTML::Zoom->from_html('<h1>Title</h1><p>foo</p>')
584 ->collect({ content => 1 })
587 would return '<h1></h1><p>foo</p>'.
589 See also L</collect_content>.
591 =item flush_before [BOOLEAN]
593 Generate C<flush> event before collecting, to ensure that the HTML generated up
594 to selected element being collected is flushed throught to the browser. Usually
595 used in L</repeat> or L</repeat_content>.
599 =head2 collect_content
601 Collects contents of L<HTML::Zoom/select> result.
603 HTML::Zoom->from_file($foo)
604 ->select('#main-content')
605 ->collect_content({ into => \@foo_body })
608 ->replace_content(\@foo_body)
611 Equivalent to running L</collect> with C<content> option set.
615 Given a L<HTML::Zoom/select> result, add given content (which might be string,
616 array or another L<HTML::Zoom> object) before it.
619 ->select('input[name="foo"]')
620 ->add_before(\ '<span class="warning">required field</span>');
624 Like L</add_before>, only after L<HTML::Zoom/select> result.
630 You can add zoom events directly
634 ->add_after([ { type => 'TEXT', raw => 'O HAI' } ]);
636 =head2 prepend_content
638 Similar to add_before, but adds the content to the match.
641 ->from_html(q[<p>World</p>])
643 ->prepend_content("Hello ")
646 ## <p>Hello World</p>
648 Acceptable values are strings, scalar refs and L<HTML::Zoom> objects
650 =head2 append_content
652 Similar to add_after, but adds the content to the match.
655 ->from_html(q[<p>Hello </p>])
657 ->prepend_content("World")
660 ## <p>Hello World</p>
662 Acceptable values are strings, scalar refs and L<HTML::Zoom> objects
666 Given a L<HTML::Zoom/select> result, replace it with a string, array or another
667 L<HTML::Zoom> object. It takes the same optional common options as L</collect>
668 (via hash reference).
670 =head2 replace_content
672 Given a L<HTML::Zoom/select> result, replace the content with a string, array
673 or another L<HTML::Zoom> object.
676 ->select('title, #greeting')
677 ->replace_content('Hello world!');
681 For a given selection, repeat over transformations, typically for the purposes
682 of populating lists. Takes either an array of anonymous subroutines or a zoom-
683 able object consisting of transformation.
685 Example of array reference style (when it doesn't matter that all iterations are
688 $zoom->select('table')->repeat([
692 $_->select('td')->replace_content($e);
697 Subroutines would be run with $_ localized to result of L<HTML::Zoom/select> (of
698 collected elements), and with said result passed as parameter to subroutine.
700 You might want to use CodeStream when you don't have all elements upfront
702 $zoom->select('.contents')->repeat(sub {
703 HTML::Zoom::CodeStream->new({
705 while (my $line = $fh->getline) {
707 $_->select('.lno')->replace_content($fh->input_line_number)
708 ->select('.line')->replace_content($line)
716 In addition to common options as in L</collect>, it also supports:
720 =item repeat_between [SELECTOR]
722 Selects object to be repeated between items. In the case of array this object
723 is put between elements, in case of iterator it is put between results of
724 subsequent iterations, in the case of streamable it is put between events
727 See documentation for L</repeat_content>
731 =head2 repeat_content
733 Given a L<HTML::Zoom/select> result, run provided iterator passing content of
734 this result to this iterator. Accepts the same options as L</repeat>.
736 Equivalent to using C<contents> option with L</repeat>.
743 $_->select('.name')->replace_content('Matt')
744 ->select('.age')->replace_content('26')
747 $_->select('.name')->replace_content('Mark')
748 ->select('.age')->replace_content('0x29')
751 $_->select('.name')->replace_content('Epitaph')
752 ->select('.age')->replace_content('<redacted>')
755 { repeat_between => '.between' }
765 See L<HTML::Zoom> for authors.
769 See L<HTML::Zoom> for the license.