Spiderpup/Transform.pm

filter: Statementstmt 99.5% 209 / 210 Branchbran 88.2% 113 / 128 Conditioncond 70.3% 45 / 64 MC/DCmcdc 47.2% 17 / 36 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 5 totaltotal 87.0% 396 / 455 CC 92 SCAR 30.6
CC
92
Cov
91%
transform_dollar_vars
33.6
parse_html
26.4
add_implicit_this
15.2
CRAP 21.3
« Spiderpup/SFC.pm ↑ Summary
1 package Yote::Spiderpup::Transform;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.06';
8  
9 2 our @EXPORT_OK = qw(
10 transform_dollar_vars
11 transform_expression
12 extract_arrow_params
13 add_implicit_this
14 parse_html
15 );
16  
17 # Transform $var syntax to this.get_var() / this.set_var()
18 58 ▶sub transform_dollar_vars {
Subroutine transform_dollar_vars: called CC=33 SCAR=35.1
Pod undocumented
19 58 my ($expr) = @_;
20 58 ▶ return $expr unless defined $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 0 58 58
21  
22 # Track positions to avoid transforming inside strings
23 58 my @protected_ranges;
24  
25 # Find string literals (both single and double quoted)
26 # Handle escaped quotes properly: match \\ (escaped backslash) or \' \" (escaped quote) or any non-quote char
27 58 ▶ while ($expr =~ /("(?:[^"\\]|\\.)*")/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /("(?:[^"\\]|\\.)*")/g) 11 58 69
28 11 push @protected_ranges, [$-[0], $+[0]];
29 }
30 58 ▶ while ($expr =~ /('(?:[^'\\]|\\.)*')/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /('(?:[^'\\]|\\.)*')/g) 6 58 64
31 6 push @protected_ranges, [$-[0], $+[0]];
32 }
33 # Find template literals (handle escaped backticks)
34 58 ▶ while ($expr =~ /(`(?:[^`\\]|\\.)*`)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /(`(?:[^`\\]|\\.)*`)/g) 8 58 66
35 8 push @protected_ranges, [$-[0], $+[0]];
36 }
37  
38 42 ▶ my $is_protected = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
39 42 my ($pos) = @_;
40 42 for my $range (@protected_ranges) {
41 22 ▶ return 1 if $pos >= $range->[0] && $pos < $range->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= $range->[0] and $pos < $range->[1] 8 14 22
Condition100%3/3
$pos >= $range->[0] and $pos < $range->[1]
!ll&&!rl&&r
778
MC/DC100%2/2
$pos >= $range->[0] and $pos < $range->[1]
$pos >= $range->[0] $pos < $range->[1]
Truth table100%3/3
A and B
A= $pos >= $range->[0]B= $pos < $range->[1]
ABresult
0X0
100
111
42 }
43 34 return 0;
44 58 };
45  
46 # Process assignments: $var = value (but not == or ===)
47 # Need to handle complex RHS with nested $vars
48 58 my $result = '';
49 58 my $pos = 0;
50  
51 58 ▶ while ($expr =~ /\$(\w+)\s*=(?!=)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\$(\w+)\s*=(?!=)/g) 9 58 67
52 9 my $var_name = $1;
53 9 my $match_start = $-[0];
54 9 my $match_end = $+[0];
55  
56 9 ▶ if (!$is_protected->($match_start)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$is_protected($match_start)) 9 0 9
57 # Find the end of the assignment (semicolon, comma, or closing paren/bracket at depth 0)
58 9 my $rhs_start = $match_end;
59 9 my $depth = 0;
60 9 my $rhs_end = length($expr);
61 9 my $in_string = '';
62  
63 9 for my $i ($rhs_start .. length($expr) - 1) {
64 97 my $char = substr($expr, $i, 1);
65 97 ▶ my $prev = $i > 0 ? substr($expr, $i - 1, 1) : '';
Branch: 1 branch
BranchTrueFalseTotal
$i > 0 ? : 97 0 97
66  
67 # Track string state (skip if escaped)
68 97 ▶ if (!$in_string && $prev ne '\\') {
Branch: 2 branches
BranchTrueFalseTotal
if (not $in_string and $prev ne '\\') { } 81 16 97
elsif ($in_string and $char eq $in_string and $prev ne '\\') { } 1 28 29
Condition66%2/3
not $in_string and $prev ne "\\"
!ll&&!rl&&r
16081
MC/DC50%1/2
not $in_string and $prev ne "\\"
not $in_string $prev ne "\\"
Truth table66%2/3
not(A) and B
A= not $in_stringB= $prev ne "\\"
ABresult
0X0
100
111
69 81 ▶ if ($char eq '"' || $char eq "'" || $char eq '`') {
Branch: 1 branch
BranchTrueFalseTotal
if ($char eq "\"" or $char eq "'" or $char eq "`") 1 80 81
Condition66%4/6
$char eq "\"" or $char eq "'" or $char eq "`"
l!l&&r!l&&!r
1080
$char eq "\"" or $char eq "'"
l!l&&r!l&&!r
1080
MC/DC33%1/3
$char eq "\"" or $char eq "'" or $char eq "`"
$char eq "\"" $char eq "'" $char eq "`"
Truth table50%2/4
A or B or C
A= $char eq "\""B= $char eq "'"C= $char eq "`"
ABCresult
1XX1
01X1
0011
0000
70 1 $in_string = $char;
71 1 next;
72 }
73 ▶ } elsif ($in_string && $char eq $in_string && $prev ne '\\') {
Condition83%5/6
$in_string and $char eq $in_string and $prev ne "\\"
!ll&&!rl&&r
1321
$in_string and $char eq $in_string
!ll&&!rl&&r
0133
MC/DC66%2/3
$in_string and $char eq $in_string and $prev ne "\\"
$in_string $char eq $in_string $prev ne "\\"
Truth table75%3/4
A and B and C
A= $in_stringB= $char eq $in_stringC= $prev ne "\\"
ABCresult
0XX0
10X0
1100
1111
74 1 $in_string = '';
75 1 next;
76 }
77  
78 95 ▶ next if $in_string;
Branch: 1 branch
BranchTrueFalseTotal
if $in_string 15 80 95
79  
80 80 ▶ if ($char eq '(' || $char eq '[' || $char eq '{') {
Branch: 3 branches
BranchTrueFalseTotal
if ($char eq '(' or $char eq '[' or $char eq '{') { } 2 78 80
elsif ($char eq ')' or $char eq ']' or $char eq '}') { } 5 73 78
elsif ($char eq ';' || $char eq ',' and $depth == 0) { } 2 142 144
Condition66%4/6
$char eq "(" or $char eq "[" or $char eq "{"
l!l&&r!l&&!r
2078
$char eq "(" or $char eq "["
l!l&&r!l&&!r
2078
MC/DC33%1/3
$char eq "(" or $char eq "[" or $char eq "{"
$char eq "(" $char eq "[" $char eq "{"
Truth table50%2/4
A or B or C
A= $char eq "("B= $char eq "["C= $char eq "{"
ABCresult
1XX1
01X1
0011
0000
81 2 $depth++;
82 ▶ } elsif ($char eq ')' || $char eq ']' || $char eq '}') {
Condition83%5/6
$char eq ")" or $char eq "]" or $char eq "}"
l!l&&r!l&&!r
3273
$char eq ")" or $char eq "]"
l!l&&r!l&&!r
3075
MC/DC66%2/3
$char eq ")" or $char eq "]" or $char eq "}"
$char eq ")" $char eq "]" $char eq "}"
Truth table75%3/4
A or B or C
A= $char eq ")"B= $char eq "]"C= $char eq "}"
ABCresult
1XX1
01X1
0011
0000
83 5 ▶ if ($depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($depth == 0) 3 2 5
84 3 $rhs_end = $i;
85 3 last;
86 }
87 2 $depth--;
88 ▶ } elsif (($char eq ';' || $char eq ',') && $depth == 0) {
Condition66%4/6
$char eq ";" || $char eq "," and $depth == 0
!ll&&!rl&&r
7102
$char eq ";" or $char eq ","
l!l&&r!l&&!r
2071
MC/DC33%1/3
$char eq ";" || $char eq "," and $depth == 0
$char eq ";" $char eq "," $depth == 0
Truth table40%2/5
A or B and C
A= $char eq ";"B= $char eq ","C= $depth == 0
ABCresult
00X0
1X00
0100
1X11
0111
89 2 $rhs_end = $i;
90 2 last;
91 }
92 }
93  
94 9 my $rhs = substr($expr, $rhs_start, $rhs_end - $rhs_start);
95 # Trim leading whitespace from RHS
96 9 $rhs =~ s/^\s+//;
97 # Recursively transform $vars in RHS
98 9 $rhs = transform_dollar_vars($rhs);
99  
100 9 $result .= substr($expr, $pos, $match_start - $pos);
101 9 $result .= "this.set_$var_name($rhs)";
102 9 $pos = $rhs_end;
103  
104 # Reset regex position
105 9 pos($expr) = $pos;
106 }
107 }
108 58 $result .= substr($expr, $pos);
109 58 $expr = $result;
110  
111 # Process reads: $var (not followed by =)
112 # Need to be careful not to transform inside strings
113 58 $result = '';
114 58 $pos = 0;
115 58 @protected_ranges = (); # Recalculate for the modified expression
116  
117 58 ▶ while ($expr =~ /("(?:[^"\\]|\\.)*")/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /("(?:[^"\\]|\\.)*")/g) 11 58 69
118 11 push @protected_ranges, [$-[0], $+[0]];
119 }
120 58 ▶ while ($expr =~ /('(?:[^'\\]|\\.)*')/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /('(?:[^'\\]|\\.)*')/g) 6 58 64
121 6 push @protected_ranges, [$-[0], $+[0]];
122 }
123 58 ▶ while ($expr =~ /(`(?:[^`\\]|\\.)*`)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /(`(?:[^`\\]|\\.)*`)/g) 8 58 66
124 8 push @protected_ranges, [$-[0], $+[0]];
125 }
126  
127 58 ▶ while ($expr =~ /\$(\w+)(?!\s*=(?!=))/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\$(\w+)(?!\s*=(?!=))/g) 33 58 91
128 33 my $var_name = $1;
129 33 my $match_start = $-[0];
130 33 my $match_end = $+[0];
131  
132 33 ▶ if (!$is_protected->($match_start)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (&$is_protected($match_start)) 25 8 33
133 25 $result .= substr($expr, $pos, $match_start - $pos);
134 25 $result .= "this.get_$var_name()";
135 25 $pos = $match_end;
136 }
137 }
138 58 $result .= substr($expr, $pos);
139  
140 58 return $result;
141 }
142  
143 # Extract parameter names from arrow function expressions
144 32 ▶sub extract_arrow_params {
Subroutine extract_arrow_params: called CC=8 SCAR=20.8
Pod undocumented
145 32 my ($expr) = @_;
146 32 my %params;
147  
148 # Match arrow functions: (param1, param2) => or param =>
149 32 ▶ while ($expr =~ /\(([^)]*)\)\s*=>/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\(([^)]*)\)\s*=>/g) 12 32 44
150 12 my $param_str = $1;
151 12 for my $param (split /,/, $param_str) {
152 8 $param =~ s/^\s+|\s+$//g;
153 8 $param =~ s/\s*=.*//; # Remove default values
154 8 ▶ $params{$param} = 1 if $param =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $param =~ /^\w+$/ 5 3 8
155 }
156 }
157  
158 # Single param without parens: x =>
159 32 ▶ while ($expr =~ /\b(\w+)\s*=>/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\b(\w+)\s*=>/g) 4 32 36
160 4 $params{$1} = 1;
161 }
162  
163 # Match regular function params: function(param1, param2)
164 32 ▶ while ($expr =~ /\bfunction\s*\(([^)]*)\)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\bfunction\s*\(([^)]*)\)/g) 21 32 53
165 21 my $param_str = $1;
166 21 for my $param (split /,/, $param_str) {
167 10 $param =~ s/^\s+|\s+$//g;
168 10 $param =~ s/\s*=.*//; # Remove default values
169 10 ▶ $params{$param} = 1 if $param =~ /^\w+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $param =~ /^\w+$/ 10 0 10
170 }
171 }
172  
173 32 return \%params;
174 }
175  
176 # Add implicit this. prefix to known method calls (whitelist approach)
177 29 ▶sub add_implicit_this {
Subroutine add_implicit_this: called CC=15 SCAR=27.2
Pod undocumented
178 29 my ($expr, $local_vars, $known_methods) = @_;
179 29 ▶ return $expr unless defined $expr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr 0 29 29
180 29 ▶ $local_vars //= {};
Condition50%1/2
$local_vars //= {}
l!l
290
MC/DC0%0/1
$local_vars //= {}
$local_vars
Truth table50%1/2
A
A= $local_vars
Aresult
11
00
181  
182 # Track string positions to avoid transforming inside strings
183 29 my @protected_ranges;
184 29 ▶ while ($expr =~ /("(?:[^"\\]|\\.)*")/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /("(?:[^"\\]|\\.)*")/g) 2 29 31
185 2 push @protected_ranges, [$-[0], $+[0]];
186 }
187 29 ▶ while ($expr =~ /('(?:[^'\\]|\\.)*')/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /('(?:[^'\\]|\\.)*')/g) 4 29 33
188 4 push @protected_ranges, [$-[0], $+[0]];
189 }
190 29 ▶ while ($expr =~ /(`(?:[^`\\]|\\.)*`)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /(`(?:[^`\\]|\\.)*`)/g) 6 29 35
191 6 push @protected_ranges, [$-[0], $+[0]];
192 }
193  
194 63 ▶ my $is_protected = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
195 63 my ($pos) = @_;
196 63 for my $range (@protected_ranges) {
197 44 ▶ return 1 if $pos >= $range->[0] && $pos < $range->[1];
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= $range->[0] and $pos < $range->[1] 1 43 44
Condition100%3/3
$pos >= $range->[0] and $pos < $range->[1]
!ll&&!rl&&r
3671
MC/DC100%2/2
$pos >= $range->[0] and $pos < $range->[1]
$pos >= $range->[0] $pos < $range->[1]
Truth table100%3/3
A and B
A= $pos >= $range->[0]B= $pos < $range->[1]
ABresult
0X0
100
111
198 }
199 62 return 0;
200 29 };
201  
202 # Replace bare method calls: only add this. for known methods
203 29 my $result = '';
204 29 my $last_end = 0;
205  
206 29 ▶ while ($expr =~ /\b(\w+)\s*\(/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expr =~ /\b(\w+)\s*\(/g) 63 29 92
207 63 my $name = $1;
208 63 my $match_start = $-[1];
209 63 my $match_end = $+[0];
210  
211 63 ▶ next if $is_protected->($match_start);
Branch: 1 branch
BranchTrueFalseTotal
if &$is_protected($match_start) 1 62 63
212  
213 # Check what precedes this identifier
214 62 my $before = substr($expr, 0, $match_start);
215  
216 # Skip if preceded by . (already a method call on something)
217 62 ▶ next if $before =~ /\.\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $before =~ /\.\s*$/ 31 31 62
218  
219 # Skip if preceded by 'new '
220 31 ▶ next if $before =~ /\bnew\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $before =~ /\bnew\s+$/ 0 31 31
221  
222 # Skip if preceded by 'function '
223 31 ▶ next if $before =~ /\bfunction\s+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $before =~ /\bfunction\s+$/ 0 31 31
224  
225 # Skip if already has this.
226 31 ▶ next if $before =~ /\bthis\.\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $before =~ /\bthis\.\s*$/ 0 31 31
227  
228 # Skip local variables (arrow params, for loop vars)
229 31 ▶ next if $local_vars->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $local_vars->{$name} 0 31 31
230  
231 # Only add this. if it's a known method (whitelist approach)
232 31 ▶ next unless $known_methods && $known_methods->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $known_methods and $known_methods->{$name} 26 5 31
Condition66%2/3
$known_methods and $known_methods->{$name}
!ll&&!rl&&r
0265
MC/DC50%1/2
$known_methods and $known_methods->{$name}
$known_methods $known_methods->{$name}
Truth table66%2/3
A and B
A= $known_methodsB= $known_methods->{$name}
ABresult
0X0
100
111
233  
234 # Add this. prefix
235 5 $result .= substr($expr, $last_end, $match_start - $last_end);
236 5 $result .= "this.$name(";
237 5 $last_end = $match_end;
238 }
239  
240 29 $result .= substr($expr, $last_end);
241 29 return $result;
242 }
243  
244 # Main transformation function that applies all shorthand transformations
245 29 ▶sub transform_expression {
Subroutine transform_expression: called CC=8 SCAR=21.7
Pod undocumented
246 29 my ($expr, $known_methods) = @_;
247 29 ▶ return $expr unless defined $expr && $expr =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $expr and $expr =~ /\S/ 0 29 29
Condition33%1/3
defined $expr and $expr =~ /\S/
!ll&&!rl&&r
0029
MC/DC0%0/2
defined $expr and $expr =~ /\S/
defined $expr $expr =~ /\S/
Truth table33%1/3
A and B
A= defined $exprB= $expr =~ /\S/
ABresult
0X0
100
111
248  
249 # Wrap as regular function so .call(scope) can rebind 'this' for slot scoping
250 # Don't wrap expressions that are already complete (async, function)
251 29 ▶ if ($expr =~ /^async\s/ || $expr =~ /^function[\s(]/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($expr =~ /^async\s/ or $expr =~ /^function[\s(]/) { } 2 27 29
elsif (not $expr =~ /^\(/) { } 14 13 27
elsif ($expr =~ /^\s*\(\s*([^)]+)\s*\)\s*=>\s*([{].*)/s) { } 0 13 13
elsif ($expr =~ /^\s*\(\s*([^)]+)\s*\)\s*=>\s*(.*)/s) { } 5 8 13
Condition66%2/3
$expr =~ /^async\s/ or $expr =~ /^function[\s(]/
l!l&&r!l&&!r
0227
MC/DC50%1/2
$expr =~ /^async\s/ or $expr =~ /^function[\s(]/
$expr =~ /^async\s/ $expr =~ /^function[\s(]/
Truth table66%2/3
A or B
A= $expr =~ /^async\s/B= $expr =~ /^function[\s(]/
ABresult
1X1
011
000
252 # Already a complete expression, just apply transforms below
253 }
254 elsif ($expr !~ /^\(/) {
255 14 $expr = "function(){return $expr}";
256 }
257 elsif($expr =~ /^\s*\(\s*([^)]+)\s*\)\s*=>\s*([{].*)/s) {
258 # Arrow with block body — convert to regular function
259 0 $expr = "function($1)$2";
260 }
261 elsif($expr =~ /^\s*\(\s*([^)]+)\s*\)\s*=>\s*(.*)/s) {
262 # Arrow with expression body — convert to regular function
263 5 $expr = "function($1){return $2}";
264 }
265  
266 29 my $local_vars = extract_arrow_params($expr);
267 29 $expr = transform_dollar_vars($expr);
268 29 $expr = add_implicit_this($expr, $local_vars, $known_methods);
269 29 return $expr;
270 }
271  
272 # Parse HTML into hierarchical structure
273 22 ▶sub parse_html {
Subroutine parse_html: called CC=26 SCAR=32.7
Pod undocumented
274 22 my ($html, $known_methods) = @_;
275  
276 22 my %result = (
277 children => [],
278 );
279  
280 # Self-closing tags
281 22 my %void_tags = map { $_ => 1 } qw(area base br col embed hr img input meta param source track wbr);
282  
283 22 my @stack = (\%result);
284 22 my $pos = 0;
285  
286 22 ▶ while ($html =~ /(<(?:[^>"']|"[^"]*"|'[^']*')+>|[^<]+)/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($html =~ /(<(?:[^>"']|"[^"]*"|'[^']*')+>|[^<]+)/g) 201 22 223
287 201 my $token = $1;
288  
289 201 ▶ if ($token =~ /^<\/([\w.!]+)>$/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($token =~ m[^</([\w.!]+)>$]) { } 56 145 201
elsif ($token =~ m[^<([\w.!]+)((?:[^>"']|"[^"]*"|'[^']*')*?)(/?)>$]) { } 64 81 145
elsif (not $token =~ /^\s*$/) { } 32 49 81
290 56 my $tag = lc($1);
291 56 ▶ pop @stack if @stack > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @stack > 1 56 0 56
292 }
293 elsif ($token =~ /^<([\w.!]+)((?:[^>"']|"[^"]*"|'[^']*')*?)(\/?)>$/) {
294 64 my $full_tag = lc($1);
295 64 ▶ my $attr_str = $2 // '';
Condition50%1/2
$2 // ""
l!l
640
MC/DC0%0/1
$2 // ""
$2
Truth table50%1/2
A
A= $2
Aresult
11
00
296 64 my $self_close = $3;
297  
298 64 ▶ next if $full_tag =~ /^!/;
Branch: 1 branch
BranchTrueFalseTotal
if $full_tag =~ /^!/ 0 64 64
299  
300 64 my ($tag, $variant) = split(/!/, $full_tag, 2);
301  
302 64 my $element = {
303 tag => $tag,
304 children => [],
305 };
306 64 ▶ $element->{variant} = $variant if defined $variant;
Branch: 1 branch
BranchTrueFalseTotal
if defined $variant 2 62 64
307  
308 64 my %attrs;
309 # First pass: match attributes with values (attr="value")
310 64 ▶ while ($attr_str =~ /([\w:@!*]+)="([^"]*)"/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attr_str =~ /([\w:\@!*]+)="([^"]*)"/g) 38 64 102
311 38 my ($attr, $value) = ($1, $2);
312  
313 # Parentheses required: 'and' has lower precedence than '||'
314 38 ▶ if (($tag eq 'if' || $tag eq 'elseif') and $attr eq 'condition') {
Branch: 5 branches
BranchTrueFalseTotal
if ($tag eq 'if' || $tag eq 'elseif' and $attr eq 'condition') { } 4 34 38
elsif ($attr eq 'for') { } 4 30 34
elsif ($attr =~ /^@(\w+)$/) { } 3 27 30
elsif ($value =~ /^\(/) { } 4 23 27
elsif ($value =~ /\$/) { } 3 20 23
Condition83%5/6
$tag eq "if" || $tag eq "elseif" and $attr eq "condition"
!ll&&!rl&&r
3404
$tag eq "if" or $tag eq "elseif"
l!l&&r!l&&!r
3134
MC/DC66%2/3
$tag eq "if" || $tag eq "elseif" and $attr eq "condition"
$tag eq "if" $tag eq "elseif" $attr eq "condition"
Truth table60%3/5
A or B and C
A= $tag eq "if"B= $tag eq "elseif"C= $attr eq "condition"
ABCresult
00X0
1X00
0100
1X11
0111
315 4 $attrs{"*condition"} = transform_expression($value, $known_methods);
316 }
317 elsif ($attr eq 'for') {
318 4 ▶ if ($value =~ /^\[/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\[/) 2 2 4
319 2 $value = "function(){return $value}";
320 }
321 4 $attrs{"*for"} = transform_expression($value, $known_methods);
322 }
323 elsif ($attr =~ /^@(\w+)$/) {
324 3 $attr = "*on" . lc($1);
325 3 $attrs{$attr} = transform_expression($value, $known_methods);
326 }
327 elsif ($value =~ /^\(/) {
328 4 $value = transform_expression($value, $known_methods);
329 4 $attrs{"*".lc($attr)} = $value;
330 }
331 elsif ($value =~ /\$/) {
332 3 $value = _transform_dollar_interpolation($value);
333 3 $attrs{"*".lc($attr)} = "function(){return `$value`}";
334 }
335 else {
336 20 $attrs{lc($attr)} = $value;
337 }
338 }
339  
340 # Second pass: match bare attributes without values (e.g., <option selected>)
341 # Remove already-matched attributes with values first
342 64 my $bare_attr_str = $attr_str;
343 64 $bare_attr_str =~ s/[\w:@*]+="[^"]*"//g;
344 64 ▶ while ($bare_attr_str =~ /\b([\w-]+)\b/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($bare_attr_str =~ /\b([\w-]+)\b/g) 1 64 65
345 1 my $attr = lc($1);
346 # Only add if not already set
347 1 ▶ unless (exists $attrs{$attr} || exists $attrs{"*$attr"}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $attrs{$attr} or exists $attrs{"*$attr"}) 1 0 1
Condition33%1/3
exists $attrs{$attr} or exists $attrs{"*$attr"}
l!l&&r!l&&!r
001
MC/DC0%0/2
exists $attrs{$attr} or exists $attrs{"*$attr"}
exists $attrs{$attr} exists $attrs{"*$attr"}
Truth table33%1/3
A or B
A= exists $attrs{$attr}B= exists $attrs{"*$attr"}
ABresult
1X1
011
000
348 1 $attrs{$attr} = 'true';
349 }
350 }
351  
352 64 $element->{attributes} = \%attrs;
353  
354 64 my $parent = $stack[-1];
355 64 push @{$parent->{children}}, $element;
356  
357 64 ▶ unless ($self_close || $void_tags{$tag}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self_close or $void_tags{$tag}) 56 8 64
Condition66%2/3
$self_close or $void_tags{$tag}
l!l&&r!l&&!r
8056
MC/DC50%1/2
$self_close or $void_tags{$tag}
$self_close $void_tags{$tag}
Truth table66%2/3
A or B
A= $self_closeB= $void_tags{$tag}
ABresult
1X1
011
000
358 56 push @stack, $element;
359 }
360 }
361 elsif ($token !~ /^\s*$/) {
362 32 my $text = $token;
363 32 $text =~ s/^\s+|\s+$//g;
364 32 my $text_node = {};
365 32 ▶ if ($text ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($text ne "") 32 0 32
366 32 ▶ if ($text =~ /^\s*(\([^)]*\)\s*=>)(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($text =~ /^\s*(\([^)]*\)\s*=>)(.*)/) { } 1 31 32
elsif ($text =~ /\$/) { } 10 21 31
367 1 $text_node->{'*content'} = transform_expression($text, $known_methods);
368 } elsif ($text =~ /\$/) {
369 10 $text = _transform_dollar_interpolation($text);
370 10 $text_node->{'*content'} = "function(){return `$text`}";
371 } else {
372 21 $text_node->{content} = $text;
373 }
374  
375 32 my $parent = $stack[-1];
376 32 push @{$parent->{children}}, $text_node;
377 }
378 }
379 }
380  
381 22 return \%result;
382 }
383  
384 # Transform $var references in template text/attributes.
385 # Handles ${$var.prop}, ${$var}, and bare $var patterns.
386 # Inside ${...} blocks, $var is replaced without adding another ${} wrapper.
387 13 ▶sub _transform_dollar_interpolation {
Subroutine _transform_dollar_interpolation: called CC=1 SCAR=0.0
388 13 my ($text) = @_;
389  
390 # First: transform ${...} blocks that contain $var references
391 13 $text =~ s/\$\{([^}]*)\}/"\${" . _transform_inner_dollars($1) . "}"/ge;
392  
393 # Then: transform bare $var (not inside ${})
394 13 $text =~ s/\$([\w]+)/\${this.get_$1()}/g;
395  
396 13 return $text;
397 }
398  
399 # Transform $var references inside a ${...} expression.
400 # $var becomes this.get_var(), no extra ${} wrapping.
401 5 ▶sub _transform_inner_dollars {
Subroutine _transform_inner_dollars: called CC=1 SCAR=0.0
402 5 my ($expr) = @_;
403 5 $expr =~ s/\$(\w+)/this.get_$1()/g;
404 5 return $expr;
405 }
406  
407 2 1;
« Spiderpup/SFC.pm ↑ Summary