XSS.pm

filter: Statementstmt 94.8% 165 / 174 Branchbran 78.5% 22 / 28 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 97.4% 38 / 39 Podpod 33.3% 4 / 12 totaltotal 89.7% 236 / 263 CC 21 SCAR 9.9
CC
21
Cov
92%
detach
8.3
master
5.0
render
5.0
CRAP 2.7
« XSS/Role/StyleAttribute.pm ↑ Summary XSS/Document.pm »
1 package XML::XSS;
2 14 our $AUTHORITY = 'cpan:YANICK';
3 # ABSTRACT: XML stylesheet system
4 14 $XML::XSS::VERSION = '0.3.5';
5  
6 14 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 14 ▶use MooseX::SemiAffordanceAccessor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 14 ▶use MooseX::ClassAttribute;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 14 ▶use Moose::Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 14 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 14 ▶use XML::XSS::Element;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 14 ▶use XML::XSS::Document;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 14 ▶use XML::XSS::Text;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 14 ▶use XML::XSS::Comment;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 14 ▶use XML::XSS::ProcessingInstruction;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 14 ▶use XML::XSS::Template;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 14 ▶use MooseX::Clone;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 14 ▶use experimental 'smartmatch';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 14 with 'MooseX::Clone';
29  
30 14 ▶no warnings qw/ uninitialized /;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 14 Moose::Exporter->setup_import_methods(
33 with_meta => [ 'style' ],
34 as_is => ['xsst'],
35 );
36  
37 7 ▶sub style {
Subroutine style: called CC=1 SCAR=0.0
Pod undocumented
38 7 my $metaclass = shift;
39 7 my $master = ($metaclass->linearized_isa)[0]->master;
40  
41 7 my $element = shift;
42  
43 7 my %attr = @_;
44  
45 7 $master->set( $element, \%attr );
46  
47 }
48  
49 #class_has 'master' => (
50 # is => 'ro',
51 # lazy => 1,
52 # lazy_build => 1,
53 #);
54  
55 0 ▶sub _build_master {
Subroutine _build_master: not called CC=1 SCAR=6.9
56 0 my $self = shift;
57  
58 0 return XML::XSS->new;
59  
60 }
61  
62 20 ▶sub master {
Subroutine master: called CC=5 SCAR=16.1
Pod undocumented
63 20 my $class = shift;
64 20 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 1 19 20
65  
66 20 my $var = '$'.$class.'::master';
67  
68 20 my $master = eval $var;
69  
70 20 ▶ return $master if $master;
Branch: 1 branch
BranchTrueFalseTotal
if $master 10 10 20
71  
72 10 $master = $class->new;
73  
74 10 for my $super ( reverse grep { $_->isa('XML::XSS') } $class->meta->superclasses ) {
75 9 ▶ $master->include( $super->master ) if $super->has_master;
Branch: 1 branch
BranchTrueFalseTotal
if $super->has_master 2 7 9
76 }
77  
78 10 eval "$var = \$master";
79  
80 10 return $master;
81 }
82  
83 59 ▶sub has_master {
Subroutine has_master: called CC=2 SCAR=7.1
Pod undocumented
84 59 my $class = shift;
85 59 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 59 59
86  
87 59 return eval '$'.$class.'::master';
88 }
89  
90 2 ▶sub include {
Subroutine include: called CC=2 SCAR=6.9
Pod undocumented
91 2 my $self = shift;
92 2 my $to_include = shift;
93  
94 2 for my $elt ( $to_include->element_keys ) {
95 2 $self->_set_element( $elt, $to_include->_element( $elt )->clone );
96 }
97  
98 2 $self->set_comment( $to_include->comment->style_attribute_hash );
99 2 $self->set_pi( $to_include->pi->style_attribute_hash );
100 2 $self->set_text( $to_include->text->style_attribute_hash );
101  
102  
103 }
104  
105 50 ▶around new => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
106 50 my $orig = shift;
107 50 my $self = shift;
108  
109 50 ▶ if ( $self->has_master ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->has_master) 6 44 50
110 6 my $self = $self->master->clone;
111 6 $self->BUILDALL( $self->BUILDARGS(@_) );
112 6 return $self;
113 }
114  
115 44 return $self->$orig(@_);
116  
117 14 };
118  
119  
120 has document => (
121 is => 'ro',
122 44 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
123 44 XML::XSS::Document->new( stylesheet => $_[0] );
124 },
125 14 traits => [ 'Clone' ],
126 );
127  
128  
129  
130 has 'text' => (
131 is => 'ro',
132 44 ▶ default => sub { XML::XSS::Text->new( stylesheet => $_[0] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
133 14 handles => {
134 set_text => 'set',
135 clear_text => 'clear',
136 },
137 traits => [ 'Clone' ],
138 );
139  
140  
141 has comment => (
142 is => 'ro',
143 default =>
144 44 ▶ sub { XML::XSS::Comment->new( stylesheet => $_[0] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
145 14 handles => {
146 set_comment => 'set',
147 },
148 traits => [ 'Clone' ],
149 );
150  
151  
152 has '_elements' => (
153 isa => 'HashRef[XML::XSS::Element]',
154 44 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
155 14 handles => {
156 '_set_element' => 'set',
157 '_element' => 'get',
158 'element_keys' => 'keys',
159 },
160 traits => [ 'Clone', 'Hash' ],
161 );
162  
163  
164 61 ▶sub element {
Subroutine element: called CC=2 SCAR=6.9
Pod documented
165 61 my ( $self, $name ) = @_;
166 61 my $elt = $self->_element($name);
167 61 ▶ unless ($elt) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($elt) 43 18 61
168 43 $elt = XML::XSS::Element->new( stylesheet => $self );
169 43 $self->_set_element( $name => $elt );
170 }
171 61 return $elt;
172 }
173  
174 2 ▶sub set_element {
Subroutine set_element: called CC=2 SCAR=7.4
Pod undocumented
175 2 my $self = shift;
176 2 my ( $name, $args ) = @_;
177  
178 2 ▶ if ( ref $args eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args eq 'HASH') { } 0 2 2
179 0 $self->element($name)->set(%$args);
180 }
181 else {
182 2 $self->_set_element( $name => $args );
183 }
184 }
185  
186  
187 has 'catchall_element' => (
188 is => 'rw',
189 isa => 'XML::XSS::Element',
190 29 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
191 29 XML::XSS::Element->new( stylesheet => $_[0] );
192 },
193 14 lazy => 1,
194 traits => [ 'Clone' ],
195 );
196  
197 has pi => (
198 is => 'ro',
199 44 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
200 44 XML::XSS::ProcessingInstruction->new( stylesheet => $_[0] );
201 },
202 14 traits => [ 'Clone' ],
203 handles => {
204 set_pi => 'set',
205 },
206 );
207  
208  
209 has stash => (
210 is => 'ro',
211 writer => '_set_stash',
212 isa => 'HashRef',
213 44 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
214 14 );
215  
216 48 ▶sub clear_stash { $_[0]->_set_stash( {} ) }
Subroutine clear_stash: called CC=1 SCAR=0.0
Pod documented
217  
218  
219 use overload
220 14 ▶ '.' => sub { $_[0]->get($_[1]) },
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
221 14 ▶ '""' => sub { return ref shift };
Subroutine __ANON__: called CC=1 SCAR=0.0
222  
223  
224 40 ▶sub set {
Subroutine set: called CC=2 SCAR=6.9
Pod documented
225 40 my $self = shift;
226  
227 40 ▶ while ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 41 40 81
228 41 my $name = shift;
229 41 my $attrs = shift;
230  
231 41 $self->get($name)->set(%$attrs);
232 }
233 }
234  
235 64 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
236 64 my ( $self, $name ) = @_;
237  
238 64 given ( $name ) {
239 64 when ( '#document' ) {
240 5 return $self->document;
241 }
242 59 when( '#text' ) {
243 6 return $self->text;
244 }
245 53 when( '#comment' ) {
246 8 return $self->comment;
247 }
248 45 when( '#pi' ) {
249 4 return $self->pi;
250 }
251 41 when( '*' ) {
252 2 return $self->catchall_element;
253 }
254 39 default {
255 39 return $self->element($name);
256 }
257 }
258  
259  
260 }
261  
262  
263 181 ▶sub render {
Subroutine render: called CC=5 SCAR=16.1
Pod documented
264 181 my $self = shift;
265  
266 181 ▶ my $args = ref( $_[-1] ) eq 'HASH' ? pop @_ : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 133 48 181
267  
268 181 ▶ if ( @_ == 1 and not ref $_[0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and not ref $_[0]) 48 133 181
Condition100%3/3
@_ == 1 and not ref $_[0]
!ll&&!rl&&r
412948
MC/DC100%2/2
@_ == 1 and not ref $_[0]
@_ == 1 not ref $_[0]
Truth table100%3/3
A and not(B)
A= @_ == 1B= not ref $_[0]
ABresult
0X0
100
111
269 48 @_ = ( XML::LibXML->load_xml( string => $_[0] ) );
270 }
271  
272 181 my $output;
273  
274 181 for my $node (@_) {
275  
276 183 my $renderer = $self->resolve($node);
277  
278 183 $output .= $renderer->apply( $node, $args );
279 }
280  
281 181 return $output;
282 }
283  
284 2 ▶sub detach {
Subroutine detach: called CC=6 SCAR=21.2
Pod undocumented
285 2 my ( $self, $node ) = @_;
286  
287 # iterate through the nodes and replace the node by a copy
288  
289 2 my $copy = $node->clone;
290 2 $node->set_is_detached(1);
291  
292 2 ▶ if ( ref $node eq 'XML::XSS::Text' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $node eq 'XML::XSS::Text') { } 0 2 2
elsif (ref $node eq 'XML::XSS::Element') { } 2 0 2
293 0 $self->set_text($copy);
294 0 return;
295 }
296 elsif ( ref $node eq 'XML::XSS::Element' ) {
297 2 for ( $self->element_keys ) {
298 2 ▶ if ( $self->element($_) eq $node ) { # FIXME
Branch: 1 branch
BranchTrueFalseTotal
if ($self->element($_) eq $node) 2 0 2
299 # FIXME set_element in Stylesheet
300 2 $self->set_element( $_ => $copy );
301 }
302 }
303 2 ▶ if ( $self->catchall_element eq $node ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->catchall_element eq $node) 0 2 2
304 0 $self->set_catchall_element( $copy );
305 }
306 }
307 else {
308 0 die;
309 }
310  
311 }
312  
313  
314 183 ▶sub resolve {
Subroutine resolve: called CC=2 SCAR=7.0
Pod undocumented
315 183 my ( $self, $node ) = @_;
316  
317 183 my $type = ref $node;
318  
319 183 given ($type) {
320 183 when ('XML::LibXML::Document') {
321 48 return $self->document;
322 }
323 135 when ('XML::LibXML::Element') {
324 94 my $name = $node->nodeName;
325 94 ▶ return $self->_element($name) || $self->catchall_element;
Condition66%2/3
$self->_element($name) || $self->catchall_element
l!l&&r!l&&!r
39550
MC/DC0%0/2
$self->_element($name) || $self->catchall_element
$self->_element($name) $self->catchall_element
Truth table66%2/3
A || B
A= $self->_element($name)B= $self->catchall_element
ABresult
1X1
011
000
326 }
327 41 when ('XML::LibXML::Text') {
328 33 return $self->text;
329 }
330 8 when ('XML::LibXML::CDATASection') {
331 1 return $self->text;
332 }
333 7 when ( 'XML::LibXML::Comment' ) {
334 4 return $self->comment;
335 }
336 3 when ( 'XML::LibXML::PI' ) {
337 3 return $self->pi;
338 }
339 0 default {
340 0 die "unknown node type: $type";
341 }
342 }
343  
344 }
345  
346  
347 14 1;
348  
349 __END__
« XSS/Role/StyleAttribute.pm ↑ Summary XSS/Document.pm »