Ex/Fill.pm

filter: Statementstmt 95.7% 202 / 211 Branchbran 86.0% 148 / 172 Conditioncond 80.3% 53 / 66 MC/DCmcdc 58.9% 23 / 39 Subroutinesub 100% 9 / 9 Podpod 60.0% 3 / 5 totaltotal 87.2% 438 / 502 CC 114 SCAR 38.6
CC
114
Cov
90%
fill
73.0
__ANON__
28.9
swap_tagval_by_key
10.0
CRAP 47.4
« Ex/Conf.pm ↑ Summary Ex.pm »
1 package CGI::Ex::Fill;
2  
3 =head1 NAME
4  
5 CGI::Ex::Fill - Fast but compliant regex based form filler
6  
7 =head1 VERSION
8  
9 version 2.54
10  
11 =cut
12  
13 ###----------------------------------------------------------------###
14 # Copyright - Paul Seamons #
15 # Distributed under the Perl Artistic License without warranty #
16 ###----------------------------------------------------------------###
17  
18 23 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 23 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 23 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 23 our $VERSION = '2.54'; # VERSION
23 23 our @EXPORT = qw(form_fill);
24 23 our @EXPORT_OK = qw(fill form_fill html_escape get_tagval_by_key swap_tagval_by_key);
25  
26 ### These directives are used to determine whether or not to
27 ### remove html comments and script sections while filling in
28 ### a form. Default is on. This may give some trouble if you
29 ### have a javascript section with form elements that you would
30 ### like filled in.
31 23 our $REMOVE_SCRIPT = 1;
32 23 our $REMOVE_COMMENT = 1;
33 23 our $MARKER_SCRIPT = "\0SCRIPT\0";
34 23 our $MARKER_COMMENT = "\0COMMENT\0";
35 23 our $OBJECT_METHOD = "param";
36 23 our $_TEMP_TARGET;
37  
38 ###----------------------------------------------------------------###
39  
40 ### Regex based filler - as opposed to HTML::Parser based HTML::FillInForm
41 ### arguments are positional
42 ### pos1 - text or textref - if textref it is modified in place
43 ### pos2 - hash or cgi obj ref, or array ref of hash and cgi obj refs
44 ### pos3 - target - to be used for choosing a specific form - default undef
45 ### pos4 - boolean fill in password fields - default is true
46 ### pos5 - hashref or arrayref of fields to ignore
47 sub form_fill {
48 68 ▶ my $text = shift;
Subroutine form_fill: called CC=4 SCAR=13.9
Pod undocumented
49 68 ▶ my $ref = ref($text) ? $text : \$text;
Branch: 1 branch
BranchTrueFalseTotal
ref $text ? : 42 26 68
50 68 my $form = shift;
51 68 my $target = shift;
52 68 my $fill_password = shift;
53 68 ▶ my $ignore = shift || {};
Condition100%2/2
shift() || {}
l!l
365
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
54  
55 68 fill({
56 text => $ref,
57 form => $form,
58 target => $target,
59 fill_password => $fill_password,
60 ignore_fields => $ignore,
61 });
62  
63 68 ▶ return ref($text) ? 1 : $$ref;
Branch: 1 branch
BranchTrueFalseTotal
ref $text ? : 42 26 68
64 }
65  
66 sub fill {
67 142 ▶ my $args = shift;
Subroutine fill: called CC=66 SCAR=42.9
Pod undocumented
68 142 my $ref = $args->{'text'};
69 142 my $form = $args->{'form'};
70 142 my $target = $args->{'target'};
71 142 my $ignore = $args->{'ignore_fields'};
72 142 my $fill_password = $args->{'fill_password'};
73  
74 142 ▶ my $forms = UNIVERSAL::isa($form, 'ARRAY') ? $form : [$form];
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($form, 'ARRAY') ? : 2 140 142
75 142 ▶ $ignore = {map {$_ => 1} @$ignore} if UNIVERSAL::isa($ignore, 'ARRAY');
Branch: 1 branch
BranchTrueFalseTotal
if UNIVERSAL::isa($ignore, "ARRAY") 3 139 142
76 142 ▶ $fill_password = 1 if ! defined $fill_password;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fill_password 139 3 142
77  
78  
79 ### allow for optionally removing comments and script
80 142 my @comment;
81 my @script;
82 142 ▶ if (defined($args->{'remove_script'}) ? $args->{'remove_script'} : $REMOVE_SCRIPT) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args->{"remove_script"} ? $args->{"remove_script"} : $CGI::Ex::Fill::REMOVE_SCRIPT) 139 3 142
defined $args->{'remove_script'} ? : 3 139 142
83 139 $$ref =~ s|(<script\b.+?</script>)|push(@script, $1);$MARKER_SCRIPT|egi;
84 }
85 142 ▶ if (defined($args->{'remove_comment'}) ? $args->{'remove_comment'} : $REMOVE_COMMENT) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $args->{"remove_comment"} ? $args->{"remove_comment"} : $CGI::Ex::Fill::REMOVE_COMMENT) 139 3 142
defined $args->{'remove_comment'} ? : 3 139 142
86 139 $$ref =~ s|(<!--.*?-->)|push(@comment, $1);$MARKER_COMMENT|eg;
87 }
88  
89 ### if there is a target - focus in on it
90 ### possible bug here - name won't be found if
91 ### there is nested html inside the form tag that comes before
92 ### the name field - if no close form tag - don't swap in anything
93 142 ▶ if ($target) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target) 3 139 142
94 3 local $_TEMP_TARGET = $target;
95 3 $$ref =~ s{(<form # open form
96 [^>]+ # some space
97 \bname=([\"\']?) # the name tag
98 $target # with the correct name (allows for regex)
99 \2 # closing quote
100 .+? # as much as there is
101 (?=</form>)) # then end
102 }{
103 3 my $str = $1;
104 3 local $args->{'text'} = \$str;
105 3 local $args->{'remove_script'} = 0;
106 3 local $args->{'remove_comment'} = 0;
107 3 local $args->{'target'} = undef;
108 3 fill($args);
109 3 $str; # return of the s///;
110 }sigex;
111  
112 ### put scripts and comments back and return
113 3 ▶ $$ref =~ s/$MARKER_COMMENT/shift(@comment)/eg if $#comment != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $#comment != -1 0 3 3
114 3 ▶ $$ref =~ s/$MARKER_SCRIPT/ shift(@script) /eg if $#script != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $#script != -1 0 3 3
115 3 return 1;
116 }
117  
118 ### build a sub to get a value from the passed forms on a request basis
119 139 my %indexes = (); # store indexes for multivalued elements
120 my $get_form_value = sub {
121 138 ▶ my $key = shift;
Subroutine __ANON__: called CC=26 SCAR=33.6
122 138 ▶ my $all = $_[0] && $_[0] eq 'all';
Condition66%2/3
$_[0] && $_[0] eq 'all'
!ll&&!rl&&r
08751
MC/DC50%1/2
$_[0] && $_[0] eq 'all'
$_[0] $_[0] eq 'all'
Truth table66%2/3
A && B
A= $_[0]B= $_[0] eq 'all'
ABresult
0X0
100
111
123 138 ▶ if (! defined $key || ! length $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key and length $key) 0 138 138
Condition33%1/3
defined $key and length $key
!ll&&!rl&&r
00138
MC/DC0%0/2
defined $key and length $key
defined $key length $key
Truth table33%1/3
A and B
A= defined $keyB= length $key
ABresult
0X0
100
111
124 0 ▶ return $all ? [] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$all ? : 0 0 0
125 }
126  
127 138 my $val;
128 my $meth;
129 138 foreach my $form (@$forms) {
130 140 ▶ next if ! ref $form;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $form 0 140 140
131 140 ▶ if (UNIVERSAL::isa($form, 'HASH') && defined $form->{$key}) {
Branch: 3 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($form, 'HASH') and defined $form->{$key}) { } 118 22 140
elsif ($meth = UNIVERSAL::can($form, $args->{'object_method'} || $CGI::Ex::Fill::OBJECT_METHOD)) { } 3 19 22
elsif (UNIVERSAL::isa($form, 'CODE')) { } 1 18 19
Condition100%3/3
UNIVERSAL::isa($form, "HASH") and defined $form->{$key}
!ll&&!rl&&r
121118
MC/DC100%2/2
UNIVERSAL::isa($form, "HASH") and defined $form->{$key}
UNIVERSAL::isa($form, "HASH") defined $form->{$key}
Truth table100%3/3
A and B
A= UNIVERSAL::isa($form, "HASH")B= defined $form->{$key}
ABresult
0X0
100
111
132 118 $val = $form->{$key};
133 118 last;
134 ▶ } elsif ($meth = UNIVERSAL::can($form, $args->{'object_method'} || $OBJECT_METHOD)) {
Condition33%1/3
$args->{'object_method'} || $CGI::Ex::Fill::OBJECT_METHOD
l!l&&r!l&&!r
0220
MC/DC0%0/2
$args->{'object_method'} || $CGI::Ex::Fill::OBJECT_METHOD
$args->{'object_method'} $CGI::Ex::Fill::OBJECT_METHOD
Truth table33%1/3
A || B
A= $args->{'object_method'}B= $CGI::Ex::Fill::OBJECT_METHOD
ABresult
1X1
011
000
135 3 $val = $form->$meth($key);
136 3 ▶ last if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 2 1 3
137 } elsif (UNIVERSAL::isa($form, 'CODE')) {
138 1 $val = $form->($key, $_TEMP_TARGET);
139 1 ▶ last if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 1 0 1
140 }
141 }
142 138 ▶ if (! defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 17 121 138
143 17 ▶ return $all ? [] : undef;
Branch: 1 branch
BranchTrueFalseTotal
$all ? : 11 6 17
144 }
145  
146 ### fix up the value some
147 121 ▶ if (UNIVERSAL::isa($val, 'CODE')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($val, "CODE")) 1 120 121
148 1 $val = $val->($key, $_TEMP_TARGET);
149 }
150 121 ▶ if (UNIVERSAL::isa($val, 'ARRAY')) {
Branch: 2 branches
BranchTrueFalseTotal
if (UNIVERSAL::isa($val, 'ARRAY')) { } 26 95 121
elsif (ref $val) { } 0 95 95
151 26 $val = [@$val]; # copy the values
152 } elsif (ref $val) {
153 # die "Value for $key is not an array or a scalar";
154 0 $val = "$val"; # stringify anything else
155 }
156  
157 ### html escape them all
158 121 ▶ html_escape(\$_) foreach (ref($val) ? @$val : $val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 26 95 121
159  
160 ### allow for returning all elements
161 ### or one at a time
162 121 ▶ if ($all) {
Branch: 2 branches
BranchTrueFalseTotal
if ($all) { } 40 81 121
elsif (ref $val) { } 16 65 81
163 40 ▶ return ref($val) ? $val : [$val];
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 10 30 40
164 } elsif (ref($val)) {
165 16 ▶ $indexes{$key} ||= 0;
Condition100%2/2
$indexes{$key} ||= 0
l!l
97
MC/DC100%1/1
$indexes{$key} ||= 0
$indexes{$key}
Truth table100%2/2
A
A= $indexes{$key}
Aresult
11
00
166 16 my $ret = $val->[$indexes{$key}];
167 16 ▶ $ret = '' if ! defined $ret;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ret 5 11 16
168 16 $indexes{$key} ++; # don't wrap - if we run out of values - we're done
169 16 return $ret;
170 } else {
171 65 return $val;
172 }
173 139 };
174  
175  
176 ###--------------------------------------------------------------###
177  
178 ### First pass
179 ### swap <input > form elements if they have a name
180 139 $$ref =~ s{
181 (<input \s (?: ([\"\'])(?:|.*?[^\\])\2 | [^>] )+ >) # nested html ok
182 }{
183 ### get the type and name - intentionally exlude names with nested "'
184 119 my $tag = $1;
185 119 ▶ my $type = uc(get_tagval_by_key(\$tag, 'type') || '');
Condition100%2/2
get_tagval_by_key(\$tag, 'type') || ''
l!l
9425
MC/DC100%1/1
get_tagval_by_key(\$tag, 'type') || ''
get_tagval_by_key(\$tag, 'type')
Truth table100%2/2
A
A= get_tagval_by_key(\$tag, 'type')
Aresult
11
00
186 119 my $name = get_tagval_by_key(\$tag, 'name');
187  
188 119 ▶ if ($name && ! $ignore->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name and not $ignore->{$name}) 115 4 119
Condition100%3/3
$name and not $ignore->{$name}
!ll&&!rl&&r
22115
MC/DC100%2/2
$name and not $ignore->{$name}
$name not $ignore->{$name}
Truth table100%3/3
A and not(B)
A= $nameB= not $ignore->{$name}
ABresult
0X0
100
111
189 115 ▶ if (! $type
Branch: 2 branches
BranchTrueFalseTotal
if (not $type or $type ne 'PASSWORD' and $type ne 'CHECKBOX' and $type ne 'RADIO' or $type eq 'PASSWORD' and $fill_password) { } 78 37 115
elsif ($type eq 'CHECKBOX' or $type eq 'RADIO') { } 35 2 37
Condition100%15/15
not $type or $type ne "PASSWORD" and $type ne "CHECKBOX" and $type ne "RADIO" or $type eq "PASSWORD" and $fill_password
l!l&&r!l&&!r
73537
not $type or $type ne "PASSWORD" and $type ne "CHECKBOX" and $type ne "RADIO"
l!l&&r!l&&!r
254842
$type ne "PASSWORD" and $type ne "CHECKBOX" and $type ne "RADIO"
!ll&&!rl&&r
311148
$type ne "PASSWORD" and $type ne "CHECKBOX"
!ll&&!rl&&r
72459
$type eq "PASSWORD" and $fill_password
!ll&&!rl&&r
3525
MC/DC83%5/6
not $type or $type ne "PASSWORD" and $type ne "CHECKBOX" and $type ne "RADIO" or $type eq "PASSWORD" and $fill_password
not $type $type ne "PASSWORD" $type ne "CHECKBOX" $type ne "RADIO" $type eq "PASSWORD" $fill_password
Truth table54%6/11
not(A) or B and C and D or E and F
A= not $typeB= $type ne "PASSWORD"C= $type ne "CHECKBOX"D= $type ne "RADIO"E= $type eq "PASSWORD"F= $fill_password
ABCDEFresult
1XXXXX1
0111XX1
00XX111
010X111
0110111
00XX0X0
00XX100
010X0X0
010X100
01100X0
0110100
190 || ($type ne 'PASSWORD' && $type ne 'CHECKBOX' && $type ne 'RADIO')
191 || ($type eq 'PASSWORD' && $fill_password)) {
192  
193 78 my $value = $get_form_value->($name, 'next');
194 78 ▶ if (defined $value) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $value) { } 73 5 78
elsif (not defined get_tagval_by_key(\$tag, 'value')) { } 2 3 5
195 73 swap_tagval_by_key(\$tag, 'value', $value);
196 } elsif (! defined get_tagval_by_key(\$tag, 'value')) {
197 2 swap_tagval_by_key(\$tag, 'value', '');
198 }
199  
200 ▶ } elsif ($type eq 'CHECKBOX'
Condition100%3/3
$type eq "CHECKBOX" or $type eq "RADIO"
l!l&&r!l&&!r
24112
MC/DC100%2/2
$type eq "CHECKBOX" or $type eq "RADIO"
$type eq "CHECKBOX" $type eq "RADIO"
Truth table100%3/3
A or B
A= $type eq "CHECKBOX"B= $type eq "RADIO"
ABresult
1X1
011
000
201 || $type eq 'RADIO') {
202 35 my $values = $get_form_value->($name, 'all');
203 35 ▶ if (@$values) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$values) 29 6 35
204 29 $tag =~ s{\s+\bCHECKED\b(?:=([\"\']?)checked\1)?(?=\s|>|/>)}{}ig;
205  
206 29 my $fvalue = get_tagval_by_key(\$tag, 'value');
207 29 ▶ $fvalue = 'on' if ! defined $fvalue;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fvalue 5 24 29
208 29 ▶ if (defined $fvalue) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fvalue) 29 0 29
209 29 foreach (@$values) {
210 35 ▶ next if $_ ne $fvalue;
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne $fvalue 24 11 35
211 11 $tag =~ s|(\s*/?>\s*)$| checked="checked"$1|;
212 11 last;
213 }
214 }
215 }
216 }
217  
218 }
219 119 $tag; # return of swap
220 }sigex;
221  
222  
223 ### Second pass
224 ### swap select boxes (must be done in such a way as to allow no closing tag)
225 139 my @start = ();
226 139 my @close = ();
227 139 ▶ push @start, pos($$ref) - length($1) while $$ref =~ m|(<\s*select\b)|ig;
Branch: 1 branch
BranchTrueFalseTotal
$$ref =~ /(<\s*select\b)/gi and do { push @start, pos($$ref) - length($1) } 18 139 157
228 139 ▶ push @close, pos($$ref) - length($1) while $$ref =~ m|(</\s*select\b)|ig;
Branch: 1 branch
BranchTrueFalseTotal
$$ref =~ m[(</\s*select\b)]gi and do { push @close, pos($$ref) - length($1) } 17 139 156
229 139 ▶ for (my $i = 0; $i <= $#start; $i ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#start) 18 139 157
230 18 ▶ while (defined($close[$i]) && $close[$i] < $start[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $close[$i] and $close[$i] < $start[$i]) 0 18 18
Condition66%2/3
defined $close[$i] and $close[$i] < $start[$i]
!ll&&!rl&&r
1170
MC/DC0%0/2
defined $close[$i] and $close[$i] < $start[$i]
defined $close[$i] $close[$i] < $start[$i]
Truth table66%2/3
A and B
A= defined $close[$i]B= $close[$i] < $start[$i]
ABresult
0X0
100
111
231 0 splice (@close,$i,1,());
232 }
233 18 ▶ if ($i == $#start) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i == $#start) { } 8 10 18
elsif (not defined $close[$i] or $close[$i] > $start[$i + 1]) { } 1 9 10
234 8 ▶ $close[$i] = length($$ref) if ! defined $close[$i]; # set to end of string if no closing
Branch: 1 branch
BranchTrueFalseTotal
unless defined $close[$i] 1 7 8
235 ▶ } elsif (! defined($close[$i]) || $close[$i] > $start[$i + 1]) {
Condition66%2/3
not defined $close[$i] or $close[$i] > $start[$i + 1]
l!l&&r!l&&!r
019
MC/DC50%1/2
not defined $close[$i] or $close[$i] > $start[$i + 1]
not defined $close[$i] $close[$i] > $start[$i + 1]
Truth table66%2/3
not(A) or B
A= not defined $close[$i]B= $close[$i] > $start[$i + 1]
ABresult
1X1
011
000
236 1 $close[$i] = $start[$i + 1]; # set to start of next select if no closing or > next select
237 }
238 }
239 139 ▶ for (my $i = $#start; $i >= 0; $i --) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 18 139 157
240 18 my $opts = substr($$ref, $start[$i], $close[$i] - $start[$i]);
241 18 ▶ $opts =~ s{
Branch: 1 branch
BranchTrueFalseTotal
$opts =~ s/ (<select \s # opening (?: "" | '' | ([\"\']).*?[^\\]\2 | [^>] )+ # nested html ok >) # end of tag //six or next 0 18 18
242 (<select \s # opening
243 (?: "" | '' | ([\"\']).*?[^\\]\2 | [^>] )+ # nested html ok
244 >) # end of tag
245 }{}sxi || next;
246 18 ▶ next if ! $opts;
Branch: 1 branch
BranchTrueFalseTotal
unless $opts 0 18 18
247 18 my $tag = $1;
248 18 my $name = get_tagval_by_key(\$tag, 'name');
249 18 ▶ next if ! defined($name) || ! length($name);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 1 17 18
Condition66%2/3
defined $name and length $name
!ll&&!rl&&r
1017
MC/DC50%1/2
defined $name and length $name
defined $name length $name
Truth table66%2/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
250 17 ▶ my $values = $ignore->{$name} ? [] : $get_form_value->($name, 'all');
Branch: 1 branch
BranchTrueFalseTotal
$ignore->{$name} ? : 1 16 17
251 17 ▶ if ($#$values != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($values->$#* != -1) 11 6 17
252 11 my $n = $opts =~ s{
253 (<option[^>]*>) # opening tag - no embedded > allowed
254 (.*?) # the text value
255 (?=<option|$|</option>) # the next tag
256 }{
257 31 my ($tag2, $opt) = ($1, $2);
258 31 $tag2 =~ s%\s+\bSELECTED\b(?:=([\"\']?)selected\1)?(?=\s|>|/>)%%ig;
259  
260 31 my $fvalues = get_tagval_by_key(\$tag2, 'value', 'all');
261 31 ▶ my $fvalue = @$fvalues ? $fvalues->[0]
Branch: 2 branches
BranchTrueFalseTotal
@$fvalues ? : 18 13 31
$opt =~ /^\s*(.*?)\s*$/ ? : 13 0 13
262 : $opt =~ /^\s*(.*?)\s*$/ ? $1 : "";
263 31 foreach (@$values) {
264 41 ▶ next if $_ ne $fvalue;
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne $fvalue 29 12 41
265 12 $tag2 =~ s|(\s*/?>\s*)$| selected="selected"$1|;
266 12 last;
267 }
268 31 "$tag2$opt"; # return of the swap
269 }sigex;
270 11 ▶ if ($n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n) 11 0 11
271 11 substr($$ref, $start[$i], $close[$i] - $start[$i], "$tag$opts");
272 }
273 }
274 }
275  
276  
277 ### Third pass
278 ### swap textareas (must be done in such a way as to allow no closing tag)
279 139 @start = ();
280 139 @close = ();
281 139 ▶ push @start, pos($$ref) - length($1) while $$ref =~ m|(<\s*textarea\b)|ig;
Branch: 1 branch
BranchTrueFalseTotal
$$ref =~ /(<\s*textarea\b)/gi and do { push @start, pos($$ref) - length($1) } 9 139 148
282 139 ▶ push @close, pos($$ref) - length($1) while $$ref =~ m|(</\s*textarea\b)|ig;
Branch: 1 branch
BranchTrueFalseTotal
$$ref =~ m[(</\s*textarea\b)]gi and do { push @close, pos($$ref) - length($1) } 7 139 146
283 139 ▶ for (my $i = 0; $i <= $#start; $i ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#start) 9 139 148
284 9 ▶ while (defined($close[$i]) && $close[$i] < $start[$i]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $close[$i] and $close[$i] < $start[$i]) 0 9 9
Condition66%2/3
defined $close[$i] and $close[$i] < $start[$i]
!ll&&!rl&&r
180
MC/DC0%0/2
defined $close[$i] and $close[$i] < $start[$i]
defined $close[$i] $close[$i] < $start[$i]
Truth table66%2/3
A and B
A= defined $close[$i]B= $close[$i] < $start[$i]
ABresult
0X0
100
111
285 0 splice (@close,$i,1,()); # get rid of extra closes
286 }
287 9 ▶ if ($i == $#start) {
Branch: 2 branches
BranchTrueFalseTotal
if ($i == $#start) { } 4 5 9
elsif (not defined $close[$i] or $close[$i] > $start[$i + 1]) { } 1 4 5
288 4 ▶ $close[$i] = length($$ref) if ! defined $close[$i]; # set to end of string if no closing
Branch: 1 branch
BranchTrueFalseTotal
unless defined $close[$i] 1 3 4
289 ▶ } elsif (! defined($close[$i]) || $close[$i] > $start[$i + 1]) {
Condition66%2/3
not defined $close[$i] or $close[$i] > $start[$i + 1]
l!l&&r!l&&!r
014
MC/DC50%1/2
not defined $close[$i] or $close[$i] > $start[$i + 1]
not defined $close[$i] $close[$i] > $start[$i + 1]
Truth table66%2/3
not(A) or B
A= not defined $close[$i]B= $close[$i] > $start[$i + 1]
ABresult
1X1
011
000
290 1 splice(@close, $i, 0, $start[$i + 1]); # set to start of next select if no closing or > next select
291 }
292 }
293 139 my $offset = 0;
294 139 ▶ for (my $i = 0; $i <= $#start; $i ++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $#start) 9 139 148
295 9 my $oldval = substr($$ref, $start[$i] + $offset, $close[$i] - $start[$i]);
296 9 ▶ $oldval =~ s{
Branch: 1 branch
BranchTrueFalseTotal
$oldval =~ s/ (<textarea \s # opening (?: "" | '' | ([\"\']).*?[^\\]\2 | [^>] )+ # nested html ok >) # end of tag //six or next 0 9 9
297 (<textarea \s # opening
298 (?: "" | '' | ([\"\']).*?[^\\]\2 | [^>] )+ # nested html ok
299 >) # end of tag
300 }{}sxi || next;
301 9 my $tag = $1;
302 9 my $name = get_tagval_by_key(\$tag, 'name');
303 9 ▶ if ($name && ! $ignore->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name and not $ignore->{$name}) 9 0 9
Condition33%1/3
$name and not $ignore->{$name}
!ll&&!rl&&r
009
MC/DC0%0/2
$name and not $ignore->{$name}
$name not $ignore->{$name}
Truth table33%1/3
A and not(B)
A= $nameB= not $ignore->{$name}
ABresult
0X0
100
111
304 9 my $value = $get_form_value->($name, 'next');
305 9 ▶ next if ! defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 8 9
306 8 substr($$ref, $start[$i] + $offset, $close[$i] - $start[$i], "$tag$value");
307 8 $offset += length($value) - length($oldval);
308 }
309 }
310  
311 ### put scripts and comments back and return
312 139 ▶ $$ref =~ s/$MARKER_COMMENT/shift(@comment)/eg if $#comment != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $#comment != -1 0 139 139
313 139 ▶ $$ref =~ s/$MARKER_SCRIPT/ shift(@script) /eg if $#script != -1;
Branch: 1 branch
BranchTrueFalseTotal
if $#script != -1 1 138 139
314 139 return 1;
315 }
316  
317  
318 ### yet another html escaper
319 ### allow pass by value or by reference (reference is modified inplace)
320 sub html_escape {
321 152 ▶ my $str = shift;
Subroutine html_escape: called CC=4 SCAR=14.2
Pod documented
322 152 ▶ return $str if ! $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str 0 152 152
323 152 ▶ my $ref = ref($str) ? $str : \$str;
Branch: 1 branch
BranchTrueFalseTotal
ref $str ? : 152 0 152
324  
325 152 $$ref =~ s/&/&amp;/g;
326 152 $$ref =~ s/</&lt;/g;
327 152 $$ref =~ s/>/&gt;/g;
328 152 $$ref =~ s/\"/&quot;/g;
329  
330 152 ▶ return ref($str) ? 1 : $$ref;
Branch: 1 branch
BranchTrueFalseTotal
ref $str ? : 152 0 152
331 }
332  
333 ### get a named value for key="value" pairs
334 ### usage: my $val = get_tagval_by_key(\$tag, $key);
335 ### usage: my $valsref = get_tagval_by_key(\$tag, $key, 'all');
336 sub get_tagval_by_key {
337 330 ▶ my $tag = shift;
Subroutine get_tagval_by_key: called CC=9 SCAR=22.0
Pod documented
338 330 ▶ my $ref = ref($tag) ? $tag : \$tag;
Branch: 1 branch
BranchTrueFalseTotal
ref $tag ? : 330 0 330
339 330 my $key = lc(shift);
340 330 ▶ my $all = $_[0] && $_[0] eq 'all';
Condition66%2/3
$_[0] && $_[0] eq 'all'
!ll&&!rl&&r
299031
MC/DC50%1/2
$_[0] && $_[0] eq 'all'
$_[0] $_[0] eq 'all'
Truth table66%2/3
A && B
A= $_[0]B= $_[0] eq 'all'
ABresult
0X0
100
111
341 330 my @all = ();
342 330 pos($$ref) = 0; # fix for regex below not resetting and forcing order on key value pairs
343  
344 ### loop looking for tag pairs
345 330 ▶ while ($$ref =~ m{
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref =~ m( (?<![\w\.\-]) # 0 - not proceded by letter or . ([\w\.\-]+) # 1 - the key \s*= # equals (?: \s*([\"\'])(|.*?[^\\])\2 # 2 - a quote, 3 - the quoted | ([^\s/]*? (?=\s|>|/>)) # 4 - a non-quoted string ) )gisx) 502 66 568
346 (?<![\w\.\-]) # 0 - not proceded by letter or .
347 ([\w\.\-]+) # 1 - the key
348 \s*= # equals
349 (?: \s*([\"\'])(|.*?[^\\])\2 # 2 - a quote, 3 - the quoted
350 | ([^\s/]*? (?=\s|>|/>)) # 4 - a non-quoted string
351 )
352 }sigx) {
353 502 ▶ next if lc($1) ne $key;
Branch: 1 branch
BranchTrueFalseTotal
if lc $1 ne $key 220 282 502
354 282 ▶ my ($val,$quot) = ($2) ? ($3,$2) : ($4,undef);
Branch: 1 branch
BranchTrueFalseTotal
$2 ? : 193 89 282
355 282 ▶ $val =~ s/\\$quot/$quot/ if $quot;
Branch: 1 branch
BranchTrueFalseTotal
if $quot 193 89 282
356 282 ▶ return $val if ! $all;
Branch: 1 branch
BranchTrueFalseTotal
unless $all 264 18 282
357 18 push @all, $val;
358 }
359 66 ▶ return undef if ! $all;
Branch: 1 branch
BranchTrueFalseTotal
unless $all 35 31 66
360 31 return \@all;
361 }
362  
363 ### swap out values for key="value" pairs
364 ### usage: my $count = &swap_tagval_by_key(\$tag, $key, $val);
365 ### usage: my $newtag = &swap_tagval_by_key($tag, $key, $val);
366 sub swap_tagval_by_key {
367 75 ▶ my $tag = shift;
Subroutine swap_tagval_by_key: called CC=10 SCAR=23.0
Pod documented
368 75 ▶ my $ref = ref($tag) ? $tag : \$tag;
Branch: 1 branch
BranchTrueFalseTotal
ref $tag ? : 75 0 75
369 75 my $key = lc(shift);
370 75 my $val = shift;
371 75 my $n = 0;
372  
373 ### swap a key/val pair at time
374 75 $$ref =~ s{(^\s*<\s*\w+\s+ | \G\s+) # 1 - open tag or previous position
375 ( ([\w\-\.]+) # 2 - group, 3 - the key
376 (\s*=) # 4 - equals
377 (?: \s* ([\"\']) (?:|.*?[^\\]) \5 # 5 - the quote mark, the quoted
378 | [^\s/]*? (?=\s|>|/>) # a non-quoted string (may be zero length)
379 )
380 | ([^\s/]+?) (?=\s|>|/>) # 6 - a non keyvalue chunk (CHECKED)
381 )
382 }{
383 181 ▶ if (defined($3) && lc($3) eq $key) { # has matching key value pair
Branch: 2 branches
BranchTrueFalseTotal
if (defined $3 and lc $3 eq $key) { } 48 133 181
elsif (defined $6 and lc $6 eq $key) { } 6 127 133
Condition100%3/3
defined $3 and lc $3 eq $key
!ll&&!rl&&r
712648
MC/DC100%2/2
defined $3 and lc $3 eq $key
defined $3 lc $3 eq $key
Truth table100%3/3
A and B
A= defined $3B= lc $3 eq $key
ABresult
0X0
100
111
384 48 ▶ if (! $n ++) { # only put value back on first match
Branch: 1 branch
BranchTrueFalseTotal
if (not $n++) { } 47 1 48
385 47 "$1$3$4\"$val\""; # always double quote
386 } else {
387 1 $1; # second match
388 }
389 ▶ } elsif (defined($6) && lc($6) eq $key) { # has matching key
Condition100%3/3
defined $6 and lc $6 eq $key
!ll&&!rl&&r
12616
MC/DC100%2/2
defined $6 and lc $6 eq $key
defined $6 lc $6 eq $key
Truth table100%3/3
A and B
A= defined $6B= lc $6 eq $key
ABresult
0X0
100
111
390 6 ▶ if (! $n ++) { # only put value back on first match
Branch: 1 branch
BranchTrueFalseTotal
if (not $n++) { } 5 1 6
391 5 "$1$6=\"$val\"";
392 } else {
393 1 $1; # second match
394 }
395 } else {
396 127 "$1$2"; # non-keyval
397 }
398 }sigex;
399  
400 ### append value on if none were swapped
401 75 ▶ if (! $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 23 52 75
402 23 $$ref =~ s|(\s*/?>\s*)$| value="$val"$1|;
403 23 $n = -1;
404 }
405  
406 75 ▶ return ref($tag) ? $n : $$ref;
Branch: 1 branch
BranchTrueFalseTotal
ref $tag ? : 75 0 75
407 }
408  
409 23 1;
410  
411 __END__
« Ex/Conf.pm ↑ Summary Ex.pm »