File Coverage

blib/lib/SyForm/Util/HTML.pm
Criterion Covered Total %
statement 47 47 100.0
branch 10 12 83.3
condition n/a
subroutine 11 11 100.0
pod 0 4 0.0
total 68 74 91.8


line stmt bran cond sub pod time code
1             package SyForm::Util::HTML;
2             BEGIN {
3 2     2   84481 $SyForm::Util::HTML::AUTHORITY = 'cpan:GETTY';
4             }
5             $SyForm::Util::HTML::VERSION = '0.103';
6 2     2   26 use strict;
  2         4  
  2         39  
7 2     2   10 use warnings;
  2         3  
  2         48  
8 2     2   10 use Exporter 'import';
  2         3  
  2         63  
9 2     2   935 use Clone 'clone';
  2         4933  
  2         1059  
10              
11             our @EXPORT = qw(
12              
13             add_html_attr
14             put_html_attr
15             set_html_attr
16             get_html_attr
17              
18             );
19              
20             sub add_html_attr {
21 1     1 0 181 my ( $html_declare, %attrs ) = @_;
22 1         7 return _add_or_put_html_attr($html_declare,0,%attrs);
23             }
24              
25             sub put_html_attr {
26 1     1 0 15 my ( $html_declare, %attrs ) = @_;
27 1         7 return _add_or_put_html_attr($html_declare,1,%attrs);
28             }
29              
30             sub _add_or_put_html_attr {
31 2     2   9 my ( $html_declare, $add_put, %attrs ) = @_;
32 2         62 my $clone = clone($html_declare);
33 2         8 for my $key (keys %attrs) {
34 4         9 my $current = _get_html_attr($clone,$key);
35 4 100       9 if ($current) {
36             my $new = join(" ",$add_put
37             ? ( $attrs{$key},$current->[1] )
38 2 100       18 : ( $current->[1],$attrs{$key} )
39             );
40 2         5 $current->[1] = $new;
41             } else {
42             $clone->attributes([
43 2         6 @{$clone->attributes},
44 2         8 [ $key, $attrs{$key} ],
45             ]);
46             }
47             }
48 2         31 return $clone;
49             }
50              
51             sub set_html_attr {
52 1     1 0 5 my ( $html_declare, %attrs ) = @_;
53 1         34 my $clone = clone($html_declare);
54 1         14 for my $key (keys %attrs) {
55 2         17 my $current = _get_html_attr($clone,$key);
56 2 100       8 if ($current) {
57 1         4 $current->[1] = $attrs{$key};
58             } else {
59             $clone->attributes([
60 1         2 @{$clone->attributes},
61 1         2 [ $key, $attrs{$key} ],
62             ]);
63             }
64             }
65 1         3 return $clone;
66             }
67              
68             sub get_html_attr {
69 7     7 0 3937 my ( $html_declare, @attrs ) = @_;
70 7         25 my @values;
71 7         15 for my $key (@attrs) {
72 7         16 my $attr = _get_html_attr($html_declare, $key);
73 7 50       26 push @values, defined $attr ? $attr->[1] : undef;
74             }
75 7 50       44 return wantarray ? @values : $values[0];
76             }
77              
78             sub _get_html_attr {
79 13     13   23 my ( $html_declare, $key ) = @_;
80 13         20 for my $attr (@{$html_declare->attributes}) {
  13         33  
81 16 100       115 return $attr if $attr->[0] eq $key;
82             }
83 3         6 return undef;
84             }
85              
86             1;
87              
88             __END__
89              
90             =pod
91              
92             =head1 NAME
93              
94             SyForm::Util::HTML
95              
96             =head1 VERSION
97              
98             version 0.103
99              
100             =head1 AUTHOR
101              
102             Torsten Raudssus <torsten@raudss.us>
103              
104             =head1 COPYRIGHT AND LICENSE
105              
106             This software is copyright (c) 2014 by Torsten Raudssus.
107              
108             This is free software; you can redistribute it and/or modify it under
109             the same terms as the Perl 5 programming language system itself.
110              
111             =cut