| 1 | package BarefootJS::SearchParams; | ||||||||||||||||||||||||||||
| 2 | 2 | our $VERSION = "0.41.3"; | |||||||||||||||||||||||||||
| 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 utf8; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # Request-scoped SSR view of the query string behind the reactive | ||||||||||||||||||||||||||||
| 10 | # `searchParams()` environment signal (router v0.5, #1922). The framework | ||||||||||||||||||||||||||||
| 11 | # integration builds one per request from the request URL and threads it into | ||||||||||||||||||||||||||||
| 12 | # the template scope as `$searchParams` (the camelCase JS name the adapters | ||||||||||||||||||||||||||||
| 13 | # keep, like every other signal/prop var); the compiled template reads it via | ||||||||||||||||||||||||||||
| 14 | # `$searchParams->get('key')` (Mojo) / `$searchParams.get('key')` (Xslate). | ||||||||||||||||||||||||||||
| 15 | # | ||||||||||||||||||||||||||||
| 16 | # This runtime is template-engine- and framework-agnostic (core Perl only), | ||||||||||||||||||||||||||||
| 17 | # matching the rest of BarefootJS.pm, so it can ship in the standalone | ||||||||||||||||||||||||||||
| 18 | # @barefootjs/perl distribution. | ||||||||||||||||||||||||||||
| 19 | # | ||||||||||||||||||||||||||||
| 20 | # Semantics mirror the browser's URLSearchParams.get exactly: get() returns | ||||||||||||||||||||||||||||
| 21 | # the first value for a key, or `undef` — JS `null`, not `undefined` | ||||||||||||||||||||||||||||
| 22 | # (`$BarefootJS::UNDEF`) — when the key is absent. The adapters' `??` | ||||||||||||||||||||||||||||
| 23 | # lowering (`keep_nonnil`) coalesces it, so an absent key falls back to the | ||||||||||||||||||||||||||||
| 24 | # author's default while a present-but-empty value (`?sort=`) keeps the empty | ||||||||||||||||||||||||||||
| 25 | # string — the same distinction JS `??` draws between `null` and `''`. (This | ||||||||||||||||||||||||||||
| 26 | # is a closer match than the Go adapter, whose `or` lowering also coalesces | ||||||||||||||||||||||||||||
| 27 | # the empty string.) | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # new($class, $query = '') | ||||||||||||||||||||||||||||
| 30 | # | ||||||||||||||||||||||||||||
| 31 | # Parse a raw query string into the reader. A leading '?' is tolerated, '+' | ||||||||||||||||||||||||||||
| 32 | # decodes to a space, and %XX escapes are decoded — mirroring URLSearchParams's | ||||||||||||||||||||||||||||
| 33 | # application/x-www-form-urlencoded parsing. A malformed pair never dies; it | ||||||||||||||||||||||||||||
| 34 | # simply contributes nothing, matching the browser's lenient parsing. | ||||||||||||||||||||||||||||
| 35 | 9 | ▶ | sub new ($class, $query = '') { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 36 | 9 | ▶ | $query //= ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$query //= ""
MC/DC100%1/1
$query //= ""
$query
Truth table100%2/2
A
A= $query
| |||||||||||||||||||||||||||||
| 37 | 9 | $query =~ s/\A\?//; | |||||||||||||||||||||||||||
| 38 | 9 | my %values; | |||||||||||||||||||||||||||
| 39 | 9 | for my $pair (split /[&;]/, $query) { | |||||||||||||||||||||||||||
| 40 | 6 | ▶ | next if $pair eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 6 | my ($key, $val) = split /=/, $pair, 2; | |||||||||||||||||||||||||||
| 42 | 6 | $key = _decode($key); | |||||||||||||||||||||||||||
| 43 | 6 | ▶ | $val = defined $val ? _decode($val) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 6 | push @{ $values{$key} }, $val; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 9 | return bless { values => \%values }, $class; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # get($self, $key) | ||||||||||||||||||||||||||||
| 50 | # | ||||||||||||||||||||||||||||
| 51 | # First value for $key, or `undef` (JS `null`) when the key is absent (see | ||||||||||||||||||||||||||||
| 52 | # the package docstring). A present-but-empty value returns ''. | ||||||||||||||||||||||||||||
| 53 | 7 | ▶ | sub get ($self, $key) { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 7 | my $vals = $self->{values}{$key}; | |||||||||||||||||||||||||||
| 55 | 7 | ▶ | return undef unless $vals && @$vals; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$vals and @$vals
MC/DC50%1/2
$vals and @$vals
$vals
@$vals
Truth table66%2/3
A and B
A= $valsB= @$vals
| |||||||||||||||||||||||||||||
| 56 | 3 | return $vals->[0]; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 12 | ▶ | sub _decode ($s) { | ||||||||||||||||||||||||||
|
Subroutine
_decode: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||
| 60 | 12 | ▶ | $s //= ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$s //= ""
MC/DC0%0/1
$s //= ""
$s
Truth table50%1/2
A
A= $s
| |||||||||||||||||||||||||||||
| 61 | 12 | $s =~ tr/+/ /; | |||||||||||||||||||||||||||
| 62 | # %XX → raw octet, then interpret the octet stream as UTF-8 (what | ||||||||||||||||||||||||||||
| 63 | # URLSearchParams does). `utf8::decode` is a core builtin — no CPAN URI / | ||||||||||||||||||||||||||||
| 64 | # URI::Escape dependency, keeping this runtime core-Perl-only. A byte run | ||||||||||||||||||||||||||||
| 65 | # that isn't valid UTF-8 is left as-is rather than dying (lenient parsing). | ||||||||||||||||||||||||||||
| 66 | 12 | $s =~ s/%([0-9A-Fa-f]{2})/chr hex $1/ge; | |||||||||||||||||||||||||||
| 67 | 12 | utf8::decode($s); | |||||||||||||||||||||||||||
| 68 | 12 | return $s; | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | 1; | |||||||||||||||||||||||||||