File Coverage

blib/lib/XML/Invisible/Receiver.pm
Criterion Covered Total %
statement 34 35 97.1
branch 16 20 80.0
condition 5 6 83.3
subroutine 5 5 100.0
pod 1 1 100.0
total 61 67 91.0


line stmt bran cond sub pod time code
1             package XML::Invisible::Receiver;
2              
3 1     1   7 use strict;
  1         1  
  1         29  
4 1     1   5 use warnings;
  1         1  
  1         25  
5 1     1   5 use base 'Pegex::Receiver';
  1         2  
  1         413  
6              
7             sub gotrule {
8 20     20 1 82060 my $self = shift;
9 20 100       47 my $param = (@_ == 1) ? $_[0] : [ @_ ];
10 20 50       46 return unless defined $param;
11 20         31 my $parser = $self->{parser};
12 20         34 my $rule = $parser->{rule};
13 20         25 my $parentrule = $parser->{parent};
14 20         34 my ($attr, $flatten) = @{$parentrule}{qw(-wrap -flat)};
  20         71  
15 20 50 66     53 die "Can't have both attribute (+) and flatten (-) on same node"
16             if $attr and $flatten;
17 20 100       37 if ($flatten) {
18 5         19 return $param;
19             }
20 15 100       44 $param = [ $param ] if ref $param ne 'ARRAY';
21 15         43 $param = $self->flatten($param);
22 15         198 my %ret = (
23             nodename => $rule,
24             type => 'element',
25             );
26 15         34 for (@$param) {
27 21 100 100     101 if (!ref $_ or !$_->{type}) {
    50          
28 17         25 push @{ $ret{children} }, $_;
  17         52  
29             } elsif ($_->{type} eq 'attr') {
30 4         11 $ret{attributes}{$_->{nodename}} = join '', _get_values($_);
31             } else {
32 0         0 die "Unknown entity type '$_->{type}'";
33             }
34             }
35 15 100       35 $ret{type} = 'attr' if $attr;
36 15 100       35 delete $ret{type} if $ret{type} eq 'element';
37 15         60 \%ret;
38             }
39              
40             sub _get_values {
41 4     4   9 my ($node) = @_;
42 4 50       6 map ref($_) ? _get_values($_) : $_, @{ $node->{children} };
  4         23  
43             }
44              
45             1;
46              
47             __END__