Commit | Line | Data |
456a815d |
1 | use strict; |
2 | use warnings FATAL => 'all'; |
3 | use Test::More; |
4 | |
5 | use HTML::Zoom::Parser::BuiltIn; |
6 | use HTML::Zoom::Producer::BuiltIn; |
7 | use HTML::Zoom::SelectorParser; |
8 | use HTML::Zoom::FilterBuilder; |
9 | use HTML::Zoom::FilterStream; |
10 | |
11 | my $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> |
19 | END |
20 | |
21 | sub src_stream { HTML::Zoom::Parser::BuiltIn->html_to_stream($tmpl); } |
22 | |
23 | sub html_sink { HTML::Zoom::Producer::BuiltIn->html_from_stream($_[0]) } |
24 | |
25 | my $fb = HTML::Zoom::FilterBuilder->new; |
26 | |
27 | my $sp = HTML::Zoom::SelectorParser->new; |
28 | |
29 | sub 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 | |
38 | sub 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 | |
51 | my $ohai = [ { type => 'TEXT', raw => 'O HAI' } ]; |
52 | |
53 | is( |
54 | run_for { $_->add_before($ohai) }, |
55 | $expect, |
56 | 'add_before ok' |
57 | ); |
58 | |
59 | ($expect = $tmpl) =~ s/(?<=<\/div>)/O HAI/; |
60 | |
61 | is( |
62 | run_for { $_->add_after($ohai) }, |
63 | $expect, |
64 | 'add_after ok' |
65 | ); |
66 | |
67 | ($expect = $tmpl) =~ s/(?<=class="main">)/O HAI/; |
68 | |
69 | is( |
70 | run_for { $_->prepend_inside($ohai) }, |
71 | $expect, |
72 | 'prepend_inside ok' |
73 | ); |
74 | |
75 | ($expect = $tmpl) =~ s/<hr \/>/<hr>O HAI<\/hr>/; |
76 | |
77 | is( |
78 | (run_for { $_->prepend_inside($ohai) } 'hr'), |
79 | $expect, |
80 | 'prepend_inside ok with in place close' |
81 | ); |
82 | |
83 | is( |
84 | run_for { $_->replace($ohai) }, |
85 | '<body> |
86 | O HAI |
87 | </body> |
88 | ', |
89 | 'replace ok' |
90 | ); |
91 | |
92 | done_testing; |