blib/lib/HTML/DisableForm.pm

filter: Statementstmt 96.8% 62 / 64 Branchbran 86.3% 38 / 44 Conditioncond 81.8% 9 / 11 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 91.4% 128 / 140 CC 29 SCAR 18.8
CC
29
Cov
92%
_can_disable
10.0
_handle_start
9.0
_parse
8.1
CRAP 6.6
↑ Summary
1 package HTML::DisableForm;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use base qw/HTML::Parser/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 7 our $VERSION = 0.01;
7  
8 7 my %can_disable = (
9 input => 1,
10 textarea => 1,
11 select => 1,
12 );
13  
14 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
15 5 my $class = shift;
16 5 my $self = $class->SUPER::new(api_version => 3);
17 5 $self->handler(start => \&_handle_start, "self, tagname, attr, text");
18 5 $self->handler(end => \&_handle_end, "self, tagname, text");
19 5 $self->handler(default => \&_handle_default, "self, text");
20 5 $self->attr_encoded(1);
21 5 bless $self, $class;
22 }
23  
24 1 ▶sub readonly_form {
Subroutine readonly_form: called CC=1 SCAR=0.0
Pod documented
25 1 my ($self, %option) = @_;
26 1 $self->disable_form(%option, readonly => 1);
27 }
28  
29 5 ▶sub disable_form {
Subroutine disable_form: called CC=4 SCAR=13.9
Pod documented
30 5 my ($self, %option) = @_;
31 5 ▶ $self->{target} = $option{target} if $option{target};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"target"} 1 4 5
32 5 ▶ $self->{readonly} = 1 if $option{readonly};
Branch: 1 branch
BranchTrueFalseTotal
if $option{"readonly"} 1 4 5
33 5 $self->_set_options_map($_, $option{$_}) for qw/ignore_fields ignore_forms/;
34 5 $self->_parse(%option);
35 5 delete $self->{output};
36 }
37  
38 10 ▶sub _set_options_map {
Subroutine _set_options_map: called CC=3 SCAR=11.0
39 10 my ($self, $name, $value) = @_;
40 10 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 8 2 10
41 2 ▶ my %options_map = map { $_ => 1 } ref $value eq 'ARRAY' ? @$value : $value;
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 1 1 2
42 2 $self->{$name} = \%options_map;
43 }
44  
45 5 ▶sub _parse {
Subroutine _parse: called CC=5 SCAR=20.9
46 5 my $self = shift;
47 5 my %option = @_;
48 5 ▶ if (my $file = $option{file}){
Branch: 3 branches
BranchTrueFalseTotal
if (my $file = $option{'file'}) { } 0 5 5
elsif (my $scalarref = $option{'scalarref'}) { } 5 0 5
elsif (my $arrayref = $option{'arrayref'}) { } 0 0 0
49 0 $self->parse_file($file);
50 } elsif (my $scalarref = $option{scalarref}){
51 5 $self->parse($$scalarref);
52 } elsif (my $arrayref = $option{arrayref}){
53 0 $self->parse($_) for @$arrayref;
54 }
55 }
56  
57 57 ▶sub _can_disable {
Subroutine _can_disable: called CC=10 SCAR=23.1
58 57 my ($self, $tagname, $attr) = @_;
59 57 ▶ $can_disable{$tagname} or return;
Branch: 1 branch
BranchTrueFalseTotal
$can_disable{$tagname} or return 24 33 57
60 24 ▶ return 0 if defined $self->{target} && !$self->{current_form};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"target"} and not $self->{"current_form"} 3 21 24
Condition100%3/3
defined $self->{"target"} and not $self->{"current_form"}
!ll&&!rl&&r
1563
MC/DC100%2/2
defined $self->{"target"} and not $self->{"current_form"}
defined $self->{"target"} not $self->{"current_form"}
Truth table100%3/3
A and not(B)
A= defined $self->{"target"}B= not $self->{"current_form"}
ABresult
0X0
100
111
61 21 ▶ if (my $current_form = $self->{current_form}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $current_form = $self->{"current_form"}) 9 12 21
62 9 ▶ if (defined $self->{target}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"target"}) 6 3 9
63 6 ▶ $current_form eq $self->{target} ? return 1 : return 0;
Branch: 1 branch
BranchTrueFalseTotal
$current_form eq $self->{'target'} ? : 3 3 6
64 }
65 3 ▶ return 0 if $self->{ignore_forms}->{$current_form};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ignore_forms"}{$current_form} 3 0 3
66 }
67 12 ▶ if ($attr->{name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr->{"name"}) 12 0 12
68 12 ▶ return 0 if $self->{ignore_fields}->{$attr->{name}};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"ignore_fields"}{$attr->{"name"}} 2 10 12
69 }
70 10 return 1;
71 }
72  
73 57 ▶sub _handle_start {
Subroutine _handle_start: called CC=9 SCAR=22.0
74 57 my ($self, $tagname, $attr, $text) = @_;
75 57 ▶ $self->{current_form} = $attr->{name} || $attr->{id} || ''
Branch: 1 branch
BranchTrueFalseTotal
if $tagname eq "form" 8 49 57
Condition80%4/5
$attr->{'name'} || $attr->{'id'} || ''
l!l
35
$attr->{"name"} or $attr->{"id"}
l!l&&r!l&&!r
305
MC/DC50%1/2
$attr->{'name'} || $attr->{'id'} || ''
$attr->{"name"} $attr->{"id"}
Truth table66%2/3
A or B
A= $attr->{"name"}B= $attr->{"id"}
ABresult
1X1
011
000
76 if $tagname eq 'form';
77  
78 57 ▶ if ($self->_can_disable($tagname, $attr)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_can_disable($tagname, $attr)) { } 13 44 57
79 13 $self->{output} .= "<$tagname";
80 13 ▶ while (my ($key, $value) = each %$attr) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$attr) 35 13 48
81 35 ▶ next if $key eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "/" 9 26 35
82 26 $self->{output} .= sprintf qq( %s="%s"), $key, $value;
83 }
84 13 ▶ $self->{output} .= $self->{readonly} ? ' readonly="readonly"' : ' disabled="disabled"';
Branch: 1 branch
BranchTrueFalseTotal
$self->{'readonly'} ? : 3 10 13
85 13 ▶ $self->{output} .= ' /' if $attr->{'/'};
Branch: 1 branch
BranchTrueFalseTotal
if $attr->{"/"} 9 4 13
86 13 $self->{output} .= '>';
87 } else {
88 44 $self->{output} .= $text;
89 }
90 }
91  
92 41 ▶sub _handle_end {
Subroutine _handle_end: called CC=3 SCAR=11.0
93 41 my ($self, $tagname, $text) = @_;
94 delete $self->{current_form}
95 41 ▶ if $tagname eq 'form' and exists $self->{current_form};
Branch: 1 branch
BranchTrueFalseTotal
if $tagname eq "form" and exists $self->{"current_form"} 8 33 41
Condition66%2/3
$tagname eq "form" and exists $self->{"current_form"}
!ll&&!rl&&r
3308
MC/DC50%1/2
$tagname eq "form" and exists $self->{"current_form"}
$tagname eq "form" exists $self->{"current_form"}
Truth table66%2/3
A and B
A= $tagname eq "form"B= exists $self->{"current_form"}
ABresult
0X0
100
111
96 41 $self->{output} .= $text;
97 }
98  
99 80 ▶sub _handle_default {
Subroutine _handle_default: called CC=1 SCAR=0.0
100 80 my ($self, $text) = @_;
101 80 $self->{output} .= $text;
102 }
103  
104 7 1;
105  
106 __END__
↑ Summary