TAL/Language/TAL.pm untested

filter: Statementstmt 0.0% 0 / 77 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 132 CC 9 SCAR 20.1
CC
9
Cov
0%
process_tag_repeat
20.0
_dom_for_content
12.0
process_tag_define
6.0
CRAP 7.5
1 =head1 NAME
2  
3 Template::TAL::Language::TAL - implement TAL tags
4  
5 =head1 DESCRIPTION
6  
7 The TAL language module for Template::TAL. This module implements the
8 TAL specification version 1.4
9 (http://www.zope.org/Wikis/DevSite/Projects/ZPT/TAL%20Specification%201.4).
10  
11 Tags in the TAL namespace (http://xml.zope.org/namespaces/tal) will be
12 handled by this module, for instance,
13  
14 <html xmlns:tal="http://xml.zope.org/namespaces/tal">
15 <ul>
16 <li tal:repeat="row rows" tal:content="row" />
17 </ul>
18 </html>
19  
20 =cut
21  
22 package Template::TAL::Language::TAL;
23 use warnings;
24 use strict;
25 use Carp qw( croak );
26 use base qw( Template::TAL::Language );
27 use overload;
28  
29 sub namespace { 'http://xml.zope.org/namespaces/tal' }
30  
31 sub tags { qw( define condition repeat replace content attributes omit-tag ) }
32  
33 ############
34 # tag definitions
35  
36 sub process_tag_define {
37 my ($self, $parent, $node, $value, $local_context, $global_context) = @_;
38  
39 # there may be multiple set statements in one attribute.
40 my @setters = Template::TAL::ValueParser->split($value);
41 for (@setters) {
42 # we can set variables into the local context or the golbal context
43 my ($set_context, $var, $set) = /^(local\b|global\b)?\s*(\S+)\s+(.*)$/;
44 $set_context ||= "local"; # defaults
45  
46 # interpret the value part as a TALES string
47 my $result = $parent->parse_tales($set, $local_context, $global_context);
48  
49 if ($set_context eq 'local') {
50 $local_context->{$var} = $result;
51 } else {
52 $global_context->{$var} = $result;
53 }
54 }
55
56 return $node; # don't replace node
57 }
58  
59 sub process_tag_condition {
60 my ($self, $parent, $node, $value, $local_context, $global_context) = @_;
61 # if the TALES string 'value' doesn't return a true value, remove this entire node
62 return $parent->parse_tales($value, $local_context, $global_context) ? $node : ();
63 }
64  
65 sub process_tag_repeat {
66 my ($self, $parent, $node, $value, $local_context, $global_context) = @_;
67 my ($var, $list) = $value =~ /^(\S+)\s+(.*)$/;
68 # $list should tales-parse to a list object, and this node will be repeated with
69 # the variable named by $var set to each element of the list
70  
71 # coerce to a list if it's not already. Template toolkit does this, I like it.
72 my $items = $parent->parse_tales($list, $local_context, $global_context);
73 my @items =
74 ( UNIVERSAL::isa($items, "ARRAY") || overload::Method($items, '@{}') ) ?
75 @$items : ($items);
76  
77 # create a scratch node to hold the things we're going to hand
78 # back. We do this rather than create a list, because the recursive
79 # processor might want to replace these nodes _again_, and it does
80 # that using the DOM tree. For instance
81 # <foo tal:repeat="..." tal:replace="..." />
82 # will clone many foo nodes, then replace them all, so we want to
83 # return the replacements.
84 my $temp = XML::LibXML::Element->new( "temp" );
85  
86 my $index = 0; # for the magic hash
87 for my $localvalue (@items) {
88 # clone the thing we're repeating, and add to the temp element
89 my $new = $node->cloneNode(1);
90 $temp->appendChild($new);
91  
92 # the local value of the list
93 $local_context->{$var} = $localvalue;
94  
95 # magic repeat hash, defined in http://www.zope.org/Wikis/DevSite/Projects/ZPT/RepeatVariable
96 $local_context->{repeat}{$var} = {
97 'index' => $index,
98 'number' => $index + 1,
99 'even' => $index % 2 ? 0 : 1,
100 'odd' => $index % 2 ? 1 : 0,
101 'start' => $index == 0,
102 'end' => $index == scalar(@items) - 1,
103 'length' => scalar(@items),
104 'letter' => "TODO", # hmm.
105 };
106 $index++;
107  
108 # now we've cloned this node, recurse into it, with the new local context
109 $parent->_process_node( $new, $local_context, $global_context );
110 }
111  
112 # return a list of new nodes, which will get put into the original
113 # document.
114 return $temp->childNodes;
115 }
116  
117 # replace the referenced node with the value of the tal:replace attribute
118 sub process_tag_replace {
119 my ($self, $parent, $node, $value, $local_context, $global_context) = @_;
120  
121 my ($type, $set) = $value =~ /^(text\b|structure\b)?\s*(.*)$/;
122 $type ||= 'text';
123 my $result = $parent->parse_tales($set, $local_context, $global_context);
124  
125 # replace the node with the content
126 my @dom = $self->_dom_for_content( $type, $result );
127 return @dom;
128 }
129  
130 # replace the contents of the node with the value of the tal:content attribute
131 sub process_tag_content {
132 my ($self, $parent, $node, $value, $local_context, $global_context) = @_;
133  
134 my ($type, $set) = $value =~ /^(text\b|structure\b)?\s*(.*)$/;
135 $type ||= 'text';
136 my $result = $parent->parse_tales($set, $local_context, $global_context);
137  
138 # place the result into the node
139 my @dom = $self->_dom_for_content( $type, $result );
140 $node->removeChildNodes(); # remove existing children
141 $node->appendChild($_) for @dom; # and add new children
142  
143 return $node; # don't replace node
144 }
145  
146 sub _dom_for_content {
147 my ($class, $type, $content) = @_;
148 if ($type eq 'text') {
149 return XML::LibXML::Text->new( defined($content) ? Encode::encode_utf8($content) : "" );
150 } else {
151 my $parser = XML::LibXML->new();
152 $parser->recover(1); # this seems like it will be useful. TODO - should be an option
153 # to allow for strings such as 'foo <bar>baz</bar>', which aren't themselves
154 # valid XML, but should sensibly be dealt with, wrap in an enclosing tag,
155 # and return all subnodes. TODO - Is this even desired behaviour?
156  
157 # convert to utf-8 bytes, and parse as a utf-8 document, to preserve non-ascii
158 my $bytes = Encode::encode_utf8($content);
159 my $document = $parser->parse_string(
160 "<?xml version='1' encoding='utf-8'?><document>$bytes</document>");
161 return $document->documentElement->childNodes;
162 }
163 }
164  
165 sub process_tag_attributes {
166 my ($self, $parent, $node, $value, $local_context, $global_context) = @_;
167 # there may be >1 attribute set here
168 my @setters = Template::TAL::ValueParser->split($value);
169 for (@setters) {
170 my ($var, $set) = /^\s*(\S+)\s+(.*)$/;
171 my $value = $parent->parse_tales($set, $local_context, $global_context);
172 $node->setAttribute( $var, $value );
173 }
174 return $node; # don't replace node
175 }
176  
177 sub process_tag_omit_tag {
178 my ($self, $parent, $node, $value, $local_context, $global_context) = @_;
179 # if value is false, don't do anything
180 return $node unless $parent->parse_tales($value, $local_context, $global_context);
181 # otherwise process this node as normal, and return the children of it
182 $parent->_process_node( $node, $local_context, $global_context );
183 return $node->childNodes();
184 }
185  
186 =head1 COPYRIGHT
187  
188 Written by Tom Insam, Copyright 2005 Fotango Ltd. All Rights Reserved
189  
190 This program is free software; you can redistribute
191 it and/or modify it under the same terms as Perl itself.
192  
193 =cut
194  
195 1;