| 1 | package XML::Chain::Selector; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use 5.010; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 4 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use MooseX::Aliases; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use XML::LibXML qw(:libxml); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | has 'current_elements' => | ||||||||||||||||||||||||||||
| 16 | 217 | ▶ | (is => 'rw', isa => 'ArrayRef', default => sub {[]}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 4 | has '_xc' => (is => 'rw', isa => 'XML::Chain', required => 1); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use overload '""' => \&as_string, fallback => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | ### chained methods | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | alias c => 'append_and_select'; | |||||||||||||||||||||||||||
| 24 | 4 | alias append_and_current => 'append_and_select'; # name until <= 0.02 | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | sub append_and_select { | ||||||||||||||||||||||||||||
| 27 | 57 | ▶ | my ($self, $el_name, @attrs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
append_and_select: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 57 | my $attrs_ns_uri = {@attrs}->{xmlns}; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | return $self->_new_related( | ||||||||||||||||||||||||||||
| 32 | [ $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 33 | sub { | ||||||||||||||||||||||||||||
| 34 | 57 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 35 | 57 | ▶ | my $ns_uri = $attrs_ns_uri // $el->{ns}; | ||||||||||||||||||||||||||
|
Condition100%3/3
$attrs_ns_uri // $el->{"ns"}
MC/DC100%2/2
$attrs_ns_uri // $el->{"ns"}
$attrs_ns_uri
$el->{"ns"}
Truth table100%3/3
A // B
A= $attrs_ns_uriB= $el->{"ns"}
| |||||||||||||||||||||||||||||
| 36 | my $child_elements = $self->{_xc} | ||||||||||||||||||||||||||||
| 37 | 57 | ->_create_element($el_name, $ns_uri, @attrs); | |||||||||||||||||||||||||||
| 38 | 57 | foreach my $child_el (@$child_elements) { | |||||||||||||||||||||||||||
| 39 | 58 | $el->{lxml}->appendChild($child_el->{lxml}); | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 57 | return @$child_elements; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | ) | ||||||||||||||||||||||||||||
| 44 | 57 | ] | |||||||||||||||||||||||||||
| 45 | ); | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 4 | alias a => 'append'; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | sub append { | ||||||||||||||||||||||||||||
| 51 | 42 | ▶ | my ($self, $el_name, @attrs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
append: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 52 | 42 | return $self->append_and_select($el_name, @attrs)->parent; | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | alias t => 'append_text'; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | sub append_text { | ||||||||||||||||||||||||||||
| 58 | 43 | ▶ | my ($self, $text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
append_text: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 61 | sub { | ||||||||||||||||||||||||||||
| 62 | 43 | ▶ | return $_[0]->{lxml}->appendText($text); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | 43 | ); | |||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 43 | return $self; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 4 | alias up => 'parent'; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | sub parent { | ||||||||||||||||||||||||||||
| 72 | 53 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | return $self->_new_related( | ||||||||||||||||||||||||||||
| 75 | [ $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 76 | sub { | ||||||||||||||||||||||||||||
| 77 | 54 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 78 | 54 | my $parent_el = $el->{lxml}->parentNode; | |||||||||||||||||||||||||||
| 79 | 54 | return $self->{_xc}->_xc_el_data($parent_el); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | ) | ||||||||||||||||||||||||||||
| 82 | 53 | ] | |||||||||||||||||||||||||||
| 83 | ); | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 4 | alias root => 'document_element'; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub document_element { | ||||||||||||||||||||||||||||
| 89 | 13 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
document_element: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 90 | 13 | return $self->{_xc}->document_element; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub find { | ||||||||||||||||||||||||||||
| 94 | 12 | ▶ | my ($self, $xpath, @namespaces) = @_; | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 95 | 12 | ▶ | croak 'need xpath as argument' unless defined($xpath); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 12 | my $xpc = XML::LibXML::XPathContext->new(); | |||||||||||||||||||||||||||
| 98 | 12 | ▶ | while (@namespaces) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | 2 | $xpc->registerNs(splice(@namespaces, 0, 2)) | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | 12 | my $new_self = eval { | |||||||||||||||||||||||||||
| 102 | $self->_new_related( | ||||||||||||||||||||||||||||
| 103 | [ $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 104 | sub { | ||||||||||||||||||||||||||||
| 105 | 12 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 106 | 12 | my $lxml_el = $el->{lxml}; | |||||||||||||||||||||||||||
| 107 | return | ||||||||||||||||||||||||||||
| 108 | 12 | map {$self->{_xc}->_xc_el_data($_)} | |||||||||||||||||||||||||||
| 109 | $xpc->findnodes($xpath, $lxml_el); | ||||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | ) | ||||||||||||||||||||||||||||
| 112 | 12 | ] | |||||||||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||||||||||
| 114 | }; | ||||||||||||||||||||||||||||
| 115 | 12 | ▶ | croak $@ if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 12 | return $new_self; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | sub children { | ||||||||||||||||||||||||||||
| 120 | 9 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
children: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | return $self->_new_related( | ||||||||||||||||||||||||||||
| 123 | [ $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 124 | sub { | ||||||||||||||||||||||||||||
| 125 | 9 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 126 | return | ||||||||||||||||||||||||||||
| 127 | 18 | map {$self->{_xc}->_xc_el_data($_)} | |||||||||||||||||||||||||||
| 128 | 9 | $el->{lxml}->childNodes; | |||||||||||||||||||||||||||
| 129 | } | ||||||||||||||||||||||||||||
| 130 | ) | ||||||||||||||||||||||||||||
| 131 | 9 | ] | |||||||||||||||||||||||||||
| 132 | ); | ||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | sub first { | ||||||||||||||||||||||||||||
| 136 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
first: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | return $self->_new_related( | ||||||||||||||||||||||||||||
| 138 | 4 | [ @{$self->current_elements} | |||||||||||||||||||||||||||
| 139 | 4 | ▶ | ? @{$self->current_elements}[0] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 140 | : () | ||||||||||||||||||||||||||||
| 141 | ] | ||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub auto_indent { | ||||||||||||||||||||||||||||
| 146 | 3 | ▶ | my ($self, $set_to) = @_; | ||||||||||||||||||||||||||
|
Subroutine
auto_indent: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 3 | ▶ | croak 'need true/false/options for auto indentation ' if @_ < 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 3 | ▶ | $self->_cur_el_iterrate(sub {$_[0]->{auto_indent} = $set_to}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | 3 | return $self; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub empty { | ||||||||||||||||||||||||||||
| 155 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
empty: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||
| 157 | $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 158 | sub { | ||||||||||||||||||||||||||||
| 159 | 1 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 160 | 1 | $el->{lxml}->removeChildNodes; | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 1 | ); | |||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 1 | return $self; | |||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | sub rename { | ||||||||||||||||||||||||||||
| 168 | 5 | ▶ | my ($self, $new_name) = @_; | ||||||||||||||||||||||||||
|
Subroutine
rename: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 169 | 5 | ▶ | croak 'need new name ' unless defined($new_name); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 172 | sub { | ||||||||||||||||||||||||||||
| 173 | 5 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 174 | 5 | $el->{lxml}->setNodeName($new_name); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | 5 | ); | |||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 5 | return $self; | |||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||
| 181 | sub each { | ||||||||||||||||||||||||||||
| 182 | 1 | ▶ | my ($self, $code_ref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
each: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 183 | 1 | ▶ | croak 'need new code ref ' if ref($code_ref) ne 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 186 | sub { | ||||||||||||||||||||||||||||
| 187 | 4 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 188 | $_ = XML::Chain::Element->new( | ||||||||||||||||||||||||||||
| 189 | _xc_el_data => $el, | ||||||||||||||||||||||||||||
| 190 | _xc => $self->{_xc}, | ||||||||||||||||||||||||||||
| 191 | 4 | ); | |||||||||||||||||||||||||||
| 192 | 4 | $code_ref->(); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | 1 | ); | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 1 | return $self; | |||||||||||||||||||||||||||
| 197 | } | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | sub remap { | ||||||||||||||||||||||||||||
| 200 | 5 | ▶ | my ($self, $code_ref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
remap: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 201 | 5 | ▶ | croak 'need new code ref ' if ref($code_ref) ne 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | return $self->_new_related([ | ||||||||||||||||||||||||||||
| 204 | $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 205 | sub { | ||||||||||||||||||||||||||||
| 206 | 9 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=18.0
| |||||||||||||||||||||||||||||
| 207 | my $el_xc = XML::Chain::Element->new( | ||||||||||||||||||||||||||||
| 208 | _xc_el_data => $el, | ||||||||||||||||||||||||||||
| 209 | _xc => $self->{_xc}, | ||||||||||||||||||||||||||||
| 210 | 9 | ); | |||||||||||||||||||||||||||
| 211 | 9 | local $_ = $el_xc; | |||||||||||||||||||||||||||
| 212 | 9 | my @new_elements = $code_ref->(); | |||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | # element removed | ||||||||||||||||||||||||||||
| 215 | 9 | ▶ | if (!defined($new_elements[0])) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 216 | 2 | $el_xc->rm; | |||||||||||||||||||||||||||
| 217 | 2 | return; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | @new_elements = map { | ||||||||||||||||||||||||||||
| 221 | 7 | ▶ | croak 'must return isa XML::Chain::Selector' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 222 | unless $_->isa('XML::Chain::Selector'); | ||||||||||||||||||||||||||||
| 223 | 13 | @{$_->current_elements} | |||||||||||||||||||||||||||
| 224 | } @new_elements; | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | # element removed | ||||||||||||||||||||||||||||
| 227 | 7 | ▶ | if (@new_elements == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 0 | $el_xc->rm; | |||||||||||||||||||||||||||
| 229 | 0 | return; | |||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | # if changed, replace first new element with the old one | ||||||||||||||||||||||||||||
| 233 | 7 | ▶ | if ($new_elements[0]->{eid} != $el->{eid}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 234 | 6 | $el->{lxml}->replaceNode($new_elements[0]->{lxml}); | |||||||||||||||||||||||||||
| 235 | 6 | $el = $new_elements[0]; | |||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | # add all the rest after | ||||||||||||||||||||||||||||
| 238 | 7 | my $i = 1; | |||||||||||||||||||||||||||
| 239 | 7 | ▶ | while ($i < @new_elements) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | 6 | $el->{lxml}->parentNode->insertAfter($new_elements[$i]->{lxml},$new_elements[$i-1]->{lxml}); | |||||||||||||||||||||||||||
| 241 | 6 | $i++; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | 7 | return @new_elements; | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | ) | ||||||||||||||||||||||||||||
| 247 | 5 | ]); | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 0 | return $self; | |||||||||||||||||||||||||||
| 250 | } | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | 4 | alias rm => 'remove_and_parent'; | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | sub remove_and_parent { | ||||||||||||||||||||||||||||
| 255 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
remove_and_parent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | 3 | my $parent = $self->parent; | |||||||||||||||||||||||||||
| 258 | $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 259 | sub { | ||||||||||||||||||||||||||||
| 260 | 3 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 261 | 3 | $el->{deleted} = 1; | |||||||||||||||||||||||||||
| 262 | 3 | $el->{lxml}->parentNode->removeChild($el->{lxml}); | |||||||||||||||||||||||||||
| 263 | 3 | $el->{lxml} = undef; | |||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||
| 265 | 3 | ); | |||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 3 | return $parent; | |||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | sub attr { | ||||||||||||||||||||||||||||
| 271 | 3 | ▶ | my ($self, @attrs) = @_; | ||||||||||||||||||||||||||
|
Subroutine
attr: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | 3 | ▶ | croak 'need new attribute name ' unless @attrs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | # getter | ||||||||||||||||||||||||||||
| 276 | 3 | ▶ | if (@attrs == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 277 | 1 | my $attr_name = $attrs[0]; | |||||||||||||||||||||||||||
| 278 | 1 | my @attribute_values; | |||||||||||||||||||||||||||
| 279 | $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 280 | sub { | ||||||||||||||||||||||||||||
| 281 | 1 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 282 | 1 | push(@attribute_values, $el->{lxml}->getAttribute($attr_name)); | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | 1 | ); | |||||||||||||||||||||||||||
| 285 | 1 | ▶ | return (@attribute_values == 1 ? $attribute_values[0] : @attribute_values); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | } | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | # setter | ||||||||||||||||||||||||||||
| 289 | 2 | ▶ | while (my ($attr_name, $attr_value) = splice(@attrs,0,2)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 291 | sub { | ||||||||||||||||||||||||||||
| 292 | 3 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 293 | 3 | ▶ | if (defined($attr_value)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 2 | $el->{lxml}->setAttribute($attr_name => $attr_value); | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | else { | ||||||||||||||||||||||||||||
| 297 | 1 | $el->{lxml}->removeAttribute($attr_name); | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | 3 | ); | |||||||||||||||||||||||||||
| 301 | } | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 2 | return $self; | |||||||||||||||||||||||||||
| 304 | } | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | ### methods | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 4 | alias toString => 'as_string'; | |||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | sub as_string { | ||||||||||||||||||||||||||||
| 311 | 36 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 312 | return join( | ||||||||||||||||||||||||||||
| 313 | '', | ||||||||||||||||||||||||||||
| 314 | $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 315 | sub { | ||||||||||||||||||||||||||||
| 316 | 36 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | 36 | my $auto_indent = $el->{auto_indent}; | |||||||||||||||||||||||||||
| 319 | my $auto_indent_chars = ( | ||||||||||||||||||||||||||||
| 320 | (ref($auto_indent) eq 'HASH') | ||||||||||||||||||||||||||||
| 321 | ? $auto_indent->{chars} | ||||||||||||||||||||||||||||
| 322 | : undef | ||||||||||||||||||||||||||||
| 323 | 36 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 324 | 36 | ▶ | $auto_indent_chars = "\t" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | unless defined($auto_indent_chars); | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | return ( | ||||||||||||||||||||||||||||
| 328 | $auto_indent | ||||||||||||||||||||||||||||
| 329 | ? _reindent_children($el->{lxml}, $auto_indent_chars)->toStringC14N | ||||||||||||||||||||||||||||
| 330 | : $el->{lxml}->toStringC14N | ||||||||||||||||||||||||||||
| 331 | 36 | ▶ | ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | ) | ||||||||||||||||||||||||||||
| 334 | 36 | ); | |||||||||||||||||||||||||||
| 335 | } | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | sub _reindent_children { | ||||||||||||||||||||||||||||
| 338 | 15 | ▶ | my ($lxml_el, $indent_chars, $level) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_reindent_children: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||
| 339 | 15 | ▶ | $level //= 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$level //= 1
MC/DC100%1/1
$level //= 1
$level
Truth table100%2/2
A
A= $level
| |||||||||||||||||||||||||||||
| 340 | 15 | my $cur_ident = join('', map { $indent_chars } (1..$level)); | |||||||||||||||||||||||||||
| 341 | 15 | my $as_string = ''; | |||||||||||||||||||||||||||
| 342 | 15 | my $cur_text = ''; | |||||||||||||||||||||||||||
| 343 | 15 | my @child_nodes = map { $lxml_el->removeChild($_) } $lxml_el->childNodes(); | |||||||||||||||||||||||||||
| 344 | 15 | my $child_nodes_count = @child_nodes; | |||||||||||||||||||||||||||
| 345 | 15 | ▶ | while (@child_nodes) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 346 | 23 | my $node = shift(@child_nodes); | |||||||||||||||||||||||||||
| 347 | 23 | my $node_type = $node->nodeType; | |||||||||||||||||||||||||||
| 348 | 23 | ▶ | if ($node_type == XML_TEXT_NODE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | 11 | ▶ | $cur_text .= ' ' if length($cur_text); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 350 | 11 | $cur_text .= $node->textContent; | |||||||||||||||||||||||||||
| 351 | 11 | $cur_text =~ s/^\s+//; | |||||||||||||||||||||||||||
| 352 | 11 | $cur_text =~ s/\s+$//; | |||||||||||||||||||||||||||
| 353 | 11 | next; | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | 12 | ▶ | if (length($cur_text)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 356 | 1 | $lxml_el->appendText("\n".$indent_chars.$cur_text); | |||||||||||||||||||||||||||
| 357 | 1 | $cur_text = ''; | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | 12 | _reindent_children($node, $indent_chars, $level+1); | |||||||||||||||||||||||||||
| 360 | 12 | $lxml_el->appendText("\n".$cur_ident); | |||||||||||||||||||||||||||
| 361 | 12 | $lxml_el->addChild($node); | |||||||||||||||||||||||||||
| 362 | } | ||||||||||||||||||||||||||||
| 363 | 15 | ▶ | if (($child_nodes_count == 1) && length($cur_text)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$child_nodes_count == 1 and length $cur_text
MC/DC100%2/2
$child_nodes_count == 1 and length $cur_text
$child_nodes_count == 1
length $cur_text
Truth table100%3/3
A and B
A= $child_nodes_count == 1B= length $cur_text
| |||||||||||||||||||||||||||||
| 364 | 10 | $lxml_el->appendText($cur_text); | |||||||||||||||||||||||||||
| 365 | } | ||||||||||||||||||||||||||||
| 366 | else { | ||||||||||||||||||||||||||||
| 367 | 5 | ▶ | if (length($cur_text)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | 0 | $lxml_el->appendText("\n".$indent_chars.$cur_text); | |||||||||||||||||||||||||||
| 369 | 0 | $cur_text = ''; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | 5 | ▶ | $lxml_el->appendText("\n".join('', map { $indent_chars } (1..$level-1))) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 372 | if ($lxml_el->childNodes()); | ||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 15 | return $lxml_el; | |||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | sub text_content { | ||||||||||||||||||||||||||||
| 379 | 9 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
text_content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | 9 | my $text = ''; | |||||||||||||||||||||||||||
| 382 | $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 383 | sub { | ||||||||||||||||||||||||||||
| 384 | 9 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 385 | 9 | $text .= $el->{lxml}->textContent; | |||||||||||||||||||||||||||
| 386 | 9 | return $el; | |||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | 9 | ); | |||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 9 | return $text; | |||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | sub as_xml_libxml { | ||||||||||||||||||||||||||||
| 394 | 0 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_xml_libxml: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | 0 | my @elements; | |||||||||||||||||||||||||||
| 397 | $self->_cur_el_iterrate( | ||||||||||||||||||||||||||||
| 398 | sub { | ||||||||||||||||||||||||||||
| 399 | 0 | ▶ | my ($el) = @_; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 400 | 0 | push(@elements, $el->{lxml}); | |||||||||||||||||||||||||||
| 401 | } | ||||||||||||||||||||||||||||
| 402 | 0 | ); | |||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||
| 404 | 0 | return @elements; | |||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 4 | alias size => 'count'; | |||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | sub count { | ||||||||||||||||||||||||||||
| 410 | 4 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 411 | 4 | my $count = 0; | |||||||||||||||||||||||||||
| 412 | 4 | ▶ | $self->_cur_el_iterrate(sub {$count++}); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 413 | 4 | return $count; | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 2 | ▶ | sub store {$_[0]->{_xc}->store} | ||||||||||||||||||||||||||
|
Subroutine
store: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 417 | 1 | ▶ | sub set_io_any {$_[0]->{_xc}->set_io_any($_[1], $_[2])} | ||||||||||||||||||||||||||
|
Subroutine
set_io_any: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | sub single { | ||||||||||||||||||||||||||||
| 420 | 3 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
single: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||
| 421 | croak 'more current elements then one' | ||||||||||||||||||||||||||||
| 422 | 3 | ▶ | if @{$self->current_elements} > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 423 | 3 | ▶ | croak 'no current element' unless @{$self->current_elements} == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 424 | 3 | my $element = @{$self->current_elements}[0]; | |||||||||||||||||||||||||||
| 425 | return XML::Chain::Element->new( | ||||||||||||||||||||||||||||
| 426 | _xc_el_data => $element, | ||||||||||||||||||||||||||||
| 427 | _xc => $self->{_xc}, | ||||||||||||||||||||||||||||
| 428 | 3 | ); | |||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | sub reg_global_ns { | ||||||||||||||||||||||||||||
| 432 | 1 | ▶ | my ($self, $ns_prefix, $ns_uri, $activate) = @_; | ||||||||||||||||||||||||||
|
Subroutine
reg_global_ns: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 433 | 1 | ▶ | $self->root->as_xml_libxml->setNamespace($ns_uri, $ns_prefix, ($activate // 0)); | ||||||||||||||||||||||||||
|
Condition50%1/2
$activate // 0
MC/DC0%0/1
$activate // 0
$activate
Truth table50%1/2
A
A= $activate
| |||||||||||||||||||||||||||||
| 434 | 1 | return $self; | |||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | ### helpers | ||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | sub _cur_el_iterrate { | ||||||||||||||||||||||||||||
| 440 | 245 | ▶ | my ($self, $code_ref) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_cur_el_iterrate: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 441 | 245 | ▶ | croak 'need code ref a argument' unless ref($code_ref) eq 'CODE'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 442 | 245 | ▶ | return map {$code_ref->($_) if !$_->{deleted}} @{$self->current_elements}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 443 | } | ||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||
| 445 | sub _new_related { | ||||||||||||||||||||||||||||
| 446 | 140 | ▶ | my ($self, $current_elements) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_new_related: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 447 | 140 | ▶ | croak 'need array ref as argument' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 448 | unless ref($current_elements) eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | return XML::Chain::Element->new( | ||||||||||||||||||||||||||||
| 451 | _xc_el_data => $current_elements->[0], | ||||||||||||||||||||||||||||
| 452 | _xc => $self->{_xc}, | ||||||||||||||||||||||||||||
| 453 | 140 | ▶ | ) if @$current_elements == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | # make current_elements uniq | ||||||||||||||||||||||||||||
| 456 | 17 | my %uniq_eid; | |||||||||||||||||||||||||||
| 457 | $current_elements = | ||||||||||||||||||||||||||||
| 458 | 17 | ▶ | [grep {$uniq_eid{$_->{eid}} ? 0 : ($uniq_eid{$_->{eid}} = 1)} | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 459 | @$current_elements]; | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | return __PACKAGE__->new( | ||||||||||||||||||||||||||||
| 462 | current_elements => $current_elements, | ||||||||||||||||||||||||||||
| 463 | _xc => $self->{_xc}, | ||||||||||||||||||||||||||||
| 464 | 17 | ); | |||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | 4 | 1; | |||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | __END__ | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =encoding utf8 | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =head1 NAME | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | XML::Chain::Selector - selector for traversing the XML::Chain | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||
| 479 | my $user = xc('user', xmlns => 'http://testns')->auto_indent({chars=>' 'x4}) | ||||||||||||||||||||||||||||
| 480 | ->a('name', '-' => 'Johnny Thinker') | ||||||||||||||||||||||||||||
| 481 | ->a('username', '-' => 'jt') | ||||||||||||||||||||||||||||
| 482 | ->c('bio') | ||||||||||||||||||||||||||||
| 483 | ->c('div', xmlns => 'http://www.w3.org/1999/xhtml') | ||||||||||||||||||||||||||||
| 484 | ->a('h1', '-' => 'about') | ||||||||||||||||||||||||||||
| 485 | ->a('p', '-' => '...') | ||||||||||||||||||||||||||||
| 486 | ->up | ||||||||||||||||||||||||||||
| 487 | ->a('greeting', '-' => 'Hey') | ||||||||||||||||||||||||||||
| 488 | ->up | ||||||||||||||||||||||||||||
| 489 | ->a('active', '-' => '1') | ||||||||||||||||||||||||||||
| 490 | ->root; | ||||||||||||||||||||||||||||
| 491 | say $user->as_string; | ||||||||||||||||||||||||||||
| 492 | |||||||||||||||||||||||||||||
| 493 | Will print: | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | <user xmlns="http://testns"> | ||||||||||||||||||||||||||||
| 496 | <name>Johnny Thinker</name> | ||||||||||||||||||||||||||||
| 497 | <username>jt</username> | ||||||||||||||||||||||||||||
| 498 | <bio> | ||||||||||||||||||||||||||||
| 499 | <div xmlns="http://www.w3.org/1999/xhtml"> | ||||||||||||||||||||||||||||
| 500 | <h1>about</h1> | ||||||||||||||||||||||||||||
| 501 | <p>...</p> | ||||||||||||||||||||||||||||
| 502 | </div> | ||||||||||||||||||||||||||||
| 503 | <greeting>Hey</greeting> | ||||||||||||||||||||||||||||
| 504 | </bio> | ||||||||||||||||||||||||||||
| 505 | <active>1</active> | ||||||||||||||||||||||||||||
| 506 | </user> | ||||||||||||||||||||||||||||
| 507 | |||||||||||||||||||||||||||||
| 508 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 509 | |||||||||||||||||||||||||||||
| 510 | =head1 CHAINED METHODS | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | =head2 c, append_and_select | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | Appends new element to current elements and changes context to them. New | ||||||||||||||||||||||||||||
| 515 | element is defined in parameters: | ||||||||||||||||||||||||||||
| 516 | |||||||||||||||||||||||||||||
| 517 | $xc->c('i', class => 'icon-download icon-white') | ||||||||||||||||||||||||||||
| 518 | # <i class="icon-download icon-white"/> | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | First parameter is name of the element, then followed by optional element | ||||||||||||||||||||||||||||
| 521 | attributes. | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | =head2 t, append_text | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | Appends text to current elements. | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | xc('span')->t('some')->t(' ')->t('more text') | ||||||||||||||||||||||||||||
| 528 | # <span>some more text</span> | ||||||||||||||||||||||||||||
| 529 | |||||||||||||||||||||||||||||
| 530 | First parameter is name of the element, then followed by optional element | ||||||||||||||||||||||||||||
| 531 | attributes. | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | =head2 root | ||||||||||||||||||||||||||||
| 534 | |||||||||||||||||||||||||||||
| 535 | Sets document element as current element. | ||||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | say xc('p') | ||||||||||||||||||||||||||||
| 538 | ->t('this ') | ||||||||||||||||||||||||||||
| 539 | ->a(xc('b')->t('is')) | ||||||||||||||||||||||||||||
| 540 | ->t(' important!') | ||||||||||||||||||||||||||||
| 541 | ->root->as_string; | ||||||||||||||||||||||||||||
| 542 | # <p>this <b>is</b> important!</p> | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | =head2 up, parent | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | Traverse current elements and replace them by their parents. | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | =head2 find | ||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||
| 550 | say $xc->find('//p/b[@class="less"]')->text_content; | ||||||||||||||||||||||||||||
| 551 | say $xc->find('//xhtml:div', xhtml => 'http://www.w3.org/1999/xhtml')->count; | ||||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | Look-up elements by xpath and set them as current elements. Optional | ||||||||||||||||||||||||||||
| 554 | look-up namespace prefixes can be specified. Any global registered | ||||||||||||||||||||||||||||
| 555 | namespace prefixes L</reg_global_ns> can be used. | ||||||||||||||||||||||||||||
| 556 | |||||||||||||||||||||||||||||
| 557 | =head2 children | ||||||||||||||||||||||||||||
| 558 | |||||||||||||||||||||||||||||
| 559 | Set all current elements child nodes as current elements. | ||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||
| 561 | =head2 first | ||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||
| 563 | Set first current elements as current elements. | ||||||||||||||||||||||||||||
| 564 | |||||||||||||||||||||||||||||
| 565 | =head2 empty | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | Removes all child nodes from current elements. | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | =head2 rename | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | my $body = xc('bodyz')->rename('body'); | ||||||||||||||||||||||||||||
| 572 | # <body/> | ||||||||||||||||||||||||||||
| 573 | |||||||||||||||||||||||||||||
| 574 | Rename node name(s). | ||||||||||||||||||||||||||||
| 575 | |||||||||||||||||||||||||||||
| 576 | =head2 attr | ||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||
| 578 | my $img = xc('img')->attr('href' => '#', 'title' => 'imaget'); | ||||||||||||||||||||||||||||
| 579 | # <img href="#" title="imaget"/> | ||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||
| 581 | say $img->attr('title') | ||||||||||||||||||||||||||||
| 582 | # imaget | ||||||||||||||||||||||||||||
| 583 | |||||||||||||||||||||||||||||
| 584 | say $img->attr('title' => undef) | ||||||||||||||||||||||||||||
| 585 | # <img href="#"/> | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | Get or set elements attributes. With one argument returns attribute value | ||||||||||||||||||||||||||||
| 588 | otherwise sets them. Setting attribute to C<undef> will remove it from | ||||||||||||||||||||||||||||
| 589 | the element. | ||||||||||||||||||||||||||||
| 590 | |||||||||||||||||||||||||||||
| 591 | =head2 each | ||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | # rename using each | ||||||||||||||||||||||||||||
| 594 | $body->rename('body'); | ||||||||||||||||||||||||||||
| 595 | $body | ||||||||||||||||||||||||||||
| 596 | ->a(xc('p.1')->t(1)) | ||||||||||||||||||||||||||||
| 597 | ->a(xc('p.2')->t(2)) | ||||||||||||||||||||||||||||
| 598 | ->a(xc('div')->t(3)) | ||||||||||||||||||||||||||||
| 599 | ->a(xc('p.3')->t(4)) | ||||||||||||||||||||||||||||
| 600 | ->each(sub { $_->rename('p') if $_->name =~ m/^p[.]/ }); | ||||||||||||||||||||||||||||
| 601 | is($body, '<body><p>1</p><p>2</p><div>3</div><p>4</p></body>','rename using each()'); | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | Loops through all selected elements and calls callback for each of them. | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | =head2 remap | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | xc('body')->a('p', i => 1)->children->remap( | ||||||||||||||||||||||||||||
| 608 | sub { | ||||||||||||||||||||||||||||
| 609 | (map {xc('e', i => $_)} 1 .. 3), $_; | ||||||||||||||||||||||||||||
| 610 | } | ||||||||||||||||||||||||||||
| 611 | )->root; | ||||||||||||||||||||||||||||
| 612 | # <body><e i="1"/><e i="2"/><e i="3"/><p i="1"/></body> | ||||||||||||||||||||||||||||
| 613 | |||||||||||||||||||||||||||||
| 614 | Replaces all selected elements by callback returned elements. | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | =head2 rm, remove_and_parent | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | my $pdiv = xc('base') | ||||||||||||||||||||||||||||
| 619 | ->a(xc('p')->t(1)) | ||||||||||||||||||||||||||||
| 620 | ->a(xc('p')->t(2)) | ||||||||||||||||||||||||||||
| 621 | ->a(xc('div')->t(3)) | ||||||||||||||||||||||||||||
| 622 | ->a(xc('p')->t(4)); | ||||||||||||||||||||||||||||
| 623 | my $p = $pdiv->find('//p'); | ||||||||||||||||||||||||||||
| 624 | # $pdiv->find('//p[position()=3]')->rm->name eq 'base' | ||||||||||||||||||||||||||||
| 625 | # $p->count == 2 # deleted elements are skipped also in old selectors | ||||||||||||||||||||||||||||
| 626 | # <base><p>1</p><p>2</p><div>3</div></base> | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | Deletes current elements and returns their parent. | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | =head2 auto_indent | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | (experimental feature (good/usefull for debug, needs more testing), | ||||||||||||||||||||||||||||
| 633 | works only on element for which as_string is called at this moment) | ||||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | my $simple = xc('div') | ||||||||||||||||||||||||||||
| 636 | ->auto_indent(1) | ||||||||||||||||||||||||||||
| 637 | ->a('div', '-' => 'in1') | ||||||||||||||||||||||||||||
| 638 | ->a('div', '-' => 'in2') | ||||||||||||||||||||||||||||
| 639 | ->t('in2.1') | ||||||||||||||||||||||||||||
| 640 | ->a('div', '-' => 'in3') | ||||||||||||||||||||||||||||
| 641 | ; | ||||||||||||||||||||||||||||
| 642 | say $simple->as_string; | ||||||||||||||||||||||||||||
| 643 | |||||||||||||||||||||||||||||
| 644 | Will print: | ||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | <div> | ||||||||||||||||||||||||||||
| 647 | <div>in1</div> | ||||||||||||||||||||||||||||
| 648 | <div>in2</div> | ||||||||||||||||||||||||||||
| 649 | in2.1 | ||||||||||||||||||||||||||||
| 650 | <div>in3</div> | ||||||||||||||||||||||||||||
| 651 | </div> | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | Turn on/off tidy/auto-indentation of document elements. Default indentation | ||||||||||||||||||||||||||||
| 654 | characters are tabs. | ||||||||||||||||||||||||||||
| 655 | |||||||||||||||||||||||||||||
| 656 | Argument can be either true/false scalar or a hashref with indentation | ||||||||||||||||||||||||||||
| 657 | options. Currently C< {chars=>' 'x4} > will set indentation characters to | ||||||||||||||||||||||||||||
| 658 | be four spaces. | ||||||||||||||||||||||||||||
| 659 | |||||||||||||||||||||||||||||
| 660 | =head1 CHAINED DOCUMENT METHODS | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | See L<XML::Chain/CHAINED DOCUMENT METHODS>. | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | =head1 METHODS | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | =head2 as_string, toString | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | Returns string representation of current XML elements. Call L<root> before | ||||||||||||||||||||||||||||
| 669 | to get a string representing the whole document. | ||||||||||||||||||||||||||||
| 670 | |||||||||||||||||||||||||||||
| 671 | $xc->as_string | ||||||||||||||||||||||||||||
| 672 | $xc->root->as_string | ||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||
| 674 | =head2 as_xml_libxml | ||||||||||||||||||||||||||||
| 675 | |||||||||||||||||||||||||||||
| 676 | Returns array of current elements as L<XML::LibXML> objects. | ||||||||||||||||||||||||||||
| 677 | |||||||||||||||||||||||||||||
| 678 | =head2 text_content | ||||||||||||||||||||||||||||
| 679 | |||||||||||||||||||||||||||||
| 680 | Returns text content of all current XML elements. | ||||||||||||||||||||||||||||
| 681 | |||||||||||||||||||||||||||||
| 682 | =head2 count / size | ||||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | say $xc->find('//b')->count; | ||||||||||||||||||||||||||||
| 685 | |||||||||||||||||||||||||||||
| 686 | Return the number of current elements. | ||||||||||||||||||||||||||||
| 687 | |||||||||||||||||||||||||||||
| 688 | =head2 single | ||||||||||||||||||||||||||||
| 689 | |||||||||||||||||||||||||||||
| 690 | my $lxml_el = $xc->find('//b')->first->as_xml_libxml; | ||||||||||||||||||||||||||||
| 691 | |||||||||||||||||||||||||||||
| 692 | Checks is there is exactly one element in current elements and return it | ||||||||||||||||||||||||||||
| 693 | as L<XML::Chain::Element> object. | ||||||||||||||||||||||||||||
| 694 | |||||||||||||||||||||||||||||
| 695 | =head2 reg_global_ns | ||||||||||||||||||||||||||||
| 696 | |||||||||||||||||||||||||||||
| 697 | $sitemap->reg_global_ns('i' => 'http://www.google.com/schemas/sitemap-image/1.1'); | ||||||||||||||||||||||||||||
| 698 | $sitemap->reg_global_ns('s' => 'http://www.sitemaps.org/schemas/sitemap/0.9'); | ||||||||||||||||||||||||||||
| 699 | say $sitemap->find('/s:urlset/s:url/i:image')->count | ||||||||||||||||||||||||||||
| 700 | # 2 | ||||||||||||||||||||||||||||
| 701 | |||||||||||||||||||||||||||||
| 702 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 703 | |||||||||||||||||||||||||||||
| 704 | Jozef Kutej | ||||||||||||||||||||||||||||
| 705 | |||||||||||||||||||||||||||||
| 706 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | Copyright 2017 Jozef Kutej, all rights reserved. | ||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||
| 710 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 711 | under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||
| 713 | =cut | ||||||||||||||||||||||||||||