blib/lib/Template/Plugin/HTML.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 16 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 129 CC 14 SCAR 24.1
CC
14
Cov
0%
element
20.0
closed_element
20.0
attributes
12.0
CRAP 11.1
1 #============================================================= -*-Perl-*-
2 #
3 # Template::Plugin::HTML
4 #
5 # DESCRIPTION
6 # Template Toolkit plugin providing useful functionality for generating
7 # HTML.
8 #
9 # AUTHOR
10 # Andy Wardley <abw@wardley.org>
11 #
12 # COPYRIGHT
13 # Copyright (C) 1996-2022 Andy Wardley. All Rights Reserved.
14 #
15 # This module is free software; you can redistribute it and/or
16 # modify it under the same terms as Perl itself.
17 #
18 #============================================================================
19  
20 package Template::Plugin::HTML;
21  
22 use strict;
23 use warnings;
24 use base 'Template::Plugin';
25  
26 our $VERSION = '3.100';
27  
28 sub new {
29 my ($class, $context, @args) = @_;
30 my $hash = ref $args[-1] eq 'HASH' ? pop @args : { };
31 bless {
32 _SORTED => $hash->{ sorted } || 0,
33 attributes => $hash->{ attributes } || $hash->{ attrs } || { },
34 }, $class;
35 }
36  
37 sub element {
38 my ($self, $name, $attr) = @_;
39 ($name, $attr) = %$name if ref $name eq 'HASH';
40 return '' unless defined $name and length $name;
41 $attr = $self->attributes($attr);
42 $attr = " $attr" if $attr;
43 return "<$name$attr>";
44 }
45  
46 sub closed_element {
47 my ($self, $name, $attr) = @_;
48 ($name, $attr) = %$name if ref $name eq 'HASH';
49 return '' unless defined $name and length $name;
50 $attr = $self->attributes( $attr );
51 $attr = " $attr" if $attr;
52 return "<$name$attr />";
53 }
54  
55 sub attributes {
56 my ($self, $hash) = @_;
57 $hash ||= $self->{ attributes };
58 return '' unless ref $hash eq 'HASH';
59  
60 my @keys = keys %$hash;
61 @keys = sort @keys if $self->{ _SORTED };
62  
63 join(' ', map {
64 "$_=\"" . $self->escape( $hash->{ $_ } ) . '"';
65 } @keys);
66 }
67  
68 sub add_attributes {
69 my ($self, $attr) = @_;
70 return unless ref $attr eq 'HASH';
71  
72 my $cur = $self->{ attributes };
73  
74 for (keys %{$attr}) {
75 $cur->{$_} = exists $cur->{$_}
76 ? $cur->{$_} . " $attr->{$_}"
77 : $attr->{$_};
78  
79 }
80  
81 return;
82 }
83  
84 *add_attribute = \&add_attributes;
85 *add = \&add_attributes;
86  
87  
88 sub replace_attributes {
89 my ($self, $attr) = @_;
90 return unless ref $attr eq 'HASH';
91  
92 my $cur = $self->{ attributes };
93  
94 for (keys %{$attr}) {
95 $cur->{$_} = $attr->{$_};
96 }
97  
98 return;
99 }
100  
101 *replace_attribute = \&replace_attributes;
102 *replace = \&replace_attributes;
103  
104 sub clear_attributes {
105 my $self = shift;
106 $self->{ attributes } = { };
107 return;
108 }
109  
110  
111 sub escape {
112 my ($self, $text) = @_;
113 for ($text) {
114 s/&/&amp;/g;
115 s/</&lt;/g;
116 s/>/&gt;/g;
117 s/"/&quot;/g;
118 s/'/&#39;/g;
119 }
120 $text;
121 }
122  
123 sub url {
124 my ($self, $text) = @_;
125 return undef unless defined $text;
126 $text =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
127 return $text;
128 }
129  
130  
131 1;
132  
133 __END__