]>
iEval git - html-element-library.git/blob - lib/HTML/Element/Library.pm
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'} } );
28 our $VERSION = '4.2.b';
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 );
76 sub HTML
::Element
::hash_map
{
77 my $container = shift;
79 my %p = validate
(@_, {
80 hash
=> { type
=> HASHREF
},
82 excluding
=> { type
=> ARRAYREF
, default => [] },
83 debug
=> { default => 0 },
86 warn 'The container tag is ', $container->tag if $p{debug
} ;
87 warn 'hash' . Dumper
($p{hash
}) if $p{debug
} ;
88 #warn 'at_under' . Dumper(\@_) if $p{debug} ;
90 my @same_as = $container->look_down( $p{to_attr
} => qr/.+/ ) ;
92 warn 'Found ' . scalar(@same_as) . ' nodes' if $p{debug
} ;
95 for my $same_as (@same_as) {
96 my $attr_val = $same_as->attr($p{to_attr
}) ;
97 if (first
{ $attr_val eq $_ } @
{$p{excluding
}}) {
98 warn "excluding $attr_val" if $p{debug
} ;
101 warn "processing $attr_val" if $p{debug
} ;
102 $same_as->replace_content( $p{hash
}->{$attr_val} ) ;
107 sub HTML
::Element
::hashmap
{
108 my ($container, $attr_name, $hashref, $excluding, $debug) = @_;
112 $container->hash_map(hash
=> $hashref,
113 to_attr
=> $attr_name,
114 excluding
=> $excluding,
120 sub HTML
::Element
::passover
{
121 my ($tree, @to_preserve) = @_;
123 warn "ARGS: my ($tree, @to_preserve)" if $DEBUG;
124 warn $tree->as_HTML(undef, ' ') if $DEBUG;
126 my $exodus = $tree->look_down(id
=> $to_preserve[0]);
128 warn "E: $exodus" if $DEBUG;
130 my @s = HTML
::Element
::siblings
($exodus);
134 if (first
{ $s->attr('id') eq $_ } @to_preserve) {
141 return $exodus; # Goodbye Egypt! http://en.wikipedia.org/wiki/Passover
145 sub HTML
::Element
::sibdex
{
148 firstidx
{ $_ eq $element } $element->siblings
152 sub HTML
::Element
::addr
{ goto &HTML
::Element
::sibdex
}
154 sub HTML
::Element
::replace_content
{
156 $elem->delete_content;
157 $elem->push_content(@_);
160 sub HTML
::Element
::wrap_content
{
161 my($self, $wrap) = @_;
162 my $content = $self->content;
164 $wrap->push_content(@
$content);
168 $self->push_content($wrap);
173 sub HTML
::Element
::Library
::super_literal
{
176 HTML
::Element
->new('~literal', text
=> $text);
180 sub HTML
::Element
::position
{
181 # Report coordinates by chasing addr's up the
182 # HTML::ElementSuper tree. We know we've reached
183 # the top when a) there is no parent, or b) the
184 # parent is some HTML::Element unable to report
190 unshift(@pos, $a) if defined $a;
197 sub HTML
::Element
::content_handler
{
198 my ($tree, %content_hash) = @_;
200 for my $k (keys %content_hash) {
201 $tree->set_child_content(id
=> $k, $content_hash{$k});
207 sub HTML
::Element
::assign
{
208 goto &HTML
::Element
::content_handler
;
220 sub HTML
::Element
::iter
{
221 my ($tree, $p, @data) = @_;
223 # warn 'P: ' , $p->attr('id') ;
224 # warn 'H: ' , $p->as_HTML;
226 # my $id_incr = make_counter;
228 my $new_item = clone
$p;
229 $new_item->replace_content($_);
233 $p->replace_with(@item);
238 sub HTML
::Element
::iter2
{
242 #warn "INPUT TO TABLE2: ", Dumper \@_;
246 wrapper_ld
=> { default => ['_tag' => 'dl'] },
248 wrapper_proc
=> { default => undef },
249 item_ld
=> { default => sub {
252 $tree->look_down('_tag' => 'dt'),
253 $tree->look_down('_tag' => 'dd')
257 item_data
=> { default => sub { my ($wrapper_data) = @_;
258 shift(@
{$wrapper_data}) ;
262 my ($item_elems, $item_data, $row_count) = @_;
263 $item_elems->[$_]->replace_content($item_data->[$_]) for (0,1) ;
266 splice => { default => sub {
267 my ($container, @item_elems) = @_;
268 $container->splice_content(0, 2, @item_elems);
271 debug
=> {default => 0}
275 warn "wrapper_data: " . Dumper
$p{wrapper_data
} if $p{debug
} ;
277 my $container = ref_or_ld
($tree, $p{wrapper_ld
});
278 warn "container: " . $container if $p{debug
} ;
279 warn "wrapper_(preproc): " . $container->as_HTML if $p{debug
} ;
280 $p{wrapper_proc
}->($container) if defined $p{wrapper_proc
} ;
281 warn "wrapper_(postproc): " . $container->as_HTML if $p{debug
} ;
283 my $_item_elems = $p{item_ld
}->($container);
290 my $item_data = $p{item_data
}->($p{wrapper_data
});
291 last unless defined $item_data;
293 warn Dumper
("item_data", $item_data);
296 my $item_elems = [ map { $_->clone } @
{$_item_elems} ] ;
299 for (@
{$item_elems}) {
300 warn "ITEM_ELEMS ", $_->as_HTML;
304 my $new_item_elems = $p{item_proc
}->($item_elems, $item_data, ++$row_count);
307 for (@
{$new_item_elems}) {
308 warn "NEWITEM_ELEMS ", $_->as_HTML;
313 push @item_elem, @
{$new_item_elems} ;
318 warn "pushing " . @item_elem . " elems " if $p{debug
} ;
320 $p{splice}->($container, @item_elem);
324 sub HTML
::Element
::dual_iter
{
325 my ($parent, $data) = @_;
327 my ($prototype_a, $prototype_b) = $parent->content_list;
329 # my $id_incr = make_counter;
334 confess
'dataset does not contain an even number of members';
336 my @iterable_data = ngroup
2 => @
$data;
339 my ($new_a, $new_b) = map { clone
$_ } ($prototype_a, $prototype_b) ;
340 $new_a->splice_content(0,1, $_->[0]);
341 $new_b->splice_content(0,1, $_->[1]);
342 #$_->attr('id', $id_incr->($_->attr('id'))) for ($new_a, $new_b) ;
346 $parent->splice_content(0, 2, @item);
351 sub HTML
::Element
::set_child_content
{
356 my $content_tag = $tree->look_down(@look_down);
358 unless ($content_tag) {
359 warn "criteria [@look_down] not found";
363 $content_tag->replace_content($content);
367 sub HTML
::Element
::highlander
{
368 my ($tree, $local_root_id, $aref, @arg) = @_;
370 ref $aref eq 'ARRAY' or confess
371 "must supply array reference";
374 @aref % 2 == 0 or confess
375 "supplied array ref must have an even number of entries";
377 warn __PACKAGE__
if $DEBUG;
380 while (my ($id, $test) = splice @aref, 0, 2) {
389 my @id_survivor = (id
=> $survivor);
390 my $survivor_node = $tree->look_down(@id_survivor);
392 # warn $local_root_id;
395 warn "survivor: $survivor" if $DEBUG;
396 warn "tree: " . $tree->as_HTML if $DEBUG;
398 $survivor_node or die "search for @id_survivor failed in tree($tree): " . $tree->as_HTML;
400 my $survivor_node_parent = $survivor_node->parent;
401 $survivor_node = $survivor_node->clone;
402 $survivor_node_parent->replace_content($survivor_node);
404 warn "new tree: " . $tree->as_HTML if $DEBUG;
410 sub HTML
::Element
::highlander2
{
413 my %p = validate
(@_, {
414 cond
=> { type
=> ARRAYREF
},
415 cond_arg
=> { type
=> ARRAYREF
,
418 debug
=> { default => 0 }
423 my @cond = @
{$p{cond
}};
424 @cond % 2 == 0 or confess
425 "supplied array ref must have an even number of entries";
427 warn __PACKAGE__
if $p{debug
};
429 my @cond_arg = @
{$p{cond_arg
}};
431 my $survivor; my $then;
432 while (my ($id, $if_then) = splice @cond, 0, 2) {
434 warn $id if $p{debug
};
437 if (ref $if_then eq 'ARRAY') {
438 ($if, $_then) = @
$if_then;
440 ($if, $_then) = ($if_then, sub {});
443 if ($if->(@cond_arg)) {
451 my @ld = (ref $survivor eq 'ARRAY')
456 warn "survivor: ", $survivor if $p{debug
};
457 warn "survivor_ld: ", Dumper \
@ld if $p{debug
};
460 my $survivor_node = $tree->look_down(@ld);
462 $survivor_node or confess
463 "search for @ld failed in tree($tree): " . $tree->as_HTML;
465 my $survivor_node_parent = $survivor_node->parent;
466 $survivor_node = $survivor_node->clone;
467 $survivor_node_parent->replace_content($survivor_node);
470 # **************** NEW FUNCTIONALITY *******************
472 # apply transforms on survivor node
475 warn "SURV::pre_trans " . $survivor_node->as_HTML if $p{debug
};
476 $then->($survivor_node, @cond_arg);
477 warn "SURV::post_trans " . $survivor_node->as_HTML if $p{debug
};
479 # **************** NEW FUNCTIONALITY *******************
488 sub overwrite_action
{
489 my ($mute_node, %X) = @_;
491 $mute_node->attr($X{local_attr
}{name
} => $X{local_attr
}{value
}{new
});
495 sub HTML
::Element
::overwrite_attr
{
498 $tree->mute_elem(@_, \
&overwrite_action
);
503 sub HTML
::Element
::mute_elem
{
504 my ($tree, $mute_attr, $closures, $post_hook) = @_;
506 warn "my mute_node = $tree->look_down($mute_attr => qr/.*/) ;";
507 my @mute_node = $tree->look_down($mute_attr => qr/.*/) ;
509 for my $mute_node (@mute_node) {
510 my ($local_attr,$mute_key) = split /\s+/, $mute_node->attr($mute_attr);
511 my $local_attr_value_current = $mute_node->attr($local_attr);
512 my $local_attr_value_new = $closures->{$mute_key}->($tree, $mute_node, $local_attr_value_current);
519 current
=> $local_attr_value_current,
520 new
=> $local_attr_value_new
529 sub HTML
::Element
::table
{
531 my ($s, %table) = @_;
535 # use Data::Dumper; warn Dumper \%table;
537 # ++$DEBUG if $table{debug} ;
540 # Get the table element
541 $table->{table_node
} = $s->look_down(id
=> $table{gi_table
});
542 $table->{table_node
} or confess
543 "table tag not found via (id => $table{gi_table}";
545 # Get the prototype tr element(s)
546 my @table_gi_tr = listify
$table{gi_tr
} ;
549 my $tr = $table->{table_node
}->look_down(id
=> $_);
550 $tr or confess
"tr with id => $_ not found";
554 warn "found " . @iter_node . " iter nodes " if $DEBUG;
555 # tie my $iter_node, 'Tie::Cycle', \@iter_node;
556 my $iter_node = List
::Rotation
::Cycle
->new(@iter_node);
559 warn Dumper
($iter_node, \
@iter_node) if $DEBUG;
561 # $table->{content} = $table{content};
562 #$table->{parent} = $table->{table_node}->parent;
565 # $table->{table_node}->detach;
566 # $_->detach for @iter_node;
571 my $row = $table{tr_data
}->($table, $table{table_data
});
572 last unless defined $row;
574 # get a sample table row and clone it.
575 my $I = $iter_node->next;
576 warn "I: $I" if $DEBUG;
577 my $new_iter_node = $I->clone;
580 $table{td_data
}->($new_iter_node, $row);
581 push @table_rows, $new_iter_node;
588 my $replace_with_elem = $s->look_down(id
=> shift @table_gi_tr) ;
590 $s->look_down(id
=> $_)->detach;
593 $replace_with_elem->replace_with(@table_rows);
601 my ($tree, $slot) = @_;
603 if (ref($slot) eq 'CODE') {
606 $tree->look_down(@
$slot);
612 sub HTML
::Element
::table2
{
620 table_ld
=> { default => ['_tag' => 'table'] },
622 table_proc
=> { default => undef },
624 tr_ld
=> { default => ['_tag' => 'tr'] },
625 tr_data
=> { default => sub { my ($self, $data) = @_;
628 tr_base_id
=> { default => undef },
629 tr_proc
=> { default => sub {} },
631 debug
=> {default => 0}
635 warn "INPUT TO TABLE2: ", Dumper \
@_ if $p{debug
};
637 warn "table_data: " . Dumper
$p{table_data
} if $p{debug
} ;
641 # use Data::Dumper; warn Dumper \%table;
643 # ++$DEBUG if $table{debug} ;
645 # Get the table element
647 $table->{table_node
} = ref_or_ld
( $tree, $p{table_ld
} ) ;
649 $table->{table_node
} or confess
650 "table tag not found via " . Dumper
($p{table_ld
}) ;
652 warn "table: " . $table->{table_node
}->as_HTML if $p{debug
};
655 # Get the prototype tr element(s)
656 my @proto_tr = ref_or_ld
( $table->{table_node
}, $p{tr_ld
} ) ;
658 warn "found " . @proto_tr . " iter nodes " if $p{debug
};
660 @proto_tr or return ;
663 warn $_->as_HTML for @proto_tr;
665 my $proto_tr = List
::Rotation
::Cycle
->new(@proto_tr);
667 my $tr_parent = $proto_tr[0]->parent;
668 warn "parent element of trs: " . $tr_parent->as_HTML if $p{debug
};
675 my $row = $p{tr_data
}->($table, $p{table_data
}, $row_count);
676 warn "data row: " . Dumper
$row if $p{debug
};
677 last unless defined $row;
679 # wont work: my $new_iter_node = $table->{iter_node}->clone;
680 my $new_tr_node = $proto_tr->next->clone;
681 warn "new_tr_node: $new_tr_node" if $p{debug
};
683 $p{tr_proc
}->($tree, $new_tr_node, $row, $p{tr_base_id
}, ++$row_count)
684 if defined $p{tr_proc
};
686 warn "data row redux: " . Dumper
$row if $p{debug
};
689 $p{td_proc
}->($new_tr_node, $row);
690 push @table_rows, $new_tr_node;
697 $_->detach for @proto_tr;
699 $tr_parent->push_content(@table_rows) if (@table_rows) ;
704 sub HTML
::Element
::unroll_select
{
706 my ($s, %select) = @_;
710 warn "Select Hash: " . Dumper
(\
%select) if $select{debug
};
712 my $select_node = $s->look_down(id
=> $select{select_label
});
713 warn "Select Node: " . $select_node if $select{debug
};
715 unless ($select{append
}) {
716 for my $option ($select_node->look_down('_tag' => 'option')) {
722 my $option = HTML
::Element
->new('option');
723 warn "Option Node: " . $option if $select{debug
};
727 while (my $row = $select{data_iter
}->($select{data
}))
729 warn "Data Row:" . Dumper
($row) if $select{debug
};
730 my $o = $option->clone;
731 $o->attr('value', $select{option_value
}->($row));
732 $o->attr('SELECTED', 1) if (exists $select{option_selected
} and $select{option_selected
}->($row)) ;
734 $o->replace_content($select{option_content
}->($row));
735 $select_node->push_content($o);
736 warn $o->as_HTML if $select{debug
};
744 sub HTML
::Element
::set_sibling_content
{
745 my ($elt, $content) = @_;
747 $elt->parent->splice_content($elt->pindex + 1, 1, $content);
751 sub HTML
::TreeBuilder
::parse_string
{
752 my ($package, $string) = @_;
754 my $h = HTML
::TreeBuilder
->new;
755 HTML
::TreeBuilder
->parse($string);
This page took 0.121326 seconds and 4 git commands to generate.