Message/Convert/Html.pm

filter: Statementstmt 97.3% 74 / 76 Branchbran 61.1% 22 / 36 Conditioncond 71.4% 5 / 7 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 11 / 11 Podpod 83.3% 5 / 6 totaltotal 84.8% 118 / 139 CC 26 SCAR 20.0
CC
26
Cov
85%
headToHtmlHead
13.7
headToHtmlTable
6.9
fieldContentsToHtml
4.1
CRAP 7.4
« Message/Construct/Rebuild.pm ↑ Summary Message/Field/Full.pm »
1 # This code is part of Perl distribution Mail-Message version 4.05.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2001-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Mail::Message::Convert::Html;{
13 1 our $VERSION = '4.05';
14 }
15  
16 1 ▶use parent 'Mail::Message::Convert';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶use Log::Report 'mail-message', import => [ qw/__x error/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 #--------------------
24  
25 sub init($)
26 2 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=4 SCAR=13.9
Pod undocumented
27  
28 2 $self->SUPER::init($args);
29  
30 2 ▶ my $produce = $args->{produce} || 'HTML';
Condition100%2/2
$args->{'produce'} || 'HTML'
l!l
11
MC/DC100%1/1
$args->{'produce'} || 'HTML'
$args->{'produce'}
Truth table100%2/2
A
A= $args->{'produce'}
Aresult
11
00
31  
32 $self->{MMCH_tail}
33 2 ▶ = $produce eq 'HTML' ? '>'
Branch: 2 branches
BranchTrueFalseTotal
$produce eq 'HTML' ? : 1 1 2
$produce eq 'XHTML' ? : 1 0 1
34 : $produce eq 'XHTML' ? ' />'
35 : error __x"produce XHTML or HTML, not {what UNKNOWN}.", what => $produce;
36  
37 2 $self;
38 }
39  
40 #--------------------
41  
42 sub textToHtml(@)
43 52 ▶{ my $self = shift;
Subroutine textToHtml: called CC=3 SCAR=11.0
Pod documented
44  
45 52 my @lines = @_; # copy is required
46 52 foreach (@lines)
47 52 { s/\&/&amp;/gs; s/\</&lt;/gs;
48 52 s/\>/&gt;/gs; s/\"/&quot;/gs;
49 }
50 52 ▶ wantarray ? @lines : join('', @lines);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 52 52
51 }
52  
53  
54 sub fieldToHtml($;$)
55 2 ▶{ my ($self, $field, $subject) = @_;
Subroutine fieldToHtml: called CC=1 SCAR=0.0
Pod documented
56 2 '<strong>'. $self->textToHtml($field->wellformedName) . ': </strong>' . $self->fieldContentsToHtml($field, $subject);
57 }
58  
59  
60 sub headToHtmlTable($;$)
61 2 ▶{ my ($self, $head) = (shift, shift);
Subroutine headToHtmlTable: called CC=6 SCAR=19.3
Pod documented
62 2 ▶ my $tp = @_ ? ' '.shift : '';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 2 0 2
63  
64 2 my $subject;
65 2 ▶ if($self->{MMHC_mailto_subject})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MMHC_mailto_subject"}) 0 2 2
66 0 { my $s = $head->get('subject');
67  
68 1 ▶ use Mail::Message::Construct;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 0 ▶ $subject = Mail::Message::Construct->replySubject($s) if defined $s;
Branch: 1 branch
BranchTrueFalseTotal
if defined $s 0 0 0
70 }
71  
72 2 my @lines = "<table $tp>\n";
73 2 foreach my $f ($self->selectedFields($head))
74 6 { my $name_html = $self->textToHtml($f->wellformedName);
75 6 my $cont_html = $self->fieldContentsToHtml($f, $subject);
76  
77 6 push @lines,
78 qq(<tr><th valign="top" align="left">$name_html:</th>\n),
79 qq( <td valign="top">$cont_html</td></tr>\n);
80 }
81  
82 2 push @lines, "</table>\n";
83 2 ▶ wantarray ? @lines : join('',@lines);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
84 }
85  
86  
87 sub headToHtmlHead($@)
88 4 ▶{ my ($self, $head) = (shift,shift);
Subroutine headToHtmlHead: called CC=13 SCAR=26.2
Pod documented
89 4 my %meta;
90 4 ▶ while(@_) { my $k = shift; $meta{lc $k} = shift }
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 4 11
91  
92 4 ▶ my $title = delete $meta{title} || $head->get('subject') || '<no subject>';
Condition60%3/5
delete $meta{'title'} || $head->get('subject') || '<no subject>'
l!l
40
delete $meta{"title"} or $head->get("subject")
l!l&&r!l&&!r
220
MC/DC0%0/2
delete $meta{'title'} || $head->get('subject') || '<no subject>'
delete $meta{"title"} $head->get("subject")
Truth table66%2/3
A or B
A= delete $meta{"title"}B= $head->get("subject")
ABresult
1X1
011
000
93  
94 4 my @lines = (
95 "<head>\n",
96 "<title>".$self->textToHtml($title) . "</title>\n",
97 );
98  
99 4 my $author = delete $meta{author};
100 4 ▶ unless(defined $author)
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $author) 4 0 4
101 4 { my $from = $head->get('from');
102 4 ▶ my @addr = defined $from ? $from->addresses : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $from ? : 4 0 4
103 4 ▶ $author = @addr ? $addr[0]->format : undef;
Branch: 1 branch
BranchTrueFalseTotal
@addr ? : 4 0 4
104 }
105  
106 4 ▶ push @lines, '<meta name="Author" content="' . $self->textToHtml($author) . "\"$self->{MMCH_tail}\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $author 4 0 4
107 if defined $author;
108  
109 4 foreach my $f (map {lc} keys %meta)
110 5 ▶ { next if $meta{$f} eq ''; # empty is skipped.
Branch: 1 branch
BranchTrueFalseTotal
if $meta{$f} eq "" 1 4 5
111 push @lines,
112 '<meta name="'. ucfirst lc $self->textToHtml($f)
113 4 . '" content="'. $self->textToHtml($meta{$f})
114 ."\"$self->{MMCH_tail}\n";
115 }
116  
117 4 foreach my $f ($self->selectedFields($head))
118 12 ▶ { next if exists $meta{$f->name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $meta{$f->name} 2 10 12
119 10 push @lines,
120 '<meta name="' . $self->textToHtml($f->wellformedName)
121 . '" content="' . $self->textToHtml($f->content)
122 . "\"$self->{MMCH_tail}\n";
123 }
124  
125 4 push @lines, "</head>\n";
126 4 ▶ wantarray ? @lines : join('',@lines);
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 4 4
127 }
128  
129  
130 1 my $atom = qr/[^()<>@,;:\\".\[\]\s[:cntrl:]]+/;
131 1 my $email_address = qr/(($atom(?:\.$atom)*)\@($atom(?:\.$atom)+))/o;
132  
133 sub fieldContentsToHtml($;$)
134 8 ▶{ my ($self, $field, $subj) = @_;
Subroutine fieldContentsToHtml: called CC=4 SCAR=14.0
Pod documented
135  
136 8 ▶ my $subject = defined $subj ? '?subject='.$self->textToHtml($subj) : '';
Branch: 1 branch
BranchTrueFalseTotal
defined $subj ? : 0 8 8
137 8 my $body = $self->textToHtml($field->body);
138 8 my $comment = $field->comment;
139  
140 8 ▶ $body =~ s#$email_address#<a href="mailto:$1$subject">$1</a>#gx
Branch: 1 branch
BranchTrueFalseTotal
if $field->name =~ /^(resent-)?(to|from|cc|bcc|reply\-to)$/ 6 2 8
141 if $field->name =~ m/^(resent-)?(to|from|cc|bcc|reply\-to)$/;
142  
143 8 ▶ $body . ($comment ? '; '.$self->textToHtml($comment) : '');
Branch: 1 branch
BranchTrueFalseTotal
$comment ? : 0 8 8
144 }
145  
146 1 1;
« Message/Construct/Rebuild.pm ↑ Summary Message/Field/Full.pm »