blib/lib/Mojolicious/Plugin/HTMX.pm

filter: Statementstmt 68.9% 100 / 145 Branchbran 50.0% 19 / 38 Conditioncond 28.5% 2 / 7 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 57.8% 22 / 38 Podpod 100% 1 / 1 totaltotal 62.2% 145 / 233 CC 25 SCAR 15.3
CC
25
Cov
64%
_header
20.0
__ANON__
12.0
_htmx_js
12.0
CRAP 4.6
↑ Summary
1 package Mojolicious::Plugin::HTMX;
2 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 2 ▶use Mojo::ByteStream;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Mojo::JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Mojo::Util qw(xml_escape);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '1.03';
9  
10 2 my @HX_RESWAPS = (qw[
11 innerHTML
12 outerHTML
13 beforebegin
14 afterbegin
15 beforeend
16 afterend
17 delete
18 none
19 ]);
20  
21 2 ▶use constant HX_TRUE => 'true';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use constant HX_FALSE => 'false';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 2 ▶use constant HTMX_STOP_POLLING => 286;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use constant HTMX_CDN_URL => 'https://unpkg.com/htmx.org';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use constant HTMX_V1_CDN_URL => 'https://unpkg.com/htmx.org@1.9.12';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28  
29 1 ▶sub register {
Subroutine register: called CC=1 SCAR=1.6
Pod documented
30  
31 1 my ($self, $app) = @_;
32  
33 0 ▶ $app->helper('is_htmx_request' => sub { _header(shift, 'HX-Request', HX_TRUE) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
34  
35 1 $app->helper('htmx.asset' => \&_htmx_js);
36 0 ▶ $app->helper('htmx.stop_polling' => sub { shift->rendered(HTMX_STOP_POLLING) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
37  
38 0 ▶ $app->helper('htmx.req.boosted' => sub { _header(shift, 'HX-Boosted', HX_TRUE) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
39 0 ▶ $app->helper('htmx.req.current_url' => sub { Mojo::URL->new(_header(shift, 'HX-Current-URL')) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
40 0 ▶ $app->helper('htmx.req.history_restore_request' => sub { _header(shift, 'HX-History-Restore-Request', HX_TRUE) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
41 0 ▶ $app->helper('htmx.req.prompt' => sub { _header(shift, 'HX-Prompt') });
Subroutine __ANON__: not called CC=1 SCAR=1.2
42 0 ▶ $app->helper('htmx.req.request' => sub { _header(shift, 'HX-Request', HX_TRUE) });
Subroutine __ANON__: not called CC=1 SCAR=1.2
43 0 ▶ $app->helper('htmx.req.target' => sub { _header(shift, 'HX-Target') });
Subroutine __ANON__: not called CC=1 SCAR=1.2
44 0 ▶ $app->helper('htmx.req.trigger_name' => sub { _header(shift, 'HX-Trigger-Name') });
Subroutine __ANON__: not called CC=1 SCAR=1.2
45 0 ▶ $app->helper('htmx.req.trigger' => sub { _header(shift, 'HX-Trigger') });
Subroutine __ANON__: not called CC=1 SCAR=1.2
46  
47 $app->helper(
48 0 ▶ 'htmx.req.triggering_event' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
49 0 ▶ eval { decode_json(_header(shift, 'Triggering-Event')) } || {};
Branch: 1 branch
BranchTrueFalseTotal
eval { do { decode_json(_header(shift(), "Triggering-Event")) } } or {} 0 0 0
50 }
51 1 );
52  
53 $app->helper(
54 0 ▶ 'htmx.req.to_hash' => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
55  
56 0 my $c = shift;
57  
58 0 my %hash = ();
59  
60 my @helpers
61 0 = (
62 qw[boosted current_url history_restore_request prompt request target trigger_name trigger triggering_event]
63 );
64  
65 0 for my $helper (@helpers) {
66 0 ▶ if (my $value = $c->helpers->htmx->req->$helper()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $value = $c->helpers->htmx->req->$helper) 0 0 0
67 0 $hash{$helper} = "$value";
68 }
69 }
70  
71 0 return \%hash;
72  
73 }
74 1 );
75  
76 1 $app->helper('htmx.res.location' => \&_res_location);
77 1 $app->helper('htmx.res.push_url' => \&_res_push_url);
78 1 $app->helper('htmx.res.redirect' => \&_res_redirect);
79 1 $app->helper('htmx.res.refresh' => \&_res_refresh);
80 1 $app->helper('htmx.res.replace_url' => \&_res_replace_url);
81 1 $app->helper('htmx.res.reswap' => \&_res_reswap);
82 1 $app->helper('htmx.res.reselect' => \&_res_reselect);
83 1 $app->helper('htmx.res.retarget' => \&_res_retarget);
84  
85 1 ▶ $app->helper('htmx.res.trigger' => sub { _res_trigger('default', @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
86 1 ▶ $app->helper('htmx.res.trigger_after_settle' => sub { _res_trigger('after_settle', @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
87 1 ▶ $app->helper('htmx.res.trigger_after_swap' => sub { _res_trigger('after_swap', @_) });
Subroutine __ANON__: called CC=1 SCAR=0.0
88  
89 $app->helper(
90 0 ▶ 'hx' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
91  
92 0 my ($c, %attrs) = @_;
93 0 my $hx = {};
94  
95 0 @$hx{map { y/_/-/; "hx-$_" } keys %attrs} = values %attrs;
96 0 return %{$hx};
97  
98 }
99 1 );
100  
101 $app->helper(
102 0 ▶ 'hx_attr' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
103  
104 0 my ($c, %attrs) = @_;
105  
106 0 my %hx = $c->hx(%attrs);
107 0 my $result = '';
108  
109 0 for my $attr (sort keys %hx) {
110 0 my $value = $hx{$attr};
111 0 $result .= qq{ $attr="} . xml_escape($value) . '"';
112 }
113  
114 0 return Mojo::ByteStream->new($result);
115  
116 }
117 1 );
118  
119 }
120  
121 0 ▶sub _htmx_js {
Subroutine _htmx_js: not called CC=3 SCAR=24.8
122  
123 0 my ($self, %params) = @_;
124 0 ▶ my $url = delete $params{url} || HTMX_CDN_URL;
Condition0%0/2
delete $params{'url'} || 'https://unpkg.com/htmx.org'
l!l
00
MC/DC0%0/1
delete $params{'url'} || 'https://unpkg.com/htmx.org'
delete $params{'url'}
Truth table0%0/2
A
A= delete $params{'url'}
Aresult
11
00
125 0 my $ext = delete $params{ext};
126  
127 0 ▶ if ($ext) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ext) 0 0 0
128 0 $url .= "/dist/ext/$ext.js";
129 }
130  
131 0 return Mojo::ByteStream->new(Mojo::DOM::HTML::tag_to_html('script', 'src' => $url));
132  
133 }
134  
135 0 ▶sub _header {
Subroutine _header: not called CC=4 SCAR=30.0
136  
137 0 my ($c, $header, $check) = @_;
138 0 my $value = $c->req->headers->header($header);
139  
140 0 ▶ if ($value && $check) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value and $check) 0 0 0
Condition0%0/3
$value and $check
!ll&&!rl&&r
000
MC/DC0%0/2
$value and $check
$value $check
Truth table0%0/3
A and B
A= $valueB= $check
ABresult
0X0
100
111
141 0 ▶ return !!1 if ($value eq $check);
Branch: 1 branch
BranchTrueFalseTotal
if $value eq $check 0 0 0
142 0 return !!0;
143 }
144  
145 0 return $value;
146  
147 }
148  
149 3 ▶sub _res_location {
Subroutine _res_location: called CC=4 SCAR=13.9
150  
151 3 my $c = shift;
152 3 ▶ my $location = (@_ > 1) ? {@_} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 2 1 3
153  
154 3 ▶ return undef unless $location;
Branch: 1 branch
BranchTrueFalseTotal
unless $location 0 3 3
155  
156 3 ▶ if (ref $location eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $location eq "HASH") 2 1 3
157 2 $location = encode_json($location);
158 }
159  
160 3 return $c->res->headers->header('HX-Location' => $location);
161  
162 }
163  
164 2 ▶sub _res_push_url {
Subroutine _res_push_url: called CC=2 SCAR=7.1
165  
166 2 my ($c, $push_url) = @_;
167 2 ▶ return undef unless $push_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $push_url 0 2 2
168  
169 2 return $c->res->headers->header('HX-Push-Url' => $push_url);
170  
171 }
172  
173 2 ▶sub _res_redirect {
Subroutine _res_redirect: called CC=2 SCAR=7.1
174  
175 2 my ($c, $redirect) = @_;
176 2 ▶ return undef unless $redirect;
Branch: 1 branch
BranchTrueFalseTotal
unless $redirect 0 2 2
177  
178 2 return $c->res->headers->header('HX-Redirect' => $redirect);
179  
180 }
181  
182 1 ▶sub _res_refresh {
Subroutine _res_refresh: called CC=1 SCAR=0.0
183 1 my ($c) = @_;
184 1 return $c->res->headers->header('HX-Refresh' => HX_TRUE);
185 }
186  
187 2 ▶sub _res_replace_url {
Subroutine _res_replace_url: called CC=2 SCAR=7.1
188  
189 2 my ($c, $replace_url) = @_;
190 2 ▶ return undef unless $replace_url;
Branch: 1 branch
BranchTrueFalseTotal
unless $replace_url 0 2 2
191  
192 2 return $c->res->headers->header('HX-Replace-Url' => $replace_url);
193  
194 }
195  
196 16 ▶sub _res_reswap {
Subroutine _res_reswap: called CC=3 SCAR=11.2
197  
198 16 my ($c, $reswap) = @_;
199 16 ▶ return undef unless $reswap;
Branch: 1 branch
BranchTrueFalseTotal
unless $reswap 0 16 16
200  
201 16 my $is_reswap = grep {/^$reswap$/} @HX_RESWAPS;
202 16 ▶ Carp::croak "Unknown reswap value" if (!$is_reswap);
Branch: 1 branch
BranchTrueFalseTotal
unless $is_reswap 0 16 16
203  
204 16 return $c->res->headers->header('HX-Reswap' => $reswap);
205  
206 }
207  
208 2 ▶sub _res_reselect {
Subroutine _res_reselect: called CC=2 SCAR=7.1
209  
210 2 my ($c, $reselect) = @_;
211 2 ▶ return undef unless $reselect;
Branch: 1 branch
BranchTrueFalseTotal
unless $reselect 0 2 2
212  
213 2 return $c->res->headers->header('HX-Reselect' => $reselect);
214  
215 }
216  
217 2 ▶sub _res_retarget {
Subroutine _res_retarget: called CC=2 SCAR=7.1
218  
219 2 my ($c, $retarget) = @_;
220 2 ▶ return undef unless $retarget;
Branch: 1 branch
BranchTrueFalseTotal
unless $retarget 0 2 2
221  
222 2 return $c->res->headers->header('HX-Retarget' => $retarget);
223  
224 }
225  
226 10 ▶sub _res_trigger {
Subroutine _res_trigger: called CC=6 SCAR=17.9
227  
228 10 my ($type, $c) = (shift, shift);
229 10 ▶ my $trigger = (@_ > 1) ? {@_} : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 6 4 10
230  
231 10 ▶ return undef unless $trigger;
Branch: 1 branch
BranchTrueFalseTotal
unless $trigger 0 10 10
232  
233 10 my $trigger_header = {after_settle => 'HX-Trigger-After-Settle', after_swap => 'HX-Trigger-After-Swap'};
234  
235 10 ▶ if (ref $trigger eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $trigger eq "HASH") 6 4 10
236 6 $trigger = encode_json($trigger);
237 }
238  
239 10 ▶ if (ref $trigger eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $trigger eq "ARRAY") 1 9 10
240 1 $trigger = join ', ', @{$trigger};
241 }
242  
243 10 ▶ my $header = $trigger_header->{$type} || 'HX-Trigger';
Condition100%2/2
$trigger_header->{$type} || 'HX-Trigger'
l!l
64
MC/DC100%1/1
$trigger_header->{$type} || 'HX-Trigger'
$trigger_header->{$type}
Truth table100%2/2
A
A= $trigger_header->{$type}
Aresult
11
00
244  
245 10 return $c->res->headers->header($header => $trigger);
246  
247 }
248  
249 2 1;
250  
251 =encoding utf8
252  
253 =head1 NAME
254  
255 Mojolicious::Plugin::HTMX - Mojolicious Plugin for htmx
256  
257 =head1 SYNOPSIS
258  
259 # Mojolicious
260 $self->plugin('Mojolicious::Plugin::HTMX');
261  
262 # Mojolicious::Lite
263 plugin 'Mojolicious::Plugin::HTMX';
264  
265 get '/trigger' => 'trigger';
266 post '/trigger' => sub ($c) {
267  
268 state $count = 0;
269 $count++;
270  
271 $c->htmx->res->trigger(showMessage => 'Here Is A Message');
272 $c->render(text => "Triggered $count times");
273  
274 };
275  
276 @@ template.html.ep
277 <html>
278 <head>
279 %= app->htmx->asset
280 </head>
281 <body>
282 %= content
283 </body>
284 </html>
285  
286 @@ trigger.html.ep
287 % layout 'default';
288 <h1>Trigger</h1>
289  
290 <!-- Common -->
291 <button hx-post="/trigger">Click Me</button>
292  
293 <!-- Use "hx" helper -->
294 %= tag 'button', hx(post => '/trigger'), 'Click Me'
295  
296 <!-- Use "hx_attr" helper -->
297 <button <%= hx_attr(post => '/trigger') %>>Click Me</button>
298  
299 <script>
300 document.body.addEventListener("showMessage", function(e){
301 alert(e.detail.value);
302 });
303 </script>
304  
305  
306 =head1 DESCRIPTION
307  
308 L<Mojolicious::Plugin::HTMX> is a L<Mojolicious> plugin to add htmx in your Mojolicious application.
309  
310  
311 =head1 HELPERS
312  
313 L<Mojolicious::Plugin::HTMX> implements the following helpers.
314  
315  
316 =head2 GENERIC HELPERS
317  
318 =head3 htmx->asset
319  
320 # Load htmx from "https://unpkg.com/htmx.org"
321 %= htmx->asset
322  
323 # Load htmx from a provided URL
324 %= htmx->asset(src => '/assets/js/htmx.min.js')
325  
326 # Load an extension from "/dist/ext" directory
327 %= htmx->asset(ext => 'debug')
328  
329 Generate C<script> tag for include htmx script file in your template.
330  
331 =head3 htmx->stop_polling
332  
333 Sets the HTTP status code to C<286> which is used by HTMX to halt polling requests.
334  
335 $c->htmx->stop_polling;
336  
337 =head3 is_htmx_request
338  
339 if ($c->is_htmx_request) {
340 # ...
341 }
342  
343 Based on C<HX-Request> header.
344  
345 =head3 hx
346  
347 %= tag 'button', hx(get => '/confirm', confirm => 'Confirm The Action'), 'Click For Confirm'
348 %= button_to Save => 'some_route', hx(patch => url_for('some_route'), swap => 'outerHTML', target => 'body')
349  
350 C<hx> helper convert the provided HASH attributes in "hx-" format. C<hx> helper is useful when use L<Mojolicious::Plugin::TagHelpers> helpers.
351  
352 =head3 hx_attr
353  
354 Alias for L<hx>.
355  
356 C<hx_attr> helper convert the HASH atteibutes in "hx-" format and generate a well-format string.
357  
358 <button <%= hx_attr(get => '/confirm', confirm => 'Confirm The Action') %>>Click For Confirm</button>
359  
360 is equivalent to:
361  
362 <button hx-get="/confirm" hx-confirm="Confirm The Action">Click For Confirm</button>
363  
364  
365 =head2 REQUEST HELPERS
366  
367 =head3 htmx->req->boosted
368  
369 Indicates that the request is via an element using C<hx-boost>.
370  
371 Based on C<HX-Boosted> header.
372  
373 =head3 htmx->req->current_url
374  
375 The current URL of the browser.
376  
377 Based on C<HX-Current-URL> header.
378  
379 =head3 htmx->req->history_restore_request
380  
381 C<true> if the request is for history restoration after a miss in the local history cache.
382  
383 Based on C<HX-History-Restore-Request> header.
384  
385 =head3 htmx->req->prompt
386  
387 The user response to an C<hx-prompt>.
388  
389 Based on C<HX-Prompt> header.
390  
391 =head3 htmx->req->request
392  
393 Always C<true>.
394  
395 Based on C<HX-Request> header.
396  
397 =head3 htmx->req->target
398  
399 The C<id> of the target element if it exists.
400  
401 Based on C<HX-Target> header.
402  
403 =head3 htmx->req->trigger_name
404  
405 The C<name> of the triggered element if it exists.
406  
407 Based on C<HX-Trigger-Name> header.
408  
409 =head3 htmx->req->trigger
410  
411 The C<id> of the triggered element if it exists.
412  
413 Based on C<HX-Trigger> header.
414  
415 =head3 htmx->req->to_hash
416  
417 Turn htmx request into a hash reference.
418  
419  
420 =head2 RESPONSE HELPERS
421  
422 =head3 htmx->res->location
423  
424 Allows you to do a client-side redirect that does not do a full page reload.
425  
426 Based on C<HX-Location> header.
427  
428 =head3 htmx->res->push_url
429  
430 Pushes a new url into the history stack.
431  
432 Based on C<HX-Push-Url> header.
433  
434 =head3 htmx->res->redirect
435  
436 Can be used to do a client-side redirect to a new location.
437  
438 Based on C<HX-Redirect> header.
439  
440 =head3 htmx->res->refresh
441  
442 Full refresh of the page.
443  
444 Based on C<HX-Refresh> header.
445  
446 =head3 htmx->res->replace_url
447  
448 Replaces the current URL in the location bar.
449  
450 Based on C<HX-Replace-Url> header.
451  
452 =head3 htmx->res->reswap
453  
454 Allows you to specify how the response will be swapped.
455  
456 The possible values of this attribute are:
457  
458 =over
459  
460 =item C<innerHTML> The default, replace the inner html of the target element
461  
462 =item C<outerHTML> Replace the entire target element with the response
463  
464 =item C<beforebegin> Insert the response before the target element
465  
466 =item C<afterbegin> Insert the response before the first child of the target element
467  
468 =item C<beforeend> Insert the response after the last child of the target element
469  
470 =item C<afterend> Insert the response after the target element
471  
472 =item C<delete> Deletes the target element regardless of the response
473  
474 =item C<none> Does not append content from response (out of band items will still be processed).
475  
476 =back
477  
478 Based on C<HX-Reswap> header.
479  
480 =head3 htmx->res->reselect
481  
482 A CSS selector that allows you to choose which part of the response is used to be swapped in. Overrides an existing C<hx-select> on the triggering element
483  
484 Based on C<HX-Reselect> header.
485  
486 =head3 htmx->res->retarget
487  
488 A CSS selector that updates the target of the content update to a different element on the page.
489  
490 Based on C<HX-Retarget> header.
491  
492 =head3 htmx->res->trigger
493  
494 Allows you to trigger client side events, see L<https://htmx.org/headers/hx-trigger> for more info.
495  
496 To trigger a single event with no additional details you can simply send the event name like so:
497  
498 if ($c->is_htmx_request) {
499 $c->htmx->res->trigger('myEvent');
500 }
501  
502 If you want to pass details along with the event, you can use HASH for the value of the trigger:
503  
504 if ($c->is_htmx_request) {
505 $c->htmx->res->trigger( showMessage => 'Here Is A Message' );
506 }
507  
508 if ($c->is_htmx_request) {
509 $c->htmx->res->trigger(
510 showMessage => {
511 level => 'info',
512 message => 'Here Is A Message'
513 }
514 );
515 }
516  
517 If you wish to invoke multiple events, you can simply add additional keys to the HASH:
518  
519 if ($c->is_htmx_request) {
520 $c->htmx->res->trigger(
521 event1 => 'A message',
522 event2 => 'Another message'
523 );
524 }
525
526 You may also trigger multiple events with no additional details by sending event
527 names in ARRAY:
528  
529 if ($c->is_htmx_request) {
530 $c->htmx->res->trigger(['event1', 'event2']);
531 }
532  
533 Based on C<HX-Trigger> header.
534  
535 =head3 htmx->res->trigger_after_settle
536  
537 Allows you to trigger client side events, see L<https://htmx.org/headers/hx-trigger> for more info.
538  
539 Based on C<HX-Trigger-After-Settle> header.
540  
541 =head3 htmx->res->trigger_after_swap
542  
543 Allows you to trigger client side events, see L<https://htmx.org/headers/hx-trigger> for more info.
544  
545 Based on C<HX-Trigger-After-Swap> header.
546  
547  
548 =head1 METHODS
549  
550 L<Mojolicious::Plugin::HTMX> inherits all methods from
551 L<Mojolicious::Plugin> and implements the following new ones.
552  
553 =head2 register
554  
555 $plugin->register(Mojolicious->new);
556  
557 Register plugin in L<Mojolicious> application.
558  
559  
560 =head1 SEE ALSO
561  
562 L<Mojolicious>, L<Mojolicious::Guides>, L<https://mojolicious.org>.
563  
564  
565 =head1 SUPPORT
566  
567 =head2 Bugs / Feature Requests
568  
569 Please report any bugs or feature requests through the issue tracker
570 at L<https://github.com/giterlizzi/perl-Mojolicious-Plugin-HTMX/issues>.
571 You will be notified automatically of any progress on your issue.
572  
573 =head2 Source Code
574  
575 This is open source software. The code repository is available for
576 public review and contribution under the terms of the license.
577  
578 L<https://github.com/giterlizzi/perl-Mojolicious-Plugin-HTMX>
579  
580 git clone https://github.com/giterlizzi/perl-Mojolicious-Plugin-HTMX.git
581  
582  
583 =head1 AUTHORS
584  
585 =over 4
586  
587 =item * Giuseppe Di Terlizzi <gdt@cpan.org>
588  
589 =back
590  
591  
592 =head1 COPYRIGHT AND LICENSE
593  
594 Copyright (c) 2022-2024, Giuseppe Di Terlizzi
595  
596 This program is free software, you can redistribute it and/or modify it under
597 the terms of the Artistic License version 2.0.
598  
599 =cut
↑ Summary