From 6ab176ee34c6b0b7e2e1e85dde5bafcd76cb7e93 Mon Sep 17 00:00:00 2001 From: Gianni Ceccarelli Date: Tue, 21 Apr 2009 15:50:39 +0200 Subject: added text nodes to XML --- lib/Tree/Template/Declare.pm | 24 +++++++++++++++++++----- lib/Tree/Template/Declare/LibXML.pm | 19 ++++++++++++++++++- t/04-xml.t | 5 +++-- 3 files changed, 40 insertions(+), 8 deletions(-) diff --git a/lib/Tree/Template/Declare.pm b/lib/Tree/Template/Declare.pm index 1c4a53a..118fcea 100644 --- a/lib/Tree/Template/Declare.pm +++ b/lib/Tree/Template/Declare.pm @@ -80,7 +80,7 @@ sub _build_group { }, }; if ($builder->can('_munge_exports')) { - return $builder->_munge_exports($normal_exports); + return $builder->_munge_exports($normal_exports,\@current_node); } else { return $normal_exports; @@ -207,10 +207,24 @@ adds the second node at the end of the children list of the first node =back The builder can also implement an C<_munge_exports> method. If it -does, C<_munge_exports> will be called with a hash ref consisting of -the methods that C wants to export, and it -should return a hash ref with the methods that will actually be -exported. +does, C<_munge_exports> will be called with: + +=over 4 + +=item * + +a hash ref consisting of the functions that C +wants to export, + +=item * + +an array ref, whose first element will be the current node whenever +the user calls an exported function + +=back + +C<_munge_exports> should return a hash ref with the functions that +will actually be exported. See L, in particular the section on group builders, for details. See L and diff --git a/lib/Tree/Template/Declare/LibXML.pm b/lib/Tree/Template/Declare/LibXML.pm index d5e208c..066d2e4 100644 --- a/lib/Tree/Template/Declare/LibXML.pm +++ b/lib/Tree/Template/Declare/LibXML.pm @@ -11,7 +11,7 @@ sub new { } sub _munge_exports { - my ($self,$exports)=@_; + my ($self,$exports,$current_node_aref)=@_; return { %$exports, @@ -19,6 +19,11 @@ sub _munge_exports { $self->{ns}->{$_[0]}=$_[1]; return; }, + text_node => sub($) { + if ($current_node_aref->[0]) { + $current_node_aref->[0]->appendTextNode($_[0]); + }, + }, }; } @@ -117,6 +122,18 @@ A function C is exported, so that you can declare XML namespaces: You I create nodes with qualified names with undeclared prefixes, but it's probably not a good idea. +To add text nodes, you could do something like: + + my $el=node { name 'elem_with_text' }; + $el->appendTextNode('some text content'); + +This is ugly, so you can do: + + node { + name 'elem_with_text'; + text_node 'some text content'; + }; + =head1 AUTHOR Gianni Ceccarelli diff --git a/t/04-xml.t b/t/04-xml.t index 84bfe06..3512469 100644 --- a/t/04-xml.t +++ b/t/04-xml.t @@ -22,6 +22,7 @@ sub make_tree { attribs id => 1; node { name 'test:sub1'; + text_node 'some content'; } }; node { @@ -36,7 +37,7 @@ sub make_tree { my $tree=make_tree(); is($tree->serialize(0), - qq{\n\n}, + qq{\nsome content\n}, 'XML document without default NS' ); } @@ -47,7 +48,7 @@ xmlns ':default' => 'ftp://test/'; my $tree=make_tree(); is($tree->serialize(0), - qq{\n\n}, + qq{\nsome content\n}, 'XML document with default NS' ); } -- cgit v1.2.3