From f25dca7f18cd90087a76cda08b300221de93e5db Mon Sep 17 00:00:00 2001 From: Terrence Brannon Date: Tue, 24 Feb 2009 09:52:35 -0500 Subject: [PATCH] implement defmap --- lib/HTML/Element/Library.pm | 45 ++++++++++++++++++++++++++++++++---- t/defmap.t | 31 +++++++++++++++++++++++++ t/{same_as.t => hashmap.t} | 0 t/html/defmap/defmap.exp | 5 ++++ t/html/defmap/defmap.gen | 5 ++++ t/html/defmap/defmap.initial | 7 ++++++ t/html/unroll_select.exp | 6 ++--- t/html/unroll_select.gen | 6 ++--- 8 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 t/defmap.t rename t/{same_as.t => hashmap.t} (100%) create mode 100644 t/html/defmap/defmap.exp create mode 100644 t/html/defmap/defmap.gen create mode 100644 t/html/defmap/defmap.initial diff --git a/lib/HTML/Element/Library.pm b/lib/HTML/Element/Library.pm index 936840b..7573cbd 100644 --- a/lib/HTML/Element/Library.pm +++ b/lib/HTML/Element/Library.pm @@ -37,6 +37,19 @@ sub HTML::Element::siblings { $p->content_list; } +sub HTML::Element::defmap { + my($tree,$attr,$hashref)=@_; + + while (my ($k, $v) = (each %$hashref)) { + my $found = $tree->look_down($attr => $k); + if ($found) { + $found->replace_content( $v ); + } + } + +} + + sub HTML::Element::hash_map { my $container = shift; @@ -49,7 +62,7 @@ sub HTML::Element::hash_map { warn 'The container tag is ', $container->tag if $p{debug} ; warn 'hash' . Dumper($p{hash}) if $p{debug} ; - warn 'at_under' . Dumper(\@_); + warn 'at_under' . Dumper(\@_) if $p{debug} ; my @same_as = $container->look_down( $p{to_attr} => qr/.+/ ) ; @@ -68,6 +81,18 @@ sub HTML::Element::hash_map { } +sub HTML::Element::hashmap { + my ($container, $attr_name, $hashref, $excluding, $debug) = @_; + + $excluding ||= [] ; + + $container->hash_map(hash => $hashref, + to_attr => $attr_name, + excluding => $excluding, + debug => $debug); + +} + sub HTML::Element::passover { my ($tree, $child_id) = @_; @@ -656,6 +681,8 @@ sub HTML::Element::unroll_select { my $select = {}; + warn "Select Hash: " . Dumper(\%select) if $select{debug}; + my $select_node = $s->look_down(id => $select{select_label}); warn "Select Node: " . $select_node if $select{debug}; @@ -769,7 +796,7 @@ One of these days, I'll around to writing a nice C section. =head2 Tree Rewriting Methods -=head3 $elem->hash_map(hash => \%h, to_attr => $attr, excluding => \@excluded) +=head3 $elem->hashmap($attr_name, \%hashref, \@excluded, $debug) This method is designed to take a hashref and populate a series of elements. For example: @@ -788,11 +815,17 @@ In the table above, there are several attributes named C<< smap >>. If we have a Then a single API call allows us to populate the HTML while excluding those ones we dont: - $tree->hash_map(hash => \%data, to_attr => 'sid', excluding => ['password']); + $tree->hashmap('sid' => \%data, ['password']); + -Of course, the other way to prevent rendering some of the hash mapping is to not give that element the attr +Note: the other way to prevent rendering some of the hash mapping is to not give that element the attr you plan to use for hash mapping. +Also note: the function C<< hashmap >> has a simple easy-to-type API. Interally, it calls C<< hash_map >> +(which has a more verbose keyword calling API). Thus, the above call to C results in this call: + + $tree->hash_map(hash => \%data, to_attr => 'sid', excluding => ['password']); + =head3 $elem->replace_content(@new_elem) @@ -1725,12 +1758,14 @@ down instead: L -=head1 AUTHOR +=head1 AUTHOR / SOURCE Terrence Brannon, Etbone@cpan.orgE Many thanks to BARBIE for his RT bug report. +The source is at L + =head1 COPYRIGHT AND LICENSE Copyright (C) 2004 by Terrence Brannon diff --git a/t/defmap.t b/t/defmap.t new file mode 100644 index 0000000..9f61672 --- /dev/null +++ b/t/defmap.t @@ -0,0 +1,31 @@ +# This might look like shell script, but it's actually -*- perl -*- +use strict;use warnings; +use lib qw(t/ t/m/); + +use File::Slurp; +use Test::More qw(no_plan); + +use TestUtils; +use HTML::TreeBuilder; +use HTML::Element::Library; + +sub tage { + + my $root = "t/html/defmap/defmap"; + + my $tree = HTML::TreeBuilder->new_from_file("$root.initial")->guts; + + #warn "TREE: $tree" . $tree->as_HTML; + + my %data = (pause => 'arsenal rules'); + + $tree->defmap(smap => \%data); + + my $generated_html = ptree($tree, "$root.gen"); + + is ($generated_html, File::Slurp::read_file("$root.exp"), "HTML for same_as"); +} + + +tage(); + diff --git a/t/same_as.t b/t/hashmap.t similarity index 100% rename from t/same_as.t rename to t/hashmap.t diff --git a/t/html/defmap/defmap.exp b/t/html/defmap/defmap.exp new file mode 100644 index 0000000..f5aabd8 --- /dev/null +++ b/t/html/defmap/defmap.exp @@ -0,0 +1,5 @@ + + Yes + arsenal rules + No + diff --git a/t/html/defmap/defmap.gen b/t/html/defmap/defmap.gen new file mode 100644 index 0000000..f5aabd8 --- /dev/null +++ b/t/html/defmap/defmap.gen @@ -0,0 +1,5 @@ + + Yes + arsenal rules + No + diff --git a/t/html/defmap/defmap.initial b/t/html/defmap/defmap.initial new file mode 100644 index 0000000..7e24e57 --- /dev/null +++ b/t/html/defmap/defmap.initial @@ -0,0 +1,7 @@ + + Yes + + No + + No + diff --git a/t/html/unroll_select.exp b/t/html/unroll_select.exp index f44f26a..f6a977e 100644 --- a/t/html/unroll_select.exp +++ b/t/html/unroll_select.exp @@ -23,9 +23,9 @@ Clan Name diff --git a/t/html/unroll_select.gen b/t/html/unroll_select.gen index f44f26a..f6a977e 100644 --- a/t/html/unroll_select.gen +++ b/t/html/unroll_select.gen @@ -23,9 +23,9 @@ Clan Name -- 2.30.2