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_from_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_from_attribute {
84 my $attr = $self->_parse_attribute_args(@_);
86 my $a = (my $evt = $_[0])->{attrs};
87 +{ %$evt, raw => undef, raw_attrs => undef,
90 #TODO needs to support multiple removes
91 map { my $tar = $_; $_ => join ' ',
92 map {$attr->{$tar} ne $_} split ' ', $a->{$_} }
93 grep {exists $a->{$_}} keys %$attr
99 sub remove_attribute {
100 my ($self, $args) = @_;
101 my $name = (ref($args) eq 'HASH') ? $args->{name} : $args;
103 my $a = (my $evt = $_[0])->{attrs};
104 return $evt unless exists $a->{$name};
105 $a = { %$a }; delete $a->{$name};
106 +{ %$evt, raw => undef, raw_attrs => undef,
108 attr_names => [ grep $_ ne $name, @{$evt->{attr_names}} ]
113 sub transform_attribute {
115 my ( $name, $code ) = @_ > 1 ? @_ : @{$_[0]}{qw(name code)};
119 my %a = %{ $evt->{attrs} };
120 my @names = @{ $evt->{attr_names} };
122 my $existed_before = exists $a{$name};
123 my $v = $code->( $a{$name} );
124 my $deleted = $existed_before && ! defined $v;
125 my $added = ! $existed_before && defined $v;
132 @names = grep $_ ne $name, @names;
136 +{ %$evt, raw => undef, raw_attrs => undef,
139 ? (attr_names => \@names )
146 my ($self, $options) = @_;
147 my ($into, $passthrough, $content, $filter, $flush_before) =
148 @{$options}{qw(into passthrough content filter flush_before)};
150 my ($evt, $stream) = @_;
151 # We wipe the contents of @$into here so that other actions depending
152 # on this (such as a repeater) can be invoked multiple times easily.
153 # I -suspect- it's better for that state reset to be managed here; if it
154 # ever becomes painful the decision should be revisited
156 @$into = $content ? () : ($evt);
158 if ($evt->{is_in_place_close}) {
159 return $evt if $passthrough || $content;
162 my $name = $evt->{name};
164 my $_next = $content ? 'peek' : 'next';
167 $stream = do { local $_ = $stream; $filter->($stream) };
170 local $_ = $self->_stream_concat(
171 $self->_stream_from_array($evt),
176 $evt = $stream->next;
179 my $collector = $self->_stream_from_code(sub {
180 return unless $stream;
181 while (my ($evt) = $stream->$_next) {
182 $depth++ if ($evt->{type} eq 'OPEN');
183 $depth-- if ($evt->{type} eq 'CLOSE');
187 push(@$into, $evt) if $into;
188 return $evt if $passthrough;
191 push(@$into, $evt) if $into;
192 $stream->next if $content;
193 return $evt if $passthrough;
195 die "Never saw closing </${name}> before end of source";
198 if ($passthrough||$content) {
199 $evt = { %$evt, flush => 1 };
201 $evt = { type => 'EMPTY', flush => 1 };
204 return ($passthrough||$content||$flush_before)
205 ? [ $evt, $collector ]
210 sub collect_content {
211 my ($self, $options) = @_;
212 $self->collect({ %{$options||{}}, content => 1 })
216 my ($self, $events) = @_;
217 my $coll_proto = $self->collect({ passthrough => 1 });
219 my $emit = $self->_stream_from_proto($events);
220 my $coll = &$coll_proto;
222 if(ref $coll eq 'ARRAY') {
223 my $firstbit = $self->_stream_from_proto([$coll->[0]]);
224 return $self->_stream_concat($emit, $firstbit, $coll->[1]);
225 } elsif(ref $coll eq 'HASH') {
226 return [$emit, $coll];
228 return $self->_stream_concat($emit, $coll);
230 } else { return $emit }
235 my ($self, $events) = @_;
236 my $coll_proto = $self->collect({ passthrough => 1 });
239 my $emit = $self->_stream_from_proto($events);
240 my $coll = &$coll_proto;
241 return ref($coll) eq 'HASH' # single event, no collect
243 : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
247 sub prepend_content {
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($emit, $coll->[1]) ];
265 my ($self, $events) = @_;
266 my $coll_proto = $self->collect({ passthrough => 1, content => 1 });
269 my $emit = $self->_stream_from_proto($events);
270 if ($evt->{is_in_place_close}) {
271 $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
272 return [ $evt, $self->_stream_from_array(
273 $emit->next, { type => 'CLOSE', name => $evt->{name} }
276 my $coll = &$coll_proto;
277 return [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
282 my ($self, $replace_with, $options) = @_;
283 my $coll_proto = $self->collect($options);
285 my ($evt, $stream) = @_;
286 my $emit = $self->_stream_from_proto($replace_with);
287 my $coll = &$coll_proto;
288 # if we're replacing the contents of an in place close
289 # then we need to handle that here
290 if ($options->{content}
291 && ref($coll) eq 'HASH'
292 && $coll->{is_in_place_close}
294 my $close = $stream->next;
295 # shallow copy and nuke in place and raw (to force smart print)
296 $_ = { %$_ }, delete @{$_}{qw(is_in_place_close raw)} for ($coll, $close);
297 $emit = $self->_stream_concat(
299 $self->_stream_from_array($close),
302 # For a straightforward replace operation we can, in fact, do the emit
303 # -before- the collect, and my first cut did so. However in order to
304 # use the captured content in generating the new content, we need
305 # the collect stage to happen first - and it seems highly unlikely
306 # that in normal operation the collect phase will take long enough
307 # for the difference to be noticeable
310 ? (ref $coll eq 'ARRAY' # [ event, stream ]
311 ? [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ]
312 : (ref $coll eq 'HASH' # event or stream?
314 : $self->_stream_concat($coll, $emit))
321 sub replace_content {
322 my ($self, $replace_with, $options) = @_;
323 $self->replace($replace_with, { %{$options||{}}, content => 1 })
327 my ($self, $repeat_for, $options) = @_;
328 $options->{into} = \my @into;
330 my $repeat_between = delete $options->{repeat_between};
331 if ($repeat_between) {
332 $options->{filter} = sub {
333 $_->select($repeat_between)->collect({ into => \@between })
337 my $s = $self->_stream_from_proto($repeat_for);
338 # We have to test $repeat_between not @between here because
339 # at the point we're constructing our return stream @between
340 # hasn't been populated yet - but we can test @between in the
341 # map routine because it has been by then and that saves us doing
342 # the extra stream construction if we don't need it.
343 $self->_flatten_stream_of_streams(do {
344 if ($repeat_between) {
346 local $_ = $self->_stream_from_array(@into);
347 (@between && $s->peek)
348 ? $self->_stream_concat(
349 $_[0]->($_), $self->_stream_from_array(@between)
355 local $_ = $self->_stream_from_array(@into);
361 $self->replace($repeater, $options);
365 my ($self, $repeat_for, $options) = @_;
366 $self->repeat($repeat_for, { %{$options||{}}, content => 1 })
370 my ($self, $to) = @_;
373 push @$to, $evt->{'attrs'}->{'name'};
379 my ($self,$to,$fill) = @_;
383 $_->select('input')->validate_and_fill($to,$fill)
384 ->select('select')->validate_and_fill($to,$fill);
390 sub validate_and_fill {
391 my ($self, $to, $fill) = @_;
394 my $nm = $evt->{'attrs'}->{'name'};
395 if(defined $fill && $fill->{$nm}) {
396 $evt->{'raw'} = undef;
397 $evt->{'raw_attrs'} = undef;
398 push @{$evt->{'attr_names'}}, 'value' unless exists $evt->{'attrs'}->{'value'};
399 $evt->{'attrs'}->{'value'} = $fill->{$nm};
402 [split ' ', $evt->{'attrs'}->{'data-validate'}||""];
412 HTML::Zoom::FilterBuilder - Add Filters to a Stream
416 Create an L<HTML::Zoom> instance:
419 my $root = HTML::Zoom
423 <title>Default Title</title>
425 <body bad_attr='junk'>
431 Create a new attribute on the C<body> tag:
435 ->set_attribute(class=>'main');
437 Add a extra value to an existing attribute:
441 ->add_to_attribute(class=>'one-column');
443 Set the content of the C<title> tag:
447 ->replace_content('Hello World');
449 Set content from another L<HTML::Zoom> instance:
451 my $body = HTML::Zoom
455 <p id="p2">Is the Time</p>
461 ->replace_content($body);
463 Set an attribute on multiple matches:
467 ->set_attribute(class=>'para');
473 ->remove_attribute('bad_attr');
479 my $output = $root->to_html;
486 <title>Hello World</title>
488 <body class="main one-column"><div id="stuff">
489 <p class="para">Well Now</p>
490 <p id="p2" class="para">Is the Time</p>
498 is($output, $expect, 'Synopsis code works ok');
504 Given a L<HTML::Zoom> stream, provide methods to apply filters which
505 alter the content of that stream.
509 This class defines the following public API
513 Sets an attribute of a given name to a given value for all matching selections.
517 ->set_attribute(class=>'paragraph')
519 ->set_attribute({class=>'paragraph', name=>'divider'});
521 Overrides existing values, if such exist. When multiple L</set_attribute>
522 calls are made against the same or overlapping selection sets, the final
525 =head2 add_to_attribute
527 Adds a value to an existing attribute, or creates one if the attribute does not
528 yet exist. You may call this method with either an Array or HashRef of Args.
532 ->set_attribute({class => 'paragraph', name => 'test'})
534 ->add_to_attribute(class=>'divider');
536 Attributes with more than one value will have a dividing space.
538 =head2 remove_attribute
540 Removes an attribute and all its values.
544 ->set_attribute(class=>'paragraph')
546 ->remove_attribute('class');
548 =head2 remove_from_attribute
550 Removes a value from existing attribute
554 ->set_attribute(class=>'paragraph lead')
556 ->remove_from_attribute('class' => 'lead');
558 Removes attributes from the original stream or events already added.
562 Add to a class attribute
566 Remove from a class attribute
568 =head2 transform_attribute
570 Transforms (or creates or deletes) an attribute by running the passed
571 coderef on it. If the coderef returns nothing, the attribute is
576 ->transform_attribute( href => sub {
577 ( my $a = shift ) =~ s/localhost/example.com/;
584 Collects and extracts results of L<HTML::Zoom/select>. It takes the following
585 optional common options as hash reference.
589 =item into [ARRAY REFERENCE]
591 Where to save collected events (selected elements).
593 $z1->select('#main-content')
594 ->collect({ into => \@body })
596 $z2->select('#main-content')
602 Run filter on collected elements (locally setting $_ to stream, and passing
603 stream as an argument to given code reference). Filtered stream would be
608 filter => sub { $_->select('.inner')->replace_content('bar!') },
612 It can be used to further filter selection. For example
616 filter => sub { $_->select('td') },
620 is equivalent to (not implemented yet) descendant selector combination, i.e.
624 =item passthrough [BOOLEAN]
626 Extract copy of elements; the stream is unchanged (it does not remove collected
627 elements). For example without 'passthrough'
629 HTML::Zoom->from_html('<foo><bar /></foo>')
631 ->collect({ content => 1 })
634 returns '<foo></foo>', while with C<passthrough> option
636 HTML::Zoom->from_html('<foo><bar /></foo>')
638 ->collect({ content => 1, passthough => 1 })
641 returns '<foo><bar /></foo>'.
643 =item content [BOOLEAN]
645 Collect content of the element, and not the element itself.
649 HTML::Zoom->from_html('<h1>Title</h1><p>foo</p>')
654 would return '<p>foo</p>', while
656 HTML::Zoom->from_html('<h1>Title</h1><p>foo</p>')
658 ->collect({ content => 1 })
661 would return '<h1></h1><p>foo</p>'.
663 See also L</collect_content>.
665 =item flush_before [BOOLEAN]
667 Generate C<flush> event before collecting, to ensure that the HTML generated up
668 to selected element being collected is flushed throught to the browser. Usually
669 used in L</repeat> or L</repeat_content>.
673 =head2 collect_content
675 Collects contents of L<HTML::Zoom/select> result.
677 HTML::Zoom->from_file($foo)
678 ->select('#main-content')
679 ->collect_content({ into => \@foo_body })
682 ->replace_content(\@foo_body)
685 Equivalent to running L</collect> with C<content> option set.
689 Given a L<HTML::Zoom/select> result, add given content (which might be string,
690 array or another L<HTML::Zoom> object) before it.
693 ->select('input[name="foo"]')
694 ->add_before(\ '<span class="warning">required field</span>');
698 Like L</add_before>, only after L<HTML::Zoom/select> result.
704 You can add zoom events directly
708 ->add_after([ { type => 'TEXT', raw => 'O HAI' } ]);
710 =head2 prepend_content
712 Similar to add_before, but adds the content to the match.
715 ->from_html(q[<p>World</p>])
717 ->prepend_content("Hello ")
720 ## <p>Hello World</p>
722 Acceptable values are strings, scalar refs and L<HTML::Zoom> objects
724 =head2 append_content
726 Similar to add_after, but adds the content to the match.
729 ->from_html(q[<p>Hello </p>])
731 ->prepend_content("World")
734 ## <p>Hello World</p>
736 Acceptable values are strings, scalar refs and L<HTML::Zoom> objects
740 Given a L<HTML::Zoom/select> result, replace it with a string, array or another
741 L<HTML::Zoom> object. It takes the same optional common options as L</collect>
742 (via hash reference).
744 =head2 replace_content
746 Given a L<HTML::Zoom/select> result, replace the content with a string, array
747 or another L<HTML::Zoom> object.
750 ->select('title, #greeting')
751 ->replace_content('Hello world!');
755 For a given selection, repeat over transformations, typically for the purposes
756 of populating lists. Takes either an array of anonymous subroutines or a zoom-
757 able object consisting of transformation.
759 Example of array reference style (when it doesn't matter that all iterations are
762 $zoom->select('table')->repeat([
766 $_->select('td')->replace_content($e);
771 Subroutines would be run with $_ localized to result of L<HTML::Zoom/select> (of
772 collected elements), and with said result passed as parameter to subroutine.
774 You might want to use CodeStream when you don't have all elements upfront
776 $zoom->select('.contents')->repeat(sub {
777 HTML::Zoom::CodeStream->new({
779 while (my $line = $fh->getline) {
781 $_->select('.lno')->replace_content($fh->input_line_number)
782 ->select('.line')->replace_content($line)
790 In addition to common options as in L</collect>, it also supports:
794 =item repeat_between [SELECTOR]
796 Selects object to be repeated between items. In the case of array this object
797 is put between elements, in case of iterator it is put between results of
798 subsequent iterations, in the case of streamable it is put between events
801 See documentation for L</repeat_content>
805 =head2 repeat_content
807 Given a L<HTML::Zoom/select> result, run provided iterator passing content of
808 this result to this iterator. Accepts the same options as L</repeat>.
810 Equivalent to using C<contents> option with L</repeat>.
817 $_->select('.name')->replace_content('Matt')
818 ->select('.age')->replace_content('26')
821 $_->select('.name')->replace_content('Mark')
822 ->select('.age')->replace_content('0x29')
825 $_->select('.name')->replace_content('Epitaph')
826 ->select('.age')->replace_content('<redacted>')
829 { repeat_between => '.between' }
839 See L<HTML::Zoom> for authors.
843 See L<HTML::Zoom> for the license.