test attribute manglers
[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
49(my $expect = $tmpl) =~ s/(?=<div)/O HAI/;
50
51my $ohai = [ { type => 'TEXT', raw => 'O HAI' } ];
52
53is(
54 run_for { $_->add_before($ohai) },
55 $expect,
56 'add_before ok'
57);
58
59($expect = $tmpl) =~ s/(?<=<\/div>)/O HAI/;
60
61is(
62 run_for { $_->add_after($ohai) },
63 $expect,
64 'add_after ok'
65);
66
67($expect = $tmpl) =~ s/(?<=class="main">)/O HAI/;
68
69is(
70 run_for { $_->prepend_inside($ohai) },
71 $expect,
72 'prepend_inside ok'
73);
74
75($expect = $tmpl) =~ s/<hr \/>/<hr>O HAI<\/hr>/;
76
77is(
78 (run_for { $_->prepend_inside($ohai) } 'hr'),
79 $expect,
80 'prepend_inside ok with in place close'
81);
82
83is(
84 run_for { $_->replace($ohai) },
85'<body>
86 O HAI
87</body>
88',
89 'replace ok'
90);
91
c86c0ce2 92my @ev;
93
94is(
95 run_for { $_->collect({ into => \@ev }) },
96 '<body>
97
98</body>
99',
100 'collect removes without passthrough'
101);
102
103is(
104 HTML::Zoom::Producer::BuiltIn->html_from_events(\@ev),
105 '<div class="main">
106 <span class="hilight name">Bob</span>
107 <span class="career">Builder</span>
108 <hr />
109 </div>',
110 'collect collected right events'
111);
41153e87 112
113($expect = $tmpl) =~ s/class="main"/class="foo"/;
114
115is(
116 run_for { $_->set_attribute({ name => 'class', value => 'foo' }) },
117 $expect,
118 'set attribute on existing attribute'
119);
120
121($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/;
122
123is(
124 run_for { $_->set_attribute({ name => 'foo', value => 'bar' }) },
125 $expect,
126 'set attribute on non existing attribute'
127);
128
129($expect = $tmpl) =~ s/class="main"/class="main foo"/;
130
131is(
132 run_for { $_->add_attribute({ name => 'class', value => 'foo' }) },
133 $expect,
134 'add attribute on existing attribute'
135);
136
137($expect = $tmpl) =~ s/class="main"/class="main" foo="bar"/;
138
139is(
140 run_for { $_->add_attribute({ name => 'foo', value => 'bar' }) },
141 $expect,
142 'add attribute on non existing attribute'
143);
144
145($expect = $tmpl) =~ s/ class="main"//;
146
147is(
148 run_for { $_->remove_attribute({ name => 'class' }) },
149 $expect,
150 'remove attribute on existing attribute'
151);
152
153is(
154 run_for { $_->remove_attribute({ name => 'foo' }) },
155 $tmpl,
156 'remove attribute on non existing attribute'
157);
158
456a815d 159done_testing;