blib/lib/HTML/ValidationRules/Parser.pm

filter: Statementstmt 98.5% 66 / 67 Branchbran 83.3% 15 / 18 Conditioncond 70.0% 14 / 20 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 5 totaltotal 83.2% 109 / 131 CC 17 SCAR 20.4
CC
17
Cov
90%
start
13.1
parser
3.1
load_rules
2.0
CRAP 7.7
↑ Summary lib/FormValidator/Simple/Plugin/HTML.pm »
1 package HTML::ValidationRules::Parser;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 my %ELEMENTS = (
8 input => [qw(
9 max
10 maxlength
11 min
12 pattern
13 required
14 ), {
15 name => 'type',
16 values => [qw(
17 url
18 email
19 number
20 range
21 )],
22 }],
23  
24 textarea => [qw(
25 maxlength
26 required
27 )],
28  
29 select => [qw(
30 required
31 )],
32 );
33  
34 3 my $ELEMENTS_PATTERN = qr/(@{[join '|', (map { quotemeta } keys %ELEMENTS)]})/o;
35 my %ATTRS_MAP = map {
36 3 ▶ my $attr = ref $_ ? $_->{name} : $_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 9 9
37 9 $attr => +{ map { $_ => 1 } @{$ELEMENTS{$_}} };
38 } keys %ELEMENTS;
39 my %TYPE_ATTR_MAP = map {
40 3 my $attr = $_;
41 3 map { $_ => 1 } @{$attr->{values}};
42 3 ▶} grep { ref $_ && $_->{name} eq 'type' } @{$ELEMENTS{input}};
Condition66%2/3
ref $_ && $_->{'name'} eq 'type'
!ll&&!rl&&r
1503
MC/DC50%1/2
ref $_ && $_->{'name'} eq 'type'
ref $_ $_->{'name'} eq 'type'
Truth table66%2/3
A && B
A= ref $_B= $_->{'name'} eq 'type'
ABresult
0X0
100
111
43  
44 52 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
45 52 my ($class, %args) = @_;
46 52 bless \%args, $class;
47 }
48  
49 208 ▶sub parser {
Subroutine parser: called CC=3 SCAR=11.4
Pod undocumented
50 208 my ($self) = @_;
51 $self->{parser} ||= HTML::Parser->new(
52 api_version => 3,
53 start_h => [\&start, 'self, tagname, attr, attrseq'],
54 208 ▶ %{$self->{options} || {}},
Condition60%3/5
$self->{"parser"} ||= "HTML::Parser"->new("api_version", 3, "start_h", [\&HTML::ValidationRules::Parser::start, "self, tagname, attr, attrseq"], %{$self->{"options"} or {};})
l!l&&r!l&&!r
156520
$self->{'options'} || {}
l!l
052
MC/DC0%0/2
$self->{"parser"} ||= "HTML::Parser"->new("api_version", 3, "start_h", [\&HTML::ValidationRules::Parser::start, "self, tagname, attr, attrseq"], %{$self->{"options"} or {};})
$self->{"parser"} "HTML::Parser"->new("api_version", 3, "start_h", [\&HTML::ValidationRules::Parser::start, "self, tagname, attr, attrseq"], %{$self->{"options"} or {};})
MC/DC0%0/1
$self->{'options'} || {}
$self->{'options'}
Truth table66%2/3
A ||= B
A= $self->{"parser"}B= "HTML::Parser"->new("api_version", 3, "start_h", [\&HTML::ValidationRules::Parser::start, "self, tagname, attr, attrseq"], %{$self->{"options"} or {};})
ABresult
1X1
011
000
Truth table50%1/2
A
A= $self->{'options'}
Aresult
11
00
55 );
56 }
57  
58 52 ▶sub load_rules {
Subroutine load_rules: called CC=2 SCAR=7.1
Pod undocumented
59 52 my ($self, %args) = @_;
60 52 my $file = delete $args{file};
61 52 my $html = delete $args{html};
62  
63 52 undef $self->parser->{rules};
64  
65 52 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 0 52 52
66 0 $self->parser->parse_file($file);
67 }
68 else {
69 52 $self->parser->parse($html);
70 52 $self->parser->eof;
71 }
72  
73 52 $self->parser->{rules};
74 }
75  
76 65 ▶sub start {
Subroutine start: called CC=13 SCAR=25.7
Pod undocumented
77 65 my ($parser, $tag, $attr, $attrseq) = @_;
78 65 ▶ return if $tag !~ $ELEMENTS_PATTERN;
Branch: 1 branch
BranchTrueFalseTotal
unless $tag =~ /$ELEMENTS_PATTERN/ 4 61 65
79  
80 61 my $name = $attr->{name};
81 61 ▶ return if !defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 1 60 61
82  
83 60 my @rules;
84 60 my $attrs = $ATTRS_MAP{lc $tag};
85  
86 60 ▶ if (defined $attr->{type} && $TYPE_ATTR_MAP{lc $attr->{type} || ''}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr->{"type"} and $TYPE_ATTR_MAP{lc $attr->{"type"} or ""}) 37 23 60
Condition80%4/5
defined $attr->{"type"} and $TYPE_ATTR_MAP{lc $attr->{"type"} or ""}
!ll&&!rl&&r
14937
lc $attr->{'type'} || ''
l!l
460
MC/DC100%2/2
defined $attr->{"type"} and $TYPE_ATTR_MAP{lc $attr->{"type"} or ""}
defined $attr->{"type"} $TYPE_ATTR_MAP{lc $attr->{"type"} or ""}
MC/DC0%0/1
lc $attr->{'type'} || ''
lc $attr->{'type'}
Truth table100%3/3
A and B
A= defined $attr->{"type"}B= $TYPE_ATTR_MAP{lc $attr->{"type"} or ""}
ABresult
0X0
100
111
Truth table50%1/2
A
A= lc $attr->{'type'}
Aresult
11
00
87 37 my $type = $attr->{type};
88 37 unshift @rules, key($type);
89 37 $attrseq = [ grep { lc $_ ne 'type' } @$attrseq ];
90 }
91  
92 60 ▶ for my $key (@{$attrseq || []}) {
Condition50%1/2
$attrseq || []
l!l
600
MC/DC0%0/1
$attrseq || []
$attrseq
Truth table50%1/2
A
A= $attrseq
Aresult
11
00
93 133 ▶ next if !$attrs->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless $attrs->{$key} 81 52 133
94  
95 52 my $value = $attr->{$key};
96 52 ▶ if (defined $value && $key ne $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value and $key ne $value) { } 43 9 52
elsif ($key eq $value) { } 9 0 9
Condition66%2/3
defined $value and $key ne $value
!ll&&!rl&&r
0943
MC/DC50%1/2
defined $value and $key ne $value
defined $value $key ne $value
Truth table66%2/3
A and B
A= defined $valueB= $key ne $value
ABresult
0X0
100
111
97 43 push @rules, [ key($key) => $value ];
98 }
99 elsif ($key eq $value) {
100 9 push @rules, key($key);
101 }
102 }
103  
104 60 ▶ $parser->{rules} ||= [];
Condition100%2/2
$parser->{"rules"} ||= []
l!l
852
MC/DC100%1/1
$parser->{"rules"} ||= []
$parser->{"rules"}
Truth table100%2/2
A
A= $parser->{"rules"}
Aresult
11
00
105 60 push @{$parser->{rules}}, $name => \@rules;
106 }
107  
108 89 ▶sub key {
Subroutine key: called CC=2 SCAR=6.9
Pod undocumented
109 89 my $key = shift;
110 89 ▶ return 'NOT_BLANK' if $key eq 'required';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "required" 9 80 89
111 80 sprintf 'HTML_%s', uc $key;
112 }
113  
114 3 !!1;
↑ Summary lib/FormValidator/Simple/Plugin/HTML.pm »