test attribute manglers
[catagits/HTML-Zoom.git] / lib / HTML / Zoom / FilterBuilder.pm
CommitLineData
456a815d 1package HTML::Zoom::FilterBuilder;
2
3use Devel::Dwarn;
4
5use strict;
6use warnings FATAL => 'all';
7use HTML::Zoom::CodeStream;
8
9sub new { bless({}, shift) }
10
11sub _stream_from_code {
12 HTML::Zoom::CodeStream->new({ code => $_[1] })
13}
14
15sub _stream_from_array {
16 shift; # lose $self
17 HTML::Zoom::CodeStream->from_array(@_)
18}
19
20sub _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
36sub set_attribute {
37 my ($self, $args) = @_;
38 my ($name, $value) = @{$args}{qw(name value)};
39 sub {
40 my $a = (my $evt = shift)->{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
51sub add_attribute {
52 my ($self, $args) = @_;
53 my ($name, $value) = @{$args}{qw(name value)};
54 sub {
55 my $a = (my $evt = shift)->{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
69sub remove_attribute {
70 my ($self, $args) = @_;
71 my $name = $args->{name};
72 sub {
73 my $a = (my $evt = shift)->{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
83sub add_before {
84 my ($self, $events) = @_;
85 sub { return $self->_stream_from_array(@$events, shift) };
86}
87
88sub add_after {
89 my ($self, $events) = @_;
90 sub {
91 my ($evt, $stream) = @_;
92 my $emit = $self->_stream_from_array(@$events);
cac9d0a5 93 my $coll = $self->collect({ passthrough => 1 })->(@_);
995bc8be 94 return ref($coll) eq 'HASH' # single event, no collect
95 ? [ $coll, $emit ]
96 : [ $coll->[0], $self->_stream_concat($coll->[1], $emit) ];
456a815d 97 };
98}
99
100sub prepend_inside {
101 my ($self, $events) = @_;
102 sub {
103 my $evt = shift;
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
114sub replace {
115 my ($self, $events) = @_;
116 sub {
117 my ($evt, $stream) = @_;
118 my $emit = $self->_stream_from_array(@$events);
995bc8be 119 my $coll = $self->collect->(@_);
120 return $coll ? $self->_stream_concat($emit, $coll) : $emit;
456a815d 121 };
122}
123
124sub collect {
cac9d0a5 125 my ($self, $attrs) = @_;
126 my ($into, $passthrough) = @{$attrs}{qw(into passthrough)};
456a815d 127 sub {
128 my ($evt, $stream) = @_;
129 push(@$into, $evt) if $into;
130 if ($evt->{is_in_place_close}) {
131 return $evt if $passthrough;
132 return;
133 }
134 my $name = $evt->{name};
135 my $depth = 1;
136 my $collector = $self->_stream_from_code(sub {
137 return unless $stream;
138 while (my ($evt) = $stream->next) {
139 $depth++ if ($evt->{type} eq 'OPEN');
140 $depth-- if ($evt->{type} eq 'CLOSE');
c86c0ce2 141 push(@$into, $evt) if $into;
456a815d 142 unless ($depth) {
143 undef $stream;
144 return $evt if $passthrough;
145 return;
146 }
456a815d 147 return $evt if $passthrough;
148 }
149 die "Never saw closing </${name}> before end of source";
150 });
151 return $passthrough ? [ $evt, $collector ] : $collector;
152 };
153}
154
1551;