| 1 | package Uniform::HTMX; | ||||||||||
| 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 Uniform::Utils qw(normalize_http_headers); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use Carp qw(croak); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | BEGIN { | ||||||||||
| 8 | 2 | ▶ | if (eval { require JSON::MaybeXS; 1 }) { | ||||||||
|
Subroutine
BEGIN: called CC=2 SCAR=7.9
Branch: 1 branch
| |||||||||||
| 9 | 2 | JSON::MaybeXS->import('encode_json', 'decode_json'); | |||||||||
| 10 | } else { | ||||||||||
| 11 | 0 | require JSON::PP; | |||||||||
| 12 | 0 | JSON::PP->import('encode_json', 'decode_json'); | |||||||||
| 13 | } | ||||||||||
| 14 | } | ||||||||||
| 15 | |||||||||||
| 16 | 2 | our $VERSION = '0.15'; | |||||||||
| 17 | |||||||||||
| 18 | # Internal normalization utility available to all independent subclasses | ||||||||||
| 19 | sub _normalize_headers { | ||||||||||
| 20 | 7 | ▶ | my ($self, $hash) = @_; | ||||||||
|
Subroutine
_normalize_headers: called CC=1 SCAR=0.0
| |||||||||||
| 21 | # Delegate cleanly to your core ecosystem utility! | ||||||||||
| 22 | 7 | return normalize_http_headers($hash); | |||||||||
| 23 | } | ||||||||||
| 24 | |||||||||||
| 25 | sub _request_header { | ||||||||||
| 26 | 8 | ▶ | my ($self, $name) = @_; | ||||||||
|
Subroutine
_request_header: called CC=2 SCAR=7.1
| |||||||||||
| 27 | 8 | ▶ | return unless ref($self->{in}) eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 8 | return $self->{in}->{lc $name}; | |||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | sub _true_header { | ||||||||||
| 32 | 2 | ▶ | my ($self, $name) = @_; | ||||||||
|
Subroutine
_true_header: called CC=3 SCAR=11.3
| |||||||||||
| 33 | 2 | my $value = $self->_request_header($name); | |||||||||
| 34 | 2 | ▶ | return 0 unless defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 2 | $value =~ s/^\s+|\s+$//g; | |||||||||
| 36 | 2 | ▶ | return lc($value) eq 'true' ? 1 : 0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | sub _response_header { | ||||||||||
| 40 | 5 | ▶ | my ($self, $name, $value) = @_; | ||||||||
|
Subroutine
_response_header: called CC=5 SCAR=16.5
| |||||||||||
| 41 | 5 | ▶ | croak "$name value must be defined" unless defined $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 42 | 5 | ▶ | croak "$name value must be a plain scalar" if ref $value; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 5 | ▶ | croak "$name value must not contain a newline" if $value =~ /[\r\n]/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 44 | 3 | ▶ | $self->{out} = {} unless ref($self->{out}) eq 'HASH'; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 45 | 3 | $self->{out}->{$name} = "$value"; | |||||||||
| 46 | 3 | return $self; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | sub _event_header { | ||||||||||
| 50 | 1 | ▶ | my ($self, $header, $event, $params) = @_; | ||||||||
|
Subroutine
_event_header: called CC=6 SCAR=19.3
| |||||||||||
| 51 | 1 | ▶ | croak "Event name must be defined" unless defined $event; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 52 | 1 | ▶ | croak "Event name must be a plain scalar" if ref $event; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | 1 | ▶ | croak "Event name must not be empty" unless length $event; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 1 | ▶ | croak "Event name must not contain a newline" if $event =~ /[\r\n]/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | |||||||||||
| 56 | 1 | ▶ | my $value = defined($params) ? encode_json({ $event => $params }) : $event; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 1 | return $self->_response_header($header, $value); | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | # ========================================================================= | ||||||||||
| 61 | # REQUEST INSPECTION METHODS | ||||||||||
| 62 | # ========================================================================= | ||||||||||
| 63 | |||||||||||
| 64 | sub is_htmx { | ||||||||||
| 65 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
is_htmx: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 66 | 1 | return $self->_true_header('hx-request'); | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | sub is_boosted { | ||||||||||
| 70 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
is_boosted: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 71 | 1 | return $self->_true_header('hx-boosted'); | |||||||||
| 72 | } | ||||||||||
| 73 | |||||||||||
| 74 | sub is_history_restore { | ||||||||||
| 75 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
is_history_restore: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 76 | 0 | return $self->_true_header('hx-history-restore-request'); | |||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | sub current_url { | ||||||||||
| 80 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
current_url: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 81 | 0 | return $self->_request_header('hx-current-url'); | |||||||||
| 82 | } | ||||||||||
| 83 | |||||||||||
| 84 | sub prompt { | ||||||||||
| 85 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
prompt: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 86 | 0 | return $self->_request_header('hx-prompt'); | |||||||||
| 87 | } | ||||||||||
| 88 | |||||||||||
| 89 | sub target { | ||||||||||
| 90 | 3 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
target: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 91 | 3 | return $self->_request_header('hx-target'); | |||||||||
| 92 | } | ||||||||||
| 93 | |||||||||||
| 94 | sub trigger_id { | ||||||||||
| 95 | 1 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
trigger_id: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 96 | 1 | return $self->_request_header('hx-trigger'); | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | sub trigger_name { | ||||||||||
| 100 | 0 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
trigger_name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 101 | 0 | return $self->_request_header('hx-trigger-name'); | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | sub trigger_event { | ||||||||||
| 105 | 2 | ▶ | my ($self) = @_; | ||||||||
|
Subroutine
trigger_event: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 106 | 2 | my $val = $self->_request_header('hx-trigger'); | |||||||||
| 107 | 2 | ▶ | return unless defined $val; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | |||||||||||
| 109 | # If it looks like a JSON string sent from an htmx event trigger, decode it safely | ||||||||||
| 110 | 2 | ▶ | if ($val =~ /^\s*[\{\[]/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 111 | 1 | return eval { decode_json($val) }; | |||||||||
| 112 | } | ||||||||||
| 113 | 1 | return $val; | |||||||||
| 114 | } | ||||||||||
| 115 | |||||||||||
| 116 | # ========================================================================= | ||||||||||
| 117 | # RESPONSE MANIPULATION METHODS | ||||||||||
| 118 | # ========================================================================= | ||||||||||
| 119 | |||||||||||
| 120 | sub res_retarget { | ||||||||||
| 121 | 1 | ▶ | my ($self, $selector) = @_; | ||||||||
|
Subroutine
res_retarget: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 122 | 1 | return $self->_response_header('HX-Retarget', $selector); | |||||||||
| 123 | } | ||||||||||
| 124 | |||||||||||
| 125 | sub res_reswap { | ||||||||||
| 126 | 3 | ▶ | my ($self, $strategy) = @_; | ||||||||
|
Subroutine
res_reswap: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 127 | 3 | return $self->_response_header('HX-Reswap', $strategy); | |||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | sub res_reselect { | ||||||||||
| 131 | 0 | ▶ | my ($self, $selector) = @_; | ||||||||
|
Subroutine
res_reselect: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 132 | 0 | return $self->_response_header('HX-Reselect', $selector); | |||||||||
| 133 | } | ||||||||||
| 134 | |||||||||||
| 135 | sub res_location { | ||||||||||
| 136 | 0 | ▶ | my ($self, $location) = @_; | ||||||||
|
Subroutine
res_location: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||
| 137 | 0 | ▶ | if (ref($location) eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 138 | 0 | return $self->_response_header('HX-Location', encode_json($location)); | |||||||||
| 139 | } | ||||||||||
| 140 | 0 | return $self->_response_header('HX-Location', $location); | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | sub res_push_url { | ||||||||||
| 144 | 0 | ▶ | my ($self, $url) = @_; | ||||||||
|
Subroutine
res_push_url: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 145 | 0 | return $self->_response_header('HX-Push-Url', $url); | |||||||||
| 146 | } | ||||||||||
| 147 | |||||||||||
| 148 | sub res_replace_url { | ||||||||||
| 149 | 0 | ▶ | my ($self, $url) = @_; | ||||||||
|
Subroutine
res_replace_url: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 150 | 0 | return $self->_response_header('HX-Replace-Url', $url); | |||||||||
| 151 | } | ||||||||||
| 152 | |||||||||||
| 153 | sub res_redirect { | ||||||||||
| 154 | 0 | ▶ | my ($self, $url) = @_; | ||||||||
|
Subroutine
res_redirect: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 155 | 0 | return $self->_response_header('HX-Redirect', $url); | |||||||||
| 156 | } | ||||||||||
| 157 | |||||||||||
| 158 | sub res_refresh { | ||||||||||
| 159 | 0 | ▶ | my ($self, $refresh) = @_; | ||||||||
|
Subroutine
res_refresh: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||
| 160 | 0 | ▶ | $refresh = 1 unless @_ > 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 161 | 0 | ▶ | return $self->_response_header('HX-Refresh', $refresh ? 'true' : 'false'); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | } | ||||||||||
| 163 | |||||||||||
| 164 | sub res_trigger_name { | ||||||||||
| 165 | 0 | ▶ | my ($self, $name) = @_; | ||||||||
|
Subroutine
res_trigger_name: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 166 | 0 | return $self->_response_header('HX-Trigger-Name', $name); | |||||||||
| 167 | } | ||||||||||
| 168 | |||||||||||
| 169 | sub res_trigger { | ||||||||||
| 170 | 1 | ▶ | my ($self, $event, $params) = @_; | ||||||||
|
Subroutine
res_trigger: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 171 | 1 | return $self->_event_header('HX-Trigger', $event, $params); | |||||||||
| 172 | } | ||||||||||
| 173 | |||||||||||
| 174 | sub res_trigger_after_settle { | ||||||||||
| 175 | 0 | ▶ | my ($self, $event, $params) = @_; | ||||||||
|
Subroutine
res_trigger_after_settle: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 176 | 0 | return $self->_event_header('HX-Trigger-After-Settle', $event, $params); | |||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | sub res_trigger_after_swap { | ||||||||||
| 180 | 0 | ▶ | my ($self, $event, $params) = @_; | ||||||||
|
Subroutine
res_trigger_after_swap: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 181 | 0 | return $self->_event_header('HX-Trigger-After-Swap', $event, $params); | |||||||||
| 182 | } | ||||||||||
| 183 | |||||||||||
| 184 | |||||||||||
| 185 | |||||||||||
| 186 | 2 | 1; | |||||||||
| 187 | |||||||||||
| 188 | __END__ | ||||||||||