| 1 | ############################################################################## | ||
| 2 | ## | ||
| 3 | ## Web::Reactor application machinery | ||
| 4 | ## Copyright (c) 2013-2022 Vladi Belperchinov-Shabanski "Cade" | ||
| 5 | ## <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||
| 6 | ## http://cade.noxrun.com | ||
| 7 | ## | ||
| 8 | ## LICENSE: GPLv2 | ||
| 9 | ## https://github.com/cade-vs/perl-web-reactor | ||
| 10 | ## | ||
| 11 | ############################################################################## | ||
| 12 | ## | ||
| 13 | ## HTML Utils | ||
| 14 | ## | ||
| 15 | ## All checks and escaping is done accordingly to HTML Living standard from | ||
| 16 | ## 5 September 2022, chapter notes are given in the comments | ||
| 17 | ## https://html.spec.whatwg.org/multipage/ | ||
| 18 | ## | ||
| 19 | ############################################################################## | ||
| 20 | package Web::Reactor::HTML::Utils; | ||
| 21 | use Exporter; | ||
| 22 | our @ISA = qw( Exporter ); | ||
| 23 | our @EXPORT = qw( | ||
| 24 | html_escape | ||
| 25 | |||
| 26 | html_element | ||
| 27 | html_table | ||
| 28 | |||
| 29 | html_ftree | ||
| 30 | |||
| 31 | html_hover_layer | ||
| 32 | html_popup_layer | ||
| 33 | |||
| 34 | html_alink | ||
| 35 | |||
| 36 | html_tabs_table | ||
| 37 | |||
| 38 | html_check_tag_name | ||
| 39 | html_check_tag_name_boom | ||
| 40 | |||
| 41 | html_check_attr_name | ||
| 42 | html_check_attr_name_boom | ||
| 43 | |||
| 44 | ); | ||
| 45 | use strict; | ||
| 46 | use Exception::Sink; | ||
| 47 | use Data::Tools; | ||
| 48 | use Web::Reactor::HTML::Tab; | ||
| 49 | use Web::Reactor::HTML::Layout; | ||
| 50 | |||
| 51 | ############################################################################## | ||
| 52 | |||
| 53 | sub html_escape | ||
| 54 | { | ||
| 55 | return Data::Tools::str_html_escape( @_ ); | ||
| 56 | } | ||
| 57 | |||
| 58 | ############################################################################## | ||
| 59 | |||
| 60 | sub html_element | ||
| 61 | { | ||
| 62 | my $tag = shift; | ||
| 63 | my $txt = shift; | ||
| 64 | my %atr = @_; | ||
| 65 | |||
| 66 | hash_lc_ipl( \%atr ); | ||
| 67 | |||
| 68 | my $extra = $atr{ 'extra' }; | ||
| 69 | delete $atr{ 'extra' }; | ||
| 70 | |||
| 71 | html_check_tag_name_boom( $tag ); | ||
| 72 | |||
| 73 | my $html; | ||
| 74 | |||
| 75 | $html .= "<$tag "; | ||
| 76 | |||
| 77 | while( my ( $k, $v ) = each %atr ) | ||
| 78 | { | ||
| 79 | html_check_attr_name_boom( $k, "invalid attribute name [$k] for tag [$tag]" ); | ||
| 80 | $html .= $k; | ||
| 81 | if( defined $v ) | ||
| 82 | { | ||
| 83 | my $vv = str_html_escape_attr( $v ); | ||
| 84 | $html .= "='$vv'"; | ||
| 85 | } | ||
| 86 | $html .= " "; | ||
| 87 | } | ||
| 88 | |||
| 89 | $html .= $extra; | ||
| 90 | $html .= defined $txt ? ">$txt</$tag>" : "/>"; | ||
| 91 | |||
| 92 | use Data::Dumper; | ||
| 93 | print STDERR Dumper( "\n"x10, $tag, $txt, \%atr, $html, "\n"x100 ); | ||
| 94 | |||
| 95 | return $html . "\n"; | ||
| 96 | } | ||
| 97 | |||
| 98 | ############################################################################## | ||
| 99 | |||
| 100 | =pod | ||
| 101 | |||
| 102 | flat tree, represented by single table with rows | ||
| 103 | |||
| 104 | DEMO: | ||
| 105 | |||
| 106 | my $a = [ | ||
| 107 | 'opa', | ||
| 108 | 'tralala', | ||
| 109 | 'heyo', | ||
| 110 | 'yoyo', | ||
| 111 | 'didi', | ||
| 112 | { | ||
| 113 | LABEL => 'opa', | ||
| 114 | DATA => [ | ||
| 115 | 'tralala', | ||
| 116 | 'heyo', | ||
| 117 | { | ||
| 118 | LABEL => 'sesssil', | ||
| 119 | DATA => [ | ||
| 120 | 'tralala', | ||
| 121 | 'heyo', | ||
| 122 | 'yoyo', | ||
| 123 | 'didi', | ||
| 124 | ], | ||
| 125 | }, | ||
| 126 | 'yoyo', | ||
| 127 | 'didi', | ||
| 128 | ], | ||
| 129 | }, | ||
| 130 | 'heyo', | ||
| 131 | 'yoyo', | ||
| 132 | ]; | ||
| 133 | |||
| 134 | print html_ftree( $a, 'ARGS' => 'cellpadding=10 width=100% border=0' ); | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | my $ftree_item_id; | ||
| 139 | sub html_ftree | ||
| 140 | { | ||
| 141 | my $data = shift; | ||
| 142 | my %opt = @_; | ||
| 143 | |||
| 144 | my $t_args; | ||
| 145 | $t_args ||= $opt{ 'ARGS' }; | ||
| 146 | $t_args ||= 'class=' . $opt{ 'CLASS' } if $opt{ 'CLASS' }; | ||
| 147 | |||
| 148 | $ftree_item_id++; | ||
| 149 | |||
| 150 | my $ftree_table_id = "FTREE_TABLE_$ftree_item_id"; | ||
| 151 | |||
| 152 | my $html; | ||
| 153 | |||
| 154 | $html .= "\n"; | ||
| 155 | $html .= "<table id=$ftree_table_id $t_args>"; | ||
| 156 | |||
| 157 | $html .= __html_ftree_branch( $data, $ftree_table_id, $ftree_table_id . '.', 0, \%opt ); | ||
| 158 | |||
| 159 | $html .= "</table>"; | ||
| 160 | $html .= "\n"; | ||
| 161 | |||
| 162 | return $html; | ||
| 163 | } | ||
| 164 | |||
| 165 | sub __html_ftree_branch | ||
| 166 | { | ||
| 167 | my $data = shift; | ||
| 168 | my $ftree_table_id = shift; | ||
| 169 | my $branch_id = shift; | ||
| 170 | my $level = shift; | ||
| 171 | my $opt = shift; | ||
| 172 | |||
| 173 | my $html; | ||
| 174 | |||
| 175 | $html .= "\n"; | ||
| 176 | |||
| 177 | for my $row ( @$data ) | ||
| 178 | { | ||
| 179 | my $label; | ||
| 180 | my $data; | ||
| 181 | |||
| 182 | my $r_args; # row args | ||
| 183 | my $c_args; # cell args | ||
| 184 | |||
| 185 | if( ref( $row ) eq 'HASH' ) | ||
| 186 | { | ||
| 187 | $label = $row->{ 'LABEL' }; | ||
| 188 | $data = $row->{ 'DATA' }; | ||
| 189 | |||
| 190 | $r_args ||= $row->{ 'ARGS' }; | ||
| 191 | $r_args ||= 'class=' . $row->{ 'CLASS' } if $row->{ 'CLASS' }; | ||
| 192 | } | ||
| 193 | else | ||
| 194 | { | ||
| 195 | $label = $row; | ||
| 196 | } | ||
| 197 | |||
| 198 | $r_args ||= $opt->{ 'ARGS_TR' }; | ||
| 199 | $c_args ||= $opt->{ 'ARGS_TD' }; | ||
| 200 | |||
| 201 | $ftree_item_id++; | ||
| 202 | |||
| 203 | my $row_id = $branch_id . $ftree_item_id . '.'; | ||
| 204 | |||
| 205 | # $label = "($row_id) $label"; # DEBUG | ||
| 206 | |||
| 207 | my $hidden = $level > 0 ? "style='display: none'" : undef; | ||
| 208 | my $pad = $level * 6 + 1; | ||
| 209 | my $cell = html_layout_2lr( ' ', $label, "$pad=<" ); | ||
| 210 | |||
| 211 | if( ref( $data ) eq 'ARRAY' ) | ||
| 212 | { | ||
| 213 | my $open_code = qq{ onclick='ftree_click( "$ftree_table_id", "$row_id" )' }; | ||
| 214 | $html .= "<tr id=$row_id $open_code $r_args $hidden><td $c_args>$cell</td></tr>"; | ||
| 215 | $html .= __html_ftree_branch( $data, $ftree_table_id, $row_id, $level + 1, $opt ); | ||
| 216 | } | ||
| 217 | else | ||
| 218 | { | ||
| 219 | $html .= "<tr id=$row_id $hidden $r_args><td $c_args>$cell</td></tr>"; | ||
| 220 | } | ||
| 221 | |||
| 222 | $html .= "\n"; | ||
| 223 | } | ||
| 224 | |||
| 225 | return $html; | ||
| 226 | } | ||
| 227 | |||
| 228 | ############################################################################## | ||
| 229 | |||
| 230 | sub html_hover_layer | ||
| 231 | { | ||
| 232 | my $reo = shift; | ||
| 233 | my %opt = @_; | ||
| 234 | |||
| 235 | if( ref( $reo ) !~ /^Web::Reactor(::|$)/ ) | ||
| 236 | { | ||
| 237 | boom "missing REO reactor object"; | ||
| 238 | } | ||
| 239 | |||
| 240 | if( @_ == 1 ) | ||
| 241 | { | ||
| 242 | %opt = ( VALUE => shift() ); | ||
| 243 | } | ||
| 244 | |||
| 245 | my $value = $opt{ 'VALUE' }; | ||
| 246 | my $class = $opt{ 'CLASS' } || 'hover-layer'; | ||
| 247 | my $delay = $opt{ 'DELAY' } || 250; | ||
| 248 | |||
| 249 | my $hover_layer_counter = $reo->create_uniq_id(); | ||
| 250 | my $hover_layer_id = "R_HOVER_LAYER_$hover_layer_counter"; | ||
| 251 | |||
| 252 | my $html; | ||
| 253 | my $handle; | ||
| 254 | |||
| 255 | $handle = qq{ onmouseover='reactor_hover_show_delay( this,"$hover_layer_id", $delay, event )' }; | ||
| 256 | $html = qq{ <div class='$class' id="$hover_layer_id">$value</div> }; | ||
| 257 | |||
| 258 | if ( wantarray ) | ||
| 259 | { | ||
| 260 | # will not use ACCUMULATOR_HTML | ||
| 261 | return ( $handle, $html ); | ||
| 262 | } | ||
| 263 | else | ||
| 264 | { | ||
| 265 | $reo->html_content_accumulator( 'ACCUMULATOR_HTML', $html ); | ||
| 266 | return $handle; | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | ############################################################################## | ||
| 271 | |||
| 272 | sub html_popup_layer | ||
| 273 | { | ||
| 274 | my $reo = shift; | ||
| 275 | my %opt = @_; | ||
| 276 | |||
| 277 | if( ref( $reo ) !~ /^Web::Reactor(::|$)/ ) | ||
| 278 | { | ||
| 279 | boom "missing REO reactor object"; | ||
| 280 | } | ||
| 281 | |||
| 282 | if( @_ == 1 ) | ||
| 283 | { | ||
| 284 | %opt = ( VALUE => shift() ); | ||
| 285 | } | ||
| 286 | |||
| 287 | my $value = $opt{ 'VALUE' }; | ||
| 288 | my $class = $opt{ 'CLASS' } || 'popup-layer'; | ||
| 289 | my $delay = $opt{ 'DELAY' } || 150; | ||
| 290 | my $type = $opt{ 'TYPE' } || 'CLICK'; | ||
| 291 | |||
| 292 | $delay = 0 unless $delay > 0; | ||
| 293 | |||
| 294 | my $trigger; | ||
| 295 | if( $type eq 'CONTEXT' ) | ||
| 296 | { | ||
| 297 | $trigger = qq( onContextMenu="return reactor_popup_mouse_over( this )" ); | ||
| 298 | } | ||
| 299 | else # ( $type eq 'CLICK' ) | ||
| 300 | { | ||
| 301 | $trigger = qq( onClick="return reactor_popup_mouse_over( this, { click_open: 1 } )" ); | ||
| 302 | } | ||
| 303 | |||
| 304 | my $popup_layer_id_counter = $reo->create_uniq_id(); | ||
| 305 | my $popup_layer_id = "R_POPUP_LAYER_$popup_layer_id_counter"; | ||
| 306 | |||
| 307 | my $handle = qq( $trigger data-popup-layer-id="$popup_layer_id" ); | ||
| 308 | my $html = qq( <div class='$class' id="$popup_layer_id">$value</div> ); | ||
| 309 | |||
| 310 | if ( wantarray ) | ||
| 311 | { | ||
| 312 | # will not use ACCUMULATOR_HTML | ||
| 313 | return ( $handle, $html ); | ||
| 314 | } | ||
| 315 | else | ||
| 316 | { | ||
| 317 | $reo->html_content_accumulator( 'ACCUMULATOR_HTML', $html ); | ||
| 318 | return $handle; | ||
| 319 | } | ||
| 320 | } | ||
| 321 | |||
| 322 | ############################################################################## | ||
| 323 | |||
| 324 | sub html_alink | ||
| 325 | { | ||
| 326 | my $reo = shift; | ||
| 327 | my $type = lc shift; | ||
| 328 | my $value = shift; | ||
| 329 | my $opts = shift; # hashref with alink options | ||
| 330 | my @args = @_; | ||
| 331 | |||
| 332 | my $href = $reo->args_type( $type, @args ); | ||
| 333 | |||
| 334 | my $tag_args; | ||
| 335 | |||
| 336 | my $tag_id = $opts->{ 'ID' }; | ||
| 337 | |||
| 338 | my $class = $opts->{ 'CLASS' }; | ||
| 339 | my $hint = $opts->{ 'HINT' }; | ||
| 340 | |||
| 341 | my $confirm = $opts->{ 'CONFIRM' }; | ||
| 342 | $tag_args .= ' ' . qq( onclick="return confirm('$confirm');" ) if $confirm =~ /^([^"']+)$/; | ||
| 343 | |||
| 344 | if( $opts->{ 'DISABLED' } ) | ||
| 345 | { | ||
| 346 | $tag_args .= ' ' . qq( onclick="return false;" ) ; | ||
| 347 | $class .= " disabled-button"; | ||
| 348 | $hint = undef; # remove button hints for disabled buttons | ||
| 349 | } | ||
| 350 | |||
| 351 | $tag_args .= ' ' . "ID='$tag_id'"; | ||
| 352 | $tag_args .= ' ' . "class='$class'"; | ||
| 353 | if( $hint ) | ||
| 354 | { | ||
| 355 | my $hint_tag_arg = html_hover_layer( $reo, VALUE => $hint, DELAY => 1000 ); | ||
| 356 | $tag_args .= ' ' . $hint_tag_arg; | ||
| 357 | } | ||
| 358 | |||
| 359 | # FIXME: FIX REACTOR TO HAVE SENSIBLE HTML_LINK FUNCTIONS, I.E. CONVERT HINT TO HASHREF! | ||
| 360 | my $disable_on_click = int( $opts->{ 'DISABLE_ON_CLICK' } ); | ||
| 361 | if( $confirm !~ /^([^"']+)$/ and $disable_on_click > 0 ) | ||
| 362 | { | ||
| 363 | my $class_off = $opts->{ 'DISABLE_ON_CLICK_CLASS' }; | ||
| 364 | $tag_args .= ' ' . "data-class-on='$class' data-class-off='$class_off'"; | ||
| 365 | $tag_args .= ' ' . qq( onclick="return reactor_element_disable_on_click( this, $disable_on_click );" ); | ||
| 366 | } | ||
| 367 | |||
| 368 | return "<a href=?_=$href $tag_args>$value</a>"; | ||
| 369 | } | ||
| 370 | |||
| 371 | ############################################################################## | ||
| 372 | |||
| 373 | =pod | ||
| 374 | |||
| 375 | sub html_tabs_table | ||
| 376 | |||
| 377 | arguments: array_ref, opt_hash | ||
| 378 | |||
| 379 | array_ref is list of hash refs with this content: | ||
| 380 | |||
| 381 | LABEL -- label text for the this tab handle | ||
| 382 | LABEL_TD_ARGS -- further optional arguments for the label TD | ||
| 383 | TEXT -- text to show when tab handle clicked | ||
| 384 | TEXT_TD_ARGS -- TD element args, same as above | ||
| 385 | ON -- if true, this tab will be initially visible | ||
| 386 | TAB_ID -- html id for this tab | ||
| 387 | |||
| 388 | opt_hash is inline with the following items: | ||
| 389 | |||
| 390 | LABELS_TABLE_ARGS -- args for the table containing labels | ||
| 391 | TEXT_TABLE_ARGS -- same as above | ||
| 392 | |||
| 393 | LABEL_CLASS_ON -- active TD class for tab handle labels | ||
| 394 | LABEL_CLASS_OFF -- inactive TD class for tab handle labels | ||
| 395 | |||
| 396 | ARGS -- args for containing TABLE element | ||
| 397 | VERTICAL -- if true, tabs will be vertical | ||
| 398 | |||
| 399 | ACTIVE_TAB_FORM_FEEDBACK_ID -- html INPUT element to hold active tab id | ||
| 400 | |||
| 401 | example: | ||
| 402 | |||
| 403 | my @tabs; | ||
| 404 | |||
| 405 | for my $z ( 1 .. 5 ) | ||
| 406 | { | ||
| 407 | push @tabs, { | ||
| 408 | LABEL => "TAB $z", | ||
| 409 | TEXT => "$z " x 128, | ||
| 410 | LABEL_TD_ARGS => "class=tab-label style='cursor: pointer;'", | ||
| 411 | TEXT_TD_ARGS => "class=tab-text", | ||
| 412 | }; | ||
| 413 | } | ||
| 414 | |||
| 415 | $html = html_tabs_table( \@tabs, ARGS => "width=70% border=2", VERTICAL => 1 ); | ||
| 416 | |||
| 417 | =cut | ||
| 418 | |||
| 419 | sub html_tabs_table | ||
| 420 | { | ||
| 421 | my $reo = shift; | ||
| 422 | my $ar = shift; | ||
| 423 | my %opt = @_; | ||
| 424 | |||
| 425 | my $vert = $opt{ 'VERTICAL' }; | ||
| 426 | |||
| 427 | my @label_td; | ||
| 428 | my @text_td; | ||
| 429 | |||
| 430 | my $cnt = @$ar; | ||
| 431 | |||
| 432 | my $class_on = $opt{ 'LABEL_CLASS_ON' }; | ||
| 433 | my $class_off = $opt{ 'LABEL_CLASS_OFF' }; | ||
| 434 | |||
| 435 | my $tab = new Web::Reactor::HTML::Tab( | ||
| 436 | REO_REACTOR => $reo, | ||
| 437 | CLASS_ON => $class_on, | ||
| 438 | CLASS_OFF => $class_off, | ||
| 439 | ACTIVE_TAB_FORM_FEEDBACK_ID => $opt{ 'ACTIVE_TAB_FORM_FEEDBACK_ID' }, | ||
| 440 | ); | ||
| 441 | |||
| 442 | for my $e ( @$ar ) | ||
| 443 | { | ||
| 444 | my $label = $e->{ 'LABEL' }; | ||
| 445 | my $label_args = $e->{ 'LABEL_TD_ARGS' }; | ||
| 446 | my $text = $e->{ 'TEXT' }; | ||
| 447 | my $text_args = $e->{ 'TEXT_TD_ARGS' }; | ||
| 448 | my $on = $e->{ 'ON' }; | ||
| 449 | my $tab_id = $e->{ 'TAB_ID' }; | ||
| 450 | |||
| 451 | if( ! $vert and $label_args !~ /WIDTH=/i ) | ||
| 452 | { | ||
| 453 | my $w = int( 100 / $cnt ); | ||
| 454 | $label_args .= " WIDTH=$w%"; | ||
| 455 | } | ||
| 456 | |||
| 457 | my ( $tab_handle, $tab_html ) = $tab->add( "<TD $text_args>$text</td>", TYPE => 'TR', ON => $on, TAB_ID => $tab_id ); | ||
| 458 | |||
| 459 | push @label_td, "<TD $label_args $tab_handle>$label</TD>"; | ||
| 460 | push @text_td, $tab_html; | ||
| 461 | } | ||
| 462 | $tab->finish(); | ||
| 463 | |||
| 464 | my $args = $opt{ 'ARGS' }; | ||
| 465 | |||
| 466 | my $text; | ||
| 467 | |||
| 468 | $text .= "<TABLE $args>\n"; | ||
| 469 | if( $vert ) | ||
| 470 | { | ||
| 471 | my $label_args = $opt{ 'VERT_LABEL_TD_ARGS' }; | ||
| 472 | my $text_args = $opt{ 'VERT_TEXT_TD_ARGS' }; | ||
| 473 | |||
| 474 | $label_args .= " WIDTH=50%" if $label_args !~ /WIDTH=/i; | ||
| 475 | |||
| 476 | $text .= "<TR>"; | ||
| 477 | $text .= "<TD $label_args><TABLE WIDTH=100%>"; | ||
| 478 | $text .= "<TR>" . join( "</TR><TR>", @label_td ) . "</TR>"; | ||
| 479 | $text .= "</TABLE></TD>"; | ||
| 480 | |||
| 481 | $text .= "<TD $text_args><TABLE WIDTH=100%>"; | ||
| 482 | $text .= join( '', @text_td ); | ||
| 483 | $text .= "</TABLE></TD>"; | ||
| 484 | |||
| 485 | $text .= "</TR>"; | ||
| 486 | } | ||
| 487 | else | ||
| 488 | { | ||
| 489 | my $labels_table_args = $opt{ 'LABELS_TABLE_ARGS' }; | ||
| 490 | $labels_table_args .= " WIDTH=100%" if $labels_table_args !~ /WIDTH=/i; | ||
| 491 | $text .= "<TR><TD><TABLE $labels_table_args><TR>" . join( '', @label_td ) . "</TR></TABLE></TD></TR>"; | ||
| 492 | $text .= join( '', @text_td ); | ||
| 493 | } | ||
| 494 | $text .= "</TABLE>\n"; | ||
| 495 | |||
| 496 | |||
| 497 | return $text; | ||
| 498 | } | ||
| 499 | |||
| 500 | ############################################################################## | ||
| 501 | |||
| 502 | sub html_check_tag_name | ||
| 503 | { | ||
| 504 | return $_[0] =~ /^[a-zA-Z0-9]+$/; # $13.1.2.1 | ||
| 505 | } | ||
| 506 | |||
| 507 | sub html_check_tag_name_boom | ||
| 508 | { | ||
| 509 | boom( $_[1] || "invalid tag name [$_[0]]" ) unless html_check_tag_name( $_[0] ); | ||
| 510 | } | ||
| 511 | |||
| 512 | sub html_check_attr_name | ||
| 513 | { | ||
| 514 | return $_[0] =~ /^[a-zA-Z_0-9\:\-]+$/; # $13.1.2.3 but extra strict | ||
| 515 | } | ||
| 516 | |||
| 517 | sub html_check_attr_name_boom | ||
| 518 | { | ||
| 519 | boom( $_[1] || "invalid attribute name [$_[0]]" ) unless html_check_attr_name( $_[0] ); | ||
| 520 | } | ||
| 521 | |||
| 522 | ############################################################################## | ||
| 523 | 1; | ||
| 524 | ############################################################################## | ||
| 525 |