| 1 | package HTML::FormHandler::Render::Util; | ||||||||||||
| 2 | # ABSTRACT: rendering utility | ||||||||||||
| 3 | 149 | $HTML::FormHandler::Render::Util::VERSION = '0.410001'; | |||||||||||
| 4 | 149 | ▶ | use Sub::Exporter; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 149 | Sub::Exporter::setup_exporter({ exports => [ 'process_attrs', 'cc_widget', 'ucc_widget' ] } ); | |||||||||||
| 6 | |||||||||||||
| 7 | |||||||||||||
| 8 | # this is a function for processing various attribute flavors | ||||||||||||
| 9 | sub process_attrs { | ||||||||||||
| 10 | 2231 | ▶ | my ($attrs) = @_; | ||||||||||
|
Subroutine
process_attrs: called CC=8 SCAR=21.0
Pod
undocumented
| |||||||||||||
| 11 | |||||||||||||
| 12 | 2231 | my @use_attrs; | |||||||||||
| 13 | 2231 | ▶ | my $javascript = delete $attrs->{javascript} || ''; | ||||||||||
|
Condition50%1/2
delete $attrs->{'javascript'} || ''
MC/DC0%0/1
delete $attrs->{'javascript'} || ''
delete $attrs->{'javascript'}
Truth table50%1/2
A
A= delete $attrs->{'javascript'}
| |||||||||||||
| 14 | 2231 | for my $attr( sort keys %$attrs ) { | |||||||||||
| 15 | 973 | my $value = ''; | |||||||||||
| 16 | 973 | ▶ | if( defined $attrs->{$attr} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 17 | 973 | ▶ | if( ref $attrs->{$attr} eq 'ARRAY' ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 18 | # we don't want class="" if no classes specified | ||||||||||||
| 19 | 511 | ▶ | next unless scalar @{$attrs->{$attr}}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 20 | 511 | $value = join (' ', @{$attrs->{$attr}} ); | |||||||||||
| 21 | } | ||||||||||||
| 22 | else { | ||||||||||||
| 23 | 462 | $value = $attrs->{$attr}; | |||||||||||
| 24 | } | ||||||||||||
| 25 | } | ||||||||||||
| 26 | 973 | push @use_attrs, sprintf( '%s="%s"', $attr, $value ); | |||||||||||
| 27 | } | ||||||||||||
| 28 | 2231 | my $output = join( ' ', @use_attrs ); | |||||||||||
| 29 | 2231 | ▶ | $output = " $output" if length $output; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 30 | 2231 | ▶ | $output .= " $javascript" if $javascript; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 31 | 2231 | return $output; | |||||||||||
| 32 | } | ||||||||||||
| 33 | |||||||||||||
| 34 | sub cc_widget { | ||||||||||||
| 35 | 4 | ▶ | my $widget = shift; | ||||||||||
|
Subroutine
cc_widget: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||
| 36 | 4 | ▶ | return '' unless $widget; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 37 | 4 | ▶ | if($widget eq lc $widget) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 38 | 4 | $widget =~ s/^(\w{1})/\u$1/g; | |||||||||||
| 39 | 4 | $widget =~ s/_(\w{1})/\u$1/g; | |||||||||||
| 40 | } | ||||||||||||
| 41 | 4 | return $widget; | |||||||||||
| 42 | } | ||||||||||||
| 43 | |||||||||||||
| 44 | sub ucc_widget { | ||||||||||||
| 45 | 199 | ▶ | my $widget = shift; | ||||||||||
|
Subroutine
ucc_widget: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 46 | 199 | ▶ | if($widget ne lc $widget) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 47 | 199 | $widget =~ s/::/_/g; | |||||||||||
| 48 | 199 | $widget = ucfirst($widget); | |||||||||||
| 49 | 199 | my @parts = $widget =~ /([A-Z][a-z]*)/g; | |||||||||||
| 50 | 199 | $widget = join('_', @parts); | |||||||||||
| 51 | 199 | $widget = lc($widget); | |||||||||||
| 52 | } | ||||||||||||
| 53 | 199 | return $widget; | |||||||||||
| 54 | } | ||||||||||||
| 55 | |||||||||||||
| 56 | |||||||||||||
| 57 | 149 | 1; | |||||||||||
| 58 | |||||||||||||
| 59 | __END__ | ||||||||||||
| 60 | |||||||||||||
| 61 | =pod | ||||||||||||
| 62 | |||||||||||||
| 63 | =encoding UTF-8 | ||||||||||||
| 64 | |||||||||||||
| 65 | =head1 NAME | ||||||||||||
| 66 | |||||||||||||
| 67 | HTML::FormHandler::Render::Util - rendering utility | ||||||||||||
| 68 | |||||||||||||
| 69 | =head1 VERSION | ||||||||||||
| 70 | |||||||||||||
| 71 | version 0.410001 | ||||||||||||
| 72 | |||||||||||||
| 73 | =head1 SYNOPSIS | ||||||||||||
| 74 | |||||||||||||
| 75 | The 'process_attrs' takes a hashref and creates an attribute string | ||||||||||||
| 76 | for constructing HTML. | ||||||||||||
| 77 | |||||||||||||
| 78 | my $attrs => { | ||||||||||||
| 79 | some_attr => 1, | ||||||||||||
| 80 | placeholder => 'Enter email...", | ||||||||||||
| 81 | class => ['help', 'special'], | ||||||||||||
| 82 | }; | ||||||||||||
| 83 | my $string = process_attrs($attrs); | ||||||||||||
| 84 | |||||||||||||
| 85 | ...will produce: | ||||||||||||
| 86 | |||||||||||||
| 87 | ' some_attr="1" placeholder="Enter email..." class="help special"' | ||||||||||||
| 88 | |||||||||||||
| 89 | If an arrayref is empty, it will be skipped. For a hash key of 'javascript' | ||||||||||||
| 90 | only the value will be appended (without '$key=""'); | ||||||||||||
| 91 | |||||||||||||
| 92 | =head1 AUTHOR | ||||||||||||
| 93 | |||||||||||||
| 94 | FormHandler Contributors - see HTML::FormHandler | ||||||||||||
| 95 | |||||||||||||
| 96 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 97 | |||||||||||||
| 98 | This software is copyright (c) 2017 by Gerda Shank. | ||||||||||||
| 99 | |||||||||||||
| 100 | This is free software; you can redistribute it and/or modify it under | ||||||||||||
| 101 | the same terms as the Perl 5 programming language system itself. | ||||||||||||
| 102 | |||||||||||||
| 103 | =cut | ||||||||||||