X-Git-Url: http://git.ieval.ro/?a=blobdiff_plain;f=lib%2FHTML%2FElement%2FLibrary.pm;h=4c2d8793da93fa49dabc1c98df05aad4b09d621a;hb=63007e38f0bdac71e72ac6e7eb4c07169d2a2bf7;hp=6b9e27ad1ecb9bde186630b767efa82e0316843e;hpb=9a087986e1896870b46e15cebc8a5fa7e2233247;p=html-element-library.git diff --git a/lib/HTML/Element/Library.pm b/lib/HTML/Element/Library.pm index 6b9e27a..4c2d879 100644 --- a/lib/HTML/Element/Library.pm +++ b/lib/HTML/Element/Library.pm @@ -28,8 +28,28 @@ our @EXPORT = qw(); our $VERSION = '3.53'; + # Preloaded methods go here. +# https://rt.cpan.org/Ticket/Display.html?id=44105 +sub HTML::Element::fillinform { + + my ($tree, $hashref, $return_tree)=@_; + + (ref $hashref) eq 'HASH' or die 'hashref not supplied as argument' ; + + use HTML::FillInForm; + my $html = $tree->as_HTML; + my $new_html = HTML::FillInForm->fill(\$html, $hashref); + + if ($return_tree) { + HTML::TreeBuilder->new_from_content($new_html); + } else { + $new_html; + } + +} + sub HTML::Element::siblings { my $element = shift; my $p = $element->parent; @@ -97,12 +117,12 @@ sub HTML::Element::hashmap { sub HTML::Element::passover { - my ($tree, $child_id) = @_; + my ($tree, @to_preserve) = @_; - warn "ARGS: my ($tree, $child_id)" if $DEBUG; + warn "ARGS: my ($tree, @to_preserve)" if $DEBUG; warn $tree->as_HTML(undef, ' ') if $DEBUG; - my $exodus = $tree->look_down(id => $child_id); + my $exodus = $tree->look_down(id => $to_preserve[0]); warn "E: $exodus" if $DEBUG; @@ -110,7 +130,7 @@ sub HTML::Element::passover { for my $s (@s) { next unless ref $s; - if ($s->attr('id') eq $child_id) { + if (first { $s->attr('id') eq $_ } @to_preserve) { ; } else { $s->delete; @@ -797,6 +817,27 @@ One of these days, I'll around to writing a nice C section. =head2 Tree Rewriting Methods +=head3 Simplifying calls to HTML::FillInForm + +Since HTML::FillInForm gets and returns strings, using HTML::Element instances +becomes tedious: + + 1. Seamstress has an HTML tree that it wants the form filled in on + 2. Seamstress converts this tree to a string + 3. FillInForm parses the string into an HTML tree and then fills in the form + 4. FillInForm converts the HTML tree to a string + 5. Seamstress re-parses the HTML for additional processing + +I've filed a bug about this: +L + +This function, fillinform, +allows you to pass a tree to fillinform (along with your data structure) and +get back a tree: + + my $new_tree = $html_tree->fillinform($data_structure); + + =head3 Mapping a hashref to HTML elements It is very common to get a hashref of data from some external source - flat file, database, XML, etc. @@ -886,7 +927,7 @@ 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->hashmap('sid' => \%data, ['password']); + $tree->hashmap(smap => \%data, ['password']); Note: the other way to prevent rendering some of the hash mapping is to not give that element the attr @@ -994,14 +1035,16 @@ id C remains. For age less than 18, the node with id C remains. Otherwise our "else" condition fires and the child with id C remains. -=head3 $tree->passover($id_of_element) +=head3 $tree->passover(@id_of_element) -In some cases, you know exactly which element should survive. In this case, -you can simply call C to remove it's siblings. For the HTML +In some cases, you know exactly which element(s) should survive. In this case, +you can simply call C to remove it's (their) siblings. For the HTML above, you could delete C and C by simply calling: $tree->passover('under18'); +Because passover takes an array, you can specify several children to preserve. + =head3 $tree->highlander2($tree, $conditionals, @conditionals_args) Right around the same time that C came into being, Seamstress