test append_inside
[catagits/HTML-Zoom.git] / t / actions.t
CommitLineData
456a815d 1use strict;
2use warnings FATAL => 'all';
3use Test::More;
4
5use HTML::Zoom::Parser::BuiltIn;
6use HTML::Zoom::Producer::BuiltIn;
7use HTML::Zoom::SelectorParser;
8use HTML::Zoom::FilterBuilder;
9use HTML::Zoom::FilterStream;
10
11my $tmpl = <<END;
12<body>
13 <div class="main">
14 <span class="hilight name">Bob</span>
15 <span class="career">Builder</span>
16 <hr />
17 </div>
18</body>
19END
20
21sub src_stream { HTML::Zoom::Parser::BuiltIn->html_to_stream($tmpl); }
22
23sub html_sink { HTML::Zoom::Producer::BuiltIn->html_from_stream($_[0]) }
24
25my $fb = HTML::Zoom::FilterBuilder->new;
26
27my $sp = HTML::Zoom::SelectorParser->new;
28
29sub filter {
30 my ($stream, $sel, $cb) = @_;
31 return HTML::Zoom::FilterStream->new({
32 stream => $stream,
33 match => $sp->parse_selector($sel),
34 filter => do { local $_ = $fb; $cb->($fb) }
35 });
36}
37
38sub run_for (&;$) {
39 my $cb = shift;
40 (html_sink
41 (filter
42 src_stream,
43 (shift or '.main'),
44 $cb
45 )
46 )
47}
48
8f962884 49my ($expect, @ev);
50
51($expect = $tmpl) =~ s/class="main"/class="foo"/;
52
53is(
54 run_for { $_->set_attribute({ name => 'class', value => 'foo' }) },
55 $expect,
56 'set attribute on existing attribute'
57);
58
59($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/;
60
61is(
62 run_for { $_->set_attribute({ name => 'foo', value => 'bar' }) },
63 $expect,
64 'set attribute on non existing attribute'
65);
66
67($expect = $tmpl) =~ s/class="main"/class="main foo"/;
68
69is(
70 run_for { $_->add_attribute({ name => 'class', value => 'foo' }) },
71 $expect,
72 'add attribute on existing attribute'
73);
74
75($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/;
76
77is(
78 run_for { $_->add_attribute({ name => 'foo', value => 'bar' }) },
79 $expect,
80 'add attribute on non existing attribute'
81);
82
83($expect = $tmpl) =~ s/ class="main"//;
84
85is(
86 run_for { $_->remove_attribute({ name => 'class' }) },
87 $expect,
88 'remove attribute on existing attribute'
89);
90
91is(
92 run_for { $_->remove_attribute({ name => 'foo' }) },
93 $tmpl,
94 'remove attribute on non existing attribute'
95);
96
97($expect = $tmpl) =~ s/(?=<div)/O HAI/;
456a815d 98
99my $ohai = [ { type => 'TEXT', raw => 'O HAI' } ];
100
101is(
102 run_for { $_->add_before($ohai) },
103 $expect,
104 'add_before ok'
105);
106
107($expect = $tmpl) =~ s/(?<=<\/div>)/O HAI/;
108
109is(
110 run_for { $_->add_after($ohai) },
111 $expect,
112 'add_after ok'
113);
114
115($expect = $tmpl) =~ s/(?<=class="main">)/O HAI/;
116
117is(
118 run_for { $_->prepend_inside($ohai) },
119 $expect,
120 'prepend_inside ok'
121);
122
123($expect = $tmpl) =~ s/<hr \/>/<hr>O HAI<\/hr>/;
124
125is(
126 (run_for { $_->prepend_inside($ohai) } 'hr'),
127 $expect,
128 'prepend_inside ok with in place close'
129);
130
131is(
132 run_for { $_->replace($ohai) },
133'<body>
134 O HAI
135</body>
136',
137 'replace ok'
138);
139
8f962884 140@ev = ();
c86c0ce2 141
142is(
143 run_for { $_->collect({ into => \@ev }) },
144 '<body>
145
146</body>
147',
148 'collect removes without passthrough'
149);
150
151is(
152 HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev),
153 '<div class="main">
154 <span class="hilight name">Bob</span>
155 <span class="career">Builder</span>
156 <hr />
157 </div>',
158 'collect collected right events'
159);
41153e87 160
8f962884 161@ev = ();
41153e87 162
163is(
8f962884 164 run_for { $_->collect({ into => \@ev, inside => 1 }) },
165 '<body>
166 <div class="main"></div>
167</body>
168',
169 'collect w/inside removes correctly'
41153e87 170);
171
172is(
8f962884 173 HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev),
174 '
175 <span class="hilight name">Bob</span>
176 <span class="career">Builder</span>
177 <hr />
178 ',
179 'collect w/inside collects correctly'
41153e87 180);
181
11cc25dd 182is(
183 run_for { $_->replace($ohai, { inside => 1 }) },
184 '<body>
185 <div class="main">O HAI</div>
186</body>
187',
188 'replace w/inside'
189);
190
a25b8d4d 191($expect = $tmpl) =~ s/(?=<\/div>)/O HAI/;
192
193is(
194 run_for { $_->append_inside($ohai) },
195 $expect,
196 'append inside ok'
197);
198
456a815d 199done_testing;