File Coverage

blib/lib/URI/mailto.pm
Criterion Covered Total %
statement 52 52 100.0
branch 18 20 90.0
condition 4 4 100.0
subroutine 6 6 100.0
pod 1 3 33.3
total 81 85 95.2


line stmt bran cond sub pod time code
1             package URI::mailto; # RFC 2368
2              
3 2     2   14 use strict;
  2         4  
  2         75  
4 2     2   11 use warnings;
  2         3  
  2         89  
5              
6             our $VERSION = '5.20';
7              
8 2     2   470 use parent qw(URI URI::_query);
  2         294  
  2         17  
9              
10             sub to
11             {
12 14     14 0 894 my $self = shift;
13 14         29 my @old = $self->headers;
14 14 100       51 if (@_) {
15 3         6 my @new = @old;
16             # get rid of any other to: fields
17 3         8 for (my $i = 0; $i < @new; $i += 2) {
18 6 100 100     24 if (lc($new[$i] || '') eq "to") {
19 3         5 splice(@new, $i, 2);
20 3         5 redo;
21             }
22             }
23              
24 3         4 my $to = shift;
25 3 50       20 $to = "" unless defined $to;
26 3         6 unshift(@new, "to" => $to);
27 3         12 $self->headers(@new);
28             }
29 14 100       31 return unless defined wantarray;
30              
31 12         14 my @to;
32 12         26 while (@old) {
33 15         34 my $h = shift @old;
34 15         21 my $v = shift @old;
35 15 100       47 push(@to, $v) if lc($h) eq "to";
36             }
37 12         69 join(",", @to);
38             }
39              
40              
41             sub headers
42             {
43 20     20 0 316 my $self = shift;
44              
45             # The trick is to just treat everything as the query string...
46 20         57 my $opaque = "to=" . $self->opaque;
47 20         47 $opaque =~ s/\?/&/;
48              
49 20 100       44 if (@_) {
50 4         9 my @new = @_;
51              
52             # strip out any "to" fields
53 4         5 my @to;
54 4         9 for (my $i=0; $i < @new; $i += 2) {
55 10 100 100     33 if (lc($new[$i] || '') eq "to") {
56 4         9 push(@to, (splice(@new, $i, 2))[1]); # remove header
57 4         7 redo;
58             }
59             }
60              
61 4         10 my $new = join(",",@to);
62 4         20 $new =~ s/%/%25/g;
63 4         5 $new =~ s/\?/%3F/g;
64 4         11 $self->opaque($new);
65 4 100       13 $self->query_form(@new) if @new;
66             }
67 20 100       56 return unless defined wantarray;
68              
69             # I am lazy today...
70 16         57 URI->new("mailto:?$opaque")->query_form;
71             }
72              
73             # https://datatracker.ietf.org/doc/html/rfc6068#section-5 requires
74             # plus signs (+) not to be turned into spaces
75             sub query_form
76             {
77 17     17 1 23 my $self = shift;
78 17         47 my @fields = $self->SUPER::query_form(@_);
79 17         54 for ( my $i = 0 ; $i < @fields ; $i += 2 ) {
80 16 50       74 if ( $fields[0] eq 'to' ) {
81 16         27 $fields[1] =~ s/ /+/g;
82 16         29 last;
83             }
84             }
85 17         51 return @fields;
86             }
87              
88             1;