]>
iEval git - html-element-library.git/blob - lib/HTML/Element/Library.pm
024e114e441a0878ee698adccfc8125bc0a11324
1 package HTML
::Element
::Library
;
2 # ABSTRACT: Convenience methods for HTML::TreeBuilder and HTML::Element
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
::_only_empty_content
{
77 my @c = $self->content_list;
78 my $length = scalar @c;
81 #warn sprintf 'Testing %s (%s)' , $self->starttag, Dumper(\@c);
82 #warn sprintf "\t\tlength of content: %d ", $length;
84 scalar @c == 1 and not length($c[0]);
87 sub HTML
::Element
::prune
{
90 for my $c ($self->content_list) {
92 #warn "C: " . Dumper($c);
97 $self->delete if ($self->is_empty or $self->_only_empty_content);
102 sub HTML
::Element
::crunch
{
103 my $container = shift;
105 my %p = validate
(@_, {
106 look_down
=> { type
=> ARRAYREF
},
107 leave
=> { default => 1 },
110 my @look_down = @
{$p{look_down
}} ;
111 my @elem = $container->look_down( @look_down ) ;
115 for my $elem (@elem) {
116 $elem->detach if $left++ >= $p{leave
} ;
121 sub HTML
::Element
::hash_map
{
122 my $container = shift;
124 my %p = validate
(@_, {
125 hash
=> { type
=> HASHREF
},
127 excluding
=> { type
=> ARRAYREF
, default => [] },
128 debug
=> { default => 0 },
131 warn 'The container tag is ', $container->tag if $p{debug
} ;
132 warn 'hash' . Dumper
($p{hash
}) if $p{debug
} ;
133 #warn 'at_under' . Dumper(\@_) if $p{debug} ;
135 my @same_as = $container->look_down( $p{to_attr
} => qr/.+/ ) ;
137 warn 'Found ' . scalar(@same_as) . ' nodes' if $p{debug
} ;
140 for my $same_as (@same_as) {
141 my $attr_val = $same_as->attr($p{to_attr
}) ;
142 if (first
{ $attr_val eq $_ } @
{$p{excluding
}}) {
143 warn "excluding $attr_val" if $p{debug
} ;
146 warn "processing $attr_val" if $p{debug
} ;
147 $same_as->replace_content( $p{hash
}->{$attr_val} ) ;
152 sub HTML
::Element
::hashmap
{
153 my ($container, $attr_name, $hashref, $excluding, $debug) = @_;
157 $container->hash_map(hash
=> $hashref,
158 to_attr
=> $attr_name,
159 excluding
=> $excluding,
165 sub HTML
::Element
::passover
{
166 my ($tree, @to_preserve) = @_;
168 warn "ARGS: my ($tree, @to_preserve)" if $DEBUG;
169 warn $tree->as_HTML(undef, ' ') if $DEBUG;
171 my $exodus = $tree->look_down(id
=> $to_preserve[0]);
173 warn "E: $exodus" if $DEBUG;
175 my @s = HTML
::Element
::siblings
($exodus);
179 if (first
{ $s->attr('id') eq $_ } @to_preserve) {
186 return $exodus; # Goodbye Egypt! http://en.wikipedia.org/wiki/Passover
190 sub HTML
::Element
::sibdex
{
193 firstidx
{ $_ eq $element } $element->siblings
197 sub HTML
::Element
::addr
{ goto &HTML
::Element
::sibdex
}
199 sub HTML
::Element
::replace_content
{
201 $elem->delete_content;
202 $elem->push_content(@_);
205 sub HTML
::Element
::wrap_content
{
206 my($self, $wrap) = @_;
207 my $content = $self->content;
209 $wrap->push_content(@
$content);
213 $self->push_content($wrap);
218 sub HTML
::Element
::Library
::super_literal
{
221 HTML
::Element
->new('~literal', text
=> $text);
225 sub HTML
::Element
::position
{
226 # Report coordinates by chasing addr's up the
227 # HTML::ElementSuper tree. We know we've reached
228 # the top when a) there is no parent, or b) the
229 # parent is some HTML::Element unable to report
235 unshift(@pos, $a) if defined $a;
242 sub HTML
::Element
::content_handler
{
243 my ($tree, %content_hash) = @_;
245 for my $k (keys %content_hash) {
246 $tree->set_child_content(id
=> $k, $content_hash{$k});
252 sub HTML
::Element
::assign
{
253 goto &HTML
::Element
::content_handler
;
265 sub HTML
::Element
::iter
{
266 my ($tree, $p, @data) = @_;
268 # warn 'P: ' , $p->attr('id') ;
269 # warn 'H: ' , $p->as_HTML;
271 # my $id_incr = make_counter;
273 my $new_item = clone
$p;
274 $new_item->replace_content($_);
278 $p->replace_with(@item);
283 sub HTML
::Element
::iter2
{
287 #warn "INPUT TO TABLE2: ", Dumper \@_;
291 wrapper_ld
=> { default => ['_tag' => 'dl'] },
293 wrapper_proc
=> { default => undef },
294 item_ld
=> { default => sub {
297 $tree->look_down('_tag' => 'dt'),
298 $tree->look_down('_tag' => 'dd')
302 item_data
=> { default => sub { my ($wrapper_data) = @_;
303 shift(@
{$wrapper_data}) ;
307 my ($item_elems, $item_data, $row_count) = @_;
308 $item_elems->[$_]->replace_content($item_data->[$_]) for (0,1) ;
311 splice => { default => sub {
312 my ($container, @item_elems) = @_;
313 $container->splice_content(0, 2, @item_elems);
316 debug
=> {default => 0}
320 warn "wrapper_data: " . Dumper
$p{wrapper_data
} if $p{debug
} ;
322 my $container = ref_or_ld
($tree, $p{wrapper_ld
});
323 warn "container: " . $container if $p{debug
} ;
324 warn "wrapper_(preproc): " . $container->as_HTML if $p{debug
} ;
325 $p{wrapper_proc
}->($container) if defined $p{wrapper_proc
} ;
326 warn "wrapper_(postproc): " . $container->as_HTML if $p{debug
} ;
328 my $_item_elems = $p{item_ld
}->($container);
335 my $item_data = $p{item_data
}->($p{wrapper_data
});
336 last unless defined $item_data;
338 warn Dumper
("item_data", $item_data);
341 my $item_elems = [ map { $_->clone } @
{$_item_elems} ] ;
344 for (@
{$item_elems}) {
345 warn "ITEM_ELEMS ", $_->as_HTML;
349 my $new_item_elems = $p{item_proc
}->($item_elems, $item_data, ++$row_count);
352 for (@
{$new_item_elems}) {
353 warn "NEWITEM_ELEMS ", $_->as_HTML;
358 push @item_elem, @
{$new_item_elems} ;
363 warn "pushing " . @item_elem . " elems " if $p{debug
} ;
365 $p{splice}->($container, @item_elem);
369 sub HTML
::Element
::dual_iter
{
370 my ($parent, $data) = @_;
372 my ($prototype_a, $prototype_b) = $parent->content_list;
374 # my $id_incr = make_counter;
379 confess
'dataset does not contain an even number of members';
381 my @iterable_data = ngroup
2 => @
$data;
384 my ($new_a, $new_b) = map { clone
$_ } ($prototype_a, $prototype_b) ;
385 $new_a->splice_content(0,1, $_->[0]);
386 $new_b->splice_content(0,1, $_->[1]);
387 #$_->attr('id', $id_incr->($_->attr('id'))) for ($new_a, $new_b) ;
391 $parent->splice_content(0, 2, @item);
396 sub HTML
::Element
::set_child_content
{
401 my $content_tag = $tree->look_down(@look_down);
403 unless ($content_tag) {
404 warn "criteria [@look_down] not found";
408 $content_tag->replace_content($content);
412 sub HTML
::Element
::highlander
{
413 my ($tree, $local_root_id, $aref, @arg) = @_;
415 ref $aref eq 'ARRAY' or confess
416 "must supply array reference";
419 @aref % 2 == 0 or confess
420 "supplied array ref must have an even number of entries";
422 warn __PACKAGE__
if $DEBUG;
425 while (my ($id, $test) = splice @aref, 0, 2) {
434 my @id_survivor = (id
=> $survivor);
435 my $survivor_node = $tree->look_down(@id_survivor);
437 # warn $local_root_id;
440 warn "survivor: $survivor" if $DEBUG;
441 warn "tree: " . $tree->as_HTML if $DEBUG;
443 $survivor_node or die "search for @id_survivor failed in tree($tree): " . $tree->as_HTML;
445 my $survivor_node_parent = $survivor_node->parent;
446 $survivor_node = $survivor_node->clone;
447 $survivor_node_parent->replace_content($survivor_node);
449 warn "new tree: " . $tree->as_HTML if $DEBUG;
455 sub HTML
::Element
::highlander2
{
458 my %p = validate
(@_, {
459 cond
=> { type
=> ARRAYREF
},
460 cond_arg
=> { type
=> ARRAYREF
,
463 debug
=> { default => 0 }
468 my @cond = @
{$p{cond
}};
469 @cond % 2 == 0 or confess
470 "supplied array ref must have an even number of entries";
472 warn __PACKAGE__
if $p{debug
};
474 my @cond_arg = @
{$p{cond_arg
}};
476 my $survivor; my $then;
477 while (my ($id, $if_then) = splice @cond, 0, 2) {
479 warn $id if $p{debug
};
482 if (ref $if_then eq 'ARRAY') {
483 ($if, $_then) = @
$if_then;
485 ($if, $_then) = ($if_then, sub {});
488 if ($if->(@cond_arg)) {
496 my @ld = (ref $survivor eq 'ARRAY')
501 warn "survivor: ", $survivor if $p{debug
};
502 warn "survivor_ld: ", Dumper \
@ld if $p{debug
};
505 my $survivor_node = $tree->look_down(@ld);
507 $survivor_node or confess
508 "search for @ld failed in tree($tree): " . $tree->as_HTML;
510 my $survivor_node_parent = $survivor_node->parent;
511 $survivor_node = $survivor_node->clone;
512 $survivor_node_parent->replace_content($survivor_node);
515 # **************** NEW FUNCTIONALITY *******************
517 # apply transforms on survivor node
520 warn "SURV::pre_trans " . $survivor_node->as_HTML if $p{debug
};
521 $then->($survivor_node, @cond_arg);
522 warn "SURV::post_trans " . $survivor_node->as_HTML if $p{debug
};
524 # **************** NEW FUNCTIONALITY *******************
533 sub overwrite_action
{
534 my ($mute_node, %X) = @_;
536 $mute_node->attr($X{local_attr
}{name
} => $X{local_attr
}{value
}{new
});
540 sub HTML
::Element
::overwrite_attr
{
543 $tree->mute_elem(@_, \
&overwrite_action
);
548 sub HTML
::Element
::mute_elem
{
549 my ($tree, $mute_attr, $closures, $post_hook) = @_;
551 warn "my mute_node = $tree->look_down($mute_attr => qr/.*/) ;";
552 my @mute_node = $tree->look_down($mute_attr => qr/.*/) ;
554 for my $mute_node (@mute_node) {
555 my ($local_attr,$mute_key) = split /\s+/, $mute_node->attr($mute_attr);
556 my $local_attr_value_current = $mute_node->attr($local_attr);
557 my $local_attr_value_new = $closures->{$mute_key}->($tree, $mute_node, $local_attr_value_current);
564 current
=> $local_attr_value_current,
565 new
=> $local_attr_value_new
574 sub HTML
::Element
::table
{
576 my ($s, %table) = @_;
580 # use Data::Dumper; warn Dumper \%table;
582 # ++$DEBUG if $table{debug} ;
585 # Get the table element
586 $table->{table_node
} = $s->look_down(id
=> $table{gi_table
});
587 $table->{table_node
} or confess
588 "table tag not found via (id => $table{gi_table}";
590 # Get the prototype tr element(s)
591 my @table_gi_tr = listify
$table{gi_tr
} ;
594 my $tr = $table->{table_node
}->look_down(id
=> $_);
595 $tr or confess
"tr with id => $_ not found";
599 warn "found " . @iter_node . " iter nodes " if $DEBUG;
600 # tie my $iter_node, 'Tie::Cycle', \@iter_node;
601 my $iter_node = List
::Rotation
::Cycle
->new(@iter_node);
604 warn Dumper
($iter_node, \
@iter_node) if $DEBUG;
606 # $table->{content} = $table{content};
607 #$table->{parent} = $table->{table_node}->parent;
610 # $table->{table_node}->detach;
611 # $_->detach for @iter_node;
616 my $row = $table{tr_data
}->($table, $table{table_data
});
617 last unless defined $row;
619 # get a sample table row and clone it.
620 my $I = $iter_node->next;
621 warn "I: $I" if $DEBUG;
622 my $new_iter_node = $I->clone;
625 $table{td_data
}->($new_iter_node, $row);
626 push @table_rows, $new_iter_node;
633 my $replace_with_elem = $s->look_down(id
=> shift @table_gi_tr) ;
635 $s->look_down(id
=> $_)->detach;
638 $replace_with_elem->replace_with(@table_rows);
646 my ($tree, $slot) = @_;
648 if (ref($slot) eq 'CODE') {
651 $tree->look_down(@
$slot);
657 sub HTML
::Element
::table2
{
665 table_ld
=> { default => ['_tag' => 'table'] },
667 table_proc
=> { default => undef },
669 tr_ld
=> { default => ['_tag' => 'tr'] },
670 tr_data
=> { default => sub { my ($self, $data) = @_;
673 tr_base_id
=> { default => undef },
674 tr_proc
=> { default => sub {} },
676 debug
=> {default => 0}
680 warn "INPUT TO TABLE2: ", Dumper \
@_ if $p{debug
};
682 warn "table_data: " . Dumper
$p{table_data
} if $p{debug
} ;
686 # use Data::Dumper; warn Dumper \%table;
688 # ++$DEBUG if $table{debug} ;
690 # Get the table element
692 $table->{table_node
} = ref_or_ld
( $tree, $p{table_ld
} ) ;
694 $table->{table_node
} or confess
695 "table tag not found via " . Dumper
($p{table_ld
}) ;
697 warn "table: " . $table->{table_node
}->as_HTML if $p{debug
};
700 # Get the prototype tr element(s)
701 my @proto_tr = ref_or_ld
( $table->{table_node
}, $p{tr_ld
} ) ;
703 warn "found " . @proto_tr . " iter nodes " if $p{debug
};
705 @proto_tr or return ;
708 warn $_->as_HTML for @proto_tr;
710 my $proto_tr = List
::Rotation
::Cycle
->new(@proto_tr);
712 my $tr_parent = $proto_tr[0]->parent;
713 warn "parent element of trs: " . $tr_parent->as_HTML if $p{debug
};
720 my $row = $p{tr_data
}->($table, $p{table_data
}, $row_count);
721 warn "data row: " . Dumper
$row if $p{debug
};
722 last unless defined $row;
724 # wont work: my $new_iter_node = $table->{iter_node}->clone;
725 my $new_tr_node = $proto_tr->next->clone;
726 warn "new_tr_node: $new_tr_node" if $p{debug
};
728 $p{tr_proc
}->($tree, $new_tr_node, $row, $p{tr_base_id
}, ++$row_count)
729 if defined $p{tr_proc
};
731 warn "data row redux: " . Dumper
$row if $p{debug
};
734 $p{td_proc
}->($new_tr_node, $row);
735 push @table_rows, $new_tr_node;
742 $_->detach for @proto_tr;
744 $tr_parent->push_content(@table_rows) if (@table_rows) ;
749 sub HTML
::Element
::unroll_select
{
751 my ($s, %select) = @_;
755 warn "Select Hash: " . Dumper
(\
%select) if $select{debug
};
757 my $select_node = $s->look_down(id
=> $select{select_label
});
758 warn "Select Node: " . $select_node if $select{debug
};
760 unless ($select{append
}) {
761 for my $option ($select_node->look_down('_tag' => 'option')) {
767 my $option = HTML
::Element
->new('option');
768 warn "Option Node: " . $option if $select{debug
};
772 while (my $row = $select{data_iter
}->($select{data
}))
774 warn "Data Row:" . Dumper
($row) if $select{debug
};
775 my $o = $option->clone;
776 $o->attr('value', $select{option_value
}->($row));
777 $o->attr('SELECTED', 1) if (exists $select{option_selected
} and $select{option_selected
}->($row)) ;
779 $o->replace_content($select{option_content
}->($row));
780 $select_node->push_content($o);
781 warn $o->as_HTML if $select{debug
};
789 sub HTML
::Element
::set_sibling_content
{
790 my ($elt, $content) = @_;
792 $elt->parent->splice_content($elt->pindex + 1, 1, $content);
796 sub HTML
::TreeBuilder
::parse_string
{
797 my ($package, $string) = @_;
799 my $h = HTML
::TreeBuilder
->new;
800 HTML
::TreeBuilder
->parse($string);
This page took 0.098934 seconds and 3 git commands to generate.