| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 11 | push @protected_ranges, [$-[0], $+[0]]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 58 | ▶ | while ($expr =~ /('(?:[^'\\]|\\.)*')/g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | 6 | push @protected_ranges, [$-[0], $+[0]]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | # Find template literals (handle escaped backticks) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | 58 | ▶ | while ($expr =~ /(`(?:[^`\\]|\\.)*`)/g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$pos >= $range->[0] and $pos < $range->[1]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | # Track string state (skip if escaped) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 97 | ▶ | if (!$in_string && $prev ne '\\') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
not $in_string and $prev ne "\\"
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 "\\"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 81 | ▶ | if ($char eq '"' || $char eq "'" || $char eq '`') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%4/6
$char eq "\"" or $char eq "'" or $char eq "`"
$char eq "\"" or $char eq "'"
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 "`"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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 "\\"
$in_string and $char eq $in_string
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 "\\"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 1 | $in_string = ''; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 95 | ▶ | next if $in_string; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | 80 | ▶ | if ($char eq '(' || $char eq '[' || $char eq '{') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%4/6
$char eq "(" or $char eq "[" or $char eq "{"
$char eq "(" or $char eq "["
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 "{"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | $depth++; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | ▶ | } elsif ($char eq ')' || $char eq ']' || $char eq '}') { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition83%5/6
$char eq ")" or $char eq "]" or $char eq "}"
$char eq ")" or $char eq "]"
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 "}"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 5 | ▶ | if ($depth == 0) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
$char eq ";" or $char eq ","
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 11 | push @protected_ranges, [$-[0], $+[0]]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | 58 | ▶ | while ($expr =~ /('(?:[^'\\]|\\.)*')/g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 6 | push @protected_ranges, [$-[0], $+[0]]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | 58 | ▶ | while ($expr =~ /(`(?:[^`\\]|\\.)*`)/g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | 8 | push @protected_ranges, [$-[0], $+[0]]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 58 | ▶ | while ($expr =~ /\$(\w+)(?!\s*=(?!=))/g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | # Single param without parens: x => | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 32 | ▶ | while ($expr =~ /\b(\w+)\s*=>/g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 29 | ▶ | $local_vars //= {}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$local_vars //= {}
MC/DC0%0/1
$local_vars //= {}
$local_vars
Truth table50%1/2
A
A= $local_vars
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | # Track string positions to avoid transforming inside strings | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 29 | my @protected_ranges; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 29 | ▶ | while ($expr =~ /("(?:[^"\\]|\\.)*")/g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 2 | push @protected_ranges, [$-[0], $+[0]]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 29 | ▶ | while ($expr =~ /('(?:[^'\\]|\\.)*')/g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | 4 | push @protected_ranges, [$-[0], $+[0]]; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | 29 | ▶ | while ($expr =~ /(`(?:[^`\\]|\\.)*`)/g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$pos >= $range->[0] and $pos < $range->[1]
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]
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | # Skip if preceded by 'new ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 31 | ▶ | next if $before =~ /\bnew\s+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | # Skip if preceded by 'function ' | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | 31 | ▶ | next if $before =~ /\bfunction\s+$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | # Skip if already has this. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | 31 | ▶ | next if $before =~ /\bthis\.\s*$/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | # Skip local variables (arrow params, for loop vars) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 31 | ▶ | next if $local_vars->{$name}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$known_methods and $known_methods->{$name}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
defined $expr and $expr =~ /\S/
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/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$expr =~ /^async\s/ or $expr =~ /^function[\s(]/
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(]/
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | 201 | my $token = $1; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 201 | ▶ | if ($token =~ /^<\/([\w.!]+)>$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | 56 | my $tag = lc($1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | 56 | ▶ | pop @stack if @stack > 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | elsif ($token =~ /^<([\w.!]+)((?:[^>"']|"[^"]*"|'[^']*')*?)(\/?)>$/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | 64 | my $full_tag = lc($1); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | 64 | ▶ | my $attr_str = $2 // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$2 // ""
MC/DC0%0/1
$2 // ""
$2
Truth table50%1/2
A
A= $2
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | 64 | my $self_close = $3; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | 64 | ▶ | next if $full_tag =~ /^!/; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | 64 | my %attrs; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | # First pass: match attributes with values (attr="value") | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 64 | ▶ | while ($attr_str =~ /([\w:@!*]+)="([^"]*)"/g) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition83%5/6
$tag eq "if" || $tag eq "elseif" and $attr eq "condition"
$tag eq "if" or $tag eq "elseif"
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"
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | 4 | $attrs{"*condition"} = transform_expression($value, $known_methods); | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | elsif ($attr eq 'for') { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 4 | ▶ | if ($value =~ /^\[/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition33%1/3
exists $attrs{$attr} or exists $attrs{"*$attr"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
$self_close or $void_tags{$tag}
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}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | 32 | ▶ | if ($text =~ /^\s*(\([^)]*\)\s*=>)(.*)/) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||