| 1 | package XML::XSS::Template; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 15 | our $AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||||||||||||||||||||||||
| 3 | 15 | $XML::XSS::Template::VERSION = '0.3.5'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: XML::XSS templates | ||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 15 | ▶ | use 5.10.0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||||||
| 9 | 15 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 15 | ▶ | use MooseX::SemiAffordanceAccessor; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 15 | ▶ | use experimental 'smartmatch'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 15 | with qw(MooseX::Clone); | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | use overload | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 19 | ▶ | '&{}' => sub { $_[0]->compiled }, | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.1
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | 'bool' => sub { length $_[0]->code }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.2
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 15 | ▶ | no warnings qw/ uninitialized /; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | 15 | our @sigils = qw/ = ~ @ /; | |||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | 15 | Moose::Exporter->setup_import_methods( as_is => ['xsst'], ); | |||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | 18 | ▶ | sub xsst($) { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
xsst: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 28 | 18 | my $template = shift; | |||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | 18 | my ( undef, $filename, $line ) = caller; | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 18 | return XML::XSS::Template->new( | |||||||||||||||||||||||||||||||||||||||||
| 33 | _filename => $filename, | ||||||||||||||||||||||||||||||||||||||||||
| 34 | _line => $line, | ||||||||||||||||||||||||||||||||||||||||||
| 35 | template => $template, | ||||||||||||||||||||||||||||||||||||||||||
| 36 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | 15 | has template => ( | |||||||||||||||||||||||||||||||||||||||||
| 41 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||||
| 42 | is => 'rw', | ||||||||||||||||||||||||||||||||||||||||||
| 43 | required => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 44 | traits => [qw(Clone)], | ||||||||||||||||||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | 15 | has code => ( isa => 'Str', is => 'rw', | |||||||||||||||||||||||||||||||||||||||||
| 49 | traits => [qw(Clone)], | ||||||||||||||||||||||||||||||||||||||||||
| 50 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | 15 | has compiled => ( is => 'rw', | |||||||||||||||||||||||||||||||||||||||||
| 53 | traits => [qw(Clone)], | ||||||||||||||||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | 15 | has _filename => ( is => 'rw', | |||||||||||||||||||||||||||||||||||||||||
| 57 | traits => [qw(Clone)], | ||||||||||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 59 | 15 | has _line => ( is => 'rw', | |||||||||||||||||||||||||||||||||||||||||
| 60 | traits => [qw(Clone)], | ||||||||||||||||||||||||||||||||||||||||||
| 61 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | 18 | ▶ | sub BUILD { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BUILD: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||
| 64 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | 18 | $self->_parse_template; | |||||||||||||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||||||||||||
| 68 | 18 | my $sub = <<"END_SUB"; | |||||||||||||||||||||||||||||||||||||||||
| 69 | sub { | ||||||||||||||||||||||||||||||||||||||||||
| 70 | my ( \$style, \$node, \$args ) = \@_; | ||||||||||||||||||||||||||||||||||||||||||
| 71 | local *STDOUT; | ||||||||||||||||||||||||||||||||||||||||||
| 72 | my \$output; | ||||||||||||||||||||||||||||||||||||||||||
| 73 | open STDOUT, '>', \\\$output or die; | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 18 | @{[ $self->code ]} | |||||||||||||||||||||||||||||||||||||||||
| 75 | return \$output; | ||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | END_SUB | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | 18 | $self->set_code($sub); | |||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | 18 | $self->set_compiled( eval $sub ); | |||||||||||||||||||||||||||||||||||||||||
| 82 | 18 | ▶ | die $@ if $@; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||||||||||||||||
| 86 | 18 | ▶ | sub _parse_template { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_template: called CC=13 SCAR=25.7
| |||||||||||||||||||||||||||||||||||||||||||
| 87 | 18 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | 18 | my $sigil_re = '[' . join( '', @sigils ) . ']'; | |||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | 18 | my @tokens = split /(<-?%$sigil_re?|%-?>)/, $self->template; | |||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | 18 | my @parsed; | |||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | TOKEN: | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 18 | ▶ | while (@tokens) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 43 | my $token = shift @tokens; | |||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | 43 | ▶ | if ( $token =~ s/<(-?)%// ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 100 | 16 | ▶ | if ( $1 and @parsed and $parsed[-1][0] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$1 and @parsed and $parsed[-1][0]
$1 and @parsed
MC/DC33%1/3
$1 and @parsed and $parsed[-1][0]
$1
@parsed
$parsed[-1][0]
Truth table50%2/4
A and B and C
A= $1B= @parsedC= $parsed[-1][0]
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 4 | $parsed[-1][1] =~ s/\s+\Z//; | |||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||||
| 103 | 16 | $self->_parse_block( $token, \@tokens, \@parsed ); | |||||||||||||||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||||||||||||||||
| 105 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | # it's a verbatim block | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 27 | my ( $f, $l ) = ( $self->_filename, $self->_line ); | |||||||||||||||||||||||||||||||||||||||||
| 109 | 27 | $self->_set_line( $l + $token =~ y/\n// ); | |||||||||||||||||||||||||||||||||||||||||
| 110 | 27 | ▶ | if ( @parsed and $parsed[-1][2] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@parsed and $parsed[-1][2]
MC/DC100%2/2
@parsed and $parsed[-1][2]
@parsed
$parsed[-1][2]
Truth table100%3/3
A and B
A= @parsedB= $parsed[-1][2]
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | 3 | $token =~ s/^\s+//; | |||||||||||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||||||||||
| 113 | 27 | push @parsed, [ 1, $token, undef, $f, $l ]; | |||||||||||||||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | 18 | my $code; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 18 | my ( $pf, $pl ); | |||||||||||||||||||||||||||||||||||||||||
| 119 | 18 | for my $block (@parsed) { | |||||||||||||||||||||||||||||||||||||||||
| 120 | 43 | ▶ | $code .= join( ' ', "\n#line ", $block->[4], $block->[3] ) . "\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$block->[4] == $pl and $block->[3] eq $pf
MC/DC50%1/2
$block->[4] == $pl and $block->[3] eq $pf
$block->[4] == $pl
$block->[3] eq $pf
Truth table66%2/3
A and B
A= $block->[4] == $plB= $block->[3] eq $pf
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | unless $block->[4] == $pl and $block->[3] eq $pf; | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 43 | ( $pf, $pl ) = ( $block->[3], $block->[4] ); | |||||||||||||||||||||||||||||||||||||||||
| 123 | 43 | ▶ | if ( $block->[0] and length $block->[1] ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$block->[0] and length $block->[1]
MC/DC100%2/2
$block->[0] and length $block->[1]
$block->[0]
length $block->[1]
Truth table100%3/3
A and B
A= $block->[0]B= length $block->[1]
| |||||||||||||||||||||||||||||||||||||||||||
| 124 | 20 | $block->[1] =~ s/\|/\\\|/g; | |||||||||||||||||||||||||||||||||||||||||
| 125 | 20 | $block->[1] = 'print(qq|' . quotemeta($block->[1]) . '|);'; | |||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | 43 | $code .= $block->[1]; | |||||||||||||||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||
| 132 | 18 | return $self->set_code($code); | |||||||||||||||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | 16 | ▶ | sub _parse_block { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_block: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 136 | 16 | my $self = shift; | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 16 | my ( $token, $tokens, $parsed ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 16 | my $code; | |||||||||||||||||||||||||||||||||||||||||
| 141 | my $closing_tag; | ||||||||||||||||||||||||||||||||||||||||||
| 142 | 16 | my $level = 1; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 16 | ▶ | while (@$tokens) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 144 | 32 | my $t = shift @$tokens; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 32 | ▶ | $level++ if $t =~ /\A<-?%/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 32 | ▶ | $level-- if $t =~ /\A%-?>/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 147 | 32 | ▶ | if ( $level == 0 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 148 | 16 | $closing_tag = $t; | |||||||||||||||||||||||||||||||||||||||||
| 149 | 16 | last; | |||||||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||||||
| 151 | 16 | $code .= $t; | |||||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||||||||||
| 154 | 16 | my ( $f, $l ) = ( $self->_filename, $self->_line ); | |||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | 16 | $self->_set_line( $l + $code =~ y/\n// ); | |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | 16 | ▶ | die "stylesheet <% %>s are unbalanced: <%$token $code\n" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 159 | unless $closing_tag; | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | 16 | given ($token) { | |||||||||||||||||||||||||||||||||||||||||
| 162 | 16 | when ('=') { | |||||||||||||||||||||||||||||||||||||||||
| 163 | 4 | $code = 'print(' . $code . ');'; | |||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | 12 | when ('~') { | |||||||||||||||||||||||||||||||||||||||||
| 166 | 3 | $code =~ s/\A\s+|\s+Z//g; # trim | |||||||||||||||||||||||||||||||||||||||||
| 167 | 3 | $code =~ s/'/\\'/g; | |||||||||||||||||||||||||||||||||||||||||
| 168 | 3 | $code = | |||||||||||||||||||||||||||||||||||||||||
| 169 | qq{eval { print \$style->render(\$node->findnodes('$code'), \$args) } or warn $@;}; | ||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||||||||||||||||
| 172 | 9 | when ('@') { | |||||||||||||||||||||||||||||||||||||||||
| 173 | 1 | $code =~ s/\A\s+|\s+Z//g; # trim | |||||||||||||||||||||||||||||||||||||||||
| 174 | 1 | $code =~ s/'/\\'/g; | |||||||||||||||||||||||||||||||||||||||||
| 175 | 1 | $code = qq{eval { print \$node->findvalue('$code') } or warn $@;}; | |||||||||||||||||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||||||||||||||||
| 177 | 8 | default { | |||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | # add a semi-colon if there is none | ||||||||||||||||||||||||||||||||||||||||||
| 180 | 8 | ▶ | $code .= ';' unless $code =~ /;\s*\Z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | 16 | push @$parsed, [ 0, $code, !!( $closing_tag =~ /-/ ), $f, $l ]; | |||||||||||||||||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||
| 187 | 15 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | __END__ | ||||||||||||||||||||||||||||||||||||||||||