| 1 | # MIT License | ||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||
| 3 | # Copyright (c) 2026 Rawley Fowler <rawleyfowler@proton.me> | ||||||||||||||||||||||||||||
| 4 | # | ||||||||||||||||||||||||||||
| 5 | # Permission is hereby granted, free of charge, to any person obtaining a copy | ||||||||||||||||||||||||||||
| 6 | # of this software and associated documentation files (the "Software"), to deal | ||||||||||||||||||||||||||||
| 7 | # in the Software without restriction, including without limitation the rights | ||||||||||||||||||||||||||||
| 8 | # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||||||||||||||||||||||||||||
| 9 | # copies of the Software, and to permit persons to whom the Software is | ||||||||||||||||||||||||||||
| 10 | # furnished to do so, subject to the following conditions: | ||||||||||||||||||||||||||||
| 11 | # | ||||||||||||||||||||||||||||
| 12 | # The above copyright notice and this permission notice shall be included in all | ||||||||||||||||||||||||||||
| 13 | # copies or substantial portions of the Software. | ||||||||||||||||||||||||||||
| 14 | # | ||||||||||||||||||||||||||||
| 15 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||||||||||||||||||||||||||||
| 16 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||||||||||||||||||||||||||||
| 17 | # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||||||||||||||||||||||||||||
| 18 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||||||||||||||||||||||||||||
| 19 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||||||||||||||||||||||||||||
| 20 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||||||||||||||||||||||||||||
| 21 | # SOFTWARE. | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | package HTML::Composer; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =head1 NAME | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | HTML::Composer - Compose validated HTML from Perl data structures | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | HTML::Composer is inspired by TyXML and Hiccup to provide a data-driven HTML builder for Perl that allows | ||||||||||||||||||||||||||||
| 32 | developers to compose data to validated HTML in an efficient, intuitive, high-performance way. | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | use HTML::Composer; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | my $h = HTML::Composer->new(); | ||||||||||||||||||||||||||||
| 37 | my $html = $h->html([ | ||||||||||||||||||||||||||||
| 38 | head => [ | ||||||||||||||||||||||||||||
| 39 | title => ["My Site"], | ||||||||||||||||||||||||||||
| 40 | script => { | ||||||||||||||||||||||||||||
| 41 | src => "/js/myScript.js", | ||||||||||||||||||||||||||||
| 42 | type => "text/javascript" | ||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | ], | ||||||||||||||||||||||||||||
| 45 | body => [ | ||||||||||||||||||||||||||||
| 46 | h1 => ["Hello World!"], | ||||||||||||||||||||||||||||
| 47 | br => {}, | ||||||||||||||||||||||||||||
| 48 | div => { class => [ "p-3", "background-red" ] } => [ | ||||||||||||||||||||||||||||
| 49 | "Hello World!", h2 => ["Test 123"] | ||||||||||||||||||||||||||||
| 50 | ] | ||||||||||||||||||||||||||||
| 51 | ] | ||||||||||||||||||||||||||||
| 52 | ] | ||||||||||||||||||||||||||||
| 53 | ); | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | This will output the following HTML: | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | <!DOCTYPE html> | ||||||||||||||||||||||||||||
| 58 | <html> | ||||||||||||||||||||||||||||
| 59 | <head> | ||||||||||||||||||||||||||||
| 60 | <title>My Site</title> | ||||||||||||||||||||||||||||
| 61 | <script src="/js/myScript.js" type="text/javascript"></script> | ||||||||||||||||||||||||||||
| 62 | </head> | ||||||||||||||||||||||||||||
| 63 | <body> | ||||||||||||||||||||||||||||
| 64 | <h1>Hello World</h1> | ||||||||||||||||||||||||||||
| 65 | <br> | ||||||||||||||||||||||||||||
| 66 | <div class="p-3 background-red"> | ||||||||||||||||||||||||||||
| 67 | Hello World! | ||||||||||||||||||||||||||||
| 68 | <h2>Test 123</h2> | ||||||||||||||||||||||||||||
| 69 | </div> | ||||||||||||||||||||||||||||
| 70 | </body> | ||||||||||||||||||||||||||||
| 71 | </html> | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | HTML elements that allow children are created like: | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | [div => ["Text!", h1 => ["Text!"]]] # <div>Text!<h1>Text!</h1></div> | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | To provide attributes to a tag: | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | [div => { class => ["p-3", "m-2"] } => ["Text!", h1 => ["Text!"]]] # <div class="p-3 m-2">Text!<h1>Text!</h1></div> | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | If a tag doesn't have any children, ie a <link> tag: | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | [link => { href => "www.google.com" }] # <link href="www.google.com"> | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | If a tag doesn't have any attributes: | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | [br => {}] # <br> | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | To render just text, make sure it isn't followed up by an array, or hash: | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | ["Text!"] | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 97 | 8 | ▶ | use feature qw(state); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 8 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 100 | 8 | ▶ | use HTML::Escape qw(escape_html); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 8 | ▶ | use Sereal::Encoder; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 103 | 8 | ▶ | use HTML::Composer::Unsafe; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 8 | my @tags_list = qw( | |||||||||||||||||||||||||||
| 106 | a abbr address area article aside audio b base bdi bdo blockquote body | ||||||||||||||||||||||||||||
| 107 | br button canvas caption cite code col colgroup data datalist dd del | ||||||||||||||||||||||||||||
| 108 | details dfn dialog div dl dt em embed fieldset figcaption figure | ||||||||||||||||||||||||||||
| 109 | footer form h1 h2 h3 h4 h5 h6 head header hgroup hr html i iframe | ||||||||||||||||||||||||||||
| 110 | img input ins kbd label legend li link main map mark menu meta | ||||||||||||||||||||||||||||
| 111 | meter nav noscript object ol optgroup option output p param picture | ||||||||||||||||||||||||||||
| 112 | plaintext pre progress q rp rt ruby s samp script search section | ||||||||||||||||||||||||||||
| 113 | select slot small source span strong style sub summary sup table | ||||||||||||||||||||||||||||
| 114 | tbody td template textarea tfoot th thead time title tr track u | ||||||||||||||||||||||||||||
| 115 | ul var video wbr | ||||||||||||||||||||||||||||
| 116 | ); | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 8 | my %tags_map = map { $_ => 1 } @tags_list; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =head2 new(%ARGS) | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | Create a new instance of HTML::Composer. Optionally, pass a cache argument, to tell | ||||||||||||||||||||||||||||
| 123 | HTML::Hash to cache the result against the hash you pass. (Caching defaults to false). | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | my $h = HTML::Composer->new(); # Cached instance of HTML::Composer | ||||||||||||||||||||||||||||
| 126 | $h->html(...); | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | my $hc = HTML::Composer->new(cache => 0); # Don't cache templates | ||||||||||||||||||||||||||||
| 129 | $hc->html(...); | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 11 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 11 | my ( $class, %args ) = @_; | |||||||||||||||||||||||||||
| 135 | return bless { | ||||||||||||||||||||||||||||
| 136 | 11 | ▶ | cache => $args{cache} // 0, | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{"cache"} // 0
MC/DC100%1/1
$args{"cache"} // 0
$args{"cache"}
Truth table100%2/2
A
A= $args{"cache"}
| |||||||||||||||||||||||||||||
| 137 | store => {}, | ||||||||||||||||||||||||||||
| 138 | encoder => Sereal::Encoder->new( | ||||||||||||||||||||||||||||
| 139 | { | ||||||||||||||||||||||||||||
| 140 | snappy => 1, | ||||||||||||||||||||||||||||
| 141 | croak_on_bless => 1, | ||||||||||||||||||||||||||||
| 142 | stringify_unknown => 1, | ||||||||||||||||||||||||||||
| 143 | canonical => 1 | ||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||
| 145 | ) | ||||||||||||||||||||||||||||
| 146 | }, $class; | ||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | =head2 html(ARRAY) | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | Create a string containing the HTML page described in ARRAY. Croaks if HTML validation fails. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | my $h = HTML::Composer->new(); | ||||||||||||||||||||||||||||
| 154 | my $html = $h->html([ | ||||||||||||||||||||||||||||
| 155 | head => [ | ||||||||||||||||||||||||||||
| 156 | title => ["My Site"], | ||||||||||||||||||||||||||||
| 157 | script => { | ||||||||||||||||||||||||||||
| 158 | src => "/js/myScript.js", | ||||||||||||||||||||||||||||
| 159 | type => "text/javascript" | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | ], | ||||||||||||||||||||||||||||
| 162 | body => [ | ||||||||||||||||||||||||||||
| 163 | h1 => ["Hello World!"], | ||||||||||||||||||||||||||||
| 164 | br => {}, | ||||||||||||||||||||||||||||
| 165 | div => { class => [ "p-3", "background-red" ] } => [ | ||||||||||||||||||||||||||||
| 166 | "Hello World!", h2 => ["Test 123"] | ||||||||||||||||||||||||||||
| 167 | ] | ||||||||||||||||||||||||||||
| 168 | ] | ||||||||||||||||||||||||||||
| 169 | ] | ||||||||||||||||||||||||||||
| 170 | ); | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | If you need to pass attributes to the root <html> tag you can do so with: | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | my $h = HTML::Composer->new(); | ||||||||||||||||||||||||||||
| 175 | my $html = $h->html({lang => 'en'} => [ | ||||||||||||||||||||||||||||
| 176 | head => [ | ||||||||||||||||||||||||||||
| 177 | title => ["My Site"], | ||||||||||||||||||||||||||||
| 178 | script => { | ||||||||||||||||||||||||||||
| 179 | src => "/js/myScript.js", | ||||||||||||||||||||||||||||
| 180 | type => "text/javascript" | ||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | ], | ||||||||||||||||||||||||||||
| 183 | body => [ | ||||||||||||||||||||||||||||
| 184 | h1 => ["Hello World!"], | ||||||||||||||||||||||||||||
| 185 | br => {}, | ||||||||||||||||||||||||||||
| 186 | div => { class => [ "p-3", "background-red" ] } => [ | ||||||||||||||||||||||||||||
| 187 | "Hello World!", h2 => ["Test 123"] | ||||||||||||||||||||||||||||
| 188 | ] | ||||||||||||||||||||||||||||
| 189 | ] | ||||||||||||||||||||||||||||
| 190 | ] | ||||||||||||||||||||||||||||
| 191 | ); | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | =cut | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 36 | ▶ | sub html { | ||||||||||||||||||||||||||
|
Subroutine
html: called CC=17 SCAR=28.4
Pod
documented
| |||||||||||||||||||||||||||||
| 196 | 36 | my ( $self, @args ) = @_; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 36 | my $h; | |||||||||||||||||||||||||||
| 199 | my $ha; | ||||||||||||||||||||||||||||
| 200 | 36 | ▶ | if ( @args > 1 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 201 | 4 | $ha = shift(@args); | |||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | 36 | $h = shift(@args); | |||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 36 | my $rh = ref($h); | |||||||||||||||||||||||||||
| 206 | 36 | my $rha = ref($ha); | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 36 | ▶ | croak "Bad value passed to HTML::Composer for" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$rha and $rha ne "HASH"
MC/DC100%2/2
$rha and $rha ne "HASH"
$rha
$rha ne "HASH"
Truth table100%3/3
A and B
A= $rhaB= $rha ne "HASH"
| |||||||||||||||||||||||||||||
| 209 | . "html attrs expected HASH got $rha" | ||||||||||||||||||||||||||||
| 210 | if ( $rha && $rha ne 'HASH' ); | ||||||||||||||||||||||||||||
| 211 | 35 | ▶ | croak "Bad value passed to HTML::Composer, expected ARRAY got $rh" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $rh or $rh ne "ARRAY"
MC/DC0%0/2
not $rh or $rh ne "ARRAY"
not $rh
$rh ne "ARRAY"
Truth table33%1/3
not(A) or B
A= not $rhB= $rh ne "ARRAY"
| |||||||||||||||||||||||||||||
| 212 | if ( !$rh || $rh ne 'ARRAY' ); | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 35 | my $s = scalar(@$h); | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | # We expect 4 args, head, (head elements), body, (body elements) | ||||||||||||||||||||||||||||
| 217 | 35 | ▶ | croak "Invalid number of elements in ARRAY, expected 4 got $s" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 218 | unless $s == 4; | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 33 | my ( $hs, $hb, $bs, $bb ) = @$h; | |||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | 33 | ▶ | croak "Invalid head tag: $hs expected head" unless $hs eq 'head'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 223 | 32 | ▶ | croak "Invalid body tag: $bs expected body" unless $bs eq 'body'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 31 | my $hr = ref($hb); | |||||||||||||||||||||||||||
| 226 | 31 | ▶ | croak "Invalid head type, expected ARRAY got $hb" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$hr and $hr eq "ARRAY"
MC/DC50%1/2
$hr and $hr eq "ARRAY"
$hr
$hr eq "ARRAY"
Truth table66%2/3
A and B
A= $hrB= $hr eq "ARRAY"
| |||||||||||||||||||||||||||||
| 227 | unless $hr && $hr eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 228 | 30 | my $br = ref($bb); | |||||||||||||||||||||||||||
| 229 | 30 | ▶ | croak "Invalid body type, expected ARRAY got $br" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$br and $br eq "ARRAY"
MC/DC50%1/2
$br and $br eq "ARRAY"
$br
$br eq "ARRAY"
Truth table66%2/3
A and B
A= $brB= $br eq "ARRAY"
| |||||||||||||||||||||||||||||
| 230 | unless $br && $br eq 'ARRAY'; | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | 29 | ▶ | my $html = [ html => ( $ha ? $ha : () ) => $h ]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 29 | ▶ | if ( $self->{cache} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 235 | 4 | my $encoded = $self->{encoder}->encode($html); | |||||||||||||||||||||||||||
| 236 | 4 | ▶ | if ( $self->{store}->{$encoded} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 2 | return $self->{store}->{$encoded}; | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | } | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 27 | my $root = _render( $html, "<!DOCTYPE html>" ); | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 25 | ▶ | if ( $self->{cache} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 244 | 2 | $self->{store}->{ $self->{encoder}->encode($html) } = $root; | |||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | 25 | return $root; | |||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | =head2 partial(ARRAY) | ||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | Create a string containing the HTML partial described in ARRAY. Must have one root element. | ||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | use HTML::Composer; | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | my $h = HTML::Composer->new; | ||||||||||||||||||||||||||||
| 257 | my $html = $h->partial([ | ||||||||||||||||||||||||||||
| 258 | div => [ | ||||||||||||||||||||||||||||
| 259 | "Hello, World!", | ||||||||||||||||||||||||||||
| 260 | a => { href => "https://www.google.com" } => ["www.google.com"] | ||||||||||||||||||||||||||||
| 261 | ] | ||||||||||||||||||||||||||||
| 262 | ]); | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | say $html; # <div>Hello, World!<a href="https://www.google.com">www.google.com</a></div> | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =cut | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 7 | ▶ | sub partial { | ||||||||||||||||||||||||||
|
Subroutine
partial: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 269 | 7 | my ( $self, $h ) = @_; | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 7 | ▶ | croak 'partial expects a single argument of type ARRAY' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not ref $h or ref $h ne "ARRAY"
MC/DC100%2/2
not ref $h or ref $h ne "ARRAY"
not ref $h
ref $h ne "ARRAY"
Truth table100%3/3
not(A) or B
A= not ref $hB= ref $h ne "ARRAY"
| |||||||||||||||||||||||||||||
| 272 | if !ref($h) || ref($h) ne 'ARRAY'; | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | 5 | return _render( $h, '' ); | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | =head2 unsafe(SCALAR) | ||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||
| 279 | Create an instance of L<HTML::Composer::Unsafe>, these objects encapsulate the value you provide, | ||||||||||||||||||||||||||||
| 280 | and are not escaped by HTML::Composer when the HTML is rendered. | ||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | use HTML::Composer; | ||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | my $h = HTML::Composer->new; | ||||||||||||||||||||||||||||
| 285 | my $unsafe_text = $h->unsafe(q[document.body.addEventListener('htmx:configRequest', (event) => {})]); | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | ref($unsafe_text) # HTML::Composer::Unsafe | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | my $html = $h->html([ | ||||||||||||||||||||||||||||
| 290 | head => [ | ||||||||||||||||||||||||||||
| 291 | title => ["My Site!"], | ||||||||||||||||||||||||||||
| 292 | script => [$unsafe_text] | ||||||||||||||||||||||||||||
| 293 | ], | ||||||||||||||||||||||||||||
| 294 | body => [ | ||||||||||||||||||||||||||||
| 295 | div => [ | ||||||||||||||||||||||||||||
| 296 | "Hello World!" | ||||||||||||||||||||||||||||
| 297 | ] | ||||||||||||||||||||||||||||
| 298 | ] | ||||||||||||||||||||||||||||
| 299 | ]); | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | =cut | ||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||
| 303 | 2 | ▶ | sub unsafe { | ||||||||||||||||||||||||||
|
Subroutine
unsafe: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 304 | 2 | my ( $self, $str ) = @_; | |||||||||||||||||||||||||||
| 305 | 2 | return HTML::Composer::Unsafe->new($str); | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 32 | ▶ | sub _render { | ||||||||||||||||||||||||||
|
Subroutine
_render: called CC=24 SCAR=32.4
| |||||||||||||||||||||||||||||
| 309 | 32 | my ( $html, $root ) = @_; | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | 32 | my @stack = reverse(@$html); | |||||||||||||||||||||||||||
| 312 | 32 | my @a = ($root); | |||||||||||||||||||||||||||
| 313 | 32 | ▶ | while (@stack) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | 5834 | my $o = pop(@stack); | |||||||||||||||||||||||||||
| 315 | 5834 | my $ro = ref($o); | |||||||||||||||||||||||||||
| 316 | 5834 | ▶ | if ( !$ro ) { # Plain text or tag | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 317 | 2358 | ▶ | if ( my $rsp = ref( $stack[-1] ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 318 | 2354 | my $rsp_array = $rsp eq 'ARRAY'; | |||||||||||||||||||||||||||
| 319 | 2354 | my $rsp_hash = $rsp eq 'HASH'; | |||||||||||||||||||||||||||
| 320 | 2354 | ▶ | if ( $rsp && ( $rsp_array | $rsp_hash ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rsp and $rsp_array | $rsp_hash
MC/DC50%1/2
$rsp and $rsp_array | $rsp_hash
$rsp
$rsp_array | $rsp_hash
Truth table66%2/3
A and B
A= $rspB= $rsp_array | $rsp_hash
| |||||||||||||||||||||||||||||
| 321 | { # It is a tag if it has a follow up attr HASH or, body ARRAY | ||||||||||||||||||||||||||||
| 322 | 1388 | push @a, '<', my $eo = escape_html($o); | |||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | croak "Invalid tag: <$eo>, expected one of " | ||||||||||||||||||||||||||||
| 325 | . join( ', ', @tags_list ) | ||||||||||||||||||||||||||||
| 326 | 1388 | ▶ | if !$tags_map{$eo}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 1388 | ▶ | if ( !grep { $_ eq $eo } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | qw(br img input meta link hr col source) ) | ||||||||||||||||||||||||||||
| 330 | { | ||||||||||||||||||||||||||||
| 331 | 1371 | ▶ | if ($rsp_array) { # If no attributes | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 332 | 647 | push @a, '>'; | |||||||||||||||||||||||||||
| 333 | 647 | splice( @stack, scalar(@stack) - 1, 0, \$o ); | |||||||||||||||||||||||||||
| 334 | } | ||||||||||||||||||||||||||||
| 335 | elsif ($rsp_hash) { | ||||||||||||||||||||||||||||
| 336 | 724 | my $rsb = ref( $stack[-2] ); | |||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 724 | ▶ | if ( $rsb && $rsb eq 'ARRAY' ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$rsb and $rsb eq "ARRAY"
MC/DC50%1/2
$rsb and $rsb eq "ARRAY"
$rsb
$rsb eq "ARRAY"
Truth table66%2/3
A and B
A= $rsbB= $rsb eq "ARRAY"
| |||||||||||||||||||||||||||||
| 339 | 722 | splice( @stack, scalar(@stack) - 2, 0, \$o ); | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | else { | ||||||||||||||||||||||||||||
| 342 | 2 | splice( @stack, scalar(@stack) - 1, 0, \$o ); | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | else { | ||||||||||||||||||||||||||||
| 347 | 17 | ▶ | croak "Cannot pass children to tag of type $o" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | if ( $rsp ne 'HASH' ) | ||||||||||||||||||||||||||||
| 349 | ; # Non child elements, ie meta, link, etc (CANNOT HAVE CHILDREN) | ||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | else { | ||||||||||||||||||||||||||||
| 353 | 966 | push @a, escape_html($o); | |||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | else { | ||||||||||||||||||||||||||||
| 357 | 4 | push @a, escape_html($o); | |||||||||||||||||||||||||||
| 358 | } | ||||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | elsif ( $ro eq 'HASH' ) { # Attributes | ||||||||||||||||||||||||||||
| 361 | 739 | for ( sort keys %$o ) { | |||||||||||||||||||||||||||
| 362 | 950 | my $ra = ref( $o->{$_} ); | |||||||||||||||||||||||||||
| 363 | my $av = ( $ra && $ra eq 'ARRAY' ) | ||||||||||||||||||||||||||||
| 364 | ? join( | ||||||||||||||||||||||||||||
| 365 | ' ', | ||||||||||||||||||||||||||||
| 366 | map { | ||||||||||||||||||||||||||||
| 367 | 409 | ▶ | ref($_) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $_ and ref $_ eq "HTML::Composer::Unsafe"
MC/DC0%0/2
ref $_ and ref $_ eq "HTML::Composer::Unsafe"
ref $_
ref $_ eq "HTML::Composer::Unsafe"
Truth table33%1/3
A and B
A= ref $_B= ref $_ eq "HTML::Composer::Unsafe"
| |||||||||||||||||||||||||||||
| 368 | && ref($_) eq 'HTML::Composer::Unsafe' | ||||||||||||||||||||||||||||
| 369 | ? $_ | ||||||||||||||||||||||||||||
| 370 | : escape_html($_) | ||||||||||||||||||||||||||||
| 371 | 204 | ▶ | } @{ $o->{$_} } | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$ra and $ra eq "HTML::Composer::Unsafe"
MC/DC0%0/2
$ra and $ra eq "HTML::Composer::Unsafe"
$ra
$ra eq "HTML::Composer::Unsafe"
Truth table33%1/3
A and B
A= $raB= $ra eq "HTML::Composer::Unsafe"
| |||||||||||||||||||||||||||||
| 372 | ) | ||||||||||||||||||||||||||||
| 373 | : ( $ra && $ra eq 'HTML::Composer::Unsafe' ) ? $o->{$_} | ||||||||||||||||||||||||||||
| 374 | 950 | ▶ | : escape_html( $o->{$_} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$ra and $ra eq "ARRAY"
MC/DC50%1/2
$ra and $ra eq "ARRAY"
$ra
$ra eq "ARRAY"
Truth table66%2/3
A and B
A= $raB= $ra eq "ARRAY"
| |||||||||||||||||||||||||||||
| 375 | 950 | push @a, ' ', escape_html($_), '="', $av, '"'; | |||||||||||||||||||||||||||
| 376 | } | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 739 | push @a, '>'; | |||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||
| 380 | elsif ( $ro eq 'ARRAY' ) { # Child elements | ||||||||||||||||||||||||||||
| 381 | 1369 | my $elems = scalar @$o; | |||||||||||||||||||||||||||
| 382 | 1369 | push @stack, reverse(@$o); | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | elsif ( $ro eq 'SCALAR' ) | ||||||||||||||||||||||||||||
| 385 | { # This is our close marker, they are pushed to be after the child elements of a tag | ||||||||||||||||||||||||||||
| 386 | 1367 | my $elem = $$o; | |||||||||||||||||||||||||||
| 387 | 1367 | push @a, '</', escape_html($elem), '>'; | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | elsif ( $ro eq 'HTML::Composer::Unsafe' ) { | ||||||||||||||||||||||||||||
| 390 | 1 | push @a, $o . ''; | |||||||||||||||||||||||||||
| 391 | } | ||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||
| 393 | |||||||||||||||||||||||||||||
| 394 | 30 | return join( '', @a ); | |||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 8 | 1; | |||||||||||||||||||||||||||