blib/lib/XML/XSS/Element.pm

filter: Statementstmt 96.0% 49 / 51 Branchbran 100% 18 / 18 Conditioncond 87.5% 7 / 8 MC/DCmcdc 80.0% 4 / 5 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 1 totaltotal 94.5% 87 / 92 CC 14 SCAR 21.0
CC
14
Cov
96%
apply
13.0
__ANON__
2.0
BEGIN
1.0
CRAP 8.2
« blib/lib/XML/XSS/Comment.pm ↑ Summary blib/lib/XML/XSS/Template.pm »
1 package XML::XSS::Element;
2 BEGIN {
3 15 ▶ $XML::XSS::Element::AUTHORITY = 'cpan:YANICK';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5 {
6 15 $XML::XSS::Element::VERSION = '0.3.4';
7 }
8 # ABSTRACT: XML::XSS element stylesheet rule
9  
10  
11 15 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use MooseX::SemiAffordanceAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use MooseX::Clone;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 ▶use Scalar::Util qw/ refaddr /;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 15 with 'XML::XSS::Role::Renderer', 'MooseX::Clone';
18  
19 15 ▶no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 sub render_attributes {
23 30 ▶ return qw/ process content showtag rename pre intro
Subroutine render_attributes: called CC=1 SCAR=0.0
Pod undocumented
24 prechildren prechild postchild postchildren extro post /;
25 }
26  
27 15 has [ render_attributes() ] => ( traits => [qw/ XML::XSS::Role::StyleAttribute Clone /] );
28  
29 before "set_$_" => sub {
30 72 ▶ my $self = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
31 72 ▶ $self->detach_from_stylesheet if $self->_within_apply;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_within_apply 2 70 72
32 }
33 15 for render_attributes();
34  
35  
36 sub apply {
37 94 ▶ my ( $self, $node, $args ) = @_;
Subroutine apply: called CC=13 SCAR=25.7
38 94 ▶ $args ||= {};
Condition50%1/2
$args ||= {}
l!l
940
MC/DC0%0/1
$args ||= {}
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
39  
40 return
41 94 ▶ if $self->has_process and !$self->_render( 'process', $node, $args );
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_process and not $self->_render("process", $node, $args) 1 93 94
Condition100%3/3
$self->has_process and not $self->_render("process", $node, $args)
!ll&&!rl&&r
9121
MC/DC100%2/2
$self->has_process and not $self->_render("process", $node, $args)
$self->has_process not $self->_render("process", $node, $args)
Truth table100%3/3
A and not(B)
A= $self->has_processB= not $self->_render("process", $node, $args)
ABresult
0X0
100
111
42  
43 93 my $output = $self->_render( 'pre', $node, $args );
44  
45 93 ▶ my $showtag =
Branch: 2 branches
BranchTrueFalseTotal
$self->has_showtag ? : 14 79 93
$self->has_pre || $self->has_content ? : 20 59 79
Condition100%3/3
$self->has_pre or $self->has_content
l!l&&r!l&&!r
91159
MC/DC100%2/2
$self->has_pre or $self->has_content
$self->has_pre $self->has_content
Truth table100%3/3
A or B
A= $self->has_preB= $self->has_content
ABresult
1X1
011
000
46 $self->has_showtag ? $self->showtag
47 : $self->has_pre || $self->has_content ? 0 : 1;
48  
49 93 my $name;
50 93 ▶ if ($showtag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($showtag) 66 27 93
51 66 ▶ $name =
Branch: 1 branch
BranchTrueFalseTotal
$self->has_rename ? : 3 63 66
52 $self->has_rename
53 ? $self->_render( 'rename', $node, $args )
54 : $node->nodeName;
55  
56 $output .= join ' ',
57 "<$name",
58 66 map { join '=', $_->nodeName, "'@{[$_->serializeContent]}'" }
59 $node->attributes;
60 66 $output .= '>';
61 }
62  
63 93 $output .= $self->_render( 'intro', $node, $args );
64  
65 93 ▶ if ( $self->has_content ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_content) { } 18 75 93
66 18 $output .= $self->_render( 'content', $node, $args );
67 }
68 else {
69 75 ▶ if ( my @children = $node->childNodes ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@children) = $node->childNodes) 57 18 75
70 57 $output .= $self->_render( 'prechildren', $node, $args );
71 57 for ( $node->childNodes ) {
72 83 $output .= $self->_render( 'prechild', $node, $args );
73 83 $output .= $self->render( $_, $args );
74 83 $output .= $self->_render( 'postchild', $node, $args );
75 }
76 57 $output .= $self->_render( 'postchildren', $node, $args );
77 }
78 }
79  
80 93 $output .= $self->_render( 'extro', $node, $args );
81  
82 93 ▶ $output .= "</$name>" if $showtag;
Branch: 1 branch
BranchTrueFalseTotal
if $showtag 66 27 93
83  
84 93 $output .= $self->_render( 'post', $node, $args );
85  
86 93 return $output;
87 }
88  
89 15 1;
90  
91 __END__
92  
93 =pod
94  
95 =head1 NAME
96  
97 XML::XSS::Element - XML::XSS element stylesheet rule
98  
99 =head1 VERSION
100  
101 version 0.3.4
102  
103 =head1 SYNOPSIS
104  
105 use XML::XSS;
106  
107 my $xss = XML::XSS->new;
108  
109 my $elt_style = $xss->element( 'foo' );
110  
111 $elt_style->set_pre( ">>>" );
112 $elt_style->set_post( "<<<" );
113  
114 print $xss->render( '<doc><foo>yadah yadah</foo></doc>' );
115  
116 =head1 DESCRIPTION
117  
118 A C<XML::XSS> rule that matches against the element nodes
119 of the xml document to be rendered.
120  
121 =head1 RENDERING ATTRIBUTES
122  
123 For a given element 'foo', the displayed attributes follows the template:
124  
125 pre
126 <foo>
127 intro
128 content
129 prechildren
130 prechild
131 [child node]
132 postchild
133 postchildren
134 extro
135 </foo>
136 post
137  
138 =head2 process
139  
140 If it resolves to false, skip the element (and its children) altogether.
141  
142 # don't show the tag if it doesn't have any child nodes
143 $xss->set( foo => {
144 process => sub {
145 my ( $self, $node, $args ) = @_;
146 return $node->childNodes->size > 0;
147 }
148 } );
149  
150 As it's always the first attribute to be evaluated, it
151 can also be used to set up the other rendering attributes.
152  
153 $xss->set( '*' => {
154 process => sub {
155 my ( $self, $node, $args ) = @_;
156 my $time = time();
157  
158 $self->set( showtag => 1 );
159 $self->set( 'pre' => "\n>>> ". localtime . "\n" );
160 $self->set( 'post' => sub { "\n>>> took " . (time-$time) . "seconds\n"; } );
161  
162 return 1;
163 }
164 } );
165  
166 =head3 get_process()
167  
168 Attribute getter.
169  
170 =head3 set_process( $process )
171  
172 Attribute setter.
173  
174 =head2 pre
175  
176 Printed before the element opening tag position.
177  
178 =head2 showtag
179  
180 If set to a true value, the open and closing tags of the xml element are printed out,
181 if not, they are omited.
182  
183 C<showtag> defaults to true
184 unless either the attribute C<pre> or C<content> is defined.
185 This exception is to accomodate the
186 common use of C<pre> to replace the tags with something else, or
187 when C<content> is used to provide a templated replacement for the
188 element.
189  
190 $css->set( 'subsection' => {
191 pre => '<section level="2">',
192 post => '</section>,
193 } );
194  
195 =head3 showtag
196  
197 Accessor.
198  
199 =head3 set_showtag( $boolean )
200  
201 =head2 rename
202  
203 If defined, and if C<showtag> is true, the element name will be
204 replaced by this value in the opening and closing tags. If the
205 opening tag has any attributes, they will be left untouched.
206  
207 =head2 intro
208  
209 Printed after the element opening tag position.
210  
211 =head2 content
212  
213 If defined, it is used instead of the child nodes of the element, which
214 are not processed (along with the
215 C<prechildren> and C<postchildren> attributes).
216  
217 =head2 prechildren
218  
219 Printed before the node's children, if there are any.
220  
221 =head2 prechild
222  
223 Printed before every child node.
224  
225 =head2 postchild
226  
227 Printed after every child node.
228  
229 =head2 postchildren
230  
231 Printed after the node's children, if there are any.
232  
233 =head2 extro
234  
235 Printed before the element closing tag position.
236  
237 =head2 post
238  
239 Printed after the element closing tag position.
240  
241 =head1 METHODS
242  
243 =head2 stylesheet()
244  
245 Returns the parent C<XML::XSS> stylesheet of this rule.
246  
247 =head2 render( .. )
248  
249 Shortcut for
250  
251 $self->stylesheet->render( ... );
252  
253 =head1 AUTHOR
254  
255 Yanick Champoux <yanick@cpan.org>
256  
257 =head1 COPYRIGHT AND LICENSE
258  
259 This software is copyright (c) 2013 by Yanick Champoux.
260  
261 This is free software; you can redistribute it and/or modify it under
262 the same terms as the Perl 5 programming language system itself.
263  
264 =cut
« blib/lib/XML/XSS/Comment.pm ↑ Summary blib/lib/XML/XSS/Template.pm »