summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGianni Ceccarelli <dakkar@dechirico.(none)>2009-04-21 15:50:39 +0200
committerGianni Ceccarelli <dakkar@dechirico.(none)>2009-04-21 15:50:39 +0200
commit6ab176ee34c6b0b7e2e1e85dde5bafcd76cb7e93 (patch)
tree77f51432877d1f316eb65caeea122063627ab6f3
parentdocs, and changed the export-munging methods for builders (diff)
downloadTree-Template-Declare-6ab176ee34c6b0b7e2e1e85dde5bafcd76cb7e93.tar.gz
Tree-Template-Declare-6ab176ee34c6b0b7e2e1e85dde5bafcd76cb7e93.tar.bz2
Tree-Template-Declare-6ab176ee34c6b0b7e2e1e85dde5bafcd76cb7e93.zip
added text nodes to XML
-rw-r--r--lib/Tree/Template/Declare.pm24
-rw-r--r--lib/Tree/Template/Declare/LibXML.pm19
-rw-r--r--t/04-xml.t5
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<Tree::Template::Declare> 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<Tree::Template::Declare>
+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<Sub::Exporter>, in particular the section on group builders, for
details. See L<Tree::Template::Declare::HTML_Element> 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<xmlns> is exported, so that you can declare XML namespaces:
You I<can> 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 <dakkar@thenautilus.net>
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{<?xml version="1.0"?>\n<stuff><test:elem1 xmlns:test="http://test/" test:buh="testing" id="1"><test:sub1/></test:elem1><elem2 id="2"/></stuff>\n},
+ qq{<?xml version="1.0"?>\n<stuff><test:elem1 xmlns:test="http://test/" test:buh="testing" id="1"><test:sub1>some content</test:sub1></test:elem1><elem2 id="2"/></stuff>\n},
'XML document without default NS'
);
}
@@ -47,7 +48,7 @@ xmlns ':default' => 'ftp://test/';
my $tree=make_tree();
is($tree->serialize(0),
- qq{<?xml version="1.0"?>\n<stuff xmlns="ftp://test/"><test:elem1 xmlns:test="http://test/" test:buh="testing" id="1"><test:sub1/></test:elem1><elem2 id="2"/></stuff>\n},
+ qq{<?xml version="1.0"?>\n<stuff xmlns="ftp://test/"><test:elem1 xmlns:test="http://test/" test:buh="testing" id="1"><test:sub1>some content</test:sub1></test:elem1><elem2 id="2"/></stuff>\n},
'XML document with default NS'
);
}