diff options
Diffstat (limited to 'lib/Tree/Template/Declare/HTML_Element.pm')
-rw-r--r-- | lib/Tree/Template/Declare/HTML_Element.pm | 111 |
1 files changed, 111 insertions, 0 deletions
diff --git a/lib/Tree/Template/Declare/HTML_Element.pm b/lib/Tree/Template/Declare/HTML_Element.pm new file mode 100644 index 0000000..f8bdb90 --- /dev/null +++ b/lib/Tree/Template/Declare/HTML_Element.pm @@ -0,0 +1,111 @@ +package Tree::Template::Declare::HTML_Element; +use strict; +use warnings; +use Carp; +use HTML::Element; + +our $VERSION='0.6'; + +sub new { + my ($class)=@_; + + return bless {},$class; +} + +sub _munge_exports { + my ($self,$exports)=@_; + + my %all_exports=( + %{$exports}, + text_node => sub($) { + $exports->{node}->( + sub { + $exports->{name}->('~text'); + $exports->{attribs}->(text => $_[0]); + }); + }, + ); + + return \%all_exports; +} + +sub new_tree { + my ($self)=@_; + + return bless [],'Tree::Template::Declare::HTML_Element::Tree'; +} + +sub finalize_tree { + my ($self,$tree)=@_; + + my $dom=$tree->[0]; + $dom->deobjectify_text(); + return $dom; +} + +sub new_node { + my ($self)=@_; + + return HTML::Element->new('~comment'); +} + +sub add_child_node { + my ($self,$parent,$child)=@_; + + + if ($parent->isa('Tree::Template::Declare::HTML_Element::Tree')) { + push @{$parent},$child; + return $parent; + } + return $parent->push_content($child); +} + +sub set_node_name { + my ($self,$node,$name)=@_; + + return $node->tag($name); +} + +sub set_node_attributes { + my ($self,$node,$attrs)=@_; + + while (my ($name,$val)=each %{$attrs}) { + $node->attr($name, $val); + } + return; +} + +1; +__END__ + +=head1 NAME + +Tree::Template::Declare::HTML_Element + +=head1 SYNOPSIS + +See L<Tree::Template::Declare>. + +=head1 SPECIFICITIES + +This module will build trees using L<HTML::Element>. + +To create text nodes, you would be forced to say: + + node { + name '~text'; + attribs text => 'some text'; + } + +which is too cumbersone. You can instead use: + + text_node 'some text'; + +HTML::Element's C<deobjectify_text> method will be called by +C<finalize_tree> before returning the tree object. + +=head1 AUTHOR + +Gianni Ceccarelli <dakkar@thenautilus.net> + +=cut |