test append_inside
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / FilterBuilder.pm
1 package HTML::Zoom::FilterBuilder;
2
3 use Devel::Dwarn;
4
5 use strict;
6 use warnings FATAL => 'all';
7 use HTML::Zoom::CodeStream;
8
9 sub new { bless({}, shift) }
10
11 sub _stream_from_code {
12   HTML::Zoom::CodeStream->new({ code => $_[1] })
13 }
14
15 sub _stream_from_array {
16   shift; # lose $self
17   HTML::Zoom::CodeStream->from_array(@_)
18 }
19
20 sub _stream_concat {
21   shift; # lose $self
22   my @streams = @_;
23   my $cur_stream = shift(@streams) or die "No streams passed";
24   HTML::Zoom::CodeStream->new({
25     code => sub {
26       return unless $cur_stream;
27       my $evt;
28       until (($evt) = $cur_stream->next) {
29         return unless $cur_stream = shift(@streams);
30       }
31       return $evt;
32     }
33   });
34 }
35
36 sub set_attribute {
37   my ($self, $args) = @_;
38   my ($name, $value) = @{$args}{qw(name value)};
39   sub {
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
45         ? ()
46         : (attr_names => [ @{$evt->{attr_names}}, $name ]))
47      }
48    };
49 }
50
51 sub add_attribute {
52   my ($self, $args) = @_;
53   my ($name, $value) = @{$args}{qw(name value)};
54   sub {
55     my $a = (my $evt = $_[0])->{attrs};
56     my $e = exists $a->{$name};
57     +{ %$evt, raw => undef, raw_attrs => undef,
58        attrs => {
59          %$a,
60          $name => join(' ', ($e ? $a->{$name} : ()), $value)
61       },
62       ($e # add to name list if not present
63         ? ()
64         : (attr_names => [ @{$evt->{attr_names}}, $name ]))
65     }
66   };
67 }
68
69 sub remove_attribute {
70   my ($self, $args) = @_;
71   my $name = $args->{name};
72   sub {
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,
77        attrs => $a,
78        attr_names => [ grep $_ ne $name, @{$evt->{attr_names}} ]
79     }
80   };
81 }
82
83 sub add_before {
84   my ($self, $events) = @_;
85   sub { return $self->_stream_from_array(@$events, $_[0]) };
86 }
87
88 sub add_after {
89   my ($self, $events) = @_;
90   sub {
91     my ($evt) = @_;
92     my $emit = $self->_stream_from_array(@$events);
93     my $coll = $self->collect({ passthrough => 1 })->(@_);
94     return ref($coll) eq 'HASH' # single event, no collect
95       ? [ $coll, $emit ]
96       : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
97   };
98 }
99
100 sub prepend_inside {
101   my ($self, $events) = @_;
102   sub {
103     my ($evt) = @_;
104     if ($evt->{is_in_place_close}) {
105       $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
106       return [ $evt, $self->_stream_from_array(
107         @$events, { type => 'CLOSE', name => $evt->{name} }
108       ) ];
109     }
110     return $self->_stream_from_array($evt, @$events);
111   };
112 }
113
114 sub append_inside {
115   my ($self, $events) = @_;
116   sub {
117     my ($evt) = @_;
118     if ($evt->{is_in_place_close}) {
119       $evt = { %$evt }; delete @{$evt}{qw(raw is_in_place_close)};
120       return [ $evt, $self->_stream_from_array(
121         @$events, { type => 'CLOSE', name => $evt->{name} }
122       ) ];
123     }
124     my $coll = $self->collect({ passthrough => 1, inside => 1 })->(@_);
125     my $emit = $self->_stream_from_array(@$events);
126     return [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
127   };
128 }
129
130 sub replace {
131   my ($self, $events, $options) = @_;
132   sub {
133     my ($evt, $stream) = @_;
134     my $emit = $self->_stream_from_array(@$events);
135     my $coll = $self->collect($options)->(@_);
136     return
137       ($coll
138         ? (ref $coll eq 'ARRAY'
139             ? [ $coll->[0], $self->_stream_concat($emit, $coll->[1]) ]
140             : $self->_stream_concat($emit, $coll)
141           )
142         : $emit
143       );
144   };
145 }
146
147 sub collect {
148   my ($self, $options) = @_;
149   my ($into, $passthrough, $inside) = @{$options}{qw(into passthrough inside)};
150   sub {
151     my ($evt, $stream) = @_;
152     push(@$into, $evt) if $into && !$inside;
153     if ($evt->{is_in_place_close}) {
154       return $evt if $passthrough || $inside;
155       return;
156     }
157     my $name = $evt->{name};
158     my $depth = 1;
159     my $_next = $inside ? 'peek' : 'next';
160     my $collector = $self->_stream_from_code(sub {
161       return unless $stream;
162       while (my ($evt) = $stream->$_next) {
163         $depth++ if ($evt->{type} eq 'OPEN');
164         $depth-- if ($evt->{type} eq 'CLOSE');
165         unless ($depth) {
166           undef $stream;
167           return if $inside;
168           push(@$into, $evt) if $into;
169           return $evt if $passthrough;
170           return;
171         }
172         push(@$into, $evt) if $into;
173         $stream->next if $inside;
174         return $evt if $passthrough;
175       }
176       die "Never saw closing </${name}> before end of source";
177     });
178     return ($passthrough||$inside) ? [ $evt, $collector ] : $collector;
179   };
180 }
181
182 1;