| 1 | package XML::XSS; | ||||||||||||||||||||||||||||
| 2 | BEGIN { | ||||||||||||||||||||||||||||
| 3 | 14 | ▶ | $XML::XSS::AUTHORITY = 'cpan:YANICK'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | } | ||||||||||||||||||||||||||||
| 5 | { | ||||||||||||||||||||||||||||
| 6 | 14 | $XML::XSS::VERSION = '0.3.4'; | |||||||||||||||||||||||||||
| 7 | } | ||||||||||||||||||||||||||||
| 8 | # ABSTRACT: XML stylesheet system | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 14 | ▶ | use 5.10.0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 14 | ▶ | use MooseX::SemiAffordanceAccessor; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 14 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 14 | ▶ | use MooseX::ClassAttribute; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 14 | ▶ | use Moose::Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 14 | ▶ | use XML::LibXML; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 14 | ▶ | use XML::XSS::Element; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 14 | ▶ | use XML::XSS::Document; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 14 | ▶ | use XML::XSS::Text; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 14 | ▶ | use XML::XSS::Comment; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 14 | ▶ | use XML::XSS::ProcessingInstruction; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 14 | ▶ | use XML::XSS::Template; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 14 | ▶ | use MooseX::Clone; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 14 | with 'MooseX::Clone'; | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 14 | ▶ | no warnings qw/ uninitialized /; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 14 | Moose::Exporter->setup_import_methods( | |||||||||||||||||||||||||||
| 35 | with_meta => [ 'style' ], | ||||||||||||||||||||||||||||
| 36 | as_is => ['xsst'], | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub style { | ||||||||||||||||||||||||||||
| 40 | 7 | ▶ | my $metaclass = shift; | ||||||||||||||||||||||||||
|
Subroutine
style: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 41 | 7 | my $master = ($metaclass->linearized_isa)[0]->master; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 7 | my $element = shift; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 7 | my %attr = @_; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 7 | $master->set( $element, \%attr ); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | #class_has 'master' => ( | ||||||||||||||||||||||||||||
| 52 | # is => 'ro', | ||||||||||||||||||||||||||||
| 53 | # lazy => 1, | ||||||||||||||||||||||||||||
| 54 | # lazy_build => 1, | ||||||||||||||||||||||||||||
| 55 | #); | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub _build_master { | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_build_master: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 0 | return XML::XSS->new; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | sub master { | ||||||||||||||||||||||||||||
| 65 | 20 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
master: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 66 | 20 | ▶ | $class = ref $class if ref $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 20 | my $var = '$'.$class.'::master'; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 20 | my $master = eval $var; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 20 | ▶ | return $master if $master; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 10 | $master = $class->new; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 10 | for my $super ( reverse grep { $_->isa('XML::XSS') } $class->meta->superclasses ) { | |||||||||||||||||||||||||||
| 77 | 9 | ▶ | $master->include( $super->master ) if $super->has_master; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 10 | eval "$var = \$master"; | |||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 10 | return $master; | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | sub has_master { | ||||||||||||||||||||||||||||
| 86 | 59 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
has_master: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 87 | 59 | ▶ | $class = ref $class if ref $class; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 59 | return eval '$'.$class.'::master'; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | sub include { | ||||||||||||||||||||||||||||
| 93 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
include: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 94 | 2 | my $to_include = shift; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 2 | for my $elt ( $to_include->element_keys ) { | |||||||||||||||||||||||||||
| 97 | 2 | $self->_set_element( $elt, $to_include->_element( $elt )->clone ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 2 | $self->set_comment( $to_include->comment->style_attribute_hash ); | |||||||||||||||||||||||||||
| 101 | 2 | $self->set_pi( $to_include->pi->style_attribute_hash ); | |||||||||||||||||||||||||||
| 102 | 2 | $self->set_text( $to_include->text->style_attribute_hash ); | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | around new => sub { | ||||||||||||||||||||||||||||
| 108 | 50 | ▶ | my $orig = shift; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 109 | 50 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 50 | ▶ | if ( $self->has_master ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | 6 | my $self = $self->master->clone; | |||||||||||||||||||||||||||
| 113 | 6 | $self->BUILDALL( $self->BUILDARGS(@_) ); | |||||||||||||||||||||||||||
| 114 | 6 | return $self; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 44 | return $self->$orig(@_); | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 14 | }; | |||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | has document => ( | ||||||||||||||||||||||||||||
| 123 | is => 'ro', | ||||||||||||||||||||||||||||
| 124 | default => sub { | ||||||||||||||||||||||||||||
| 125 | 44 | ▶ | XML::XSS::Document->new( stylesheet => $_[0] ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 126 | }, | ||||||||||||||||||||||||||||
| 127 | 14 | traits => [ 'Clone' ], | |||||||||||||||||||||||||||
| 128 | ); | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | has 'text' => ( | ||||||||||||||||||||||||||||
| 133 | is => 'ro', | ||||||||||||||||||||||||||||
| 134 | 44 | ▶ | default => sub { XML::XSS::Text->new( stylesheet => $_[0] ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | 14 | handles => { | |||||||||||||||||||||||||||
| 136 | set_text => 'set', | ||||||||||||||||||||||||||||
| 137 | clear_text => 'clear', | ||||||||||||||||||||||||||||
| 138 | }, | ||||||||||||||||||||||||||||
| 139 | traits => [ 'Clone' ], | ||||||||||||||||||||||||||||
| 140 | ); | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | has comment => ( | ||||||||||||||||||||||||||||
| 144 | is => 'ro', | ||||||||||||||||||||||||||||
| 145 | default => | ||||||||||||||||||||||||||||
| 146 | 44 | ▶ | sub { XML::XSS::Comment->new( stylesheet => $_[0] ) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 147 | 14 | handles => { | |||||||||||||||||||||||||||
| 148 | set_comment => 'set', | ||||||||||||||||||||||||||||
| 149 | }, | ||||||||||||||||||||||||||||
| 150 | traits => [ 'Clone' ], | ||||||||||||||||||||||||||||
| 151 | ); | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | has '_elements' => ( | ||||||||||||||||||||||||||||
| 155 | isa => 'HashRef[XML::XSS::Element]', | ||||||||||||||||||||||||||||
| 156 | 44 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 157 | 14 | handles => { | |||||||||||||||||||||||||||
| 158 | '_set_element' => 'set', | ||||||||||||||||||||||||||||
| 159 | '_element' => 'get', | ||||||||||||||||||||||||||||
| 160 | 'element_keys' => 'keys', | ||||||||||||||||||||||||||||
| 161 | }, | ||||||||||||||||||||||||||||
| 162 | traits => [ 'Clone', 'Hash' ], | ||||||||||||||||||||||||||||
| 163 | ); | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | sub element { | ||||||||||||||||||||||||||||
| 167 | 61 | ▶ | my ( $self, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
element: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 168 | 61 | my $elt = $self->_element($name); | |||||||||||||||||||||||||||
| 169 | 61 | ▶ | unless ($elt) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 43 | $elt = XML::XSS::Element->new( stylesheet => $self ); | |||||||||||||||||||||||||||
| 171 | 43 | $self->_set_element( $name => $elt ); | |||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||
| 173 | 61 | return $elt; | |||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | sub set_element { | ||||||||||||||||||||||||||||
| 177 | 2 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
set_element: called CC=2 SCAR=7.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 178 | 2 | my ( $name, $args ) = @_; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 2 | ▶ | if ( ref $args eq 'HASH' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | 0 | $self->element($name)->set(%$args); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | else { | ||||||||||||||||||||||||||||
| 184 | 2 | $self->_set_element( $name => $args ); | |||||||||||||||||||||||||||
| 185 | } | ||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | has 'catchall_element' => ( | ||||||||||||||||||||||||||||
| 190 | is => 'rw', | ||||||||||||||||||||||||||||
| 191 | isa => 'XML::XSS::Element', | ||||||||||||||||||||||||||||
| 192 | default => sub { | ||||||||||||||||||||||||||||
| 193 | 29 | ▶ | XML::XSS::Element->new( stylesheet => $_[0] ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 194 | }, | ||||||||||||||||||||||||||||
| 195 | 14 | lazy => 1, | |||||||||||||||||||||||||||
| 196 | traits => [ 'Clone' ], | ||||||||||||||||||||||||||||
| 197 | ); | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | has pi => ( | ||||||||||||||||||||||||||||
| 200 | is => 'ro', | ||||||||||||||||||||||||||||
| 201 | default => sub { | ||||||||||||||||||||||||||||
| 202 | 44 | ▶ | XML::XSS::ProcessingInstruction->new( stylesheet => $_[0] ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 203 | }, | ||||||||||||||||||||||||||||
| 204 | 14 | traits => [ 'Clone' ], | |||||||||||||||||||||||||||
| 205 | handles => { | ||||||||||||||||||||||||||||
| 206 | set_pi => 'set', | ||||||||||||||||||||||||||||
| 207 | }, | ||||||||||||||||||||||||||||
| 208 | ); | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | has stash => ( | ||||||||||||||||||||||||||||
| 212 | is => 'ro', | ||||||||||||||||||||||||||||
| 213 | writer => '_set_stash', | ||||||||||||||||||||||||||||
| 214 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 215 | 44 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 216 | 14 | ); | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | 48 | ▶ | sub clear_stash { $_[0]->_set_stash( {} ) } | ||||||||||||||||||||||||||
|
Subroutine
clear_stash: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | use overload | ||||||||||||||||||||||||||||
| 222 | 14 | ▶ | '.' => sub { $_[0]->get($_[1]) }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 223 | 14 | ▶ | '""' => sub { return ref shift }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | sub set { | ||||||||||||||||||||||||||||
| 227 | 40 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | 40 | ▶ | while ( @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | 41 | my $name = shift; | |||||||||||||||||||||||||||
| 231 | 41 | my $attrs = shift; | |||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 41 | $self->get($name)->set(%$attrs); | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||
| 237 | sub get { | ||||||||||||||||||||||||||||
| 238 | 64 | ▶ | my ( $self, $name ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | 64 | given ( $name ) { | |||||||||||||||||||||||||||
| 241 | 64 | when ( '#document' ) { | |||||||||||||||||||||||||||
| 242 | 5 | return $self->document; | |||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||
| 244 | 59 | when( '#text' ) { | |||||||||||||||||||||||||||
| 245 | 6 | return $self->text; | |||||||||||||||||||||||||||
| 246 | } | ||||||||||||||||||||||||||||
| 247 | 53 | when( '#comment' ) { | |||||||||||||||||||||||||||
| 248 | 8 | return $self->comment; | |||||||||||||||||||||||||||
| 249 | } | ||||||||||||||||||||||||||||
| 250 | 45 | when( '#pi' ) { | |||||||||||||||||||||||||||
| 251 | 4 | return $self->pi; | |||||||||||||||||||||||||||
| 252 | } | ||||||||||||||||||||||||||||
| 253 | 41 | when( '*' ) { | |||||||||||||||||||||||||||
| 254 | 2 | return $self->catchall_element; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | 39 | default { | |||||||||||||||||||||||||||
| 257 | 39 | return $self->element($name); | |||||||||||||||||||||||||||
| 258 | } | ||||||||||||||||||||||||||||
| 259 | } | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | } | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | sub render { | ||||||||||||||||||||||||||||
| 266 | 181 | ▶ | my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
render: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 181 | ▶ | my $args = ref( $_[-1] ) eq 'HASH' ? pop @_ : {}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 181 | ▶ | if ( @_ == 1 and not ref $_[0] ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@_ == 1 and not ref $_[0]
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]
| |||||||||||||||||||||||||||||
| 271 | 48 | @_ = ( XML::LibXML->load_xml( string => $_[0] ) ); | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 181 | my $output; | |||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | 181 | for my $node (@_) { | |||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | 183 | my $renderer = $self->resolve($node); | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 183 | $output .= $renderer->apply( $node, $args ); | |||||||||||||||||||||||||||
| 281 | } | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 181 | return $output; | |||||||||||||||||||||||||||
| 284 | } | ||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | sub detach { | ||||||||||||||||||||||||||||
| 287 | 2 | ▶ | my ( $self, $node ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
detach: called CC=6 SCAR=21.2
Pod
undocumented
| |||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | # iterate through the nodes and replace the node by a copy | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | 2 | my $copy = $node->clone; | |||||||||||||||||||||||||||
| 292 | 2 | $node->set_is_detached(1); | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 2 | ▶ | if ( ref $node eq 'XML::XSS::Text' ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 295 | 0 | $self->set_text($copy); | |||||||||||||||||||||||||||
| 296 | 0 | return; | |||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||
| 298 | elsif ( ref $node eq 'XML::XSS::Element' ) { | ||||||||||||||||||||||||||||
| 299 | 2 | for ( $self->element_keys ) { | |||||||||||||||||||||||||||
| 300 | 2 | ▶ | if ( $self->element($_) eq $node ) { # FIXME | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 301 | # FIXME set_element in Stylesheet | ||||||||||||||||||||||||||||
| 302 | 2 | $self->set_element( $_ => $copy ); | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | 2 | ▶ | if ( $self->catchall_element eq $node ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 306 | 0 | $self->set_catchall_element( $copy ); | |||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | else { | ||||||||||||||||||||||||||||
| 310 | 0 | die; | |||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | sub resolve { | ||||||||||||||||||||||||||||
| 317 | 183 | ▶ | my ( $self, $node ) = @_; | ||||||||||||||||||||||||||
|
Subroutine
resolve: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | 183 | my $type = ref $node; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 183 | given ($type) { | |||||||||||||||||||||||||||
| 322 | 183 | when ('XML::LibXML::Document') { | |||||||||||||||||||||||||||
| 323 | 48 | return $self->document; | |||||||||||||||||||||||||||
| 324 | } | ||||||||||||||||||||||||||||
| 325 | 135 | when ('XML::LibXML::Element') { | |||||||||||||||||||||||||||
| 326 | 94 | my $name = $node->nodeName; | |||||||||||||||||||||||||||
| 327 | 94 | ▶ | return $self->_element($name) || $self->catchall_element; | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->_element($name) || $self->catchall_element
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
| |||||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | 41 | when ('XML::LibXML::Text') { | |||||||||||||||||||||||||||
| 330 | 33 | return $self->text; | |||||||||||||||||||||||||||
| 331 | } | ||||||||||||||||||||||||||||
| 332 | 8 | when ('XML::LibXML::CDATASection') { | |||||||||||||||||||||||||||
| 333 | 1 | return $self->text; | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | 7 | when ( 'XML::LibXML::Comment' ) { | |||||||||||||||||||||||||||
| 336 | 4 | return $self->comment; | |||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | 3 | when ( 'XML::LibXML::PI' ) { | |||||||||||||||||||||||||||
| 339 | 3 | return $self->pi; | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | 0 | default { | |||||||||||||||||||||||||||
| 342 | 0 | die "unknown node type: $type"; | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | 14 | 1; | |||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | __END__ | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | =pod | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | =head1 NAME | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | XML::XSS - XML stylesheet system | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | =head1 VERSION | ||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||
| 361 | version 0.3.4 | ||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | use XML::XSS; | ||||||||||||||||||||||||||||
| 366 | |||||||||||||||||||||||||||||
| 367 | my $xss = XML::XSS->new; | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | $xss->set( pod => { | ||||||||||||||||||||||||||||
| 370 | pre => "=pod\n", | ||||||||||||||||||||||||||||
| 371 | post => "=cut\n", | ||||||||||||||||||||||||||||
| 372 | } ); | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | $xss->set( section => { | ||||||||||||||||||||||||||||
| 375 | pre => \&pre_section | ||||||||||||||||||||||||||||
| 376 | } ); | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | sub pre_section { | ||||||||||||||||||||||||||||
| 379 | my ( $self, $node, $args ) = @_; | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | return "=head1 " . $node->findvalue( '@title' ) . "\n\n"; | ||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | print $xss->render( <<'END_XML' ); | ||||||||||||||||||||||||||||
| 385 | <pod> | ||||||||||||||||||||||||||||
| 386 | <section title="NAME">XML::XSS - a XML stylesheet system</section> | ||||||||||||||||||||||||||||
| 387 | ... | ||||||||||||||||||||||||||||
| 388 | </pod> | ||||||||||||||||||||||||||||
| 389 | END_XML | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | Caution: this is alpha-quality software. Here be enough dragons to send | ||||||||||||||||||||||||||||
| 394 | Beowulf packing. Caveat maximus emptor. | ||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | C<XML::XSS> is a XML stylesheet system loosely similar to | ||||||||||||||||||||||||||||
| 397 | CSS and XSLT. A C<XML::XSS> object is made up of | ||||||||||||||||||||||||||||
| 398 | rendering rules that dictate how the different nodes of | ||||||||||||||||||||||||||||
| 399 | an XML document are to be rendered, and can be applied | ||||||||||||||||||||||||||||
| 400 | against one or many XML documents. | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | C<XML::XSS> is a rewrite of L<XML::XPathScript>, which was | ||||||||||||||||||||||||||||
| 403 | initially part of the L<AxKit> framework. | ||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | =head2 The XML Document | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | C<XML::XSS> uses L<XML::LibXML> under the hood as its XML DOM | ||||||||||||||||||||||||||||
| 408 | API. Documents can be passed as strings, in which case the creation | ||||||||||||||||||||||||||||
| 409 | of the XML::LibXML object will be done behind the curtain | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | $xss->render( '<foo>yadah</foo>' ); | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | or the L<XML::LibXML> object can be passed directly | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | my $doc = XML::LibXML->load_xml( location => 'foo.xml' ); | ||||||||||||||||||||||||||||
| 416 | $xss->render( $doc ); | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =head2 Stylesheet Rules | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | C<XML::XSS> has 5 different kinds of rules that reflect the | ||||||||||||||||||||||||||||
| 421 | different kinds of nodes that a XML document can have (as per | ||||||||||||||||||||||||||||
| 422 | L<XML::LibXML>): L<XML::XSS::Document>, L<XML::XSS::Text>, | ||||||||||||||||||||||||||||
| 423 | L<XML::XSS::Comment>, L<XML::XSS::ProcessingInstruction> and | ||||||||||||||||||||||||||||
| 424 | L<XML::XSS::Element>. Whereas there are can many C<XML::LibXML::Element> | ||||||||||||||||||||||||||||
| 425 | rules, there is only one instance of each of the first 4 rules per | ||||||||||||||||||||||||||||
| 426 | stylesheet. In addition of the regular C<XML::LibXML::Element> rules, | ||||||||||||||||||||||||||||
| 427 | a special I<catch-all> C<XML::LibXML::Element> also exists that will | ||||||||||||||||||||||||||||
| 428 | be applied to any document element not explicitly matched by one of the | ||||||||||||||||||||||||||||
| 429 | element rules. | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | =head2 Rules Style Attributes | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | Each rule has a set of style attributes that control how the matching | ||||||||||||||||||||||||||||
| 434 | document node is transformed. The different types of rule | ||||||||||||||||||||||||||||
| 435 | (L<XML::XSS::Document>, L<XML::XSS::Element>, | ||||||||||||||||||||||||||||
| 436 | L<XML::XSS::Text>, L<XML::XSS::Comment> and L<XML::XSS::ProcessingInstruction>) | ||||||||||||||||||||||||||||
| 437 | have each a different set of style attributes, which are | ||||||||||||||||||||||||||||
| 438 | described in their relative manpages. | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | Unless specified otherwise, a style attribute can be assigned a | ||||||||||||||||||||||||||||
| 441 | scalar value or a reference to a sub. In the second case, the sub will | ||||||||||||||||||||||||||||
| 442 | be evaluated in the context of the processed node and its return value will | ||||||||||||||||||||||||||||
| 443 | be used as the style attribute value. | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | Upon execution, the sub references will be passed three parameters: | ||||||||||||||||||||||||||||
| 446 | the invoking rule, the C<XML::LibXML> node it is rendering and the arguments | ||||||||||||||||||||||||||||
| 447 | ref given to C<render()>. | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | $css->set( 'foo' => { | ||||||||||||||||||||||||||||
| 450 | pre => '[[[', | ||||||||||||||||||||||||||||
| 451 | post => sub { | ||||||||||||||||||||||||||||
| 452 | my ( $self, $node, $args ) = @_; | ||||||||||||||||||||||||||||
| 453 | return $node->findvalue( '@bar' ); | ||||||||||||||||||||||||||||
| 454 | } | ||||||||||||||||||||||||||||
| 455 | } ); | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =head2 Modifying Rules While Rendering | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | Rules attributes changed while rendering only apply to | ||||||||||||||||||||||||||||
| 460 | the current element. | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | $xss->set( 'section' => { | ||||||||||||||||||||||||||||
| 463 | process => sub { | ||||||||||||||||||||||||||||
| 464 | my ( $self, $node ) = @_; | ||||||||||||||||||||||||||||
| 465 | $self->stash->{section_nbr}++; | ||||||||||||||||||||||||||||
| 466 | if ( $self->stash->{section_nbr} == 5 ) { | ||||||||||||||||||||||||||||
| 467 | # only applies to the one section | ||||||||||||||||||||||||||||
| 468 | $self->set_pre( '>>> this is the fifth section <<<' ); | ||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||
| 470 | return 1; | ||||||||||||||||||||||||||||
| 471 | } | ||||||||||||||||||||||||||||
| 472 | } ); | ||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | If you want to change the global rule, you have to access the rule | ||||||||||||||||||||||||||||
| 475 | from the stylesheet, like so | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | $xss->set( 'section' => { | ||||||||||||||||||||||||||||
| 478 | process => sub { | ||||||||||||||||||||||||||||
| 479 | my ( $self, $node ) = @_; | ||||||||||||||||||||||||||||
| 480 | $self->stash->{section_nbr}++; | ||||||||||||||||||||||||||||
| 481 | if ( $self->stash->{section_nbr} == 6 ) { | ||||||||||||||||||||||||||||
| 482 | $self->stylesheet->element('section')->set_pre( | ||||||||||||||||||||||||||||
| 483 | '>>> this is after the fifth section <<<' | ||||||||||||||||||||||||||||
| 484 | ); | ||||||||||||||||||||||||||||
| 485 | } | ||||||||||||||||||||||||||||
| 486 | return 1; | ||||||||||||||||||||||||||||
| 487 | } | ||||||||||||||||||||||||||||
| 488 | } ); | ||||||||||||||||||||||||||||
| 489 | |||||||||||||||||||||||||||||
| 490 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | =head2 document | ||||||||||||||||||||||||||||
| 493 | |||||||||||||||||||||||||||||
| 494 | The document rule. Note that this matches against the | ||||||||||||||||||||||||||||
| 495 | C<XML::LibXML::Document> node, not the root element node of | ||||||||||||||||||||||||||||
| 496 | the document. | ||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | =head3 document() | ||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||
| 500 | Attribute getter. | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | =head2 text | ||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | The text rule. | ||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | =head3 text() | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | Attribute getter. | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | =head3 set_text( ... ) | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | Shortcut for | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | $xss->text->set( ... ); | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | =head3 clear_text() | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | Shortcut for | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | $xss->text->clear; | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | =head2 comment | ||||||||||||||||||||||||||||
| 523 | |||||||||||||||||||||||||||||
| 524 | The comment rule. | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | =head3 comment() | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | Attribute getter. | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | =head3 set_comment( ... ) | ||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | Shortcut for | ||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | $xss->comment->set( ... ) | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | =head2 elements | ||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | The collection of user-defined element rules. | ||||||||||||||||||||||||||||
| 539 | |||||||||||||||||||||||||||||
| 540 | =head3 element( $name ) | ||||||||||||||||||||||||||||
| 541 | |||||||||||||||||||||||||||||
| 542 | Returns the L<XML::XSS::Element> node associated to the tag C<$name>. | ||||||||||||||||||||||||||||
| 543 | If the element didn't already exist, it is automatically created. | ||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||
| 545 | my $elt = $xss->element( 'foo' ); # element for <foo> | ||||||||||||||||||||||||||||
| 546 | $elt->set( pre => '[foo]' ); | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | =head2 catchall_element | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | The catch-all element rule, which is applied to | ||||||||||||||||||||||||||||
| 551 | all the element nodes that aren't explicitly matched. | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | # change all tags to <unknown> except for <foo> | ||||||||||||||||||||||||||||
| 554 | $xss->set( 'foo' => { showtag => 1 } ); | ||||||||||||||||||||||||||||
| 555 | $xss->set( '*' => { rename => 'unknown' } ); | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | =head3 catchall_element() | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | The attribute getter. | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | =head2 stash | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | The stylesheet has a stash (an hashref) that is accessible to all the | ||||||||||||||||||||||||||||
| 564 | rules during the rendering of a document, and can be used to pass | ||||||||||||||||||||||||||||
| 565 | information back and forth. | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | $xss->set( section => { | ||||||||||||||||||||||||||||
| 568 | intro => \§ion_title, | ||||||||||||||||||||||||||||
| 569 | } ); | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | # turns <section title="blah"> ... | ||||||||||||||||||||||||||||
| 572 | # into 1. blah | ||||||||||||||||||||||||||||
| 573 | sub section_title { | ||||||||||||||||||||||||||||
| 574 | my ( $self, $node, $args ) = @_; | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | my $section_nbr = $self->stash->{section_nbr}++; | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | return $section_nbr . ". " . $node->findvalue( '@title' ); | ||||||||||||||||||||||||||||
| 579 | } | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | By default, the stash is cleared when rendering a document. | ||||||||||||||||||||||||||||
| 582 | To change this behavior, see L<XML::XSS::Document/use_clean_stash>. | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | =head3 stash() | ||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||
| 586 | The attribute getter. | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | =head3 clear_stash() | ||||||||||||||||||||||||||||
| 589 | |||||||||||||||||||||||||||||
| 590 | Clear the stash. | ||||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | =head1 OVERLOADING | ||||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | =head2 Concatenation (.) | ||||||||||||||||||||||||||||
| 595 | |||||||||||||||||||||||||||||
| 596 | The concatenation operator is overloaded to behave as an alias for C<get()>. | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | my $chapter = $xss.'chapter'; # just like $xss->get('chapter') | ||||||||||||||||||||||||||||
| 599 | |||||||||||||||||||||||||||||
| 600 | $chapter->set_pre( '<div class="chapter">' ); | ||||||||||||||||||||||||||||
| 601 | $chapter->set_post( '</div>' ); | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | Gets really powerful when used in concert with the overloading of the rules | ||||||||||||||||||||||||||||
| 604 | and style attributes: | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | # equivalent as example above | ||||||||||||||||||||||||||||
| 607 | $xss.'chapter'.'pre' *= '<div class="chapter">'; | ||||||||||||||||||||||||||||
| 608 | $xss.'chapter'.'post' *= '</div>'; | ||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||
| 610 | =head1 METHODS | ||||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | =head2 set( $element_1 => \%attrs, $element_2 => \%attrs_2, ... ) | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | Sets attributes for a rendering node. | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | The C<$name> can be | ||||||||||||||||||||||||||||
| 617 | an XML element name, or one of the special keywords C<#document>, | ||||||||||||||||||||||||||||
| 618 | C<#text>, C<#comment>, C<#pi> or C<*> (for the | ||||||||||||||||||||||||||||
| 619 | I<catch-all> element), | ||||||||||||||||||||||||||||
| 620 | which will resolve to the corresponding rendering object. | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | $xss->set( 'foo' => { rename => 'bar' } ); | ||||||||||||||||||||||||||||
| 623 | # same as $xss->element('foo')->set( rename => 'bar' ); | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | $xss->set( '#text' => { filter => { uc shift } } ); | ||||||||||||||||||||||||||||
| 626 | # same as $xss->text->set( filter => { uc shift } ); | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | Note that subsequent calls to C<set()> are additive. I.e.: | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | $xss->set( foo => { pre => 'X' } ); | ||||||||||||||||||||||||||||
| 631 | $xss->set( foo => { post => 'Y' } ); # pre is still set to 'X' | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | If you want to delete an attribute, passes it C<undef> as its | ||||||||||||||||||||||||||||
| 634 | value. | ||||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | =head2 render( $xml, \%args ) | ||||||||||||||||||||||||||||
| 637 | |||||||||||||||||||||||||||||
| 638 | Returns the output produced by the application of the | ||||||||||||||||||||||||||||
| 639 | stylesheet to the xml document. The xml can | ||||||||||||||||||||||||||||
| 640 | be passed as a string, or as a C<XML::LibXML> object. | ||||||||||||||||||||||||||||
| 641 | Several C<XML::LibXML> objects can also be passed, in | ||||||||||||||||||||||||||||
| 642 | which case the return value will be the concatenation | ||||||||||||||||||||||||||||
| 643 | of their transformations. | ||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | my $sections = $xss->render( $doc->findnodes( 'section' ) ); | ||||||||||||||||||||||||||||
| 646 | |||||||||||||||||||||||||||||
| 647 | The C<%args> is optional, and will defaults to an empty | ||||||||||||||||||||||||||||
| 648 | hash if not provided. The reference to C<%args> is also passed to | ||||||||||||||||||||||||||||
| 649 | the recursive calls to C<render()> for the children of the processed | ||||||||||||||||||||||||||||
| 650 | node, which allows for another way for parent/children nodes to pass | ||||||||||||||||||||||||||||
| 651 | information in addition to the C<stash>. | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | # count the descendents of all nodes | ||||||||||||||||||||||||||||
| 654 | $xss->set( | ||||||||||||||||||||||||||||
| 655 | '*' => { | ||||||||||||||||||||||||||||
| 656 | process => sub { | ||||||||||||||||||||||||||||
| 657 | my ( $self, $node, $attrs ) = @_; | ||||||||||||||||||||||||||||
| 658 | $attrs->{children}++; | ||||||||||||||||||||||||||||
| 659 | return 1; | ||||||||||||||||||||||||||||
| 660 | }, | ||||||||||||||||||||||||||||
| 661 | content => sub { | ||||||||||||||||||||||||||||
| 662 | my ( $self, $node, $attrs ) = @_; | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | my %c_attrs; | ||||||||||||||||||||||||||||
| 665 | my $c_ref = \%c_attrs; | ||||||||||||||||||||||||||||
| 666 | my $output = $self->render( $node->childNodes, $c_ref ); | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | $attrs->{children} += $c_ref->{children}; | ||||||||||||||||||||||||||||
| 669 | |||||||||||||||||||||||||||||
| 670 | $self->{post} = | ||||||||||||||||||||||||||||
| 671 | "\n>>> node has " | ||||||||||||||||||||||||||||
| 672 | . ($c_ref->{children}||0) | ||||||||||||||||||||||||||||
| 673 | . " descendents\n"; | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | return $output; | ||||||||||||||||||||||||||||
| 676 | }, | ||||||||||||||||||||||||||||
| 677 | } ); | ||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | Yanick Champoux <yanick@cpan.org> | ||||||||||||||||||||||||||||
| 682 | |||||||||||||||||||||||||||||
| 683 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||
| 685 | This software is copyright (c) 2013 by Yanick Champoux. | ||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||
| 687 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 688 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | =cut | ||||||||||||||||||||||||||||