Commit | Line | Data |
c877066a |
1 | use strict; |
2 | use warnings FATAL => 'all'; |
3 | |
4 | use HTML::Zoom; |
5 | use Test::More; |
6 | |
7 | ok my $zoom = HTML::Zoom->from_html(<<HTML); |
8 | <html> |
9 | <head> |
10 | <title>Hi!</title> |
11 | </head> |
12 | <body id="content-area"> |
13 | <h1>Test</h1> |
14 | <div> |
15 | <p class="first-para">Some Stuff</p> |
16 | <p class="body-para">More Stuff</p> |
17 | <p class="body-para">Even More Stuff</p> |
18 | <ol> |
19 | <li class="first-item odd">First</li> |
20 | <li class="body-items even">Stuff A</li> |
21 | <li class="body-items odd">Stuff B</li> |
22 | <li class="body-items even">Stuff C</li> |
23 | <li class="last-item odd">Last</li> |
24 | </ol> |
c2d0669e |
25 | <ul class="items"> |
b9c27a5a |
26 | <li class="first">space</li> |
27 | <li class="second">space</li> |
c2d0669e |
28 | </ul> |
c877066a |
29 | <p class="body-para">Even More stuff</p> |
30 | <p class="last-para">Some Stuff</p> |
31 | </div> |
b9c27a5a |
32 | <div id="blocks"> |
33 | <h2 class="h2-item">Sub Item</h2> |
34 | </div> |
35 | <ul id="people"> |
36 | <li class="name">name</li> |
37 | <li class="email">email</li> |
38 | </ul> |
39 | <ul id="people2"> |
40 | <li class="name">name</li> |
41 | <li class="email">email</li> |
42 | </ul> |
43 | <ul id="object"> |
44 | <li class="aa">AA</li> |
45 | <li class="bb">BB</li> |
46 | </ul> |
c877066a |
47 | <p id="footer">Copyright 2222</p> |
48 | </body> |
49 | </html> |
50 | HTML |
51 | |
b9c27a5a |
52 | ok ! eval { $zoom->non_method('.first-param' => 'First!'); 1}, |
c877066a |
53 | 'Properly die on bad method'; |
54 | |
b9c27a5a |
55 | ok my $title = sub { |
56 | my ($z, $content) = @_; |
57 | $z = $z->replace_content(title =>$content); |
58 | return $z; |
59 | }; |
60 | |
61 | ok my $dwim = $zoom |
62 | ->$title('Hello NYC') |
c877066a |
63 | ->replace_content('.first-para' => 'First!') |
64 | ->replace_content('.last-para' => 'Last!') |
b9c27a5a |
65 | ->add_to_attribute (p => class => 'para') |
66 | ->prepend_content('.first-item' => [{type=>'TEXT', raw=>'FIRST: '}]) |
67 | ->prepend_content('.last-item' => [{type=>'TEXT', raw=>'LAST: '}]) |
68 | ->replace_content('title' => 'Hello World') |
69 | ->repeat_content( |
70 | '.items' => [ |
71 | map { |
72 | my $v = $_; |
73 | sub { |
74 | $_->replace_content('.first' => $v) |
75 | ->replace_content('.second' => $v); |
76 | }, |
77 | } (111,222) |
78 | ] |
c877066a |
79 | ) |
80 | ->to_html; |
81 | |
b9c27a5a |
82 | ##warn $dwim; |
83 | |
84 | like( |
85 | HTML::Zoom |
86 | ->from_html(q[<title>Hi!</title>]) |
87 | ->$title('Hello NYC') |
88 | ->to_html, |
89 | qr/Hello NYC/, |
90 | 'Got correct title(custom subref)' |
91 | ); |
92 | |
93 | like( |
94 | HTML::Zoom |
95 | ->from_html(q[<p>Hi!</p>]) |
96 | ->replace_content(p=>'Ask not what your country can do for you...') |
97 | ->to_html, |
98 | qr/Ask not what your country can do for you\.\.\./, |
99 | 'Got correct from replace_content' |
100 | ); |
101 | |
102 | like( |
103 | HTML::Zoom |
104 | ->from_html(q[<p class="first">Hi!</p>]) |
105 | ->add_to_attribute('p', {name => 'class', value => 'para'}) |
106 | ->to_html, |
107 | qr/first para/, |
108 | 'Got correct from add_to_attribute' |
109 | ); |
110 | |
111 | like( |
112 | HTML::Zoom |
113 | ->from_html(q[<p class="first">Hi!</p>]) |
114 | ->add_to_attribute('p', class => 'para') |
115 | ->to_html, |
116 | qr/first para/, |
117 | 'Got correct from add_to_attribute' |
118 | ); |
119 | |
120 | like( |
121 | HTML::Zoom |
122 | ->from_html(q[<p class="first">Hi!</p>]) |
123 | ->set_attribute('p', class => 'para') |
124 | ->to_html, |
125 | qr/class="para"/, |
126 | 'Got correct from set_attribute' |
127 | ); |
128 | |
129 | like( |
130 | HTML::Zoom |
131 | ->from_html(q[<p class="first">Hi!</p>]) |
132 | ->set_attribute('p', {name => 'class', value => 'para'}) |
133 | ->to_html, |
134 | qr/class="para"/, |
135 | 'Got correct from set_attribute' |
136 | ); |
137 | |
138 | is( |
139 | HTML::Zoom |
140 | ->from_html(q[<p class="first">Hi!</p>]) |
141 | ->add_before(p=>[{type=>'TEXT', raw=>'added before'}]) |
142 | ->to_html, |
143 | 'added before<p class="first">Hi!</p>', |
144 | 'Got correct from add_before' |
145 | ); |
146 | |
147 | is( |
148 | HTML::Zoom |
149 | ->from_html(q[<p class="first">Hi!</p>]) |
150 | ->add_after(p=>[{type=>'TEXT', raw=>'added after'}]) |
151 | ->to_html, |
152 | '<p class="first">Hi!</p>added after', |
153 | 'Got correct from add_after' |
154 | ); |
155 | |
156 | is( |
157 | HTML::Zoom |
158 | ->from_html(q[<p class="first">Hi!</p>]) |
159 | ->prepend_content(p=>[{type=>'TEXT', raw=>'prepend_content'}]) |
160 | ->to_html, |
161 | '<p class="first">prepend_contentHi!</p>', |
162 | 'Got correct from prepend_content' |
163 | ); |
164 | |
165 | is( |
166 | HTML::Zoom |
167 | ->from_html(q[<p class="first">Hi!</p>]) |
168 | ->append_content(p=>[{type=>'TEXT', raw=>'append_content'}]) |
169 | ->to_html, |
170 | '<p class="first">Hi!append_content</p>', |
171 | 'Got correct from append_content' |
172 | ); |
173 | |
174 | { |
175 | my @body; |
176 | ok my $body = HTML::Zoom |
177 | ->from_html(q[<div>My Stuff Is Here</div>]) |
178 | ->collect_content(div => { into => \@body, passthrough => 1}) |
179 | ->to_html, 'Collected Content'; |
180 | |
181 | is $body, "<div>My Stuff Is Here</div>", "Got expected"; |
182 | |
183 | is( |
184 | HTML::Zoom->from_events(\@body)->to_html, |
185 | 'My Stuff Is Here', |
186 | 'Collected the right stuff', |
187 | ); |
188 | } |
189 | |
190 | like( |
191 | HTML::Zoom |
192 | ->from_html(q[<ul class="items"><li class="first">first</li><li class="second">second</li></ul>]) |
193 | ->repeat_content( |
194 | '.items' => [ |
195 | map { |
196 | my $v = $_; |
197 | sub { |
198 | $_->replace_content('.first' => $v) |
199 | ->replace_content('.second' => $v); |
200 | }, |
201 | } (111,222) |
202 | ] |
203 | ) |
204 | ->to_html, |
205 | qr[<ul class="items"><li class="first">111</li><li class="second">111</li><li class="first">222</li><li class="second">222</li></ul>], |
206 | 'Got correct list' |
207 | ); |
208 | |
209 | { |
210 | ok my $z = HTML::Zoom |
211 | ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>]) |
212 | ->add_before(p=>'added before'); |
213 | |
214 | is $z->to_html, '<div>Life, whatever</div>added before<p class="first">Hi!</p>', |
215 | 'Got correct from add_before'; |
216 | } |
217 | |
218 | { |
219 | ok my $z = HTML::Zoom |
220 | ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>]) |
221 | ->add_after(p=>'added after'); |
222 | |
223 | is $z->to_html, '<div>Life, whatever</div><p class="first">Hi!</p>added after', |
224 | 'Got correct from add_after'; |
225 | } |
226 | |
227 | { |
228 | ok my $z = HTML::Zoom |
229 | ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>]) |
230 | ->append_content(p=>'appended'); |
231 | |
232 | is $z->to_html, '<div>Life, whatever</div><p class="first">Hi!appended</p>', |
233 | 'Got correct from append_content'; |
234 | } |
235 | |
236 | { |
237 | ok my $z = HTML::Zoom |
238 | ->from_html(q[<div>Life, whatever</div><p class="first">Hi!</p>]) |
239 | ->prepend_content(p=>'prepended'); |
240 | |
241 | |
242 | is $z->to_html, '<div>Life, whatever</div><p class="first">prependedHi!</p>', |
243 | 'Got correct from prepend_content'; |
244 | } |
c877066a |
245 | |
c877066a |
246 | done_testing; |