1 package HTML
::Element
::Library
;
11 use Array
::Group
qw(:all);
15 use List
::Util
qw(first);
16 use List
::MoreUtils qw
/:all/;
17 use Params
::Validate
qw(:all);
20 use List
::Rotation
::Cycle
;
22 our %EXPORT_TAGS = ( 'all' => [ qw() ] );
23 our @EXPORT_OK = ( @
{ $EXPORT_TAGS{'all'} } );
32 # Preloaded methods go here.
34 # https://rt.cpan.org/Ticket/Display.html?id=44105
35 sub HTML
::Element
::fillinform
{
37 my ($tree, $hashref, $return_tree, $guts)=@_;
39 (ref $hashref) eq 'HASH' or die 'hashref not supplied as argument' ;
42 my $html = $tree->as_HTML;
43 my $new_html = HTML
::FillInForm
->fill(\
$html, $hashref);
46 my $tree = HTML
::TreeBuilder
->new_from_content($new_html);
47 $tree = $guts ?
$tree->guts : $tree ;
54 sub HTML
::Element
::siblings
{
56 my $p = $element->parent;
61 sub HTML
::Element
::defmap
{
62 my($tree,$attr,$hashref,$debug)=@_;
64 while (my ($k, $v) = (each %$hashref)) {
65 warn "defmap looks for ($attr => $k)" if $debug;
66 my $found = $tree->look_down($attr => $k);
68 warn "($attr => $k) was found.. replacing with '$v'" if $debug;
69 $found->replace_content( $v );
75 sub HTML
::Element
::crunch
{
76 my $container = shift;
78 my %p = validate
(@_, {
79 look_down
=> { type
=> ARRAYREF
},
80 leave
=> { default => 1 },
83 my @look_down = @
{$p{look_down
}} ;
84 my @elem = $container->look_down( @look_down ) ;
88 for my $elem (@elem) {
89 $elem->detach if $left++ >= $p{leave
} ;
94 sub HTML
::Element
::hash_map
{
95 my $container = shift;
97 my %p = validate
(@_, {
98 hash
=> { type
=> HASHREF
},
100 excluding
=> { type
=> ARRAYREF
, default => [] },
101 debug
=> { default => 0 },
104 warn 'The container tag is ', $container->tag if $p{debug
} ;
105 warn 'hash' . Dumper
($p{hash
}) if $p{debug
} ;
106 #warn 'at_under' . Dumper(\@_) if $p{debug} ;
108 my @same_as = $container->look_down( $p{to_attr
} => qr/.+/ ) ;
110 warn 'Found ' . scalar(@same_as) . ' nodes' if $p{debug
} ;
113 for my $same_as (@same_as) {
114 my $attr_val = $same_as->attr($p{to_attr
}) ;
115 if (first
{ $attr_val eq $_ } @
{$p{excluding
}}) {
116 warn "excluding $attr_val" if $p{debug
} ;
119 warn "processing $attr_val" if $p{debug
} ;
120 $same_as->replace_content( $p{hash
}->{$attr_val} ) ;
125 sub HTML
::Element
::hashmap
{
126 my ($container, $attr_name, $hashref, $excluding, $debug) = @_;
130 $container->hash_map(hash
=> $hashref,
131 to_attr
=> $attr_name,
132 excluding
=> $excluding,
138 sub HTML
::Element
::passover
{
139 my ($tree, @to_preserve) = @_;
141 warn "ARGS: my ($tree, @to_preserve)" if $DEBUG;
142 warn $tree->as_HTML(undef, ' ') if $DEBUG;
144 my $exodus = $tree->look_down(id
=> $to_preserve[0]);
146 warn "E: $exodus" if $DEBUG;
148 my @s = HTML
::Element
::siblings
($exodus);
152 if (first
{ $s->attr('id') eq $_ } @to_preserve) {
159 return $exodus; # Goodbye Egypt! http://en.wikipedia.org/wiki/Passover
163 sub HTML
::Element
::sibdex
{
166 firstidx
{ $_ eq $element } $element->siblings
170 sub HTML
::Element
::addr
{ goto &HTML
::Element
::sibdex
}
172 sub HTML
::Element
::replace_content
{
174 $elem->delete_content;
175 $elem->push_content(@_);
178 sub HTML
::Element
::wrap_content
{
179 my($self, $wrap) = @_;
180 my $content = $self->content;
182 $wrap->push_content(@
$content);
186 $self->push_content($wrap);
191 sub HTML
::Element
::Library
::super_literal
{
194 HTML
::Element
->new('~literal', text
=> $text);
198 sub HTML
::Element
::position
{
199 # Report coordinates by chasing addr's up the
200 # HTML::ElementSuper tree. We know we've reached
201 # the top when a) there is no parent, or b) the
202 # parent is some HTML::Element unable to report
208 unshift(@pos, $a) if defined $a;
215 sub HTML
::Element
::content_handler
{
216 my ($tree, %content_hash) = @_;
218 for my $k (keys %content_hash) {
219 $tree->set_child_content(id
=> $k, $content_hash{$k});
225 sub HTML
::Element
::assign
{
226 goto &HTML
::Element
::content_handler
;
238 sub HTML
::Element
::iter
{
239 my ($tree, $p, @data) = @_;
241 # warn 'P: ' , $p->attr('id') ;
242 # warn 'H: ' , $p->as_HTML;
244 # my $id_incr = make_counter;
246 my $new_item = clone
$p;
247 $new_item->replace_content($_);
251 $p->replace_with(@item);
256 sub HTML
::Element
::iter2
{
260 #warn "INPUT TO TABLE2: ", Dumper \@_;
264 wrapper_ld
=> { default => ['_tag' => 'dl'] },
266 wrapper_proc
=> { default => undef },
267 item_ld
=> { default => sub {
270 $tree->look_down('_tag' => 'dt'),
271 $tree->look_down('_tag' => 'dd')
275 item_data
=> { default => sub { my ($wrapper_data) = @_;
276 shift(@
{$wrapper_data}) ;
280 my ($item_elems, $item_data, $row_count) = @_;
281 $item_elems->[$_]->replace_content($item_data->[$_]) for (0,1) ;
284 splice => { default => sub {
285 my ($container, @item_elems) = @_;
286 $container->splice_content(0, 2, @item_elems);
289 debug
=> {default => 0}
293 warn "wrapper_data: " . Dumper
$p{wrapper_data
} if $p{debug
} ;
295 my $container = ref_or_ld
($tree, $p{wrapper_ld
});
296 warn "container: " . $container if $p{debug
} ;
297 warn "wrapper_(preproc): " . $container->as_HTML if $p{debug
} ;
298 $p{wrapper_proc
}->($container) if defined $p{wrapper_proc
} ;
299 warn "wrapper_(postproc): " . $container->as_HTML if $p{debug
} ;
301 my $_item_elems = $p{item_ld
}->($container);
308 my $item_data = $p{item_data
}->($p{wrapper_data
});
309 last unless defined $item_data;
311 warn Dumper
("item_data", $item_data);
314 my $item_elems = [ map { $_->clone } @
{$_item_elems} ] ;
317 for (@
{$item_elems}) {
318 warn "ITEM_ELEMS ", $_->as_HTML;
322 my $new_item_elems = $p{item_proc
}->($item_elems, $item_data, ++$row_count);
325 for (@
{$new_item_elems}) {
326 warn "NEWITEM_ELEMS ", $_->as_HTML;
331 push @item_elem, @
{$new_item_elems} ;
336 warn "pushing " . @item_elem . " elems " if $p{debug
} ;
338 $p{splice}->($container, @item_elem);
342 sub HTML
::Element
::dual_iter
{
343 my ($parent, $data) = @_;
345 my ($prototype_a, $prototype_b) = $parent->content_list;
347 # my $id_incr = make_counter;
352 confess
'dataset does not contain an even number of members';
354 my @iterable_data = ngroup
2 => @
$data;
357 my ($new_a, $new_b) = map { clone
$_ } ($prototype_a, $prototype_b) ;
358 $new_a->splice_content(0,1, $_->[0]);
359 $new_b->splice_content(0,1, $_->[1]);
360 #$_->attr('id', $id_incr->($_->attr('id'))) for ($new_a, $new_b) ;
364 $parent->splice_content(0, 2, @item);
369 sub HTML
::Element
::set_child_content
{
374 my $content_tag = $tree->look_down(@look_down);
376 unless ($content_tag) {
377 warn "criteria [@look_down] not found";
381 $content_tag->replace_content($content);
385 sub HTML
::Element
::highlander
{
386 my ($tree, $local_root_id, $aref, @arg) = @_;
388 ref $aref eq 'ARRAY' or confess
389 "must supply array reference";
392 @aref % 2 == 0 or confess
393 "supplied array ref must have an even number of entries";
395 warn __PACKAGE__
if $DEBUG;
398 while (my ($id, $test) = splice @aref, 0, 2) {
407 my @id_survivor = (id
=> $survivor);
408 my $survivor_node = $tree->look_down(@id_survivor);
410 # warn $local_root_id;
413 warn "survivor: $survivor" if $DEBUG;
414 warn "tree: " . $tree->as_HTML if $DEBUG;
416 $survivor_node or die "search for @id_survivor failed in tree($tree): " . $tree->as_HTML;
418 my $survivor_node_parent = $survivor_node->parent;
419 $survivor_node = $survivor_node->clone;
420 $survivor_node_parent->replace_content($survivor_node);
422 warn "new tree: " . $tree->as_HTML if $DEBUG;
428 sub HTML
::Element
::highlander2
{
431 my %p = validate
(@_, {
432 cond
=> { type
=> ARRAYREF
},
433 cond_arg
=> { type
=> ARRAYREF
,
436 debug
=> { default => 0 }
441 my @cond = @
{$p{cond
}};
442 @cond % 2 == 0 or confess
443 "supplied array ref must have an even number of entries";
445 warn __PACKAGE__
if $p{debug
};
447 my @cond_arg = @
{$p{cond_arg
}};
449 my $survivor; my $then;
450 while (my ($id, $if_then) = splice @cond, 0, 2) {
452 warn $id if $p{debug
};
455 if (ref $if_then eq 'ARRAY') {
456 ($if, $_then) = @
$if_then;
458 ($if, $_then) = ($if_then, sub {});
461 if ($if->(@cond_arg)) {
469 my @ld = (ref $survivor eq 'ARRAY')
474 warn "survivor: ", $survivor if $p{debug
};
475 warn "survivor_ld: ", Dumper \
@ld if $p{debug
};
478 my $survivor_node = $tree->look_down(@ld);
480 $survivor_node or confess
481 "search for @ld failed in tree($tree): " . $tree->as_HTML;
483 my $survivor_node_parent = $survivor_node->parent;
484 $survivor_node = $survivor_node->clone;
485 $survivor_node_parent->replace_content($survivor_node);
488 # **************** NEW FUNCTIONALITY *******************
490 # apply transforms on survivor node
493 warn "SURV::pre_trans " . $survivor_node->as_HTML if $p{debug
};
494 $then->($survivor_node, @cond_arg);
495 warn "SURV::post_trans " . $survivor_node->as_HTML if $p{debug
};
497 # **************** NEW FUNCTIONALITY *******************
506 sub overwrite_action
{
507 my ($mute_node, %X) = @_;
509 $mute_node->attr($X{local_attr
}{name
} => $X{local_attr
}{value
}{new
});
513 sub HTML
::Element
::overwrite_attr
{
516 $tree->mute_elem(@_, \
&overwrite_action
);
521 sub HTML
::Element
::mute_elem
{
522 my ($tree, $mute_attr, $closures, $post_hook) = @_;
524 warn "my mute_node = $tree->look_down($mute_attr => qr/.*/) ;";
525 my @mute_node = $tree->look_down($mute_attr => qr/.*/) ;
527 for my $mute_node (@mute_node) {
528 my ($local_attr,$mute_key) = split /\s+/, $mute_node->attr($mute_attr);
529 my $local_attr_value_current = $mute_node->attr($local_attr);
530 my $local_attr_value_new = $closures->{$mute_key}->($tree, $mute_node, $local_attr_value_current);
537 current
=> $local_attr_value_current,
538 new
=> $local_attr_value_new
547 sub HTML
::Element
::table
{
549 my ($s, %table) = @_;
553 # use Data::Dumper; warn Dumper \%table;
555 # ++$DEBUG if $table{debug} ;
558 # Get the table element
559 $table->{table_node
} = $s->look_down(id
=> $table{gi_table
});
560 $table->{table_node
} or confess
561 "table tag not found via (id => $table{gi_table}";
563 # Get the prototype tr element(s)
564 my @table_gi_tr = listify
$table{gi_tr
} ;
567 my $tr = $table->{table_node
}->look_down(id
=> $_);
568 $tr or confess
"tr with id => $_ not found";
572 warn "found " . @iter_node . " iter nodes " if $DEBUG;
573 # tie my $iter_node, 'Tie::Cycle', \@iter_node;
574 my $iter_node = List
::Rotation
::Cycle
->new(@iter_node);
577 warn Dumper
($iter_node, \
@iter_node) if $DEBUG;
579 # $table->{content} = $table{content};
580 #$table->{parent} = $table->{table_node}->parent;
583 # $table->{table_node}->detach;
584 # $_->detach for @iter_node;
589 my $row = $table{tr_data
}->($table, $table{table_data
});
590 last unless defined $row;
592 # get a sample table row and clone it.
593 my $I = $iter_node->next;
594 warn "I: $I" if $DEBUG;
595 my $new_iter_node = $I->clone;
598 $table{td_data
}->($new_iter_node, $row);
599 push @table_rows, $new_iter_node;
606 my $replace_with_elem = $s->look_down(id
=> shift @table_gi_tr) ;
608 $s->look_down(id
=> $_)->detach;
611 $replace_with_elem->replace_with(@table_rows);
619 my ($tree, $slot) = @_;
621 if (ref($slot) eq 'CODE') {
624 $tree->look_down(@
$slot);
630 sub HTML
::Element
::table2
{
638 table_ld
=> { default => ['_tag' => 'table'] },
640 table_proc
=> { default => undef },
642 tr_ld
=> { default => ['_tag' => 'tr'] },
643 tr_data
=> { default => sub { my ($self, $data) = @_;
646 tr_base_id
=> { default => undef },
647 tr_proc
=> { default => sub {} },
649 debug
=> {default => 0}
653 warn "INPUT TO TABLE2: ", Dumper \
@_ if $p{debug
};
655 warn "table_data: " . Dumper
$p{table_data
} if $p{debug
} ;
659 # use Data::Dumper; warn Dumper \%table;
661 # ++$DEBUG if $table{debug} ;
663 # Get the table element
665 $table->{table_node
} = ref_or_ld
( $tree, $p{table_ld
} ) ;
667 $table->{table_node
} or confess
668 "table tag not found via " . Dumper
($p{table_ld
}) ;
670 warn "table: " . $table->{table_node
}->as_HTML if $p{debug
};
673 # Get the prototype tr element(s)
674 my @proto_tr = ref_or_ld
( $table->{table_node
}, $p{tr_ld
} ) ;
676 warn "found " . @proto_tr . " iter nodes " if $p{debug
};
678 @proto_tr or return ;
681 warn $_->as_HTML for @proto_tr;
683 my $proto_tr = List
::Rotation
::Cycle
->new(@proto_tr);
685 my $tr_parent = $proto_tr[0]->parent;
686 warn "parent element of trs: " . $tr_parent->as_HTML if $p{debug
};
693 my $row = $p{tr_data
}->($table, $p{table_data
}, $row_count);
694 warn "data row: " . Dumper
$row if $p{debug
};
695 last unless defined $row;
697 # wont work: my $new_iter_node = $table->{iter_node}->clone;
698 my $new_tr_node = $proto_tr->next->clone;
699 warn "new_tr_node: $new_tr_node" if $p{debug
};
701 $p{tr_proc
}->($tree, $new_tr_node, $row, $p{tr_base_id
}, ++$row_count)
702 if defined $p{tr_proc
};
704 warn "data row redux: " . Dumper
$row if $p{debug
};
707 $p{td_proc
}->($new_tr_node, $row);
708 push @table_rows, $new_tr_node;
715 $_->detach for @proto_tr;
717 $tr_parent->push_content(@table_rows) if (@table_rows) ;
722 sub HTML
::Element
::unroll_select
{
724 my ($s, %select) = @_;
728 warn "Select Hash: " . Dumper
(\
%select) if $select{debug
};
730 my $select_node = $s->look_down(id
=> $select{select_label
});
731 warn "Select Node: " . $select_node if $select{debug
};
733 unless ($select{append
}) {
734 for my $option ($select_node->look_down('_tag' => 'option')) {
740 my $option = HTML
::Element
->new('option');
741 warn "Option Node: " . $option if $select{debug
};
745 while (my $row = $select{data_iter
}->($select{data
}))
747 warn "Data Row:" . Dumper
($row) if $select{debug
};
748 my $o = $option->clone;
749 $o->attr('value', $select{option_value
}->($row));
750 $o->attr('SELECTED', 1) if (exists $select{option_selected
} and $select{option_selected
}->($row)) ;
752 $o->replace_content($select{option_content
}->($row));
753 $select_node->push_content($o);
754 warn $o->as_HTML if $select{debug
};
762 sub HTML
::Element
::set_sibling_content
{
763 my ($elt, $content) = @_;
765 $elt->parent->splice_content($elt->pindex + 1, 1, $content);
769 sub HTML
::TreeBuilder
::parse_string
{
770 my ($package, $string) = @_;
772 my $h = HTML
::TreeBuilder
->new;
773 HTML
::TreeBuilder
->parse($string);
This page took 0.058268 seconds and 4 git commands to generate.