lib/Template/Plugin/HTML.pm

filter: Statementstmt 65.6% 44 / 67 Branchbran 34.6% 9 / 26 Conditioncond 37.5% 6 / 16 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 66.6% 8 / 12 Podpod 55.5% 5 / 9 totaltotal 52.5% 73 / 139 CC 23 SCAR 23.4
CC
23
Cov
54%
closed_element
30.0
add_attributes
20.0
replace_attributes
12.0
CRAP 10.4
« lib/Template.pm ↑ Summary lib/Template/Plugin/File.pm »
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 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 1 ▶use base 'Template::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 1 our $VERSION = '3.106';
27  
28 7 ▶sub new {
Subroutine new: called CC=5 SCAR=16.8
Pod documented
29 7 my ($class, $context, @args) = @_;
30 7 ▶ my $hash = ref $args[-1] eq 'HASH' ? pop @args : { };
Branch: 1 branch
BranchTrueFalseTotal
ref $args[-1] eq 'HASH' ? : 1 6 7
31 bless {
32 _SORTED => $hash->{ sorted } || 0,
33 attributes => $hash->{ attributes } || $hash->{ attrs } || { },
34 7 ▶ }, $class;
Condition57%4/7
$hash->{'sorted'} || 0
l!l
16
$hash->{'attributes'} || $hash->{'attrs'} || {}
l!l
07
$hash->{"attributes"} or $hash->{"attrs"}
l!l&&r!l&&!r
007
MC/DC100%1/1
$hash->{'sorted'} || 0
$hash->{'sorted'}
MC/DC0%0/2
$hash->{'attributes'} || $hash->{'attrs'} || {}
$hash->{"attributes"} $hash->{"attrs"}
Truth table100%2/2
A
A= $hash->{'sorted'}
Aresult
11
00
Truth table33%1/3
A or B
A= $hash->{"attributes"}B= $hash->{"attrs"}
ABresult
1X1
011
000
35 }
36  
37 1 ▶sub element {
Subroutine element: called CC=5 SCAR=17.8
Pod documented
38 1 my ($self, $name, $attr) = @_;
39 1 ▶ ($name, $attr) = %$name if ref $name eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $name eq "HASH" 1 0 1
40 1 ▶ return '' unless defined $name and length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 1 1
Condition33%1/3
defined $name and length $name
!ll&&!rl&&r
001
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table33%1/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
41 1 $attr = $self->attributes($attr);
42 1 ▶ $attr = " $attr" if $attr;
Branch: 1 branch
BranchTrueFalseTotal
if $attr 1 0 1
43 1 return "<$name$attr>";
44 }
45  
46 0 ▶sub closed_element {
Subroutine closed_element: not called CC=5 SCAR=34.0
Pod undocumented
47 0 my ($self, $name, $attr) = @_;
48 0 ▶ ($name, $attr) = %$name if ref $name eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $name eq "HASH" 0 0 0
49 0 ▶ return '' unless defined $name and length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 0 0 0
Condition0%0/3
defined $name and length $name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $name and length $name
defined $name length $name
Truth table0%0/3
A and B
A= defined $nameB= length $name
ABresult
0X0
100
111
50 0 $attr = $self->attributes( $attr );
51 0 ▶ $attr = " $attr" if $attr;
Branch: 1 branch
BranchTrueFalseTotal
if $attr 0 0 0
52 0 return "<$name$attr />";
53 }
54  
55 2 ▶sub attributes {
Subroutine attributes: called CC=4 SCAR=14.2
Pod documented
56 2 my ($self, $hash) = @_;
57 2 ▶ $hash ||= $self->{ attributes };
Condition33%1/3
$hash ||= $self->{"attributes"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$hash ||= $self->{"attributes"}
$hash $self->{"attributes"}
Truth table33%1/3
A ||= B
A= $hashB= $self->{"attributes"}
ABresult
1X1
011
000
58 2 ▶ return '' unless ref $hash eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 0 2 2
59  
60 2 my @keys = keys %$hash;
61 2 ▶ @keys = sort @keys if $self->{ _SORTED };
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_SORTED"} 1 1 2
62  
63 join(' ', map {
64 2 "$_=\"" . $self->escape( $hash->{ $_ } ) . '"';
65 } @keys);
66 }
67  
68 0 ▶sub add_attributes {
Subroutine add_attributes: not called CC=4 SCAR=30.0
Pod undocumented
69 0 my ($self, $attr) = @_;
70 0 ▶ return unless ref $attr eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attr eq "HASH" 0 0 0
71  
72 0 my $cur = $self->{ attributes };
73  
74 0 for (keys %{$attr}) {
75 $cur->{$_} = exists $cur->{$_}
76 ? $cur->{$_} . " $attr->{$_}"
77 0 ▶ : $attr->{$_};
Branch: 1 branch
BranchTrueFalseTotal
exists $cur->{$_} ? : 0 0 0
78  
79 }
80  
81 0 return;
82 }
83  
84 1 *add_attribute = \&add_attributes;
85 1 *add = \&add_attributes;
86  
87  
88 0 ▶sub replace_attributes {
Subroutine replace_attributes: not called CC=3 SCAR=24.8
Pod undocumented
89 0 my ($self, $attr) = @_;
90 0 ▶ return unless ref $attr eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $attr eq "HASH" 0 0 0
91  
92 0 my $cur = $self->{ attributes };
93  
94 0 for (keys %{$attr}) {
95 0 $cur->{$_} = $attr->{$_};
96 }
97  
98 0 return;
99 }
100  
101 1 *replace_attribute = \&replace_attributes;
102 1 *replace = \&replace_attributes;
103  
104 0 ▶sub clear_attributes {
Subroutine clear_attributes: not called CC=1 SCAR=6.9
Pod documented
105 0 my $self = shift;
106 0 $self->{ attributes } = { };
107 0 return;
108 }
109  
110  
111 6 ▶sub escape {
Subroutine escape: called CC=2 SCAR=6.9
Pod documented
112 6 my ($self, $text) = @_;
113 6 for ($text) {
114 6 s/&/&amp;/g;
115 6 s/</&lt;/g;
116 6 s/>/&gt;/g;
117 6 s/"/&quot;/g;
118 6 s/'/&#39;/g;
119 }
120 6 $text;
121 }
122  
123 1 ▶sub url {
Subroutine url: called CC=2 SCAR=7.0
Pod undocumented
124 1 my ($self, $text) = @_;
125 1 ▶ return undef unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 1 1
126 1 $text =~ s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
127 1 return $text;
128 }
129  
130  
131 1 1;
132  
133 __END__
« lib/Template.pm ↑ Summary lib/Template/Plugin/File.pm »