Commit | Line | Data |
---|---|---|
67e78ff2 | 1 | package HTML::Element::Library; |
2 | ||
3 | use 5.006001; | |
4 | use strict; | |
5 | use warnings; | |
6 | ||
7 | ||
8 | our $DEBUG = 0; | |
9 | #our $DEBUG = 1; | |
10 | ||
11 | use Array::Group qw(:all); | |
12 | use Carp qw(confess); | |
13 | use Data::Dumper; | |
14 | use HTML::Element; | |
3dad7198 | 15 | use List::Util qw(first); |
67e78ff2 | 16 | use List::MoreUtils qw/:all/; |
17 | use Params::Validate qw(:all); | |
18 | use Scalar::Listify; | |
19 | #use Tie::Cycle; | |
20 | use List::Rotation::Cycle; | |
21 | ||
22 | our %EXPORT_TAGS = ( 'all' => [ qw() ] ); | |
23 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |
24 | our @EXPORT = qw(); | |
25 | ||
26 | ||
27 | ||
de64e3d9 | 28 | our $VERSION = '3.53'; |
67e78ff2 | 29 | |
30 | ||
2fcbbeb3 | 31 | |
67e78ff2 | 32 | # Preloaded methods go here. |
33 | ||
2fcbbeb3 TB |
34 | # https://rt.cpan.org/Ticket/Display.html?id=44105 |
35 | sub HTML::Element::fillinform { | |
36 | ||
37 | my ($tree, $hashref)=@_; | |
38 | ||
39 | use HTML::FillInForm; | |
40 | my $html = $tree->as_HTML; | |
41 | my $new_html = HTML::FillInForm->fill(\$html, $hashref); | |
42 | ||
43 | } | |
44 | ||
67e78ff2 | 45 | sub HTML::Element::siblings { |
46 | my $element = shift; | |
47 | my $p = $element->parent; | |
48 | return () unless $p; | |
49 | $p->content_list; | |
50 | } | |
51 | ||
f25dca7f | 52 | sub HTML::Element::defmap { |
9a087986 | 53 | my($tree,$attr,$hashref,$debug)=@_; |
f25dca7f TB |
54 | |
55 | while (my ($k, $v) = (each %$hashref)) { | |
9a087986 | 56 | warn "defmap looks for ($attr => $k)" if $debug; |
f25dca7f TB |
57 | my $found = $tree->look_down($attr => $k); |
58 | if ($found) { | |
9a087986 | 59 | warn "($attr => $k) was found.. replacing with '$v'" if $debug; |
f25dca7f TB |
60 | $found->replace_content( $v ); |
61 | } | |
62 | } | |
63 | ||
64 | } | |
65 | ||
66 | ||
9b7a5679 TB |
67 | sub HTML::Element::hash_map { |
68 | my $container = shift; | |
69 | ||
70 | my %p = validate(@_, { | |
71 | hash => { type => HASHREF }, | |
72 | to_attr => 1, | |
d9f4bd5a | 73 | excluding => { type => ARRAYREF , default => [] }, |
9b7a5679 TB |
74 | debug => { default => 0 }, |
75 | }); | |
76 | ||
d9f4bd5a TB |
77 | warn 'The container tag is ', $container->tag if $p{debug} ; |
78 | warn 'hash' . Dumper($p{hash}) if $p{debug} ; | |
f25dca7f | 79 | warn 'at_under' . Dumper(\@_) if $p{debug} ; |
d9f4bd5a TB |
80 | |
81 | my @same_as = $container->look_down( $p{to_attr} => qr/.+/ ) ; | |
82 | ||
83 | warn 'Found ' . scalar(@same_as) . ' nodes' if $p{debug} ; | |
84 | ||
9b7a5679 TB |
85 | |
86 | for my $same_as (@same_as) { | |
d9f4bd5a TB |
87 | my $attr_val = $same_as->attr($p{to_attr}) ; |
88 | if (first { $attr_val eq $_ } @{$p{excluding}}) { | |
89 | warn "excluding $attr_val" if $p{debug} ; | |
90 | next; | |
91 | } | |
92 | warn "processing $attr_val" if $p{debug} ; | |
93 | $same_as->replace_content( $p{hash}->{$attr_val} ) ; | |
9b7a5679 | 94 | } |
3dad7198 TB |
95 | |
96 | } | |
97 | ||
f25dca7f TB |
98 | sub HTML::Element::hashmap { |
99 | my ($container, $attr_name, $hashref, $excluding, $debug) = @_; | |
100 | ||
101 | $excluding ||= [] ; | |
102 | ||
103 | $container->hash_map(hash => $hashref, | |
104 | to_attr => $attr_name, | |
105 | excluding => $excluding, | |
106 | debug => $debug); | |
107 | ||
108 | } | |
109 | ||
3dad7198 | 110 | |
de64e3d9 | 111 | sub HTML::Element::passover { |
96cec221 | 112 | my ($tree, @to_preserve) = @_; |
de64e3d9 | 113 | |
96cec221 | 114 | warn "ARGS: my ($tree, @to_preserve)" if $DEBUG; |
3dad7198 | 115 | warn $tree->as_HTML(undef, ' ') if $DEBUG; |
de64e3d9 | 116 | |
96cec221 | 117 | my $exodus = $tree->look_down(id => $to_preserve[0]); |
de64e3d9 | 118 | |
3dad7198 TB |
119 | warn "E: $exodus" if $DEBUG; |
120 | ||
de64e3d9 | 121 | my @s = HTML::Element::siblings($exodus); |
122 | ||
de64e3d9 | 123 | for my $s (@s) { |
de64e3d9 | 124 | next unless ref $s; |
96cec221 | 125 | if (first { $s->attr('id') eq $_ } @to_preserve) { |
de64e3d9 | 126 | ; |
127 | } else { | |
128 | $s->delete; | |
129 | } | |
130 | } | |
131 | ||
132 | return $exodus; # Goodbye Egypt! http://en.wikipedia.org/wiki/Passover | |
133 | ||
134 | } | |
135 | ||
67e78ff2 | 136 | sub HTML::Element::sibdex { |
137 | ||
138 | my $element = shift; | |
139 | firstidx { $_ eq $element } $element->siblings | |
140 | ||
141 | } | |
142 | ||
143 | sub HTML::Element::addr { goto &HTML::Element::sibdex } | |
144 | ||
145 | sub HTML::Element::replace_content { | |
146 | my $elem = shift; | |
147 | $elem->delete_content; | |
148 | $elem->push_content(@_); | |
149 | } | |
150 | ||
151 | sub HTML::Element::wrap_content { | |
152 | my($self, $wrap) = @_; | |
153 | my $content = $self->content; | |
154 | if (ref $content) { | |
155 | $wrap->push_content(@$content); | |
156 | @$content = ($wrap); | |
157 | } | |
158 | else { | |
159 | $self->push_content($wrap); | |
160 | } | |
161 | $wrap; | |
162 | } | |
163 | ||
164 | sub HTML::Element::Library::super_literal { | |
165 | my($text) = @_; | |
166 | ||
167 | HTML::Element->new('~literal', text => $text); | |
168 | } | |
169 | ||
170 | ||
171 | sub HTML::Element::position { | |
172 | # Report coordinates by chasing addr's up the | |
173 | # HTML::ElementSuper tree. We know we've reached | |
174 | # the top when a) there is no parent, or b) the | |
175 | # parent is some HTML::Element unable to report | |
176 | # it's position. | |
177 | my $p = shift; | |
178 | my @pos; | |
179 | while ($p) { | |
180 | my $a = $p->addr; | |
181 | unshift(@pos, $a) if defined $a; | |
182 | $p = $p->parent; | |
183 | } | |
184 | @pos; | |
185 | } | |
186 | ||
187 | ||
188 | sub HTML::Element::content_handler { | |
3c14ea1e | 189 | my ($tree, %content_hash) = @_; |
190 | ||
191 | for my $k (keys %content_hash) { | |
192 | $tree->set_child_content(id => $k, $content_hash{$k}); | |
193 | } | |
67e78ff2 | 194 | |
67e78ff2 | 195 | |
196 | } | |
197 | ||
198 | ||
199 | sub make_counter { | |
200 | my $i = 1; | |
201 | sub { | |
202 | shift() . ':' . $i++ | |
203 | } | |
204 | } | |
205 | ||
206 | ||
207 | sub HTML::Element::iter { | |
208 | my ($tree, $p, @data) = @_; | |
209 | ||
210 | # warn 'P: ' , $p->attr('id') ; | |
211 | # warn 'H: ' , $p->as_HTML; | |
212 | ||
213 | # my $id_incr = make_counter; | |
214 | my @item = map { | |
215 | my $new_item = clone $p; | |
216 | $new_item->replace_content($_); | |
67e78ff2 | 217 | $new_item; |
218 | } @data; | |
219 | ||
220 | $p->replace_with(@item); | |
221 | ||
222 | } | |
223 | ||
224 | ||
225 | sub HTML::Element::iter2 { | |
226 | ||
227 | my $tree = shift; | |
228 | ||
229 | #warn "INPUT TO TABLE2: ", Dumper \@_; | |
230 | ||
231 | my %p = validate( | |
232 | @_, { | |
233 | wrapper_ld => { default => ['_tag' => 'dl'] }, | |
234 | wrapper_data => 1, | |
235 | wrapper_proc => { default => undef }, | |
236 | item_ld => { default => sub { | |
237 | my $tree = shift; | |
238 | [ | |
239 | $tree->look_down('_tag' => 'dt'), | |
240 | $tree->look_down('_tag' => 'dd') | |
241 | ]; | |
242 | } | |
243 | }, | |
244 | item_data => { default => sub { my ($wrapper_data) = @_; | |
245 | shift(@{$wrapper_data}) ; | |
246 | }}, | |
247 | item_proc => { | |
248 | default => sub { | |
249 | my ($item_elems, $item_data, $row_count) = @_; | |
250 | $item_elems->[$_]->replace_content($item_data->[$_]) for (0,1) ; | |
251 | $item_elems; | |
252 | }}, | |
253 | splice => { default => sub { | |
254 | my ($container, @item_elems) = @_; | |
255 | $container->splice_content(0, 2, @item_elems); | |
256 | } | |
257 | }, | |
258 | debug => {default => 0} | |
259 | } | |
260 | ); | |
261 | ||
262 | warn "wrapper_data: " . Dumper $p{wrapper_data} if $p{debug} ; | |
263 | ||
264 | my $container = ref_or_ld($tree, $p{wrapper_ld}); | |
3dad7198 | 265 | warn "container: " . $container if $p{debug} ; |
67e78ff2 | 266 | warn "wrapper_(preproc): " . $container->as_HTML if $p{debug} ; |
267 | $p{wrapper_proc}->($container) if defined $p{wrapper_proc} ; | |
268 | warn "wrapper_(postproc): " . $container->as_HTML if $p{debug} ; | |
269 | ||
270 | my $_item_elems = $p{item_ld}->($container); | |
271 | ||
272 | ||
273 | ||
274 | my $row_count; | |
275 | my @item_elem; | |
276 | { | |
277 | my $item_data = $p{item_data}->($p{wrapper_data}); | |
278 | last unless defined $item_data; | |
279 | ||
280 | warn Dumper("item_data", $item_data); | |
281 | ||
282 | ||
283 | my $item_elems = [ map { $_->clone } @{$_item_elems} ] ; | |
284 | ||
285 | if ($p{debug}) { | |
286 | for (@{$item_elems}) { | |
287 | warn "ITEM_ELEMS ", $_->as_HTML; | |
288 | } | |
289 | } | |
290 | ||
291 | my $new_item_elems = $p{item_proc}->($item_elems, $item_data, ++$row_count); | |
292 | ||
293 | if ($p{debug}) { | |
294 | for (@{$new_item_elems}) { | |
295 | warn "NEWITEM_ELEMS ", $_->as_HTML; | |
296 | } | |
297 | } | |
298 | ||
299 | ||
300 | push @item_elem, @{$new_item_elems} ; | |
301 | ||
302 | redo; | |
303 | } | |
304 | ||
305 | warn "pushing " . @item_elem . " elems " if $p{debug} ; | |
306 | ||
307 | $p{splice}->($container, @item_elem); | |
308 | ||
309 | } | |
310 | ||
311 | sub HTML::Element::dual_iter { | |
312 | my ($parent, $data) = @_; | |
313 | ||
314 | my ($prototype_a, $prototype_b) = $parent->content_list; | |
315 | ||
316 | # my $id_incr = make_counter; | |
317 | ||
318 | my $i; | |
319 | ||
320 | @$data %2 == 0 or | |
321 | confess 'dataset does not contain an even number of members'; | |
322 | ||
323 | my @iterable_data = ngroup 2 => @$data; | |
324 | ||
325 | my @item = map { | |
326 | my ($new_a, $new_b) = map { clone $_ } ($prototype_a, $prototype_b) ; | |
327 | $new_a->splice_content(0,1, $_->[0]); | |
328 | $new_b->splice_content(0,1, $_->[1]); | |
329 | #$_->attr('id', $id_incr->($_->attr('id'))) for ($new_a, $new_b) ; | |
330 | ($new_a, $new_b) | |
331 | } @iterable_data; | |
332 | ||
333 | $parent->splice_content(0, 2, @item); | |
334 | ||
335 | } | |
336 | ||
337 | ||
338 | sub HTML::Element::set_child_content { | |
339 | my $tree = shift; | |
340 | my $content = pop; | |
341 | my @look_down = @_; | |
342 | ||
343 | my $content_tag = $tree->look_down(@look_down); | |
344 | ||
345 | unless ($content_tag) { | |
346 | warn "criteria [@look_down] not found"; | |
347 | return; | |
348 | } | |
349 | ||
350 | $content_tag->replace_content($content); | |
351 | ||
352 | } | |
353 | ||
354 | sub HTML::Element::highlander { | |
355 | my ($tree, $local_root_id, $aref, @arg) = @_; | |
356 | ||
357 | ref $aref eq 'ARRAY' or confess | |
358 | "must supply array reference"; | |
359 | ||
360 | my @aref = @$aref; | |
361 | @aref % 2 == 0 or confess | |
362 | "supplied array ref must have an even number of entries"; | |
363 | ||
364 | warn __PACKAGE__ if $DEBUG; | |
365 | ||
366 | my $survivor; | |
367 | while (my ($id, $test) = splice @aref, 0, 2) { | |
368 | warn $id if $DEBUG; | |
369 | if ($test->(@arg)) { | |
370 | $survivor = $id; | |
371 | last; | |
372 | } | |
373 | } | |
374 | ||
375 | ||
376 | my @id_survivor = (id => $survivor); | |
377 | my $survivor_node = $tree->look_down(@id_survivor); | |
378 | # warn $survivor; | |
379 | # warn $local_root_id; | |
380 | # warn $node; | |
381 | ||
382 | warn "survivor: $survivor" if $DEBUG; | |
383 | warn "tree: " . $tree->as_HTML if $DEBUG; | |
384 | ||
385 | $survivor_node or die "search for @id_survivor failed in tree($tree): " . $tree->as_HTML; | |
386 | ||
387 | my $survivor_node_parent = $survivor_node->parent; | |
388 | $survivor_node = $survivor_node->clone; | |
389 | $survivor_node_parent->replace_content($survivor_node); | |
390 | ||
391 | warn "new tree: " . $tree->as_HTML if $DEBUG; | |
392 | ||
393 | $survivor_node; | |
394 | } | |
395 | ||
396 | ||
397 | sub HTML::Element::highlander2 { | |
398 | my $tree = shift; | |
399 | ||
400 | my %p = validate(@_, { | |
401 | cond => { type => ARRAYREF }, | |
402 | cond_arg => { type => ARRAYREF, | |
403 | default => [] | |
404 | }, | |
405 | debug => { default => 0 } | |
406 | } | |
407 | ); | |
408 | ||
409 | ||
410 | my @cond = @{$p{cond}}; | |
411 | @cond % 2 == 0 or confess | |
412 | "supplied array ref must have an even number of entries"; | |
413 | ||
414 | warn __PACKAGE__ if $p{debug}; | |
415 | ||
416 | my @cond_arg = @{$p{cond_arg}}; | |
417 | ||
418 | my $survivor; my $then; | |
419 | while (my ($id, $if_then) = splice @cond, 0, 2) { | |
420 | ||
421 | warn $id if $p{debug}; | |
422 | my ($if, $_then); | |
423 | ||
424 | if (ref $if_then eq 'ARRAY') { | |
425 | ($if, $_then) = @$if_then; | |
426 | } else { | |
427 | ($if, $_then) = ($if_then, sub {}); | |
428 | } | |
429 | ||
430 | if ($if->(@cond_arg)) { | |
431 | $survivor = $id; | |
432 | $then = $_then; | |
433 | last; | |
434 | } | |
435 | ||
436 | } | |
437 | ||
438 | my @ld = (ref $survivor eq 'ARRAY') | |
439 | ? @$survivor | |
440 | : (id => $survivor) | |
441 | ; | |
442 | ||
443 | warn "survivor: ", $survivor if $p{debug}; | |
444 | warn "survivor_ld: ", Dumper \@ld if $p{debug}; | |
445 | ||
446 | ||
447 | my $survivor_node = $tree->look_down(@ld); | |
448 | ||
449 | $survivor_node or confess | |
450 | "search for @ld failed in tree($tree): " . $tree->as_HTML; | |
451 | ||
452 | my $survivor_node_parent = $survivor_node->parent; | |
453 | $survivor_node = $survivor_node->clone; | |
454 | $survivor_node_parent->replace_content($survivor_node); | |
455 | ||
456 | ||
457 | # **************** NEW FUNCTIONALITY ******************* | |
458 | ||
459 | # apply transforms on survivor node | |
460 | ||
461 | ||
462 | warn "SURV::pre_trans " . $survivor_node->as_HTML if $p{debug}; | |
463 | $then->($survivor_node, @cond_arg); | |
464 | warn "SURV::post_trans " . $survivor_node->as_HTML if $p{debug}; | |
465 | ||
466 | # **************** NEW FUNCTIONALITY ******************* | |
467 | ||
468 | ||
469 | ||
470 | ||
471 | $survivor_node; | |
472 | } | |
473 | ||
474 | ||
475 | sub overwrite_action { | |
476 | my ($mute_node, %X) = @_; | |
477 | ||
478 | $mute_node->attr($X{local_attr}{name} => $X{local_attr}{value}{new}); | |
479 | } | |
480 | ||
481 | ||
482 | sub HTML::Element::overwrite_attr { | |
483 | my $tree = shift; | |
484 | ||
485 | $tree->mute_elem(@_, \&overwrite_action); | |
486 | } | |
487 | ||
488 | ||
489 | ||
490 | sub HTML::Element::mute_elem { | |
491 | my ($tree, $mute_attr, $closures, $post_hook) = @_; | |
492 | ||
493 | warn "my mute_node = $tree->look_down($mute_attr => qr/.*/) ;"; | |
494 | my @mute_node = $tree->look_down($mute_attr => qr/.*/) ; | |
495 | ||
496 | for my $mute_node (@mute_node) { | |
497 | my ($local_attr,$mute_key) = split /\s+/, $mute_node->attr($mute_attr); | |
498 | my $local_attr_value_current = $mute_node->attr($local_attr); | |
499 | my $local_attr_value_new = $closures->{$mute_key}->($tree, $mute_node, $local_attr_value_current); | |
500 | $post_hook->( | |
501 | $mute_node, | |
502 | tree => $tree, | |
503 | local_attr => { | |
504 | name => $local_attr, | |
505 | value => { | |
506 | current => $local_attr_value_current, | |
507 | new => $local_attr_value_new | |
508 | } | |
509 | } | |
510 | ) if ($post_hook) ; | |
511 | } | |
512 | } | |
513 | ||
514 | ||
515 | ||
516 | sub HTML::Element::table { | |
517 | ||
518 | my ($s, %table) = @_; | |
519 | ||
520 | my $table = {}; | |
521 | ||
522 | # use Data::Dumper; warn Dumper \%table; | |
523 | ||
524 | # ++$DEBUG if $table{debug} ; | |
525 | ||
526 | ||
527 | # Get the table element | |
528 | $table->{table_node} = $s->look_down(id => $table{gi_table}); | |
529 | $table->{table_node} or confess | |
530 | "table tag not found via (id => $table{gi_table}"; | |
531 | ||
532 | # Get the prototype tr element(s) | |
533 | my @table_gi_tr = listify $table{gi_tr} ; | |
534 | my @iter_node = map | |
535 | { | |
536 | my $tr = $table->{table_node}->look_down(id => $_); | |
537 | $tr or confess "tr with id => $_ not found"; | |
538 | $tr; | |
539 | } @table_gi_tr; | |
540 | ||
541 | warn "found " . @iter_node . " iter nodes " if $DEBUG; | |
542 | # tie my $iter_node, 'Tie::Cycle', \@iter_node; | |
543 | my $iter_node = List::Rotation::Cycle->new(@iter_node); | |
544 | ||
545 | # warn $iter_node; | |
546 | warn Dumper ($iter_node, \@iter_node) if $DEBUG; | |
547 | ||
548 | # $table->{content} = $table{content}; | |
549 | #$table->{parent} = $table->{table_node}->parent; | |
550 | ||
551 | ||
552 | # $table->{table_node}->detach; | |
553 | # $_->detach for @iter_node; | |
554 | ||
555 | my @table_rows; | |
556 | ||
557 | { | |
558 | my $row = $table{tr_data}->($table, $table{table_data}); | |
559 | last unless defined $row; | |
560 | ||
561 | # get a sample table row and clone it. | |
562 | my $I = $iter_node->next; | |
563 | warn "I: $I" if $DEBUG; | |
564 | my $new_iter_node = $I->clone; | |
565 | ||
566 | ||
567 | $table{td_data}->($new_iter_node, $row); | |
568 | push @table_rows, $new_iter_node; | |
569 | ||
570 | redo; | |
571 | } | |
572 | ||
573 | if (@table_rows) { | |
574 | ||
575 | my $replace_with_elem = $s->look_down(id => shift @table_gi_tr) ; | |
576 | for (@table_gi_tr) { | |
577 | $s->look_down(id => $_)->detach; | |
578 | } | |
579 | ||
580 | $replace_with_elem->replace_with(@table_rows); | |
581 | ||
582 | } | |
583 | ||
584 | } | |
585 | ||
586 | sub ref_or_ld { | |
587 | ||
588 | my ($tree, $slot) = @_; | |
589 | ||
590 | if (ref($slot) eq 'CODE') { | |
591 | $slot->($tree); | |
592 | } else { | |
593 | $tree->look_down(@$slot); | |
594 | } | |
595 | } | |
596 | ||
597 | ||
598 | ||
599 | sub HTML::Element::table2 { | |
600 | ||
601 | my $tree = shift; | |
602 | ||
603 | ||
604 | ||
605 | my %p = validate( | |
606 | @_, { | |
607 | table_ld => { default => ['_tag' => 'table'] }, | |
608 | table_data => 1, | |
609 | table_proc => { default => undef }, | |
610 | ||
611 | tr_ld => { default => ['_tag' => 'tr'] }, | |
612 | tr_data => { default => sub { my ($self, $data) = @_; | |
613 | shift(@{$data}) ; | |
614 | }}, | |
615 | tr_base_id => { default => undef }, | |
616 | tr_proc => { default => sub {} }, | |
617 | td_proc => 1, | |
618 | debug => {default => 0} | |
619 | } | |
620 | ); | |
621 | ||
622 | warn "INPUT TO TABLE2: ", Dumper \@_ if $p{debug}; | |
623 | ||
624 | warn "table_data: " . Dumper $p{table_data} if $p{debug} ; | |
625 | ||
626 | my $table = {}; | |
627 | ||
628 | # use Data::Dumper; warn Dumper \%table; | |
629 | ||
630 | # ++$DEBUG if $table{debug} ; | |
631 | ||
632 | # Get the table element | |
5f53bf21 | 633 | #warn 1; |
67e78ff2 | 634 | $table->{table_node} = ref_or_ld( $tree, $p{table_ld} ) ; |
5f53bf21 | 635 | #warn 2; |
67e78ff2 | 636 | $table->{table_node} or confess |
637 | "table tag not found via " . Dumper($p{table_ld}) ; | |
638 | ||
639 | warn "table: " . $table->{table_node}->as_HTML if $p{debug}; | |
640 | ||
641 | ||
642 | # Get the prototype tr element(s) | |
643 | my @proto_tr = ref_or_ld( $table->{table_node}, $p{tr_ld} ) ; | |
644 | ||
645 | warn "found " . @proto_tr . " iter nodes " if $p{debug}; | |
646 | ||
647 | @proto_tr or return ; | |
648 | ||
649 | if ($p{debug}) { | |
650 | warn $_->as_HTML for @proto_tr; | |
651 | } | |
652 | my $proto_tr = List::Rotation::Cycle->new(@proto_tr); | |
653 | ||
654 | my $tr_parent = $proto_tr[0]->parent; | |
655 | warn "parent element of trs: " . $tr_parent->as_HTML if $p{debug}; | |
656 | ||
657 | my $row_count; | |
658 | ||
659 | my @table_rows; | |
660 | ||
661 | { | |
662 | my $row = $p{tr_data}->($table, $p{table_data}, $row_count); | |
663 | warn "data row: " . Dumper $row if $p{debug}; | |
664 | last unless defined $row; | |
665 | ||
666 | # wont work: my $new_iter_node = $table->{iter_node}->clone; | |
667 | my $new_tr_node = $proto_tr->next->clone; | |
668 | warn "new_tr_node: $new_tr_node" if $p{debug}; | |
669 | ||
670 | $p{tr_proc}->($tree, $new_tr_node, $row, $p{tr_base_id}, ++$row_count) | |
671 | if defined $p{tr_proc}; | |
672 | ||
673 | warn "data row redux: " . Dumper $row if $p{debug}; | |
5f53bf21 | 674 | #warn 3.3; |
67e78ff2 | 675 | |
676 | $p{td_proc}->($new_tr_node, $row); | |
677 | push @table_rows, $new_tr_node; | |
678 | ||
5f53bf21 | 679 | #warn 4.4; |
67e78ff2 | 680 | |
681 | redo; | |
682 | } | |
683 | ||
684 | $_->detach for @proto_tr; | |
685 | ||
686 | $tr_parent->push_content(@table_rows) if (@table_rows) ; | |
687 | ||
688 | } | |
689 | ||
690 | ||
691 | sub HTML::Element::unroll_select { | |
692 | ||
693 | my ($s, %select) = @_; | |
694 | ||
695 | my $select = {}; | |
696 | ||
f25dca7f TB |
697 | warn "Select Hash: " . Dumper(\%select) if $select{debug}; |
698 | ||
67e78ff2 | 699 | my $select_node = $s->look_down(id => $select{select_label}); |
3caedb5b | 700 | warn "Select Node: " . $select_node if $select{debug}; |
67e78ff2 | 701 | |
3caedb5b TB |
702 | unless ($select{append}) { |
703 | for my $option ($select_node->look_down('_tag' => 'option')) { | |
704 | $option->delete; | |
705 | } | |
706 | } | |
67e78ff2 | 707 | |
67e78ff2 | 708 | |
3caedb5b TB |
709 | my $option = HTML::Element->new('option'); |
710 | warn "Option Node: " . $option if $select{debug}; | |
67e78ff2 | 711 | |
712 | $option->detach; | |
713 | ||
714 | while (my $row = $select{data_iter}->($select{data})) | |
715 | { | |
3caedb5b TB |
716 | warn "Data Row:" . Dumper($row) if $select{debug}; |
717 | my $o = $option->clone; | |
718 | $o->attr('value', $select{option_value}->($row)); | |
719 | $o->attr('SELECTED', 1) if (exists $select{option_selected} and $select{option_selected}->($row)) ; | |
720 | ||
721 | $o->replace_content($select{option_content}->($row)); | |
722 | $select_node->push_content($o); | |
723 | warn $o->as_HTML if $select{debug}; | |
67e78ff2 | 724 | } |
725 | ||
726 | ||
727 | } | |
728 | ||
729 | ||
730 | ||
731 | sub HTML::Element::set_sibling_content { | |
732 | my ($elt, $content) = @_; | |
733 | ||
734 | $elt->parent->splice_content($elt->pindex + 1, 1, $content); | |
735 | ||
736 | } | |
737 | ||
738 | sub HTML::TreeBuilder::parse_string { | |
739 | my ($package, $string) = @_; | |
740 | ||
741 | my $h = HTML::TreeBuilder->new; | |
742 | HTML::TreeBuilder->parse($string); | |
743 | ||
744 | } | |
745 | ||
746 | ||
747 | ||
748 | 1; | |
749 | __END__ | |
750 | # Below is stub documentation for your module. You'd better edit it! | |
751 | ||
752 | =head1 NAME | |
753 | ||
754 | HTML::Element::Library - HTML::Element convenience functions | |
755 | ||
756 | =head1 SYNOPSIS | |
757 | ||
758 | use HTML::Element::Library; | |
759 | use HTML::TreeBuilder; | |
760 | ||
761 | =head1 DESCRIPTION | |
762 | ||
763 | This method provides API calls for common actions on trees when using | |
764 | L<HTML::Tree>. | |
765 | ||
766 | =head1 METHODS | |
767 | ||
768 | The test suite contains examples of each of these methods in a | |
769 | file C<t/$method.t> | |
770 | ||
771 | =head2 Positional Querying Methods | |
772 | ||
773 | =head3 $elem->siblings | |
774 | ||
775 | Return a list of all nodes under the same parent. | |
776 | ||
777 | =head3 $elem->sibdex | |
778 | ||
779 | Return the index of C<$elem> into the array of siblings of which it is | |
780 | a part. L<HTML::ElementSuper> calls this method C<addr> but I don't think | |
781 | that is a descriptive name. And such naming is deceptively close to the | |
782 | C<address> function of C<HTML::Element>. HOWEVER, in the interest of | |
783 | backwards compatibility, both methods are available. | |
784 | ||
785 | =head3 $elem->addr | |
786 | ||
787 | Same as sibdex | |
788 | ||
789 | =head3 $elem->position() | |
790 | ||
791 | Returns the coordinates of this element in the tree it inhabits. | |
792 | This is accomplished by succesively calling addr() on ancestor | |
793 | elements until either a) an element that does not support these | |
794 | methods is found, or b) there are no more parents. The resulting | |
795 | list is the n-dimensional coordinates of the element in the tree. | |
796 | ||
797 | =head2 Element Decoration Methods | |
798 | ||
799 | =head3 HTML::Element::Library::super_literal($text) | |
800 | ||
801 | In L<HTML::Element>, Sean Burke discusses super-literals. They are | |
802 | text which does not get escaped. Great for includng Javascript in | |
803 | HTML. Also great for including foreign language into a document. | |
804 | ||
805 | So, you basically toss C<super_literal> your text and back comes | |
806 | your text wrapped in a C<~literal> element. | |
807 | ||
808 | One of these days, I'll around to writing a nice C<EXPORT> section. | |
809 | ||
810 | =head2 Tree Rewriting Methods | |
811 | ||
2fcbbeb3 TB |
812 | =head3 Simplifying calls to HTML::FillInForm |
813 | ||
814 | Since HTML::FillInForm gets and returns strings, using HTML::Element instances | |
815 | becomes tedious: | |
816 | ||
817 | 1. Seamstress has an HTML tree that it wants the form filled in on | |
818 | 2. Seamstress converts this tree to a string | |
819 | 3. FillInForm parses the string into an HTML tree and then fills in the form | |
820 | 4. FillInForm converts the HTML tree to a string | |
821 | 5. Seamstress re-parses the HTML for additional processing | |
822 | ||
823 | I've filed a bug about this: | |
824 | L<https://rt.cpan.org/Ticket/Display.html?id=44105> | |
825 | ||
826 | This function, fillinform, | |
827 | allows you to pass a tree to fillinform (along with your data structure) and | |
828 | get back a tree: | |
829 | ||
830 | my $new_tree = $html_tree->fillinform($data_structure); | |
831 | ||
832 | ||
9a087986 TB |
833 | =head3 Mapping a hashref to HTML elements |
834 | ||
835 | It is very common to get a hashref of data from some external source - flat file, database, XML, etc. | |
836 | Therefore, it is important to have a convenient way of mapping this data to HTML. | |
837 | ||
838 | As it turns out, there are 3 ways to do this in HTML::Element::Library. | |
839 | The most strict and structured way to do this is with | |
840 | C<content_handler>. Two other methods, C<hashmap> and C<datamap> require less manual mapping and may prove | |
841 | even more easy to use in certain cases. | |
842 | ||
843 | As is usual with Perl, a practical example is always best. So let's take some sample HTML: | |
844 | ||
845 | <h1>user data</h1> | |
846 | <span id="name">?</span> | |
847 | <span id="email">?</span> | |
848 | <span id="gender">?</span> | |
849 | ||
850 | Now, let's say our data structure is this: | |
851 | ||
852 | $ref = { email => 'jim@beam.com', gender => 'lots' } ; | |
853 | ||
854 | And let's start with the most strict way to get what you want: | |
855 | ||
856 | $tree->content_handler(email => $ref->{email} , gender => $ref->{gender}) ; | |
857 | ||
858 | ||
859 | In this case, you manually state the mapping between id tags and hashref keys and | |
860 | then C<content_handler> retrieves the hashref data and pops it in the specified place. | |
861 | ||
862 | Now let's look at the two (actually 2 and a half) other hash-mapping methods. | |
863 | ||
864 | $tree->hashmap(id => $ref); | |
865 | ||
866 | Now, what this function does is super-destructive. It finds every element in the tree | |
867 | with an attribute named id (since 'id' is a parameter, it could find every element with | |
868 | some other attribute also) and replaces the content of those elements with the hashref | |
869 | value. | |
870 | ||
871 | So, in the case above, the | |
872 | ||
873 | <span id="name">?</span> | |
874 | ||
875 | would come out as | |
876 | ||
877 | <span id="name"></span> | |
878 | ||
879 | (it would be blank) - because there is nothing in the hash with that value, so it substituted | |
880 | ||
881 | $ref->{name} | |
882 | ||
883 | which was blank and emptied the contents. | |
884 | ||
885 | Now, let's assume we want to protect name from being auto-assigned. Here is what you do: | |
886 | ||
887 | $tree->hashmap(id => $ref, ['name']); | |
888 | ||
889 | That last array ref is an exclusion list. | |
890 | ||
891 | But wouldnt it be nice if you could do a hashmap, but only assigned things which are defined | |
892 | in the hashref? C<< defmap() >> to the rescue: | |
893 | ||
894 | $tree->defmap(id => $ref); | |
895 | ||
896 | does just that, so | |
897 | ||
898 | <span id="name">?</span> | |
899 | ||
900 | would be left alone. | |
901 | ||
902 | ||
903 | =head4 $elem->hashmap($attr_name, \%hashref, \@excluded, $debug) | |
9b7a5679 TB |
904 | |
905 | This method is designed to take a hashref and populate a series of elements. For example: | |
906 | ||
907 | ||
908 | <table> | |
909 | <tr sclass="tr" class="alt" align="left" valign="top"> | |
3caedb5b TB |
910 | <td smap="people_id">1</td> |
911 | <td smap="phone">(877) 255-3239</td> | |
912 | <td smap="password">*********</td> | |
9b7a5679 TB |
913 | </tr> |
914 | </table> | |
915 | ||
3caedb5b | 916 | In the table above, there are several attributes named C<< smap >>. If we have a hashref whose keys are the same: |
9b7a5679 TB |
917 | |
918 | my %data = (people_id => 888, phone => '444-4444', password => 'dont-you-dare-render'); | |
919 | ||
920 | Then a single API call allows us to populate the HTML while excluding those ones we dont: | |
921 | ||
96cec221 | 922 | $tree->hashmap(smap => \%data, ['password']); |
f25dca7f | 923 | |
9b7a5679 | 924 | |
f25dca7f | 925 | Note: the other way to prevent rendering some of the hash mapping is to not give that element the attr |
9b7a5679 TB |
926 | you plan to use for hash mapping. |
927 | ||
f25dca7f TB |
928 | Also note: the function C<< hashmap >> has a simple easy-to-type API. Interally, it calls C<< hash_map >> |
929 | (which has a more verbose keyword calling API). Thus, the above call to C<hashmap()> results in this call: | |
930 | ||
931 | $tree->hash_map(hash => \%data, to_attr => 'sid', excluding => ['password']); | |
932 | ||
9a087986 TB |
933 | =head4 $elem->defmap($attr_name, \%hashref, $debug) |
934 | ||
935 | C<defmap> was described above. | |
936 | ||
937 | ||
938 | =head4 $elem->content_handler(%hashref) | |
939 | ||
940 | C<content_handler> is described below. | |
941 | ||
9b7a5679 | 942 | |
67e78ff2 | 943 | =head3 $elem->replace_content(@new_elem) |
944 | ||
945 | Replaces all of C<$elem>'s content with C<@new_elem>. | |
946 | ||
947 | =head3 $elem->wrap_content($wrapper_element) | |
948 | ||
949 | Wraps the existing content in the provided element. If the provided element | |
950 | happens to be a non-element, a push_content is performed instead. | |
951 | ||
952 | =head3 $elem->set_child_content(@look_down, $content) | |
953 | ||
954 | This method looks down $tree using the criteria specified in @look_down using the the HTML::Element look_down() method. | |
955 | ||
956 | After finding the node, it detaches the node's content and pushes $content as the node's content. | |
957 | ||
3c14ea1e | 958 | =head3 $tree->content_handler(%id_content) |
67e78ff2 | 959 | |
960 | This is a convenience method. Because the look_down criteria will often simply be: | |
961 | ||
962 | id => 'fixme' | |
963 | ||
964 | to find things like: | |
965 | ||
966 | <a id=fixme href=http://www.somesite.org>replace_content</a> | |
967 | ||
968 | You can call this method to shorten your typing a bit. You can simply type | |
969 | ||
970 | $elem->content_handler( fixme => 'new text' ) | |
971 | ||
972 | Instead of typing: | |
973 | ||
974 | $elem->set_child_content(sid => 'fixme', 'new text') | |
975 | ||
9a087986 | 976 | ALSO NOTE: you can pass a hash whose keys are C<id>s and whose values are the content you want there and it will perform the replacement on each hash member: |
3c14ea1e | 977 | |
978 | my %id_content = (name => "Terrence Brannon", | |
979 | email => 'tbrannon@in.com', | |
980 | balance => 666, | |
981 | content => $main_content); | |
982 | ||
983 | $tree->content_handler(%id_content); | |
984 | ||
67e78ff2 | 985 | =head3 $tree->highlander($subtree_span_id, $conditionals, @conditionals_args) |
986 | ||
987 | This allows for "if-then-else" style processing. Highlander was a movie in | |
988 | which only one would survive. Well, in terms of a tree when looking at a | |
989 | structure that you want to process in C<if-then-else> style, only one child | |
990 | will survive. For example, given this HTML template: | |
991 | ||
992 | <span klass="highlander" id="age_dialog"> | |
993 | <span id="under10"> | |
994 | Hello, does your mother know you're | |
995 | using her AOL account? | |
996 | </span> | |
997 | <span id="under18"> | |
998 | Sorry, you're not old enough to enter | |
999 | (and too dumb to lie about your age) | |
1000 | </span> | |
1001 | <span id="welcome"> | |
1002 | Welcome | |
1003 | </span> | |
1004 | </span> | |
1005 | ||
1006 | We only want one child of the C<span> tag with id C<age_dialog> to remain | |
1007 | based on the age of the person visiting the page. | |
1008 | ||
1009 | So, let's setup a call that will prune the subtree as a function of age: | |
1010 | ||
1011 | sub process_page { | |
1012 | my $age = shift; | |
1013 | my $tree = HTML::TreeBuilder->new_from_file('t/html/highlander.html'); | |
1014 | ||
1015 | $tree->highlander | |
1016 | (age_dialog => | |
1017 | [ | |
1018 | under10 => sub { $_[0] < 10} , | |
1019 | under18 => sub { $_[0] < 18} , | |
1020 | welcome => sub { 1 } | |
1021 | ], | |
1022 | $age | |
1023 | ); | |
1024 | ||
1025 | And there we have it. If the age is less than 10, then the node with | |
1026 | id C<under10> remains. For age less than 18, the node with id C<under18> | |
1027 | remains. | |
1028 | Otherwise our "else" condition fires and the child with id C<welcome> remains. | |
1029 | ||
96cec221 | 1030 | =head3 $tree->passover(@id_of_element) |
4b02c173 | 1031 | |
96cec221 TB |
1032 | In some cases, you know exactly which element(s) should survive. In this case, |
1033 | you can simply call C<passover> to remove it's (their) siblings. For the HTML | |
4b02c173 | 1034 | above, you could delete C<under10> and C<welcome> by simply calling: |
1035 | ||
1036 | $tree->passover('under18'); | |
1037 | ||
96cec221 TB |
1038 | Because passover takes an array, you can specify several children to preserve. |
1039 | ||
67e78ff2 | 1040 | =head3 $tree->highlander2($tree, $conditionals, @conditionals_args) |
1041 | ||
1042 | Right around the same time that C<table2()> came into being, Seamstress | |
1043 | began to tackle tougher and tougher processing problems. It became clear that | |
1044 | a more powerful highlander was needed... one that not only snipped the tree | |
1045 | of the nodes that should not survive, but one that allows for | |
1046 | post-processing of the survivor node. And one that was more flexible with | |
1047 | how to find the nodes to snip. | |
1048 | ||
1049 | Thus (drum roll) C<highlander2()>. | |
1050 | ||
1051 | So let's look at our HTML which requires post-selection processing: | |
1052 | ||
1053 | <span klass="highlander" id="age_dialog"> | |
1054 | <span id="under10"> | |
1055 | Hello, little <span id=age>AGE</span>-year old, | |
1056 | does your mother know you're using her AOL account? | |
1057 | </span> | |
1058 | <span id="under18"> | |
1059 | Sorry, you're only <span id=age>AGE</span> | |
1060 | (and too dumb to lie about your age) | |
1061 | </span> | |
1062 | <span id="welcome"> | |
1063 | Welcome, isn't it good to be <span id=age>AGE</span> years old? | |
1064 | </span> | |
1065 | </span> | |
1066 | ||
1067 | In this case, a branch survives, but it has dummy data in it. We must take | |
1068 | the surviving segment of HTML and rewrite the age C<span> with the age. | |
1069 | Here is how we use C<highlander2()> to do so: | |
1070 | ||
1071 | sub replace_age { | |
1072 | my $branch = shift; | |
1073 | my $age = shift; | |
1074 | $branch->look_down(id => 'age')->replace_content($age); | |
1075 | } | |
1076 | ||
1077 | my $if_then = $tree->look_down(id => 'age_dialog'); | |
1078 | ||
1079 | $if_then->highlander2( | |
1080 | cond => [ | |
1081 | under10 => [ | |
1082 | sub { $_[0] < 10} , | |
1083 | \&replace_age | |
1084 | ], | |
1085 | under18 => [ | |
1086 | sub { $_[0] < 18} , | |
1087 | \&replace_age | |
1088 | ], | |
1089 | welcome => [ | |
1090 | sub { 1 }, | |
1091 | \&replace_age | |
1092 | ] | |
1093 | ], | |
1094 | cond_arg => [ $age ] | |
1095 | ); | |
1096 | ||
1097 | We pass it the tree (C<$if_then>), an arrayref of conditions | |
1098 | (C<cond>) and an arrayref of arguments which are passed to the | |
1099 | C<cond>s and to the replacement subs. | |
1100 | ||
1101 | The C<under10>, C<under18> and C<welcome> are id attributes in the | |
1102 | tree of the siblings of which only one will survive. However, | |
1103 | should you need to do | |
1104 | more complex look-downs to find the survivor, | |
1105 | then supply an array ref instead of a simple | |
1106 | scalar: | |
1107 | ||
1108 | ||
1109 | $if_then->highlander2( | |
1110 | cond => [ | |
1111 | [class => 'r12'] => [ | |
1112 | sub { $_[0] < 10} , | |
1113 | \&replace_age | |
1114 | ], | |
1115 | [class => 'z22'] => [ | |
1116 | sub { $_[0] < 18} , | |
1117 | \&replace_age | |
1118 | ], | |
1119 | [class => 'w88'] => [ | |
1120 | sub { 1 }, | |
1121 | \&replace_age | |
1122 | ] | |
1123 | ], | |
1124 | cond_arg => [ $age ] | |
1125 | ); | |
1126 | ||
1127 | ||
1128 | =head3 $tree->overwrite_attr($mutation_attr => $mutating_closures) | |
1129 | ||
1130 | This method is designed for taking a tree and reworking a set of nodes in | |
1131 | a stereotyped fashion. For instance let's say you have 3 remote image | |
1132 | archives, but you don't want to put long URLs in your img src | |
1133 | tags for reasons of abstraction, re-use and brevity. So instead you do this: | |
1134 | ||
1135 | <img src="/img/smiley-face.jpg" fixup="src lnc"> | |
1136 | <img src="/img/hot-babe.jpg" fixup="src playboy"> | |
1137 | <img src="/img/footer.jpg" fixup="src foobar"> | |
1138 | ||
1139 | and then when the tree of HTML is being processed, you make this call: | |
1140 | ||
1141 | my %closures = ( | |
1142 | lnc => sub { my ($tree, $mute_node, $attr_value)= @_; "http://lnc.usc.edu$attr_value" }, | |
1143 | playboy => sub { my ($tree, $mute_node, $attr_value)= @_; "http://playboy.com$attr_value" } | |
1144 | foobar => sub { my ($tree, $mute_node, $attr_value)= @_; "http://foobar.info$attr_value" } | |
1145 | ) | |
1146 | ||
1147 | $tree->overwrite_attr(fixup => \%closures) ; | |
1148 | ||
1149 | and the tags come out modified like so: | |
1150 | ||
1151 | <img src="http://lnc.usc.edu/img/smiley-face.jpg" fixup="src lnc"> | |
1152 | <img src="http://playboy.com/img/hot-babe.jpg" fixup="src playboy"> | |
1153 | <img src="http://foobar.info/img/footer.jpg" fixup="src foobar"> | |
1154 | ||
1155 | =head3 $tree->mute_elem($mutation_attr => $mutating_closures, [ $post_hook ] ) | |
1156 | ||
1157 | This is a generalization of C<overwrite_attr>. C<overwrite_attr> | |
1158 | assumes the return value of the | |
1159 | closure is supposed overwrite an attribute value and does it for you. | |
1160 | C<mute_elem> is a more general function which does nothing but | |
1161 | hand the closure the element and let it mutate it as it jolly well pleases :) | |
1162 | ||
1163 | In fact, here is the implementation of C<overwrite_attr> | |
1164 | to give you a taste of how C<mute_attr> is used: | |
1165 | ||
1166 | sub overwrite_action { | |
1167 | my ($mute_node, %X) = @_; | |
1168 | ||
1169 | $mute_node->attr($X{local_attr}{name} => $X{local_attr}{value}{new}); | |
1170 | } | |
1171 | ||
1172 | ||
1173 | sub HTML::Element::overwrite_attr { | |
1174 | my $tree = shift; | |
1175 | ||
1176 | $tree->mute_elem(@_, \&overwrite_action); | |
1177 | } | |
1178 | ||
1179 | ||
1180 | ||
1181 | ||
9b7a5679 TB |
1182 | =head2 Tree-Building Methods |
1183 | ||
1184 | ||
1185 | ||
1186 | =head3 Unrolling an array via a single sample element (<ul> container) | |
67e78ff2 | 1187 | |
1188 | This is best described by example. Given this HTML: | |
1189 | ||
1190 | <strong>Here are the things I need from the store:</strong> | |
1191 | <ul> | |
1192 | <li class="store_items">Sample item</li> | |
1193 | </ul> | |
1194 | ||
1195 | We can unroll it like so: | |
1196 | ||
1197 | my $li = $tree->look_down(class => 'store_items'); | |
1198 | ||
1199 | my @items = qw(bread butter vodka); | |
1200 | ||
1201 | $tree->iter($li => @items); | |
1202 | ||
1203 | To produce this: | |
1204 | ||
1205 | ||
1206 | <html> | |
1207 | <head></head> | |
1208 | <body>Here are the things I need from the store: | |
1209 | <ul> | |
1210 | <li class="store_items">bread</li> | |
1211 | <li class="store_items">butter</li> | |
1212 | <li class="store_items">vodka</li> | |
1213 | </ul> | |
1214 | </body> | |
1215 | </html> | |
1216 | ||
9a087986 TB |
1217 | Now, you might be wondering why the API call is: |
1218 | ||
1219 | $tree->iter($li => @items) | |
1220 | ||
1221 | instead of: | |
1222 | ||
1223 | $li->iter(@items) | |
1224 | ||
1225 | and there is no good answer. The latter would be more concise and it is what I | |
1226 | should have done. | |
1227 | ||
9b7a5679 | 1228 | =head3 Unrolling an array via n sample elements (<dl> container) |
67e78ff2 | 1229 | |
1230 | C<iter()> was fine for awhile, but some things | |
1231 | (e.g. definition lists) need a more general function to make them easy to | |
1232 | do. Hence C<iter2()>. This function will be explained by example of unrolling | |
1233 | a simple definition list. | |
1234 | ||
1235 | So here's our mock-up HTML from the designer: | |
1236 | ||
1237 | <dl class="dual_iter" id="service_plan"> | |
1238 | <dt> | |
1239 | Artist | |
1240 | </dt> | |
1241 | <dd> | |
1242 | A person who draws blood. | |
1243 | </dd> | |
1244 | ||
1245 | <dt> | |
1246 | Musician | |
1247 | </dt> | |
1248 | <dd> | |
1249 | A clone of Iggy Pop. | |
1250 | </dd> | |
1251 | ||
1252 | <dt> | |
1253 | Poet | |
1254 | </dt> | |
1255 | <dd> | |
1256 | A relative of Edgar Allan Poe. | |
1257 | </dd> | |
1258 | ||
1259 | <dt class="adstyle">sample header</dt> | |
1260 | <dd class="adstyle2">sample data</dd> | |
1261 | ||
1262 | </dl> | |
1263 | ||
1264 | ||
1265 | And we want to unroll our data set: | |
1266 | ||
1267 | my @items = ( | |
1268 | ['the pros' => 'never have to worry about service again'], | |
1269 | ['the cons' => 'upfront extra charge on purchase'], | |
1270 | ['our choice' => 'go with the extended service plan'] | |
1271 | ); | |
1272 | ||
1273 | ||
1274 | Now, let's make this problem a bit harder to show off the power of C<iter2()>. | |
1275 | Let's assume that we want only the last <dt> and it's accompanying <dd> | |
1276 | (the one with "sample data") to be used as the sample data | |
1277 | for unrolling with our data set. Let's further assume that we want them to | |
1278 | remain in the final output. | |
1279 | ||
1280 | So now, the API to C<iter2()> will be discussed and we will explain how our | |
1281 | goal of getting our data into HTML fits into the API. | |
1282 | ||
1283 | =over 4 | |
1284 | ||
1285 | =item * wrapper_ld | |
1286 | ||
1287 | This is how to look down and find the container of all the elements we will | |
1288 | be unrolling. The <dl> tag is the container for the dt and dd tags we will be | |
1289 | unrolling. | |
1290 | ||
1291 | If you pass an anonymous subroutine, then it is presumed that execution of | |
1292 | this subroutine will return the HTML::Element representing the container tag. | |
1293 | If you pass an array ref, then this will be dereferenced and passed to | |
1294 | C<HTML::Element::look_down()>. | |
1295 | ||
1296 | default value: C<< ['_tag' => 'dl'] >> | |
1297 | ||
1298 | Based on the mock HTML above, this default is fine for finding our container | |
1299 | tag. So let's move on. | |
1300 | ||
1301 | =item * wrapper_data | |
1302 | ||
1303 | This is an array reference of data that we will be putting into the container. | |
1304 | You must supply this. C<@items> above is our C<wrapper_data>. | |
1305 | ||
1306 | =item * wrapper_proc | |
1307 | ||
1308 | After we find the container via C<wrapper_ld>, we may want to pre-process | |
1309 | some aspect of this tree. In our case the first two sets of dt and dd need | |
1310 | to be removed, leaving the last dt and dd. So, we supply a C<wrapper_proc> | |
1311 | which will do this. | |
1312 | ||
1313 | default: undef | |
1314 | ||
1315 | =item * item_ld | |
1316 | ||
1317 | This anonymous subroutine returns an array ref of C<HTML::Element>s that will | |
1318 | be cloned and populated with item data | |
1319 | (item data is a "row" of C<wrapper_data>). | |
1320 | ||
1321 | default: returns an arrayref consisting of the dt and dd element inside the | |
1322 | container. | |
1323 | ||
1324 | =item * item_data | |
1325 | ||
1326 | This is a subroutine that takes C<wrapper_data> and retrieves one "row" | |
1327 | to be "pasted" into the array ref of C<HTML::Element>s found via C<item_ld>. | |
1328 | I hope that makes sense. | |
1329 | ||
1330 | default: shifts C<wrapper_data>. | |
1331 | ||
1332 | =item * item_proc | |
1333 | ||
1334 | This is a subroutine that takes the C<item_data> and the C<HTML::Element>s | |
1335 | found via C<item_ld> and produces an arrayref of C<HTML::Element>s which will | |
1336 | eventually be spliced into the container. | |
1337 | ||
1338 | Note that this subroutine MUST return the new items. This is done | |
1339 | So that more items than were passed in can be returned. This is | |
1340 | useful when, for example, you must return 2 dts for an input data item. | |
1341 | And when would you do this? When a single term has multiple spellings | |
1342 | for instance. | |
1343 | ||
1344 | default: expects C<item_data> to be an arrayref of two elements and | |
1345 | C<item_elems> to be an arrayref of two C<HTML::Element>s. It replaces the | |
1346 | content of the C<HTML::Element>s with the C<item_data>. | |
1347 | ||
1348 | =item * splice | |
1349 | ||
1350 | After building up an array of C<@item_elems>, the subroutine passed as | |
1351 | C<splice> will be given the parent container HTML::Element and the | |
1352 | C<@item_elems>. How the C<@item_elems> end up in the container is up to this | |
1353 | routine: it could put half of them in. It could unshift them or whatever. | |
1354 | ||
1355 | default: C<< $container->splice_content(0, 2, @item_elems) >> | |
1356 | In other words, kill the 2 sample elements with the newly generated | |
1357 | @item_elems | |
1358 | ||
1359 | =back | |
1360 | ||
1361 | So now that we have documented the API, let's see the call we need: | |
1362 | ||
1363 | $tree->iter2( | |
1364 | # default wrapper_ld ok. | |
1365 | wrapper_data => \@items, | |
1366 | wrapper_proc => sub { | |
1367 | my ($container) = @_; | |
1368 | ||
1369 | # only keep the last 2 dts and dds | |
1370 | my @content_list = $container->content_list; | |
1371 | $container->splice_content(0, @content_list - 2); | |
1372 | }, | |
1373 | ||
1374 | # default item_ld is fine. | |
1375 | # default item_data is fine. | |
1376 | # default item_proc is fine. | |
1377 | splice => sub { | |
1378 | my ($container, @item_elems) = @_; | |
1379 | $container->unshift_content(@item_elems); | |
1380 | }, | |
1381 | debug => 1, | |
1382 | ); | |
1383 | ||
1384 | ||
9b7a5679 TB |
1385 | |
1386 | ||
1387 | =head3 Select Unrolling | |
67e78ff2 | 1388 | |
1389 | The C<unroll_select> method has this API: | |
1390 | ||
1391 | $tree->unroll_select( | |
1392 | select_label => $id_label, | |
1393 | option_value => $closure, # how to get option value from data row | |
1394 | option_content => $closure, # how to get option content from data row | |
1395 | option_selected => $closure, # boolean to decide if SELECTED | |
1396 | data => $data # the data to be put into the SELECT | |
1397 | data_iter => $closure # the thing that will get a row of data | |
3caedb5b TB |
1398 | debug => $boolean, |
1399 | append => $boolean, # remove the sample <OPTION> data or append? | |
67e78ff2 | 1400 | ); |
1401 | ||
1402 | Here's an example: | |
1403 | ||
1404 | $tree->unroll_select( | |
1405 | select_label => 'clan_list', | |
1406 | option_value => sub { my $row = shift; $row->clan_id }, | |
1407 | option_content => sub { my $row = shift; $row->clan_name }, | |
1408 | option_selected => sub { my $row = shift; $row->selected }, | |
1409 | data => \@query_results, | |
3caedb5b TB |
1410 | data_iter => sub { my $data = shift; $data->next }, |
1411 | append => 0, | |
1412 | debug => 0 | |
1413 | ); | |
67e78ff2 | 1414 | |
1415 | ||
1416 | ||
1417 | =head2 Tree-Building Methods: Table Generation | |
1418 | ||
1419 | Matthew Sisk has a much more intuitive (imperative) | |
1420 | way to generate tables via his module | |
1421 | L<HTML::ElementTable|HTML::ElementTable>. | |
1422 | However, for those with callback fever, the following | |
1423 | method is available. First, we look at a nuts and bolts way to build a table | |
1424 | using only standard L<HTML::Tree> API calls. Then the C<table> method | |
1425 | available here is discussed. | |
1426 | ||
1427 | =head3 Sample Model | |
1428 | ||
1429 | package Simple::Class; | |
1430 | ||
1431 | use Set::Array; | |
1432 | ||
1433 | my @name = qw(bob bill brian babette bobo bix); | |
1434 | my @age = qw(99 12 44 52 12 43); | |
1435 | my @weight = qw(99 52 80 124 120 230); | |
1436 | ||
1437 | ||
1438 | sub new { | |
1439 | my $this = shift; | |
1440 | bless {}, ref($this) || $this; | |
1441 | } | |
1442 | ||
1443 | sub load_data { | |
1444 | my @data; | |
1445 | ||
1446 | for (0 .. 5) { | |
1447 | push @data, { | |
1448 | age => $age[rand $#age] + int rand 20, | |
1449 | name => shift @name, | |
1450 | weight => $weight[rand $#weight] + int rand 40 | |
1451 | } | |
1452 | } | |
1453 | ||
1454 | Set::Array->new(@data); | |
1455 | } | |
1456 | ||
1457 | ||
1458 | 1; | |
1459 | ||
1460 | ||
1461 | =head4 Sample Usage: | |
1462 | ||
1463 | my $data = Simple::Class->load_data; | |
1464 | ++$_->{age} for @$data | |
1465 | ||
1466 | =head3 Inline Code to Unroll a Table | |
1467 | ||
1468 | =head4 HTML | |
1469 | ||
1470 | <html> | |
1471 | ||
1472 | <table id="load_data"> | |
1473 | ||
1474 | <tr> <th>name</th><th>age</th><th>weight</th> </tr> | |
1475 | ||
1476 | <tr id="iterate"> | |
1477 | ||
1478 | <td id="name"> NATURE BOY RIC FLAIR </td> | |
1479 | <td id="age"> 35 </td> | |
1480 | <td id="weight"> 220 </td> | |
1481 | ||
1482 | </tr> | |
1483 | ||
1484 | </table> | |
1485 | ||
1486 | </html> | |
1487 | ||
1488 | ||
1489 | =head4 The manual way (*NOT* recommended) | |
1490 | ||
1491 | require 'simple-class.pl'; | |
1492 | use HTML::Seamstress; | |
1493 | ||
1494 | # load the view | |
1495 | my $seamstress = HTML::Seamstress->new_from_file('simple.html'); | |
1496 | ||
1497 | # load the model | |
1498 | my $o = Simple::Class->new; | |
1499 | my $data = $o->load_data; | |
1500 | ||
1501 | # find the <table> and <tr> | |
1502 | my $table_node = $seamstress->look_down('id', 'load_data'); | |
1503 | my $iter_node = $table_node->look_down('id', 'iterate'); | |
1504 | my $table_parent = $table_node->parent; | |
1505 | ||
1506 | ||
1507 | # drop the sample <table> and <tr> from the HTML | |
1508 | # only add them in if there is data in the model | |
1509 | # this is achieved via the $add_table flag | |
1510 | ||
1511 | $table_node->detach; | |
1512 | $iter_node->detach; | |
1513 | my $add_table; | |
1514 | ||
1515 | # Get a row of model data | |
1516 | while (my $row = shift @$data) { | |
1517 | ||
1518 | # We got row data. Set the flag indicating ok to hook the table into the HTML | |
1519 | ++$add_table; | |
1520 | ||
1521 | # clone the sample <tr> | |
1522 | my $new_iter_node = $iter_node->clone; | |
1523 | ||
1524 | # find the tags labeled name age and weight and | |
1525 | # set their content to the row data | |
1526 | $new_iter_node->content_handler($_ => $row->{$_}) | |
1527 | for qw(name age weight); | |
1528 | ||
1529 | $table_node->push_content($new_iter_node); | |
1530 | ||
1531 | } | |
1532 | ||
1533 | # reattach the table to the HTML tree if we loaded data into some table rows | |
1534 | ||
1535 | $table_parent->push_content($table_node) if $add_table; | |
1536 | ||
1537 | print $seamstress->as_HTML; | |
1538 | ||
1539 | ||
1540 | ||
1541 | =head3 $tree->table() : API call to Unroll a Table | |
1542 | ||
1543 | require 'simple-class.pl'; | |
1544 | use HTML::Seamstress; | |
1545 | ||
1546 | # load the view | |
1547 | my $seamstress = HTML::Seamstress->new_from_file('simple.html'); | |
1548 | # load the model | |
1549 | my $o = Simple::Class->new; | |
1550 | ||
1551 | $seamstress->table | |
1552 | ( | |
1553 | # tell seamstress where to find the table, via the method call | |
1554 | # ->look_down('id', $gi_table). Seamstress detaches the table from the | |
1555 | # HTML tree automatically if no table rows can be built | |
1556 | ||
1557 | gi_table => 'load_data', | |
1558 | ||
1559 | # tell seamstress where to find the tr. This is a bit useless as | |
1560 | # the <tr> usually can be found as the first child of the parent | |
1561 | ||
1562 | gi_tr => 'iterate', | |
1563 | ||
1564 | # the model data to be pushed into the table | |
1565 | ||
1566 | table_data => $o->load_data, | |
1567 | ||
1568 | # the way to take the model data and obtain one row | |
1569 | # if the table data were a hashref, we would do: | |
1570 | # my $key = (keys %$data)[0]; my $val = $data->{$key}; delete $data->{$key} | |
1571 | ||
1572 | tr_data => sub { my ($self, $data) = @_; | |
1573 | shift(@{$data}) ; | |
1574 | }, | |
1575 | ||
1576 | # the way to take a row of data and fill the <td> tags | |
1577 | ||
1578 | td_data => sub { my ($tr_node, $tr_data) = @_; | |
1579 | $tr_node->content_handler($_ => $tr_data->{$_}) | |
1580 | for qw(name age weight) } | |
1581 | ||
1582 | ); | |
1583 | ||
1584 | ||
1585 | print $seamstress->as_HTML; | |
1586 | ||
1587 | ||
1588 | ||
1589 | =head4 Looping over Multiple Sample Rows | |
1590 | ||
1591 | * HTML | |
1592 | ||
1593 | <html> | |
1594 | ||
1595 | <table id="load_data" CELLPADDING=8 BORDER=2> | |
1596 | ||
1597 | <tr> <th>name</th><th>age</th><th>weight</th> </tr> | |
1598 | ||
1599 | <tr id="iterate1" BGCOLOR="white" > | |
1600 | ||
1601 | <td id="name"> NATURE BOY RIC FLAIR </td> | |
1602 | <td id="age"> 35 </td> | |
1603 | <td id="weight"> 220 </td> | |
1604 | ||
1605 | </tr> | |
1606 | <tr id="iterate2" BGCOLOR="#CCCC99"> | |
1607 | ||
1608 | <td id="name"> NATURE BOY RIC FLAIR </td> | |
1609 | <td id="age"> 35 </td> | |
1610 | <td id="weight"> 220 </td> | |
1611 | ||
1612 | </tr> | |
1613 | ||
1614 | </table> | |
1615 | ||
1616 | </html> | |
1617 | ||
1618 | ||
1619 | * Only one change to last API call. | |
1620 | ||
1621 | This: | |
1622 | ||
1623 | gi_tr => 'iterate', | |
1624 | ||
1625 | becomes this: | |
1626 | ||
1627 | gi_tr => ['iterate1', 'iterate2'] | |
1628 | ||
1629 | =head3 $tree->table2() : New API Call to Unroll a Table | |
1630 | ||
1631 | After 2 or 3 years with C<table()>, I began to develop | |
1632 | production websites with it and decided it needed a cleaner | |
1633 | interface, particularly in the area of handling the fact that | |
1634 | C<id> tags will be the same after cloning a table row. | |
1635 | ||
1636 | First, I will give a dry listing of the function's argument parameters. | |
1637 | This will not be educational most likely. A better way to understand how | |
1638 | to use the function is to read through the incremental unrolling of the | |
1639 | function's interface given in conversational style after the dry listing. | |
1640 | But take your pick. It's the same information given in two different | |
1641 | ways. | |
1642 | ||
1643 | =head4 Dry/technical parameter documentation | |
1644 | ||
1645 | C<< $tree->table2(%param) >> takes the following arguments: | |
1646 | ||
1647 | =over | |
1648 | ||
1649 | =item * C<< table_ld => $look_down >> : optional | |
1650 | ||
1651 | How to find the C<table> element in C<$tree>. If C<$look_down> is an | |
1652 | arrayref, then use C<look_down>. If it is a CODE ref, then call it, | |
1653 | passing it C<$tree>. | |
1654 | ||
1655 | Defaults to C<< ['_tag' => 'table'] >> if not passed in. | |
1656 | ||
1657 | =item * C<< table_data => $tabular_data >> : required | |
1658 | ||
1659 | The data to fill the table with. I<Must> be passed in. | |
1660 | ||
1661 | =item * C<< table_proc => $code_ref >> : not implemented | |
1662 | ||
1663 | A subroutine to do something to the table once it is found. | |
1664 | Not currently implemented. Not obviously necessary. Just | |
1665 | created because there is a C<tr_proc> and C<td_proc>. | |
1666 | ||
1667 | =item * C<< tr_ld => $look_down >> : optional | |
1668 | ||
1669 | Same as C<table_ld> but for finding the table row elements. Please note | |
1670 | that the C<tr_ld> is done on the table node that was found I<instead> | |
1671 | of the whole HTML tree. This makes sense. The C<tr>s that you want exist | |
1672 | below the table that was just found. | |
1673 | ||
1674 | Defaults to C<< ['_tag' => 'tr'] >> if not passed in. | |
1675 | ||
1676 | =item * C<< tr_data => $code_ref >> : optional | |
1677 | ||
1678 | How to take the C<table_data> and return a row. Defaults to: | |
1679 | ||
1680 | sub { my ($self, $data) = @_; | |
1681 | shift(@{$data}) ; | |
1682 | } | |
1683 | ||
1684 | =item * C<< tr_proc => $code_ref >> : optional | |
1685 | ||
1686 | Something to do to the table row we are about to add to the | |
1687 | table we are making. Defaults to a routine which makes the C<id> | |
1688 | attribute unique: | |
1689 | ||
1690 | sub { | |
1691 | my ($self, $tr, $tr_data, $tr_base_id, $row_count) = @_; | |
1692 | $tr->attr(id => sprintf "%s_%d", $tr_base_id, $row_count); | |
1693 | } | |
1694 | ||
1695 | =item * C<< td_proc => $code_ref >> : required | |
1696 | ||
1697 | This coderef will take the row of data and operate on the C<td> cells that | |
1698 | are children of the C<tr>. See C<t/table2.t> for several usage examples. | |
1699 | ||
1700 | Here's a sample one: | |
1701 | ||
1702 | sub { | |
1703 | my ($tr, $data) = @_; | |
1704 | my @td = $tr->look_down('_tag' => 'td'); | |
1705 | for my $i (0..$#td) { | |
1706 | $td[$i]->splice_content(0, 1, $data->[$i]); | |
1707 | } | |
1708 | } | |
1709 | ||
1710 | =cut | |
1711 | ||
1712 | =head4 Conversational parameter documentation | |
1713 | ||
1714 | The first thing you need is a table. So we need a look down for that. If you | |
1715 | don't give one, it defaults to | |
1716 | ||
1717 | ['_tag' => 'table'] | |
1718 | ||
1719 | What good is a table to display in without data to display?! | |
1720 | So you must supply a scalar representing your tabular | |
1721 | data source. This scalar might be an array reference, a C<next>able iterator, | |
1722 | a DBI statement handle. Whatever it is, it can be iterated through to build | |
1723 | up rows of table data. | |
1724 | These two required fields (the way to find the table and the data to | |
1725 | display in the table) are C<table_ld> and C<table_data> | |
1726 | respectively. A little more on C<table_ld>. If this happens to be a CODE ref, | |
1727 | then execution | |
1728 | of the code ref is presumed to return the C<HTML::Element> | |
1729 | representing the table in the HTML tree. | |
1730 | ||
1731 | Next, we get the row or rows which serve as sample C<tr> elements by doing | |
1732 | a C<look_down> from the C<table_elem>. While normally one sample row | |
1733 | is enough to unroll a table, consider when you have alternating | |
1734 | table rows. This API call would need one of each row so that it can | |
1735 | cycle through the | |
1736 | sample rows as it loops through the data. | |
1737 | Alternatively, you could always just use one row and | |
1738 | make the necessary changes to the single C<tr> row by | |
1739 | mutating the element in C<tr_proc>, | |
1740 | discussed below. The default C<tr_ld> is | |
1741 | C<< ['_tag' => 'tr'] >> but you can overwrite it. Note well, if you overwrite | |
1742 | it with a subroutine, then it is expected that the subroutine will return | |
1743 | the C<HTML::Element>(s) | |
1744 | which are C<tr> element(s). | |
1745 | The reason a subroutine might be preferred is in the case | |
1746 | that the HTML designers gave you 8 sample C<tr> rows but only one | |
1747 | prototype row is needed. | |
1748 | So you can write a subroutine, to splice out the 7 rows you don't need | |
1749 | and leave the one sample | |
1750 | row remaining so that this API call can clone it and supply it to | |
1751 | the C<tr_proc> and C<td_proc> calls. | |
1752 | ||
1753 | Now, as we move through the table rows with table data, | |
1754 | we need to do two different things on | |
1755 | each table row: | |
1756 | ||
1757 | =over 4 | |
1758 | ||
1759 | =item * get one row of data from the C<table_data> via C<tr_data> | |
1760 | ||
1761 | The default procedure assumes the C<table_data> is an array reference and | |
1762 | shifts a row off of it: | |
1763 | ||
1764 | sub { my ($self, $data) = @_; | |
1765 | shift(@{$data}) ; | |
1766 | } | |
1767 | ||
1768 | Your function MUST return undef when there is no more rows to lay out. | |
1769 | ||
1770 | =item * take the C<tr> element and mutate it via C<tr_proc> | |
1771 | ||
1772 | The default procedure simply makes the id of the table row unique: | |
1773 | ||
1774 | sub { my ($self, $tr, $tr_data, $row_count, $root_id) = @_; | |
1775 | $tr->attr(id => sprintf "%s_%d", $root_id, $row_count); | |
1776 | } | |
1777 | ||
1778 | =back | |
1779 | ||
1780 | Now that we have our row of data, we call C<td_proc> so that it can | |
1781 | take the data and the C<td> cells in this C<tr> and process them. | |
1782 | This function I<must> be supplied. | |
1783 | ||
1784 | ||
1785 | =head3 Whither a Table with No Rows | |
1786 | ||
1787 | Often when a table has no rows, we want to display a message | |
1788 | indicating this to the view. Use conditional processing to decide what | |
1789 | to display: | |
1790 | ||
1791 | <span id=no_data> | |
1792 | <table><tr><td>No Data is Good Data</td></tr></table> | |
1793 | </span> | |
1794 | <span id=load_data> | |
1795 | <html> | |
1796 | ||
1797 | <table id="load_data"> | |
1798 | ||
1799 | <tr> <th>name</th><th>age</th><th>weight</th> </tr> | |
1800 | ||
1801 | <tr id="iterate"> | |
1802 | ||
1803 | <td id="name"> NATURE BOY RIC FLAIR </td> | |
1804 | <td id="age"> 35 </td> | |
1805 | <td id="weight"> 220 </td> | |
1806 | ||
1807 | </tr> | |
1808 | ||
1809 | </table> | |
1810 | ||
1811 | </html> | |
1812 | ||
1813 | </span> | |
1814 | ||
1815 | ||
1816 | ||
1817 | ||
1818 | =head1 SEE ALSO | |
1819 | ||
1820 | =over | |
1821 | ||
1822 | =item * L<HTML::Tree> | |
1823 | ||
1824 | A perl package for creating and manipulating HTML trees | |
1825 | ||
1826 | =item * L<HTML::ElementTable> | |
1827 | ||
1828 | An L<HTML::Tree> - based module which allows for manipulation of HTML | |
1829 | trees using cartesian coordinations. | |
1830 | ||
1831 | =item * L<HTML::Seamstress> | |
1832 | ||
1833 | An L<HTML::Tree> - based module inspired by | |
1834 | XMLC (L<http://xmlc.enhydra.org>), allowing for dynamic | |
1835 | HTML generation via tree rewriting. | |
1836 | ||
1837 | =head1 TODO | |
1838 | ||
1839 | =over | |
1840 | ||
1841 | =item * highlander2 | |
1842 | ||
1843 | currently the API expects the subtrees to survive or be pruned to be | |
1844 | identified by id: | |
1845 | ||
1846 | $if_then->highlander2([ | |
1847 | under10 => sub { $_[0] < 10} , | |
1848 | under18 => sub { $_[0] < 18} , | |
1849 | welcome => [ | |
1850 | sub { 1 }, | |
1851 | sub { | |
1852 | my $branch = shift; | |
1853 | $branch->look_down(id => 'age')->replace_content($age); | |
1854 | } | |
1855 | ] | |
1856 | ], | |
1857 | $age | |
1858 | ); | |
1859 | ||
1860 | but, it should be more flexible. the C<under10>, and C<under18> are | |
1861 | expected to be ids in the tree... but it is not hard to have a check to | |
1862 | see if this field is an array reference and if it, then to do a look | |
1863 | down instead: | |
1864 | ||
1865 | $if_then->highlander2([ | |
1866 | [class => 'under10'] => sub { $_[0] < 10} , | |
1867 | [class => 'under18'] => sub { $_[0] < 18} , | |
1868 | [class => 'welcome'] => [ | |
1869 | sub { 1 }, | |
1870 | sub { | |
1871 | my $branch = shift; | |
1872 | $branch->look_down(id => 'age')->replace_content($age); | |
1873 | } | |
1874 | ] | |
1875 | ], | |
1876 | $age | |
1877 | ); | |
1878 | ||
1879 | ||
1880 | ||
1881 | =cut | |
1882 | ||
1883 | =head1 SEE ALSO | |
1884 | ||
1885 | L<HTML::Seamstress> | |
1886 | ||
f25dca7f | 1887 | =head1 AUTHOR / SOURCE |
67e78ff2 | 1888 | |
1889 | Terrence Brannon, E<lt>tbone@cpan.orgE<gt> | |
1890 | ||
1891 | Many thanks to BARBIE for his RT bug report. | |
1892 | ||
f25dca7f TB |
1893 | The source is at L<http://github.com/metaperl/html-element-library/tree/master> |
1894 | ||
67e78ff2 | 1895 | =head1 COPYRIGHT AND LICENSE |
1896 | ||
1897 | Copyright (C) 2004 by Terrence Brannon | |
1898 | ||
1899 | This library is free software; you can redistribute it and/or modify | |
1900 | it under the same terms as Perl itself, either Perl version 5.8.4 or, | |
1901 | at your option, any later version of Perl 5 you may have available. | |
1902 | ||
1903 | ||
1904 | =cut |