lib/Text/JSContact.pm

filter: Statementstmt 95.4% 1473 / 1544 Branchbran 75.3% 606 / 804 Conditioncond 61.3% 382 / 623 MC/DCmcdc 37.4% 131 / 350 Subroutinesub 96.9% 129 / 133 Podpod 0.0% 0 / 3 totaltotal 78.7% 2721 / 3457 CC 762 SCAR 29.2
CC
762
Cov
83%
_patch_kind
132.0
_patch_speak_to_as
56.0
_to_v3
55.8
CRAP 18.5
↑ Summary
1 #!/usr/bin/perl -cw
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package Text::JSContact;
7  
8 20 our $VERSION = '0.07';
9  
10 # vCard <=> JSContact (RFC 9553) conversion, writing JSContact version 2.0
11 # (RFC 9982); follows RFC 9555 for vCard mapping and RFC 9554 for extensions
12  
13 20 ▶use Text::VCardFast qw(vcard2hash);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use Encode qw(decode_utf8 encode_utf8);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use MIME::Base64 qw(decode_base64);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use Scalar::Util qw(looks_like_number refaddr);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶use feature 'state';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 20 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 20 our @EXPORT_OK = qw(vcard_to_jscontact jscontact_to_vcard patch_vcard);
22  
23 ###############################################################################
24 # HELPERS
25 ###############################################################################
26  
27 3321 ▶sub _first_prop {
Subroutine _first_prop: called CC=3 SCAR=11.0
28 3321 my ($props, $name) = @_;
29 3321 my $items = $props->{$name};
30 3321 ▶ return undef unless $items && @$items;
Branch: 1 branch
BranchTrueFalseTotal
unless $items and @$items 2124 1197 3321
Condition66%2/3
$items and @$items
!ll&&!rl&&r
212401197
MC/DC50%1/2
$items and @$items
$items @$items
Truth table66%2/3
A and B
A= $itemsB= @$items
ABresult
0X0
100
111
31 1197 return $items->[0];
32 }
33  
34 3066 ▶sub _first_value {
Subroutine _first_value: called CC=2 SCAR=6.9
35 3066 my ($props, $name) = @_;
36 3066 my $prop = _first_prop($props, $name);
37 3066 ▶ return undef unless $prop;
Branch: 1 branch
BranchTrueFalseTotal
unless $prop 2058 1008 3066
38 1008 return $prop->{value};
39 }
40  
41 # vCard 4 parameters that a vCard 3 card carries as X- (see _to_v3)
42 20 my %X_FALLBACK = map { $_ => 1 } qw(label geo tz cc mediatype sort-as level
43 index created author author-name);
44  
45 13305 ▶sub _param {
Subroutine _param: called CC=5 SCAR=16.1
46 13305 my ($prop, $name) = @_;
47 13305 my $val = $prop->{params}{$name};
48 13305 ▶ $val = $prop->{params}{"x-$name"} if !defined $val && $X_FALLBACK{$name};
Branch: 1 branch
BranchTrueFalseTotal
if not defined $val and $X_FALLBACK{$name} 1750 11555 13305
Condition100%3/3
not defined $val and $X_FALLBACK{$name}
!ll&&!rl&&r
235791981750
MC/DC100%2/2
not defined $val and $X_FALLBACK{$name}
not defined $val $X_FALLBACK{$name}
Truth table100%3/3
not(A) and B
A= not defined $valB= $X_FALLBACK{$name}
ABresult
0X0
100
111
49 13305 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 10948 2357 13305
50 2357 ▶ return ref $val eq 'ARRAY' ? $val->[0] : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 2357 0 2357
51 }
52  
53 3558 ▶sub _params_list {
Subroutine _params_list: called CC=3 SCAR=11.0
54 3558 my ($prop, $name) = @_;
55 3558 my $val = $prop->{params}{$name};
56 3558 ▶ return () unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 1439 2119 3558
57 # Split comma-separated values (e.g. TYPE=work,voice)
58 2119 ▶ return map { split /,/ } (ref $val eq 'ARRAY' ? @$val : ($val));
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 2119 0 2119
59 }
60  
61 ###############################################################################
62 # CONVERSION CONTEXT
63 ###############################################################################
64  
65 # Everything one conversion allocates lives in a context made for that
66 # call: the ids given to vCard properties without a PROP-ID, and the
67 # itemN groups that tie an X-ABLabel to its property. Each allocator is
68 # seeded with what the vCard already uses, so an allocated id never
69 # equals an explicit PROP-ID and a new group never takes an existing one.
70 616 ▶sub _new_context {
Subroutine _new_context: called CC=1 SCAR=0.0
71 616 my (%args) = @_;
72 return {
73 616 next_id => 0,
74 used_ids => {}, # PROP-IDs present in the vCard
75 next_group => 0,
76 used_groups => {}, # groups present in the vCard, lower-cased
77 %args,
78 };
79 }
80  
81 # Seed a context with the PROP-IDs and groups of parsed vCard properties
82 255 ▶sub _seed_context {
Subroutine _seed_context: called CC=6 SCAR=18.0
83 255 my ($ctx, $props) = @_;
84 255 for my $item (map { @$_ } values %$props) {
85 5624 my $pid = _param($item, 'prop-id');
86 5624 ▶ $ctx->{used_ids}{$pid} = 1 if defined $pid && $pid ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $pid and $pid ne "" 617 5007 5624
Condition66%2/3
defined $pid and $pid ne ""
!ll&&!rl&&r
50070617
MC/DC50%1/2
defined $pid and $pid ne ""
defined $pid $pid ne ""
Truth table66%2/3
A and B
A= defined $pidB= $pid ne ""
ABresult
0X0
100
111
87 5624 ▶ $ctx->{used_groups}{lc $item->{group}} = 1 if defined $item->{group} && $item->{group} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $item->{"group"} and $item->{"group"} ne "" 1503 4121 5624
Condition66%2/3
defined $item->{"group"} and $item->{"group"} ne ""
!ll&&!rl&&r
412101503
MC/DC50%1/2
defined $item->{"group"} and $item->{"group"} ne ""
defined $item->{"group"} $item->{"group"} ne ""
Truth table66%2/3
A and B
A= defined $item->{"group"}B= $item->{"group"} ne ""
ABresult
0X0
100
111
88 }
89 255 return $ctx;
90 }
91  
92 # TODO(registry): an allocated id is a position in reading order, across
93 # the whole card. A vCard without PROP-IDs (every vCard 3 card, which
94 # patch_vcard keeps free of them, and many vCard 4 ones) therefore keeps
95 # its ids only while no entry before them is added or removed: patching
96 # one entry out renumbers every entry read after it. Counting per map
97 # would confine that to the one map; stable ids need either PROP-IDs
98 # written on every line or ids the registry derives some other way.
99 2089 ▶sub _alloc_id {
Subroutine _alloc_id: called CC=2 SCAR=6.9
100 2089 my ($ctx) = @_;
101 2089 my $id;
102 2089 ▶ do { $id = '' . ++$ctx->{next_id} } while $ctx->{used_ids}{$id};
Branch: 1 branch
BranchTrueFalseTotal
$ctx->{"used_ids"}{$id} and do { do { $id = '' . ++$ctx->{"next_id"} } } 1 2089 2090
103 2089 return $id;
104 }
105  
106 168 ▶sub _alloc_group {
Subroutine _alloc_group: called CC=2 SCAR=6.9
107 168 my ($ctx) = @_;
108 168 my $group;
109 168 ▶ do { $group = 'item' . ++$ctx->{next_group} } while $ctx->{used_groups}{$group};
Branch: 1 branch
BranchTrueFalseTotal
$ctx->{"used_groups"}{$group} and do { do { $group = 'item' . ++$ctx->{"next_group"} } } 22 168 190
110 168 $ctx->{used_groups}{$group} = 1;
111 168 return $group;
112 }
113  
114 2706 ▶sub _prop_id {
Subroutine _prop_id: called CC=3 SCAR=11.0
115 2706 my ($prop, $ctx) = @_;
116 2706 my $pid = _param($prop, 'prop-id');
117 2706 ▶ $pid = _alloc_id($ctx) unless defined $pid && $pid ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pid and $pid ne "" 2089 617 2706
Condition66%2/3
defined $pid and $pid ne ""
!ll&&!rl&&r
20890617
MC/DC50%1/2
defined $pid and $pid ne ""
defined $pid $pid ne ""
Truth table66%2/3
A and B
A= defined $pidB= $pid ne ""
ABresult
0X0
100
111
118 2706 push @{$ctx->{pending}}, [$pid, $prop];
119 2706 return $pid;
120 }
121  
122 # A vCard read from a CardDAV server is UTF-8 bytes (RFC 6350 S3.1).
123 # Decode it once, before parsing, so every value is characters; one that
124 # is not valid UTF-8 is read as Latin-1, a character per byte. A string
125 # that is characters already is left alone.
126 259 ▶sub _decode_input {
Subroutine _decode_input: called CC=5 SCAR=16.2
127 259 my ($s) = @_;
128 259 ▶ return $s if !defined $s || utf8::is_utf8($s) || $s !~ /[\x80-\xff]/;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $s or utf8::is_utf8($s) or not $s =~ /[\x80-\xff]/ 210 49 259
Condition83%5/6
not defined $s or utf8::is_utf8($s) or not $s =~ /[\x80-\xff]/
l!l&&r!l&&!r
320749
not defined $s or utf8::is_utf8($s)
l!l&&r!l&&!r
03256
MC/DC66%2/3
not defined $s or utf8::is_utf8($s) or not $s =~ /[\x80-\xff]/
not defined $s utf8::is_utf8($s) not $s =~ /[\x80-\xff]/
Truth table75%3/4
not(A) or B or not(C)
A= not defined $sB= utf8::is_utf8($s)C= not $s =~ /[\x80-\xff]/
ABCresult
1XX1
01X1
0011
0000
129 49 my $bytes = $s;
130 49 my $chars = eval { Encode::decode('UTF-8', $bytes, Encode::FB_CROAK) };
131 49 ▶ return defined $chars ? $chars : $s;
Branch: 1 branch
BranchTrueFalseTotal
defined $chars ? : 49 0 49
132 }
133  
134 1887 ▶sub _make_contexts {
Subroutine _make_contexts: called CC=5 SCAR=16.1
135 1887 my @types = @_;
136 1887 my %ctx;
137 1887 for my $t (@types) {
138 1584 my $lt = lc $t;
139 1584 ▶ if ($lt eq 'home') { $ctx{private} = JSON::true }
Branch: 2 branches
BranchTrueFalseTotal
if ($lt eq 'home') { } 315 1269 1584
elsif ($lt eq 'work') { } 435 834 1269
140 435 elsif ($lt eq 'work') { $ctx{work} = JSON::true }
141 }
142 1887 ▶ return %ctx ? \%ctx : undef;
Branch: 1 branch
BranchTrueFalseTotal
%ctx ? : 750 1137 1887
143 }
144  
145 1881 ▶sub _make_pref {
Subroutine _make_pref: called CC=5 SCAR=16.1
146 1881 my $prop = shift;
147 # vCard 4: PREF parameter
148 1881 my $pref = _param($prop, 'pref');
149 1881 ▶ if (defined $pref && looks_like_number($pref)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pref and &looks_like_number($pref)) 358 1523 1881
Condition66%2/3
defined $pref and &looks_like_number($pref)
!ll&&!rl&&r
15230358
MC/DC50%1/2
defined $pref and &looks_like_number($pref)
defined $pref &looks_like_number($pref)
Truth table66%2/3
A and B
A= defined $prefB= &looks_like_number($pref)
ABresult
0X0
100
111
150 358 return $pref + 0;
151 }
152 # vCard 3: TYPE=pref
153 1523 for my $t (_params_list($prop, 'type')) {
154 1417 ▶ return 1 if lc $t eq 'pref';
Branch: 1 branch
BranchTrueFalseTotal
if lc $t eq "pref" 232 1185 1417
155 }
156 1291 return undef;
157 }
158  
159 153 ▶sub _to_utc_datetime {
Subroutine _to_utc_datetime: called CC=3 SCAR=12.0
160 153 my $val = shift;
161 153 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 153 153
162 # Normalize compact timestamps (e.g. 19940930T143510Z -> 1994-09-30T14:35:10Z)
163 153 ▶ if ($val =~ /^(\d{4})-?(\d{2})-?(\d{2})T(\d{2}):?(\d{2}):?(\d{2})/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(\d{4})-?(\d{2})-?(\d{2})T(\d{2}):?(\d{2}):?(\d{2})/) 153 0 153
164 153 return "$1-$2-$3T$4:$5:$6Z";
165 }
166 0 return $val;
167 }
168  
169 1530 ▶sub _set_if {
Subroutine _set_if: called CC=5 SCAR=16.3
170 1530 my ($hash, $key, $val, $transform) = @_;
171 1530 ▶ return unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 971 559 1530
Condition66%2/3
defined $val and $val ne ""
!ll&&!rl&&r
9710559
MC/DC50%1/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table66%2/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
172 559 ▶ $val = $transform->($val) if $transform;
Branch: 1 branch
BranchTrueFalseTotal
if $transform 186 373 559
173 559 ▶ $hash->{$key} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 559 0 559
174 }
175  
176 # Collect X-ABLabel associations (Apple extension)
177 3500 ▶sub _collect_labels {
Subroutine _collect_labels: called CC=6 SCAR=18.0
178 3500 my $props = shift;
179 3500 my %labels;
180 3500 ▶ for my $lp (@{$props->{'x-ablabel'} // []}) {
Condition100%2/2
$props->{"x-ablabel"} // []
l!l
13782122
MC/DC100%1/1
$props->{"x-ablabel"} // []
$props->{"x-ablabel"}
Truth table100%2/2
A
A= $props->{"x-ablabel"}
Aresult
11
00
181 9433 ▶ my $group = $lp->{group} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $group = $lp->{"group"} or next 9433 0 9433
182 9433 ▶ my $val = $lp->{value} // '';
Condition50%1/2
$lp->{"value"} // ""
l!l
94330
MC/DC0%0/1
$lp->{"value"} // ""
$lp->{"value"}
Truth table50%1/2
A
A= $lp->{"value"}
Aresult
11
00
183 9433 ▶ $val = $1 if $val =~ m{^_\$\!<([^>]*)>\!\$_$};
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^_\$\!<([^>]*)>\!\$_$/ 3600 5833 9433
184 9433 $labels{$group} = $val;
185 }
186 3500 return \%labels;
187 }
188  
189 1630 ▶sub _apply_label {
Subroutine _apply_label: called CC=3 SCAR=11.0
190 1630 my ($obj, $prop, $labels) = @_;
191 1630 ▶ if (my $group = $prop->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $prop->{"group"}) 481 1149 1630
192 481 ▶ if (my $label = $labels->{$group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $label = $labels->{$group}) 332 149 481
193 332 $obj->{label} = $label;
194 }
195 }
196 }
197  
198 299 ▶sub _convert_vcard_date {
Subroutine _convert_vcard_date: called CC=6 SCAR=19.6
199 299 my $val = shift;
200 299 ▶ return undef unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 299 299
201  
202 # vCard 4 compact: YYYYMMDD or --MMDD
203 299 ▶ if ($val =~ /^(\d{4}|--)-?(\d{2})-?(\d{2})(?:T|$)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ /^(\d{4}|--)-?(\d{2})-?(\d{2})(?:T|$)/) 299 0 299
204 299 my ($y, $m, $d) = ($1, $2, $3);
205 299 ▶ $y = '0000' if $y eq '--';
Branch: 1 branch
BranchTrueFalseTotal
if $y eq "--" 31 268 299
206 299 ▶ $y = '0000' if $y eq '1604'; # iOS "no year" magic
Branch: 1 branch
BranchTrueFalseTotal
if $y eq 1604 37 262 299
207 299 return "$y-$m-$d";
208 }
209  
210 # Already ISO format
211 0 ▶ return $val if $val =~ /^\d{4}-\d{2}-\d{2}/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /^\d{4}-\d{2}-\d{2}/ 0 0 0
212  
213 0 return $val;
214 }
215  
216 1068 ▶sub _adr_values {
Subroutine _adr_values: called CC=5 SCAR=19.1
217 1068 my ($values, $idx) = @_;
218 1068 my $val = $values->[$idx];
219 1068 ▶ return () unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 1068 1068
220 1068 ▶ if (ref $val eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY") 0 1068 1068
221 0 ▶ return grep { defined $_ && $_ ne '' } @$val;
Condition0%0/3
defined $_ && $_ ne ''
!ll&&!rl&&r
000
MC/DC0%0/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table0%0/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
222 }
223 1068 ▶ return ($val) if $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $val ne "" 924 144 1068
224 144 return ();
225 }
226  
227 ###############################################################################
228 # VCARD -> JSCONTACT
229 ###############################################################################
230  
231 205 ▶sub vcard_to_jscontact {
Subroutine vcard_to_jscontact: called CC=1 SCAR=0.0
Pod undocumented
232 205 my ($vcard_string) = @_;
233 205 my ($card) = _read_vcard(_decode_input($vcard_string));
234 205 return $card;
235 }
236  
237 # The converters that fill a map of the Card, each with the key it fills:
238 # the ids a converter allocates are recorded against that key, so a patch
239 # knows which vCard property each entry came from
240 20 my @MAP_CONVERTERS = (
241 [ nicknames => \&_convert_nicknames ],
242 [ emails => \&_convert_emails ],
243 [ phones => \&_convert_phones ],
244 [ addresses => \&_convert_addresses ],
245 [ organizations => \&_convert_organizations ],
246 [ titles => \&_convert_titles ],
247 [ anniversaries => \&_convert_anniversaries ],
248 [ notes => \&_convert_notes ],
249 [ onlineServices => \&_convert_online_services ],
250 [ media => \&_convert_media ],
251 [ links => \&_convert_links ],
252 [ calendars => \&_convert_calendars ],
253 [ schedulingAddresses => \&_convert_scheduling_addresses ],
254 [ cryptoKeys => \&_convert_crypto_keys ],
255 [ directories => \&_convert_directories ],
256 [ preferredLanguages => \&_convert_preferred_languages ],
257 [ personalInfo => \&_convert_personal_info ],
258 );
259  
260 # Read a vCard given as characters. Returns the Card, the conversion
261 # context (whose {sources} maps each Card entry to the vCard properties it
262 # was read from) and the parsed properties; nothing if it does not parse.
263 # Text::VCardFast recurses once per BEGIN, in C: nesting tens of thousands
264 # deep overflows the stack and kills the process (no eval can catch that).
265 # A vCard nests at most a level or two (an AGENT); refuse anything deeper.
266 20 our $MAX_NESTING = 16;
267 259 ▶sub _nesting_ok {
Subroutine _nesting_ok: called CC=5 SCAR=16.1
268 259 my ($text) = @_;
269 259 my $depth = 0;
270 259 ▶ while ($text =~ /^(BEGIN|END)[ \t]*:/mgi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /^(BEGIN|END)[ \t]*:/mig) 528 258 786
271 528 ▶ if (uc $1 eq 'BEGIN') { $depth++; return 0 if $depth > $MAX_NESTING }
Branch: 2 branches
BranchTrueFalseTotal
if (uc $1 eq 'BEGIN') { } 273 255 528
if $depth > $Text::JSContact::MAX_NESTING 1 272 273
272 255 ▶ else { $depth-- if $depth > 0 }
Branch: 1 branch
BranchTrueFalseTotal
if $depth > 0 255 0 255
273 }
274 258 return 1;
275 }
276  
277 259 ▶sub _read_vcard {
Subroutine _read_vcard: called CC=10 SCAR=23.1
278 259 my ($vcard_string) = @_;
279  
280 259 ▶ return unless _nesting_ok($vcard_string // '');
Branch: 1 branch
BranchTrueFalseTotal
unless _nesting_ok($vcard_string // "") 1 258 259
Condition50%1/2
$vcard_string // ""
l!l
2590
MC/DC0%0/1
$vcard_string // ""
$vcard_string
Truth table50%1/2
A
A= $vcard_string
Aresult
11
00
281 258 my $parsed = eval { vcard2hash($vcard_string, multival => [qw(n adr org)]) };
282 258 ▶ return unless $parsed && $parsed->{objects} && @{$parsed->{objects}};
Branch: 1 branch
BranchTrueFalseTotal
unless $parsed and $parsed->{"objects"} and @{$parsed->{"objects"};} 3 255 258
Condition83%5/6
$parsed and $parsed->{"objects"} and @{$parsed->{"objects"};}
!ll&&!rl&&r
30255
$parsed and $parsed->{"objects"}
!ll&&!rl&&r
21255
MC/DC66%2/3
$parsed and $parsed->{"objects"} and @{$parsed->{"objects"};}
$parsed $parsed->{"objects"} @{$parsed->{"objects"};}
Truth table75%3/4
A and B and C
A= $parsedB= $parsed->{"objects"}C= @{$parsed->{"objects"};}
ABCresult
0XX0
10X0
1100
1111
283  
284 255 my $vcard = $parsed->{objects}[0];
285 255 ▶ return unless $vcard && $vcard->{type} eq 'vcard';
Branch: 1 branch
BranchTrueFalseTotal
unless $vcard and $vcard->{"type"} eq "vcard" 0 255 255
Condition33%1/3
$vcard and $vcard->{"type"} eq "vcard"
!ll&&!rl&&r
00255
MC/DC0%0/2
$vcard and $vcard->{"type"} eq "vcard"
$vcard $vcard->{"type"} eq "vcard"
Truth table33%1/3
A and B
A= $vcardB= $vcard->{"type"} eq "vcard"
ABresult
0X0
100
111
286  
287 255 my $props = $vcard->{properties};
288 255 my $ctx = _seed_context(_new_context(), $props);
289  
290 255 my $card = {
291 '@type' => 'Card',
292 version => '2.0',
293 };
294  
295 # UID: optional since JSContact 2.0 (RFC 9982), which forbids making one
296 # up for a vCard that has none -- a protocol that needs a uid (CardDAV,
297 # JMAP Contacts) assigns it itself
298 255 _set_if($card, 'uid', _first_value($props, 'uid'));
299  
300 # Simple scalar properties
301 _set_if($card, 'kind', _first_value($props, 'kind')
302 // _first_value($props, 'x-addressbookserver-kind'),
303 255 ▶ sub { lc $_[0] });
Subroutine __ANON__: called CC=1 SCAR=0.0
Condition100%3/3
_first_value($props, "kind") // _first_value($props, "x-addressbookserver-kind")
l!l&&r!l&&!r
4520190
MC/DC100%2/2
_first_value($props, "kind") // _first_value($props, "x-addressbookserver-kind")
_first_value($props, "kind") _first_value($props, "x-addressbookserver-kind")
Truth table100%3/3
A // B
A= _first_value($props, "kind")B= _first_value($props, "x-addressbookserver-kind")
ABresult
1X1
011
000
304 255 _set_if($card, 'prodId', _first_value($props, 'prodid'));
305 255 _set_if($card, 'updated', _first_value($props, 'rev'), \&_to_utc_datetime);
306 255 _set_if($card, 'created', _first_value($props, 'created'), \&_to_utc_datetime);
307 255 _set_if($card, 'language', _first_value($props, 'language'));
308  
309 # Complex properties
310 255 _convert_name($card, $props);
311 255 for my $conv (@MAP_CONVERTERS) {
312 4335 my ($key, $fn) = @$conv;
313 4335 $fn->($card, $props, $ctx);
314 4335 _claim_sources($ctx, $key);
315 }
316 255 _convert_keywords($card, $props);
317 255 _convert_members($card, $props, $ctx);
318 255 _convert_related($card, $props, $ctx);
319 255 _convert_speak_to_as($card, $props, $ctx);
320 255 _claim_sources($ctx, 'speakToAs');
321  
322 255 return ($card, $ctx, $props);
323 }
324  
325 # The ids handed out since the last claim belong to $key
326 4590 ▶sub _claim_sources {
Subroutine _claim_sources: called CC=3 SCAR=11.0
327 4590 my ($ctx, $key) = @_;
328 4590 ▶ for my $pair (@{$ctx->{pending} // []}) {
Condition100%2/2
$ctx->{"pending"} // []
l!l
4431159
MC/DC100%1/1
$ctx->{"pending"} // []
$ctx->{"pending"}
Truth table100%2/2
A
A= $ctx->{"pending"}
Aresult
11
00
329 2706 my ($id, $item) = @$pair;
330 2706 push @{$ctx->{sources}{$key}{$id}}, $item;
331 }
332 4590 $ctx->{pending} = [];
333 }
334  
335 ###############################################################################
336 # INDIVIDUAL VCARD -> JSCONTACT CONVERTERS
337 ###############################################################################
338  
339 # FN + N -> name
340 255 ▶sub _convert_name {
Subroutine _convert_name: called CC=25 SCAR=33.7
341 255 my ($card, $props) = @_;
342 255 my $name = {};
343  
344 255 my $fn = _first_value($props, 'fn');
345 255 ▶ $name->{full} = $fn if defined $fn;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fn 253 2 255
346  
347 255 my $n_prop = _first_prop($props, 'n');
348 255 ▶ if ($n_prop) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n_prop) 189 66 255
349 189 ▶ my $values = $n_prop->{values} || [];
Condition50%1/2
$n_prop->{'values'} || []
l!l
1890
MC/DC0%0/1
$n_prop->{'values'} || []
$n_prop->{'values'}
Truth table50%1/2
A
A= $n_prop->{'values'}
Aresult
11
00
350 # N component order: surname, given, given2, title(prefix), credential(suffix), surname2, generation
351 189 my @kind_map = qw(surname given given2 title credential surname2 generation);
352  
353 189 my @components;
354 189 for my $idx (0..$#kind_map) {
355 1323 my $raw = $values->[$idx];
356 1323 ▶ next unless defined $raw;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $raw 420 903 1323
357 # Handle both arrayref and string; split comma-separated values
358 903 my @vals;
359 903 ▶ for my $v (ref $raw eq 'ARRAY' ? @$raw : ($raw)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $raw eq 'ARRAY' ? : 0 903 903
360 903 push @vals, split /,/, $v;
361 }
362 903 for my $v (@vals) {
363 653 ▶ next unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 0 653 653
Condition33%1/3
defined $v and $v ne ""
!ll&&!rl&&r
00653
MC/DC0%0/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table33%1/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
364 653 push @components, {
365 '@type' => 'NameComponent',
366 kind => $kind_map[$idx],
367 value => $v,
368 };
369 }
370 }
371  
372 189 ▶ $name->{components} = \@components if @components;
Branch: 1 branch
BranchTrueFalseTotal
if @components 168 21 189
373  
374 # SORT-AS parameter
375 189 my $sort_as_param = _param($n_prop, 'sort-as');
376 189 ▶ if ($sort_as_param) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort_as_param) 32 157 189
377 32 my @sa = split /,/, $sort_as_param;
378 32 my %sort_map;
379 32 for my $i (0..$#sa) {
380 64 ▶ $sort_map{$kind_map[$i]} = $sa[$i]
Branch: 1 branch
BranchTrueFalseTotal
if $i <= $#kind_map and defined $sa[$i] and $sa[$i] ne "" 64 0 64
Condition33%2/6
$i <= $#kind_map and defined $sa[$i] and $sa[$i] ne ""
!ll&&!rl&&r
0064
$i <= $#kind_map and defined $sa[$i]
!ll&&!rl&&r
0064
MC/DC0%0/3
$i <= $#kind_map and defined $sa[$i] and $sa[$i] ne ""
$i <= $#kind_map defined $sa[$i] $sa[$i] ne ""
Truth table25%1/4
A and B and C
A= $i <= $#kind_mapB= defined $sa[$i]C= $sa[$i] ne ""
ABCresult
0XX0
10X0
1100
1111
381 if $i <= $#kind_map && defined $sa[$i] && $sa[$i] ne '';
382 }
383 32 ▶ $name->{sortAs} = \%sort_map if %sort_map;
Branch: 1 branch
BranchTrueFalseTotal
if %sort_map 32 0 32
384 }
385 }
386  
387 # Apple X-PHONETIC-*-NAME -> phonetic name components
388 255 my $phonFirst = _first_value($props, 'x-phonetic-first-name');
389 255 my $phonLast = _first_value($props, 'x-phonetic-last-name');
390 255 my $phonMid = _first_value($props, 'x-phonetic-middle-name');
391 255 ▶ if ($phonFirst || $phonLast || $phonMid) {
Branch: 1 branch
BranchTrueFalseTotal
if ($phonFirst or $phonLast or $phonMid) 38 217 255
Condition66%4/6
$phonFirst or $phonLast or $phonMid
l!l&&r!l&&!r
380217
$phonFirst or $phonLast
l!l&&r!l&&!r
380217
MC/DC33%1/3
$phonFirst or $phonLast or $phonMid
$phonFirst $phonLast $phonMid
Truth table50%2/4
A or B or C
A= $phonFirstB= $phonLastC= $phonMid
ABCresult
1XX1
01X1
0011
0000
392 38 my @phon;
393 38 ▶ push @phon, { '@type' => 'NameComponent', kind => 'given', value => $phonFirst, phonetic => 'ipa' } if $phonFirst;
Branch: 1 branch
BranchTrueFalseTotal
if $phonFirst 38 0 38
394 38 ▶ push @phon, { '@type' => 'NameComponent', kind => 'given2', value => $phonMid, phonetic => 'ipa' } if $phonMid;
Branch: 1 branch
BranchTrueFalseTotal
if $phonMid 37 1 38
395 38 ▶ push @phon, { '@type' => 'NameComponent', kind => 'surname', value => $phonLast, phonetic => 'ipa' } if $phonLast;
Branch: 1 branch
BranchTrueFalseTotal
if $phonLast 38 0 38
396 38 $name->{phoneticComponents} = \@phon;
397 }
398  
399 255 ▶ $card->{name} = $name if %$name;
Branch: 1 branch
BranchTrueFalseTotal
if %$name 253 2 255
400 }
401  
402 # NICKNAME -> nicknames
403 255 ▶sub _convert_nicknames {
Subroutine _convert_nicknames: called CC=8 SCAR=21.1
404 255 my ($card, $props, $ctx) = @_;
405 255 ▶ my $items = $props->{nickname} || return;
Condition100%2/2
$props->{'nickname'} || (return)
l!l
96159
MC/DC100%1/1
$props->{'nickname'} || (return)
$props->{'nickname'}
Truth table100%2/2
A
A= $props->{'nickname'}
Aresult
11
00
406  
407 96 my %map;
408 96 for my $item (@$items) {
409 97 my $val = $item->{value};
410 97 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 97 97
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0097
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
411 97 my $id = _prop_id($item, $ctx);
412 97 my $obj = { '@type' => 'Nickname', name => $val };
413 97 ▶ if (my $ctx = _make_contexts(_params_list($item, 'type'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(_params_list($item, "type"))) 18 79 97
414 18 $obj->{contexts} = $ctx;
415 }
416 97 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 1 96 97
417 97 $map{$id} = $obj;
418 }
419  
420 96 ▶ $card->{nicknames} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 96 0 96
421 }
422  
423 # EMAIL -> emails
424 255 ▶sub _convert_emails {
Subroutine _convert_emails: called CC=8 SCAR=21.0
425 255 my ($card, $props, $ctx) = @_;
426 255 ▶ my $items = $props->{email} || return;
Condition100%2/2
$props->{'email'} || (return)
l!l
15798
MC/DC100%1/1
$props->{'email'} || (return)
$props->{'email'}
Truth table100%2/2
A
A= $props->{'email'}
Aresult
11
00
427 157 my $labels = _collect_labels($props);
428  
429 157 my %map;
430 157 for my $item (@$items) {
431 303 my $val = $item->{value};
432 303 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 303 303
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00303
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
433 303 my $id = _prop_id($item, $ctx);
434 303 my $obj = { '@type' => 'EmailAddress', address => $val };
435 303 ▶ if (my $ctx = _make_contexts(_params_list($item, 'type'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(_params_list($item, "type"))) 219 84 303
436 219 $obj->{contexts} = $ctx;
437 }
438 303 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 100 203 303
439 303 _apply_label($obj, $item, $labels);
440 303 $map{$id} = $obj;
441 }
442  
443 157 ▶ $card->{emails} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 157 0 157
444 }
445  
446 # TEL -> phones
447 255 ▶sub _convert_phones {
Subroutine _convert_phones: called CC=11 SCAR=24.1
448 255 my ($card, $props, $ctx) = @_;
449 255 ▶ my $items = $props->{tel} || return;
Condition100%2/2
$props->{'tel'} || (return)
l!l
144111
MC/DC100%1/1
$props->{'tel'} || (return)
$props->{'tel'}
Truth table100%2/2
A
A= $props->{'tel'}
Aresult
11
00
450 144 my $labels = _collect_labels($props);
451  
452 144 my %tel_feature_map = (
453 cell => 'mobile', voice => 'voice', fax => 'fax',
454 video => 'video', pager => 'pager', text => 'text',
455 textphone => 'textphone',
456 );
457  
458 144 my %map;
459 144 for my $item (@$items) {
460 351 my $val = $item->{value};
461 351 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 351 351
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00351
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
462 351 my $id = _prop_id($item, $ctx);
463 351 my $obj = { '@type' => 'Phone', number => $val };
464  
465 351 my @types = _params_list($item, 'type');
466 351 ▶ if (my $ctx = _make_contexts(@types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 175 176 351
467 175 $obj->{contexts} = $ctx;
468 }
469  
470 351 my %features;
471 351 for my $t (@types) {
472 555 my $feat = $tel_feature_map{lc $t};
473 555 ▶ $features{$feat} = JSON::true if $feat;
Branch: 1 branch
BranchTrueFalseTotal
if $feat 321 234 555
474 }
475 351 ▶ $obj->{features} = \%features if %features;
Branch: 1 branch
BranchTrueFalseTotal
if %features 225 126 351
476  
477 351 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 85 266 351
478 351 _apply_label($obj, $item, $labels);
479 351 $map{$id} = $obj;
480 }
481  
482 144 ▶ $card->{phones} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 144 0 144
483 }
484  
485 # ADR -> addresses (+ GEO, TZ by group association)
486 255 ▶sub _convert_addresses {
Subroutine _convert_addresses: called CC=34 SCAR=35.7
487 255 my ($card, $props, $ctx) = @_;
488 255 ▶ my $items = $props->{adr} || return;
Condition100%2/2
$props->{'adr'} || (return)
l!l
139116
MC/DC100%1/1
$props->{'adr'} || (return)
$props->{'adr'}
Truth table100%2/2
A
A= $props->{'adr'}
Aresult
11
00
489 139 my $labels = _collect_labels($props);
490  
491 # Collect grouped GEO/TZ for association with ADR
492 139 my %geo_by_group;
493 my %tz_by_group;
494 139 ▶ for my $geo (@{$props->{geo} // []}) {
Condition100%2/2
$props->{"geo"} // []
l!l
12127
MC/DC100%1/1
$props->{"geo"} // []
$props->{"geo"}
Truth table100%2/2
A
A= $props->{"geo"}
Aresult
11
00
495 12 ▶ $geo_by_group{$geo->{group}} = $geo->{value} if $geo->{group};
Branch: 1 branch
BranchTrueFalseTotal
if $geo->{"group"} 0 12 12
496 }
497 139 ▶ for my $tz (@{$props->{tz} // []}) {
Condition100%2/2
$props->{"tz"} // []
l!l
12127
MC/DC100%1/1
$props->{"tz"} // []
$props->{"tz"}
Truth table100%2/2
A
A= $props->{"tz"}
Aresult
11
00
498 12 ▶ $tz_by_group{$tz->{group}} = $tz->{value} if $tz->{group};
Branch: 1 branch
BranchTrueFalseTotal
if $tz->{"group"} 0 12 12
499 }
500  
501 139 my %map;
502 139 for my $item (@$items) {
503 178 my $id = _prop_id($item, $ctx);
504 178 ▶ my $values = $item->{values} || [];
Condition50%1/2
$item->{'values'} || []
l!l
1780
MC/DC0%0/1
$item->{'values'} || []
$item->{'values'}
Truth table50%1/2
A
A= $item->{'values'}
Aresult
11
00
505 178 my $obj = { '@type' => 'Address' };
506  
507 # Build components from ADR structured value
508 # Indices: 0=POBox 1=Extended 2=Street 3=Locality 4=Region 5=PostCode 6=Country
509 178 my @components;
510 178 for my $v (_adr_values($values, 2)) {
511 177 push @components, { '@type' => 'AddressComponent', kind => 'name', value => $v };
512 }
513 178 for my $v (_adr_values($values, 1)) {
514 68 push @components, { '@type' => 'AddressComponent', kind => 'apartment', value => $v };
515 }
516 178 for my $v (_adr_values($values, 3)) {
517 178 push @components, { '@type' => 'AddressComponent', kind => 'locality', value => $v };
518 }
519 178 for my $v (_adr_values($values, 4)) {
520 152 push @components, { '@type' => 'AddressComponent', kind => 'region', value => $v };
521 }
522 178 for my $v (_adr_values($values, 5)) {
523 174 push @components, { '@type' => 'AddressComponent', kind => 'postcode', value => $v };
524 }
525 178 for my $v (_adr_values($values, 6)) {
526 175 push @components, { '@type' => 'AddressComponent', kind => 'country', value => $v };
527 }
528 178 ▶ $obj->{components} = \@components if @components;
Branch: 1 branch
BranchTrueFalseTotal
if @components 178 0 178
529  
530 # Parameters
531 178 my $label_param = _param($item, 'label');
532 178 ▶ $obj->{full} = $label_param if defined $label_param;
Branch: 1 branch
BranchTrueFalseTotal
if defined $label_param 18 160 178
533  
534 178 my $geo = _param($item, 'geo');
535 178 ▶ $geo //= $geo_by_group{$item->{group}} if $item->{group};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"group"} 96 82 178
Condition33%1/3
$geo //= $geo_by_group{$item->{"group"}}
l!l&&r!l&&!r
0096
MC/DC0%0/2
$geo //= $geo_by_group{$item->{"group"}}
$geo $geo_by_group{$item->{"group"}}
Truth table33%1/3
A //= B
A= $geoB= $geo_by_group{$item->{"group"}}
ABresult
1X1
011
000
536 178 ▶ $obj->{coordinates} = $geo if defined $geo;
Branch: 1 branch
BranchTrueFalseTotal
if defined $geo 17 161 178
537  
538 178 my $tz = _param($item, 'tz');
539 178 ▶ $tz //= $tz_by_group{$item->{group}} if $item->{group};
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"group"} 96 82 178
Condition33%1/3
$tz //= $tz_by_group{$item->{"group"}}
l!l&&r!l&&!r
0096
MC/DC0%0/2
$tz //= $tz_by_group{$item->{"group"}}
$tz $tz_by_group{$item->{"group"}}
Truth table33%1/3
A //= B
A= $tzB= $tz_by_group{$item->{"group"}}
ABresult
1X1
011
000
540 178 ▶ $obj->{timeZone} = $tz if defined $tz;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tz 17 161 178
541  
542 178 my $cc = _param($item, 'cc');
543 # Apple X-ABADR grouped with ADR
544 178 ▶ if (!$cc && $item->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $cc and $item->{"group"}) 96 82 178
Condition100%3/3
not $cc and $item->{"group"}
!ll&&!rl&&r
354796
MC/DC100%2/2
not $cc and $item->{"group"}
not $cc $item->{"group"}
Truth table100%3/3
not(A) and B
A= not $ccB= $item->{"group"}
ABresult
0X0
100
111
545 96 ▶ for my $xabadr (@{$props->{'x-abadr'} // []}) {
Condition50%1/2
$props->{"x-abadr"} // []
l!l
960
MC/DC0%0/1
$props->{"x-abadr"} // []
$props->{"x-abadr"}
Truth table50%1/2
A
A= $props->{"x-abadr"}
Aresult
11
00
546 133 ▶ if (($xabadr->{group} // '') eq $item->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (($xabadr->{"group"} // "") eq $item->{"group"}) 96 37 133
Condition50%1/2
$xabadr->{"group"} // ""
l!l
1330
MC/DC0%0/1
$xabadr->{"group"} // ""
$xabadr->{"group"}
Truth table50%1/2
A
A= $xabadr->{"group"}
Aresult
11
00
547 96 $cc = $xabadr->{value};
548 96 last;
549 }
550 }
551 }
552 178 ▶ $obj->{countryCode} = $cc if defined $cc;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cc 131 47 178
553  
554 178 my @types = _params_list($item, 'type');
555 178 ▶ if (my $ctx = _make_contexts(@types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 175 3 178
556 175 $obj->{contexts} = $ctx;
557 }
558 178 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 79 99 178
559 178 _apply_label($obj, $item, $labels);
560  
561 178 $map{$id} = $obj;
562 }
563  
564 139 ▶ $card->{addresses} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 139 0 139
565 }
566  
567 # ORG -> organizations
568 255 ▶sub _convert_organizations {
Subroutine _convert_organizations: called CC=22 SCAR=34.1
569 255 my ($card, $props, $ctx) = @_;
570 255 ▶ my $items = $props->{org} || return;
Condition100%2/2
$props->{'org'} || (return)
l!l
138117
MC/DC100%1/1
$props->{'org'} || (return)
$props->{'org'}
Truth table100%2/2
A
A= $props->{'org'}
Aresult
11
00
571  
572 138 my %map;
573 138 for my $item (@$items) {
574 138 my $id = _prop_id($item, $ctx);
575 138 ▶ my $values = $item->{values} || [];
Condition50%1/2
$item->{'values'} || []
l!l
1380
MC/DC0%0/1
$item->{'values'} || []
$item->{'values'}
Truth table50%1/2
A
A= $item->{'values'}
Aresult
11
00
576 138 my $obj = { '@type' => 'Organization' };
577  
578 # First component -> name
579 0 ▶ my @name_vals = ref $values->[0] eq 'ARRAY' ? @{$values->[0]} : ($values->[0] // ());
Branch: 1 branch
BranchTrueFalseTotal
ref $values->[0] eq 'ARRAY' ? : 0 138 138
Condition33%1/3
$values->[0] // ()
l!l&&r!l&&!r
13800
MC/DC0%0/2
$values->[0] // ()
$values->[0] ()
Truth table33%1/3
A // B
A= $values->[0]B= ()
ABresult
1X1
011
000
580 138 ▶ my $org_name = join(' ', grep { defined $_ && $_ ne '' } @name_vals);
Condition33%1/3
defined $_ && $_ ne ''
!ll&&!rl&&r
00138
MC/DC0%0/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table33%1/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
581 138 ▶ $obj->{name} = $org_name if $org_name ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $org_name ne "" 138 0 138
582  
583 # Subsequent components -> units
584 138 my @units;
585 138 for my $i (1..$#$values) {
586 0 ▶ my @vals = ref $values->[$i] eq 'ARRAY' ? @{$values->[$i]} : ($values->[$i]);
Branch: 1 branch
BranchTrueFalseTotal
ref $values->[$i] eq 'ARRAY' ? : 0 167 167
587 167 for my $v (@vals) {
588 167 ▶ next unless defined $v && $v ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v and $v ne "" 0 167 167
Condition33%1/3
defined $v and $v ne ""
!ll&&!rl&&r
00167
MC/DC0%0/2
defined $v and $v ne ""
defined $v $v ne ""
Truth table33%1/3
A and B
A= defined $vB= $v ne ""
ABresult
0X0
100
111
589 167 push @units, { '@type' => 'OrgUnit', name => $v };
590 }
591 }
592 138 ▶ $obj->{units} = \@units if @units;
Branch: 1 branch
BranchTrueFalseTotal
if @units 97 41 138
593  
594 # SORT-AS
595 138 my $sort_as = _param($item, 'sort-as');
596 138 ▶ if (defined $sort_as) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sort_as) 32 106 138
597 32 my @sa = split /,/, $sort_as;
598 32 ▶ $obj->{sortAs} = $sa[0] if defined $sa[0] && $sa[0] ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $sa[0] and $sa[0] ne "" 32 0 32
Condition33%1/3
defined $sa[0] and $sa[0] ne ""
!ll&&!rl&&r
0032
MC/DC0%0/2
defined $sa[0] and $sa[0] ne ""
defined $sa[0] $sa[0] ne ""
Truth table33%1/3
A and B
A= defined $sa[0]B= $sa[0] ne ""
ABresult
0X0
100
111
599 32 for my $i (0..$#units) {
600 64 ▶ $units[$i]{sortAs} = $sa[$i+1] if defined $sa[$i+1] && $sa[$i+1] ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $sa[$i + 1] and $sa[$i + 1] ne "" 1 63 64
Condition66%2/3
defined $sa[$i + 1] and $sa[$i + 1] ne ""
!ll&&!rl&&r
6301
MC/DC50%1/2
defined $sa[$i + 1] and $sa[$i + 1] ne ""
defined $sa[$i + 1] $sa[$i + 1] ne ""
Truth table66%2/3
A and B
A= defined $sa[$i + 1]B= $sa[$i + 1] ne ""
ABresult
0X0
100
111
601 }
602 }
603  
604 138 my @types = _params_list($item, 'type');
605 138 ▶ if (my $ctx = _make_contexts(@types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 14 124 138
606 14 $obj->{contexts} = $ctx;
607 }
608  
609 138 $map{$id} = $obj;
610 }
611  
612 138 ▶ $card->{organizations} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 138 0 138
613 }
614  
615 # TITLE + ROLE -> titles
616 255 ▶sub _convert_titles {
Subroutine _convert_titles: called CC=7 SCAR=19.7
617 255 my ($card, $props, $ctx) = @_;
618  
619 255 my %map;
620 255 for my $spec (['title', 'title'], ['role', 'role']) {
621 510 my ($propname, $kind) = @$spec;
622 510 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
128382
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
623 142 my $val = $item->{value};
624 142 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 142 142
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00142
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
625 142 my $id = _prop_id($item, $ctx);
626 142 $map{$id} = { '@type' => 'Title', kind => $kind, name => $val };
627 }
628 }
629  
630 255 ▶ $card->{titles} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 109 146 255
631 }
632  
633 # BDAY, ANNIVERSARY, DEATHDATE, X-ABDATE -> anniversaries
634 255 ▶sub _convert_anniversaries {
Subroutine _convert_anniversaries: called CC=15 SCAR=28.2
635 255 my ($card, $props, $ctx) = @_;
636 255 my $labels = _collect_labels($props);
637  
638 255 my %map;
639 255 for my $spec (['bday', 'birth'], ['anniversary', 'wedding'], ['deathdate', 'death']) {
640 765 my ($propname, $kind) = @$spec;
641 765 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
180585
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
642 183 my $val = $item->{value};
643 183 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 183 183
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00183
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
644 183 my $id = _prop_id($item, $ctx);
645 183 $map{$id} = {
646 '@type' => 'Anniversary',
647 kind => $kind,
648 date => _convert_vcard_date($val),
649 };
650 }
651 }
652  
653 # Apple X-ABDATE + X-ABLabel -> anniversaries
654 255 ▶ for my $item (@{$props->{'x-abdate'} // []}) {
Condition100%2/2
$props->{"x-abdate"} // []
l!l
58197
MC/DC100%1/1
$props->{"x-abdate"} // []
$props->{"x-abdate"}
Truth table100%2/2
A
A= $props->{"x-abdate"}
Aresult
11
00
655 116 my $val = $item->{value};
656 116 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 116 116
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00116
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
657 116 my $id = _prop_id($item, $ctx);
658  
659 116 my $kind = 'other';
660 116 ▶ if (my $group = $item->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $item->{"group"}) 116 0 116
661 116 my $label = $labels->{$group};
662 116 ▶ if ($label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label) 116 0 116
663 116 my $lt = lc $label;
664 116 ▶ if ($lt eq 'anniversary') { $kind = 'wedding' }
Branch: 2 branches
BranchTrueFalseTotal
if ($lt eq 'anniversary') { } 58 58 116
elsif ($lt eq 'other') { } 0 58 58
665 0 elsif ($lt eq 'other') { $kind = 'other' }
666 58 else { $kind = $lt }
667 }
668 }
669  
670 116 $map{$id} = {
671 '@type' => 'Anniversary',
672 kind => $kind,
673 date => _convert_vcard_date($val),
674 };
675 }
676  
677 255 ▶ $card->{anniversaries} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 116 139 255
678 }
679  
680 # NOTE -> notes
681 255 ▶sub _convert_notes {
Subroutine _convert_notes: called CC=11 SCAR=24.4
682 255 my ($card, $props, $ctx) = @_;
683 255 ▶ my $items = $props->{note} || return;
Condition100%2/2
$props->{'note'} || (return)
l!l
141114
MC/DC100%1/1
$props->{'note'} || (return)
$props->{'note'}
Truth table100%2/2
A
A= $props->{'note'}
Aresult
11
00
684  
685 141 my %map;
686 141 for my $item (@$items) {
687 156 my $val = $item->{value};
688 156 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 156 156
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00156
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
689 156 my $id = _prop_id($item, $ctx);
690 156 my $obj = { '@type' => 'Note', note => $val };
691  
692 156 my $created = _param($item, 'created');
693 156 ▶ $obj->{created} = _to_utc_datetime($created) if defined $created;
Branch: 1 branch
BranchTrueFalseTotal
if defined $created 32 124 156
694  
695 156 my $author_name = _param($item, 'author-name');
696 156 my $author_uri = _param($item, 'author');
697 156 ▶ if ($author_name || $author_uri) {
Branch: 1 branch
BranchTrueFalseTotal
if ($author_name or $author_uri) 33 123 156
Condition66%2/3
$author_name or $author_uri
l!l&&r!l&&!r
330123
MC/DC50%1/2
$author_name or $author_uri
$author_name $author_uri
Truth table66%2/3
A or B
A= $author_nameB= $author_uri
ABresult
1X1
011
000
698 33 my $author = {};
699 33 ▶ $author->{name} = $author_name if $author_name;
Branch: 1 branch
BranchTrueFalseTotal
if $author_name 33 0 33
700 33 ▶ $author->{uri} = $author_uri if $author_uri;
Branch: 1 branch
BranchTrueFalseTotal
if $author_uri 1 32 33
701 33 $obj->{author} = $author;
702 }
703  
704 156 $map{$id} = $obj;
705 }
706  
707 141 ▶ $card->{notes} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 141 0 141
708 }
709  
710 # IMPP, SOCIALPROFILE, X-SOCIALPROFILE -> onlineServices
711 255 ▶sub _convert_online_services {
Subroutine _convert_online_services: called CC=40 SCAR=39.6
712 255 my ($card, $props, $ctx) = @_;
713 255 my $labels = _collect_labels($props);
714  
715 255 my %map;
716  
717 255 ▶ for my $item (@{$props->{impp} // []}) {
Condition100%2/2
$props->{"impp"} // []
l!l
99156
MC/DC100%1/1
$props->{"impp"} // []
$props->{"impp"}
Truth table100%2/2
A
A= $props->{"impp"}
Aresult
11
00
718 110 my $val = $item->{value};
719 110 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 110 110
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00110
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
720 110 my $id = _prop_id($item, $ctx);
721 110 my $obj = { '@type' => 'OnlineService' };
722 # RFC 9554 SERVICE-TYPE and USERNAME, as _unconvert_online_services
723 # writes them, or the older x- forms
724 110 ▶ my $service = _param($item, 'service-type') // _param($item, 'x-service-type');
Condition100%3/3
_param($item, "service-type") // _param($item, "x-service-type")
l!l&&r!l&&!r
343838
MC/DC100%2/2
_param($item, "service-type") // _param($item, "x-service-type")
_param($item, "service-type") _param($item, "x-service-type")
Truth table100%3/3
A // B
A= _param($item, "service-type")B= _param($item, "x-service-type")
ABresult
1X1
011
000
725 110 ▶ $obj->{service} = $service if defined $service;
Branch: 1 branch
BranchTrueFalseTotal
if defined $service 72 38 110
726 110 ▶ my $user = _param($item, 'username') // _param($item, 'x-user');
Condition66%2/3
_param($item, "username") // _param($item, "x-user")
l!l&&r!l&&!r
29081
MC/DC50%1/2
_param($item, "username") // _param($item, "x-user")
_param($item, "username") _param($item, "x-user")
Truth table66%2/3
A // B
A= _param($item, "username")B= _param($item, "x-user")
ABresult
1X1
011
000
727 110 ▶ $obj->{user} = $user if defined $user;
Branch: 1 branch
BranchTrueFalseTotal
if defined $user 29 81 110
728 # a service with only a user name is written with that name as the value
729 110 ▶ $obj->{uri} = $val unless defined $user && $val eq $user && $val !~ /^[a-z][a-z0-9+.-]*:/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user and $val eq $user and not $val =~ /^[a-z][a-z0-9+.-]*:/i 103 7 110
Condition83%5/6
defined $user and $val eq $user and not $val =~ /^[a-z][a-z0-9+.-]*:/i
!ll&&!rl&&r
10307
defined $user and $val eq $user
!ll&&!rl&&r
81227
MC/DC66%2/3
defined $user and $val eq $user and not $val =~ /^[a-z][a-z0-9+.-]*:/i
defined $user $val eq $user not $val =~ /^[a-z][a-z0-9+.-]*:/i
Truth table75%3/4
A and B and not(C)
A= defined $userB= $val eq $userC= not $val =~ /^[a-z][a-z0-9+.-]*:/i
ABCresult
0XX0
10X0
1100
1111
730 110 my @types = _params_list($item, 'type');
731 110 ▶ if (my $ctx = _make_contexts(@types)) { $obj->{contexts} = $ctx }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 2 108 110
732 110 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 56 54 110
733 110 _apply_label($obj, $item, $labels);
734 110 $map{$id} = $obj;
735 }
736  
737 255 for my $propname (qw(socialprofile x-socialprofile)) {
738 510 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
67443
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
739 105 my $val = $item->{value};
740 105 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 105 105
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00105
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
741 105 my $id = _prop_id($item, $ctx);
742 105 my $obj = { '@type' => 'OnlineService' };
743  
744 105 ▶ if ($val =~ m{://}) { $obj->{uri} = $val }
Branch: 1 branch
BranchTrueFalseTotal
if ($val =~ m[://]) { } 105 0 105
745 0 else { $obj->{user} = $val }
746  
747 105 ▶ my $service = _param($item, 'service-type') // _param($item, 'x-service-type');
Condition66%2/3
_param($item, "service-type") // _param($item, "x-service-type")
l!l&&r!l&&!r
29076
MC/DC50%1/2
_param($item, "service-type") // _param($item, "x-service-type")
_param($item, "service-type") _param($item, "x-service-type")
Truth table66%2/3
A // B
A= _param($item, "service-type")B= _param($item, "x-service-type")
ABresult
1X1
011
000
748 105 ▶ unless ($service) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($service) 76 29 105
749 76 for my $t (_params_list($item, 'type')) {
750 76 my $lt = lc $t;
751 76 ▶ if ($lt ne 'home' && $lt ne 'work' && $lt ne 'pref') {
Branch: 1 branch
BranchTrueFalseTotal
if ($lt ne "home" and $lt ne "work" and $lt ne "pref") 76 0 76
Condition33%2/6
$lt ne "home" and $lt ne "work" and $lt ne "pref"
!ll&&!rl&&r
0076
$lt ne "home" and $lt ne "work"
!ll&&!rl&&r
0076
MC/DC0%0/3
$lt ne "home" and $lt ne "work" and $lt ne "pref"
$lt ne "home" $lt ne "work" $lt ne "pref"
Truth table25%1/4
A and B and C
A= $lt ne "home"B= $lt ne "work"C= $lt ne "pref"
ABCresult
0XX0
10X0
1100
1111
752 76 $service = $lt;
753 76 last;
754 }
755 }
756 }
757 105 ▶ $obj->{service} = $service if defined $service;
Branch: 1 branch
BranchTrueFalseTotal
if defined $service 105 0 105
758  
759 105 ▶ my $user = _param($item, 'x-user') // _param($item, 'username');
Condition66%2/3
_param($item, "x-user") // _param($item, "username")
l!l&&r!l&&!r
76029
MC/DC50%1/2
_param($item, "x-user") // _param($item, "username")
_param($item, "x-user") _param($item, "username")
Truth table66%2/3
A // B
A= _param($item, "x-user")B= _param($item, "username")
ABresult
1X1
011
000
760 105 ▶ $obj->{user} = $user if defined $user && !$obj->{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $user and not $obj->{"user"} 76 29 105
Condition66%2/3
defined $user and not $obj->{"user"}
!ll&&!rl&&r
29076
MC/DC50%1/2
defined $user and not $obj->{"user"}
defined $user not $obj->{"user"}
Truth table66%2/3
A and not(B)
A= defined $userB= not $obj->{"user"}
ABresult
0X0
100
111
761  
762 105 my @types = _params_list($item, 'type');
763 0 ▶ if (my $ctx = _make_contexts(@types)) { $obj->{contexts} = $ctx }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 0 105 105
764 0 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 0 105 105
765 105 _apply_label($obj, $item, $labels);
766 105 $map{$id} = $obj;
767 }
768 }
769  
770 # Legacy X-service IM properties (Apple/others)
771 255 my %xservice_map = (
772 'x-aim' => 'AIM',
773 'x-icq' => 'ICQ',
774 'x-msn' => 'MSN',
775 'x-yahoo' => 'Yahoo',
776 'x-jabber' => 'Jabber',
777 'x-skype' => 'Skype',
778 'x-skype-username' => 'Skype',
779 'x-twitter' => 'Twitter',
780 'x-google-talk' => 'GoogleTalk',
781 );
782 255 for my $propname (sort keys %xservice_map) {
783 2295 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
392256
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
784 39 my $val = $item->{value};
785 39 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 39 39
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0039
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
786 39 my $id = _prop_id($item, $ctx);
787 39 my $obj = { '@type' => 'OnlineService', user => $val, service => $xservice_map{$propname} };
788 39 my @types = _params_list($item, 'type');
789 39 ▶ if (my $ctx = _make_contexts(@types)) { $obj->{contexts} = $ctx }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 37 2 39
790 0 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 0 39 39
791 39 _apply_label($obj, $item, $labels);
792 39 $map{$id} = $obj;
793 }
794 }
795  
796 255 ▶ $card->{onlineServices} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 100 155 255
797 }
798  
799 # PHOTO, LOGO, SOUND -> media
800 255 ▶sub _convert_media {
Subroutine _convert_media: called CC=17 SCAR=29.5
801 255 my ($card, $props, $ctx) = @_;
802 255 my $labels = _collect_labels($props);
803  
804 255 my %map;
805 255 for my $spec (['photo', 'photo'], ['logo', 'logo'], ['sound', 'sound']) {
806 765 my ($propname, $kind) = @$spec;
807 765 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
131634
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
808 132 my $val = $item->{value};
809 132 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 132 132
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00132
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
810 132 my $id = _prop_id($item, $ctx);
811 132 my $obj = { '@type' => 'Media', kind => $kind };
812  
813 132 my $encoding = _param($item, 'encoding');
814 132 ▶ if ($encoding && lc($encoding) eq 'b') {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding and lc $encoding eq 'b') { } 39 93 132
Condition66%2/3
$encoding and lc $encoding eq "b"
!ll&&!rl&&r
93039
MC/DC50%1/2
$encoding and lc $encoding eq "b"
$encoding lc $encoding eq "b"
Truth table66%2/3
A and B
A= $encodingB= lc $encoding eq "b"
ABresult
0X0
100
111
815 # vCard 3 inline data (RFC 2426 S3.1.4): TYPE is the format,
816 # e.g. TYPE=PNG; media types are case-insensitive, lower-case
817 # them as RFC 2045 writes them
818 39 ▶ my $mediatype = _param($item, 'mediatype') // _param($item, 'type');
Condition33%1/3
_param($item, "mediatype") // _param($item, "type")
l!l&&r!l&&!r
0390
MC/DC0%0/2
_param($item, "mediatype") // _param($item, "type")
_param($item, "mediatype") _param($item, "type")
Truth table33%1/3
A // B
A= _param($item, "mediatype")B= _param($item, "type")
ABresult
1X1
011
000
819 # TYPE is a list (RFC 2426 S3.1.4 allows one format, but servers
820 # merge: Cyrus writes TYPE=PNG,PNG for a vCard 4 PHOTO that had both
821 # a MEDIATYPE and a data: URI); the format is its first value
822 39 ▶ ($mediatype) = grep { length } split /\s*,\s*/, $mediatype if defined $mediatype;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mediatype 39 0 39
823 39 ▶ $mediatype = ($kind eq 'sound' ? 'audio/' : 'image/') . $mediatype
Branch: 2 branches
BranchTrueFalseTotal
if $mediatype and not $mediatype =~ m[/] 39 0 39
$kind eq 'sound' ? : 0 39 39
Condition33%1/3
$mediatype and not $mediatype =~ m[/]
!ll&&!rl&&r
0039
MC/DC0%0/2
$mediatype and not $mediatype =~ m[/]
$mediatype not $mediatype =~ m[/]
Truth table33%1/3
A and not(B)
A= $mediatypeB= not $mediatype =~ m[/]
ABresult
0X0
100
111
824 if $mediatype && $mediatype !~ m{/};
825 39 ▶ $mediatype = lc($mediatype // 'application/octet-stream');
Condition50%1/2
$mediatype // "application/octet-stream"
l!l
390
MC/DC0%0/1
$mediatype // "application/octet-stream"
$mediatype
Truth table50%1/2
A
A= $mediatype
Aresult
11
00
826 # the value is the base64 text already: Text::VCardFast does
827 # not decode it
828 39 (my $b64 = $val) =~ s/\s+//g;
829 39 $obj->{uri} = "data:$mediatype;base64,$b64";
830 39 $obj->{mediaType} = $mediatype;
831 } else {
832 93 $obj->{uri} = $val;
833 93 my $mt = _param($item, 'mediatype');
834 93 ▶ $obj->{mediaType} = $mt if defined $mt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mt 24 69 93
835 }
836  
837 132 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 1 131 132
838 132 _apply_label($obj, $item, $labels);
839 132 $map{$id} = $obj;
840 }
841 }
842  
843 255 ▶ $card->{media} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 101 154 255
844 }
845  
846 # Generic resource converter for URI-based properties
847 2040 ▶sub _convert_resource_to {
Subroutine _convert_resource_to: called CC=11 SCAR=24.0
848 2040 my ($card, $key, $props, $propname, $kind, $ctx, $type) = @_;
849 2040 my $labels = _collect_labels($props);
850  
851 2040 my %map;
852 2040 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
3141726
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
853 412 my $val = $item->{value};
854 412 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 412 412
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00412
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
855 412 my $id = _prop_id($item, $ctx);
856 412 my $obj = { '@type' => $type, uri => $val };
857 412 ▶ $obj->{kind} = $kind if defined $kind;
Branch: 1 branch
BranchTrueFalseTotal
if defined $kind 122 290 412
858 412 my $mt = _param($item, 'mediatype');
859 412 ▶ $obj->{mediaType} = $mt if defined $mt;
Branch: 1 branch
BranchTrueFalseTotal
if defined $mt 63 349 412
860 412 my @types = _params_list($item, 'type');
861 412 ▶ if (my $ctx = _make_contexts(@types)) { $obj->{contexts} = $ctx }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 47 365 412
862 412 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 130 282 412
863 412 _apply_label($obj, $item, $labels);
864 412 $map{$id} = $obj;
865 }
866  
867 2040 ▶ if (%map) {
Branch: 1 branch
BranchTrueFalseTotal
if (%map) 314 1726 2040
868 314 ▶ $card->{$key} = { %{$card->{$key} // {}}, %map };
Condition100%2/2
$card->{$key} // {}
l!l
61253
MC/DC100%1/1
$card->{$key} // {}
$card->{$key}
Truth table100%2/2
A
A= $card->{$key}
Aresult
11
00
869 }
870 }
871  
872 # URL, FBURL, CONTACT-URI -> links
873 255 ▶sub _convert_links {
Subroutine _convert_links: called CC=1 SCAR=0.0
874 255 my ($card, $props, $ctx) = @_;
875 255 _convert_resource_to($card, 'links', $props, 'url', undef, $ctx, 'Link');
876 255 _convert_resource_to($card, 'links', $props, 'fburl', 'freeBusy', $ctx, 'Link');
877 255 _convert_resource_to($card, 'links', $props, 'contact-uri', 'contact', $ctx, 'Link');
878 }
879  
880 # CALURI -> calendars
881 255 ▶sub _convert_calendars {
Subroutine _convert_calendars: called CC=1 SCAR=0.0
882 255 my ($card, $props, $ctx) = @_;
883 255 _convert_resource_to($card, 'calendars', $props, 'caluri', undef, $ctx, 'Calendar');
884 }
885  
886 # CALADRURI -> schedulingAddresses
887 255 ▶sub _convert_scheduling_addresses {
Subroutine _convert_scheduling_addresses: called CC=1 SCAR=0.0
888 255 my ($card, $props, $ctx) = @_;
889 255 _convert_resource_to($card, 'schedulingAddresses', $props, 'caladruri', undef, $ctx, 'SchedulingAddress');
890 }
891  
892 # KEY -> cryptoKeys
893 255 ▶sub _convert_crypto_keys {
Subroutine _convert_crypto_keys: called CC=1 SCAR=0.0
894 255 my ($card, $props, $ctx) = @_;
895 255 _convert_resource_to($card, 'cryptoKeys', $props, 'key', undef, $ctx, 'CryptoKey');
896 }
897  
898 # SOURCE, ORG-DIRECTORY -> directories
899 255 ▶sub _convert_directories {
Subroutine _convert_directories: called CC=1 SCAR=0.0
900 255 my ($card, $props, $ctx) = @_;
901 255 _convert_resource_to($card, 'directories', $props, 'source', 'entry', $ctx, 'Directory');
902 255 _convert_resource_to($card, 'directories', $props, 'org-directory', 'directory', $ctx, 'Directory');
903 }
904  
905 # LANG -> preferredLanguages
906 255 ▶sub _convert_preferred_languages {
Subroutine _convert_preferred_languages: called CC=8 SCAR=21.0
907 255 my ($card, $props, $ctx) = @_;
908 255 ▶ my $items = $props->{lang} || return;
Condition100%2/2
$props->{'lang'} || (return)
l!l
45210
MC/DC100%1/1
$props->{'lang'} || (return)
$props->{'lang'}
Truth table100%2/2
A
A= $props->{'lang'}
Aresult
11
00
909  
910 45 my %map;
911 45 for my $item (@$items) {
912 105 my $val = $item->{value};
913 105 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 105 105
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00105
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
914 105 my $id = _prop_id($item, $ctx);
915 105 my $obj = { '@type' => 'LanguagePref', language => $val };
916 105 my @types = _params_list($item, 'type');
917 105 ▶ if (my $ctx = _make_contexts(@types)) { $obj->{contexts} = $ctx }
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(@types)) 62 43 105
918 105 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 90 15 105
919 105 $map{$id} = $obj;
920 }
921  
922 45 ▶ $card->{preferredLanguages} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 45 0 45
923 }
924  
925 # EXPERTISE, HOBBY, INTEREST -> personalInfo
926 255 ▶sub _convert_personal_info {
Subroutine _convert_personal_info: called CC=10 SCAR=23.1
927 255 my ($card, $props, $ctx) = @_;
928  
929 255 my %level_map = (beginner => 'low', average => 'medium', expert => 'high');
930 255 my %map;
931  
932 255 for my $spec (['expertise', 'expertise'], ['hobby', 'hobby'], ['interest', 'interest']) {
933 765 my ($propname, $kind) = @$spec;
934 765 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
47718
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
935 90 my $val = $item->{value};
936 90 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 90 90
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0090
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
937 90 my $id = _prop_id($item, $ctx);
938 90 my $obj = { '@type' => 'PersonalInfo', kind => $kind, value => $val };
939  
940 90 my $level = _param($item, 'level');
941 90 ▶ if ($level) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level) 89 1 90
942 89 ▶ $obj->{level} = $level_map{lc $level} // lc $level;
Condition100%2/2
$level_map{lc $level} // lc $level
l!l
3752
MC/DC100%1/1
$level_map{lc $level} // lc $level
$level_map{lc $level}
Truth table100%2/2
A
A= $level_map{lc $level}
Aresult
11
00
943 }
944  
945 90 my $index = _param($item, 'index');
946 90 ▶ $obj->{listAs} = $index + 0 if defined $index;
Branch: 1 branch
BranchTrueFalseTotal
if defined $index 88 2 90
947  
948 90 $map{$id} = $obj;
949 }
950 }
951  
952 255 ▶ $card->{personalInfo} = \%map if %map;
Branch: 1 branch
BranchTrueFalseTotal
if %map 17 238 255
953 }
954  
955 # CATEGORIES -> keywords
956 255 ▶sub _convert_keywords {
Subroutine _convert_keywords: called CC=7 SCAR=19.7
957 255 my ($card, $props) = @_;
958 255 ▶ my $items = $props->{categories} || return;
Condition100%2/2
$props->{'categories'} || (return)
l!l
94161
MC/DC100%1/1
$props->{'categories'} || (return)
$props->{'categories'}
Truth table100%2/2
A
A= $props->{'categories'}
Aresult
11
00
959  
960 94 my %keywords;
961 94 for my $item (@$items) {
962 94 my $val = $item->{value};
963 94 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 94 94
964 94 for my $cat (split /,/, $val) {
965 240 $cat =~ s/^\s+//;
966 240 $cat =~ s/\s+$//;
967 240 ▶ $keywords{$cat} = JSON::true if $cat ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $cat ne "" 240 0 240
968 }
969 }
970  
971 94 ▶ $card->{keywords} = \%keywords if %keywords;
Branch: 1 branch
BranchTrueFalseTotal
if %keywords 94 0 94
972 }
973  
974 # MEMBER / X-ADDRESSBOOKSERVER-MEMBER -> members
975 255 ▶sub _convert_members {
Subroutine _convert_members: called CC=8 SCAR=21.3
976 255 my ($card, $props, $ctx) = @_;
977  
978 255 my %members;
979 255 for my $propname (qw(member x-addressbookserver-member)) {
980 510 ▶ for my $item (@{$props->{$propname} // []}) {
Condition100%2/2
$props->{$propname} // []
l!l
38472
MC/DC100%1/1
$props->{$propname} // []
$props->{$propname}
Truth table100%2/2
A
A= $props->{$propname}
Aresult
11
00
981 94 my $val = $item->{value};
982 94 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 94 94
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0094
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
983 94 $members{$val} = JSON::true;
984 94 ▶ push @{$ctx->{sources}{members}{$val}}, $item if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 94 0 94
985 }
986 }
987  
988 255 ▶ $card->{members} = \%members if %members;
Branch: 1 branch
BranchTrueFalseTotal
if %members 38 217 255
989 }
990  
991 # RELATED / X-ABRELATEDNAMES -> relatedTo
992 255 ▶sub _convert_related {
Subroutine _convert_related: called CC=19 SCAR=30.3
993 255 my ($card, $props, $ctx) = @_;
994 255 my $labels = _collect_labels($props);
995  
996 255 my %related;
997  
998 # Standard RELATED property
999 255 ▶ for my $item (@{$props->{related} // []}) {
Condition100%2/2
$props->{"related"} // []
l!l
37218
MC/DC100%1/1
$props->{"related"} // []
$props->{"related"}
Truth table100%2/2
A
A= $props->{"related"}
Aresult
11
00
1000 72 my $val = $item->{value};
1001 72 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 72 72
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0072
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
1002  
1003 72 my $rel = { '@type' => 'Relation' };
1004 72 my %relation;
1005 72 for my $t (_params_list($item, 'type')) {
1006 55 my $lt = lc $t;
1007 55 ▶ $relation{$lt} = JSON::true
Branch: 1 branch
BranchTrueFalseTotal
if $lt =~ /^(acquaintance|agent|child|co-resident|co-worker|colleague|contact|crush|date|emergency|friend|kin|me|met|muse|neighbor|parent|sibling|spouse|sweetheart)$/ 55 0 55
1008 if $lt =~ /^(acquaintance|agent|child|co-resident|co-worker|colleague|contact|crush|date|emergency|friend|kin|me|met|muse|neighbor|parent|sibling|spouse|sweetheart)$/;
1009 }
1010 72 ▶ $rel->{relation} = \%relation if %relation;
Branch: 1 branch
BranchTrueFalseTotal
if %relation 54 18 72
1011 72 $related{$val} = $rel;
1012 72 ▶ push @{$ctx->{sources}{relatedTo}{$val}}, $item if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 72 0 72
1013 }
1014  
1015 # Apple X-ABRELATEDNAMES + X-ABLabel -> relatedTo
1016 255 my %ablabel_relation = (
1017 'mother' => 'parent', 'father' => 'parent', 'parent' => 'parent',
1018 'brother' => 'sibling', 'sister' => 'sibling',
1019 'child' => 'child',
1020 'friend' => 'friend',
1021 'spouse' => 'spouse', 'partner' => 'spouse',
1022 'assistant' => 'colleague', 'manager' => 'colleague',
1023 );
1024 255 ▶ for my $item (@{$props->{'x-abrelatednames'} // []}) {
Condition100%2/2
$props->{"x-abrelatednames"} // []
l!l
65190
MC/DC100%1/1
$props->{"x-abrelatednames"} // []
$props->{"x-abrelatednames"}
Truth table100%2/2
A
A= $props->{"x-abrelatednames"}
Aresult
11
00
1025 141 my $val = $item->{value};
1026 141 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 141 141
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
00141
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
1027  
1028 141 my $rel = { '@type' => 'Relation' };
1029 141 my %relation;
1030  
1031 # Get relation type from X-ABLabel
1032 my $label;
1033 141 ▶ if (my $group = $item->{group}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $group = $item->{"group"}) 141 0 141
1034 141 $label = $labels->{$group};
1035 }
1036 141 ▶ if ($label) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label) 141 0 141
1037 141 my $lt = lc $label;
1038 141 ▶ if (my $mapped = $ablabel_relation{$lt}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mapped = $ablabel_relation{$lt}) 104 37 141
1039 104 $relation{$mapped} = JSON::true;
1040 }
1041 }
1042 141 ▶ $rel->{relation} = \%relation if %relation;
Branch: 1 branch
BranchTrueFalseTotal
if %relation 104 37 141
1043 141 $related{$val} = $rel;
1044 141 ▶ push @{$ctx->{sources}{relatedTo}{$val}}, $item if $ctx;
Branch: 1 branch
BranchTrueFalseTotal
if $ctx 141 0 141
1045 }
1046  
1047 255 ▶ $card->{relatedTo} = \%related if %related;
Branch: 1 branch
BranchTrueFalseTotal
if %related 102 153 255
1048 }
1049  
1050 # GRAMGENDER, PRONOUNS (RFC 9554) -> speakToAs
1051 255 ▶sub _convert_speak_to_as {
Subroutine _convert_speak_to_as: called CC=10 SCAR=23.2
1052 255 my ($card, $props, $ctx) = @_;
1053  
1054 255 my $speak = {};
1055  
1056 255 ▶ if (my $gg = _first_value($props, 'gramgender')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $gg = _first_value($props, "gramgender")) 32 223 255
1057 32 $speak->{grammaticalGender} = lc $gg;
1058 }
1059  
1060 255 ▶ if (my $items = $props->{pronouns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $items = $props->{"pronouns"}) 33 222 255
1061 33 my %pronouns;
1062 33 for my $item (@$items) {
1063 49 my $val = $item->{value};
1064 49 ▶ next unless defined $val && $val ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val and $val ne "" 0 49 49
Condition33%1/3
defined $val and $val ne ""
!ll&&!rl&&r
0049
MC/DC0%0/2
defined $val and $val ne ""
defined $val $val ne ""
Truth table33%1/3
A and B
A= defined $valB= $val ne ""
ABresult
0X0
100
111
1065 49 my $id = _prop_id($item, $ctx);
1066 49 my $obj = { '@type' => 'Pronouns', pronouns => $val };
1067 49 ▶ if (my $ctx = _make_contexts(_params_list($item, 'type'))) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ctx = _make_contexts(_params_list($item, "type"))) 1 48 49
1068 1 $obj->{contexts} = $ctx;
1069 }
1070 49 ▶ if (my $pref = _make_pref($item)) { $obj->{pref} = $pref }
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = _make_pref($item)) 48 1 49
1071 49 $pronouns{$id} = $obj;
1072 }
1073 33 ▶ $speak->{pronouns} = \%pronouns if %pronouns;
Branch: 1 branch
BranchTrueFalseTotal
if %pronouns 33 0 33
1074 }
1075  
1076 255 ▶ $card->{speakToAs} = $speak if %$speak;
Branch: 1 branch
BranchTrueFalseTotal
if %$speak 33 222 255
1077 }
1078  
1079 ###############################################################################
1080 # JSCONTACT -> VCARD
1081 ###############################################################################
1082  
1083 # jscontact_to_vcard($card) writes the Card as a new vCard 4.0.
1084 #
1085 # jscontact_to_vcard($card, original => $vcard) writes it as a patch of
1086 # $vcard, the vCard the Card is an edit of (as fetched from the server,
1087 # bytes or characters): the Card is compared with what $vcard itself
1088 # reads as, and only the entries that differ are rewritten, so whatever
1089 # else $vcard holds is kept (see patch_vcard). The caller need not keep
1090 # the Card it first read, and may have changed anything in it. If
1091 # $vcard cannot be parsed, the Card is written as a new vCard instead.
1092 #
1093 # Either way the result is a character string.
1094 386 ▶sub jscontact_to_vcard {
Subroutine jscontact_to_vcard: called CC=10 SCAR=23.0
Pod undocumented
1095 386 my ($card, %opts) = @_;
1096  
1097 386 ▶ if (defined $opts{original}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"original"}) 28 358 386
1098 28 my $patched = _patch_vcard($opts{original}, $card);
1099 28 ▶ return $patched if defined $patched;
Branch: 1 branch
BranchTrueFalseTotal
if defined $patched 25 3 28
1100 }
1101  
1102 361 my $ctx = _new_context();
1103 361 my @props;
1104  
1105 # VERSION (always 4.0)
1106 361 push @props, { name => 'version', value => '4.0' };
1107  
1108 # UID
1109 361 ▶ push @props, { name => 'uid', value => $card->{uid} } if $card->{uid};
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"uid"} 295 66 361
1110  
1111 # KIND
1112 push @props, { name => 'kind', value => $card->{kind} }
1113 361 ▶ if $card->{kind} && $card->{kind} ne 'individual';
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"kind"} and $card->{"kind"} ne "individual" 65 296 361
Condition100%3/3
$card->{"kind"} and $card->{"kind"} ne "individual"
!ll&&!rl&&r
2296765
MC/DC100%2/2
$card->{"kind"} and $card->{"kind"} ne "individual"
$card->{"kind"} $card->{"kind"} ne "individual"
Truth table100%3/3
A and B
A= $card->{"kind"}B= $card->{"kind"} ne "individual"
ABresult
0X0
100
111
1114  
1115 # PRODID
1116 361 ▶ push @props, { name => 'prodid', value => $card->{prodId} } if $card->{prodId};
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"prodId"} 165 196 361
1117  
1118 # REV
1119 361 ▶ push @props, { name => 'rev', value => $card->{updated} } if $card->{updated};
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"updated"} 132 229 361
1120  
1121 # CREATED (RFC 9554)
1122 361 ▶ push @props, { name => 'created', value => $card->{created} } if $card->{created};
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"created"} 67 294 361
1123  
1124 # LANGUAGE (RFC 9554)
1125 361 ▶ push @props, { name => 'language', value => $card->{language} } if $card->{language};
Branch: 1 branch
BranchTrueFalseTotal
if $card->{"language"} 99 262 361
1126  
1127 # Name -> N + FN
1128 361 _unconvert_name(\@props, $card, $ctx);
1129  
1130 # All map-based properties
1131 361 _unconvert_nicknames(\@props, $card, $ctx);
1132 361 _unconvert_emails(\@props, $card, $ctx);
1133 361 _unconvert_phones(\@props, $card, $ctx);
1134 361 _unconvert_addresses(\@props, $card, $ctx);
1135 361 _unconvert_organizations(\@props, $card, $ctx);
1136 361 _unconvert_titles(\@props, $card, $ctx);
1137 361 _unconvert_anniversaries(\@props, $card, $ctx);
1138 361 _unconvert_notes(\@props, $card, $ctx);
1139 361 _unconvert_online_services(\@props, $card, $ctx);
1140 361 _unconvert_media(\@props, $card, $ctx);
1141 361 _unconvert_links(\@props, $card, $ctx);
1142 361 _unconvert_calendars(\@props, $card, $ctx);
1143 361 _unconvert_scheduling_addresses(\@props, $card, $ctx);
1144 361 _unconvert_crypto_keys(\@props, $card, $ctx);
1145 361 _unconvert_directories(\@props, $card, $ctx);
1146 361 _unconvert_preferred_languages(\@props, $card, $ctx);
1147 361 _unconvert_personal_info(\@props, $card, $ctx);
1148 361 _unconvert_keywords(\@props, $card, $ctx);
1149 361 _unconvert_members(\@props, $card, $ctx);
1150 361 _unconvert_related(\@props, $card, $ctx);
1151 361 _unconvert_speak_to_as(\@props, $card, $ctx);
1152  
1153 361 return _write_vcard(\@props);
1154 }
1155  
1156 ###############################################################################
1157 # INDIVIDUAL JSCONTACT -> VCARD CONVERTERS
1158 ###############################################################################
1159  
1160 1144 ▶sub _contexts_to_types {
Subroutine _contexts_to_types: called CC=5 SCAR=16.5
1161 1144 my $contexts = shift;
1162 1144 ▶ return () unless $contexts && ref $contexts eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless $contexts and ref $contexts eq "HASH" 0 1144 1144
Condition33%1/3
$contexts and ref $contexts eq "HASH"
!ll&&!rl&&r
001144
MC/DC0%0/2
$contexts and ref $contexts eq "HASH"
$contexts ref $contexts eq "HASH"
Truth table33%1/3
A and B
A= $contextsB= ref $contexts eq "HASH"
ABresult
0X0
100
111
1163 1144 my @types;
1164 1144 ▶ push @types, 'home' if $contexts->{private};
Branch: 1 branch
BranchTrueFalseTotal
if $contexts->{"private"} 404 740 1144
1165 1144 ▶ push @types, 'work' if $contexts->{work};
Branch: 1 branch
BranchTrueFalseTotal
if $contexts->{"work"} 739 405 1144
1166 1144 return @types;
1167 }
1168  
1169 2878 ▶sub _add_pref_params {
Subroutine _add_pref_params: called CC=2 SCAR=6.9
1170 2878 my ($params, $pref) = @_;
1171 2878 ▶ $params->{pref} = [$pref] if defined $pref;
Branch: 1 branch
BranchTrueFalseTotal
if defined $pref 977 1901 2878
1172 }
1173  
1174 881 ▶sub _add_label {
Subroutine _add_label: called CC=4 SCAR=13.9
1175 881 my ($out, $prop, $obj, $ctx) = @_;
1176 881 ▶ return if $ctx->{no_labels};
Branch: 1 branch
BranchTrueFalseTotal
if $ctx->{"no_labels"} 14 867 881
1177 867 my $label = $obj->{label};
1178 867 ▶ return unless defined $label && $label ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $label and $label ne "" 701 166 867
Condition66%2/3
defined $label and $label ne ""
!ll&&!rl&&r
7010166
MC/DC50%1/2
defined $label and $label ne ""
defined $label $label ne ""
Truth table66%2/3
A and B
A= defined $labelB= $label ne ""
ABresult
0X0
100
111
1179  
1180 166 my $group = _alloc_group($ctx);
1181 166 $prop->{group} = $group;
1182 166 push @$out, {
1183 name => 'x-ablabel',
1184 value => $label,
1185 params => {},
1186 group => $group,
1187 };
1188 }
1189  
1190 2868 ▶sub _add_context_params {
Subroutine _add_context_params: called CC=3 SCAR=11.0
1191 2868 my ($params, $obj) = @_;
1192 2868 ▶ if ($obj->{contexts}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($obj->{"contexts"}) 1144 1724 2868
1193 1144 my @types = _contexts_to_types($obj->{contexts});
1194 1144 ▶ push @{$params->{type}}, @types if @types;
Branch: 1 branch
BranchTrueFalseTotal
if @types 1143 1 1144
1195 }
1196 }
1197  
1198 369 ▶sub _unconvert_name {
Subroutine _unconvert_name: called CC=23 SCAR=36.0
1199 369 my ($out, $card, $ctx) = @_;
1200 369 ▶ my $name = $card->{name} || return;
Condition100%2/2
$card->{'name'} || (return)
l!l
3618
MC/DC100%1/1
$card->{'name'} || (return)
$card->{'name'}
Truth table100%2/2
A
A= $card->{'name'}
Aresult
11
00
1201  
1202 # FN
1203 361 ▶ if (my $full = $name->{full}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $full = $name->{"full"}) 361 0 361
1204 361 push @$out, { name => 'fn', value => $full, params => {} };
1205 }
1206  
1207 # N from components
1208 361 ▶ if (my $components = $name->{components}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $components = $name->{"components"}) 270 91 361
1209 270 my %by_kind;
1210 270 for my $c (@$components) {
1211 1081 push @{$by_kind{$c->{kind}}}, $c->{value};
1212 }
1213  
1214 # N order: surname, given, given2(additional), title(prefix), credential(suffix), surname2, generation
1215 270 my @n_values;
1216 270 for my $kind (qw(surname given given2 title credential surname2 generation)) {
1217 1890 my $vals = $by_kind{$kind};
1218 # several values of one kind are a list (RFC 6350 S6.2.2)
1219 1890 ▶ push @n_values, $vals ? [ grep { defined $_ } @$vals ] : '';
Branch: 1 branch
BranchTrueFalseTotal
$vals ? : 919 971 1890
1220 }
1221  
1222 # Trim trailing empty components
1223 270 ▶ pop @n_values while @n_values && !ref $n_values[-1] && $n_values[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
@n_values and not ref $n_values[-1] and $n_values[-1] eq "" and do { pop @n_values } 874 270 1144
Condition66%4/6
@n_values and not ref $n_values[-1] and $n_values[-1] eq ""
!ll&&!rl&&r
2700874
@n_values and not ref $n_values[-1]
!ll&&!rl&&r
0270874
MC/DC33%1/3
@n_values and not ref $n_values[-1] and $n_values[-1] eq ""
@n_values not ref $n_values[-1] $n_values[-1] eq ""
Truth table50%2/4
A and not(B) and C
A= @n_valuesB= not ref $n_values[-1]C= $n_values[-1] eq ""
ABCresult
0XX0
10X0
1100
1111
1224  
1225 270 my $params = {};
1226 270 ▶ if ($name->{sortAs} && ref $name->{sortAs} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name->{"sortAs"} and ref $name->{"sortAs"} eq "HASH") 67 203 270
Condition66%2/3
$name->{"sortAs"} and ref $name->{"sortAs"} eq "HASH"
!ll&&!rl&&r
203067
MC/DC50%1/2
$name->{"sortAs"} and ref $name->{"sortAs"} eq "HASH"
$name->{"sortAs"} ref $name->{"sortAs"} eq "HASH"
Truth table66%2/3
A and B
A= $name->{"sortAs"}B= ref $name->{"sortAs"} eq "HASH"
ABresult
0X0
100
111
1227 67 my @sa;
1228 67 for my $kind (qw(surname given given2 title credential surname2 generation)) {
1229 469 ▶ push @sa, $name->{sortAs}{$kind} // '';
Condition100%2/2
$name->{"sortAs"}{$kind} // ""
l!l
134335
MC/DC100%1/1
$name->{"sortAs"}{$kind} // ""
$name->{"sortAs"}{$kind}
Truth table100%2/2
A
A= $name->{"sortAs"}{$kind}
Aresult
11
00
1230 }
1231 67 ▶ pop @sa while @sa && $sa[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
@sa and $sa[-1] eq "" and do { pop @sa } 335 67 402
Condition66%2/3
@sa and $sa[-1] eq ""
!ll&&!rl&&r
067335
MC/DC50%1/2
@sa and $sa[-1] eq ""
@sa $sa[-1] eq ""
Truth table66%2/3
A and B
A= @saB= $sa[-1] eq ""
ABresult
0X0
100
111
1232 67 ▶ $params->{'sort-as'} = [join(',', @sa)] if @sa;
Branch: 1 branch
BranchTrueFalseTotal
if @sa 67 0 67
1233 }
1234  
1235 270 push @$out, { name => 'n', values => \@n_values, params => $params };
1236 }
1237  
1238 # Generate FN from components if no full name given
1239 361 ▶ if (!$name->{full} && $name->{components}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $name->{"full"} and $name->{"components"}) 0 361 361
Condition33%1/3
not $name->{"full"} and $name->{"components"}
!ll&&!rl&&r
36100
MC/DC0%0/2
not $name->{"full"} and $name->{"components"}
not $name->{"full"} $name->{"components"}
Truth table33%1/3
not(A) and B
A= not $name->{"full"}B= $name->{"components"}
ABresult
0X0
100
111
1240 0 my @parts;
1241 0 for my $c (@{$name->{components}}) {
1242 0 ▶ next if ($c->{kind} // '') eq 'separator';
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"kind"} // "") eq "separator" 0 0 0
Condition0%0/2
$c->{"kind"} // ""
l!l
00
MC/DC0%0/1
$c->{"kind"} // ""
$c->{"kind"}
Truth table0%0/2
A
A= $c->{"kind"}
Aresult
11
00
1243 0 push @parts, $c->{value};
1244 }
1245 0 ▶ push @$out, { name => 'fn', value => join(' ', @parts), params => {} } if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 0 0 0
1246 }
1247 }
1248  
1249 361 ▶sub _unconvert_nicknames {
Subroutine _unconvert_nicknames: called CC=3 SCAR=11.0
1250 361 my ($out, $card, $ctx) = @_;
1251 361 ▶ my $nicknames = $card->{nicknames} || return;
Condition100%2/2
$card->{'nicknames'} || (return)
l!l
134227
MC/DC100%1/1
$card->{'nicknames'} || (return)
$card->{'nicknames'}
Truth table100%2/2
A
A= $card->{'nicknames'}
Aresult
11
00
1252  
1253 134 for my $id (sort keys %$nicknames) {
1254 136 my $nn = $nicknames->{$id};
1255 136 my $params = { 'prop-id' => [$id] };
1256 136 _add_context_params($params, $nn);
1257 136 _add_pref_params($params, $nn->{pref});
1258 136 push @$out, { name => 'nickname', value => $nn->{name}, params => $params };
1259 }
1260 }
1261  
1262 373 ▶sub _unconvert_emails {
Subroutine _unconvert_emails: called CC=3 SCAR=11.0
1263 373 my ($out, $card, $ctx) = @_;
1264 373 ▶ my $emails = $card->{emails} || return;
Condition100%2/2
$card->{'emails'} || (return)
l!l
245128
MC/DC100%1/1
$card->{'emails'} || (return)
$card->{'emails'}
Truth table100%2/2
A
A= $card->{'emails'}
Aresult
11
00
1265  
1266 245 for my $id (sort keys %$emails) {
1267 413 my $em = $emails->{$id};
1268 413 my $params = { 'prop-id' => [$id] };
1269 413 _add_context_params($params, $em);
1270 413 _add_pref_params($params, $em->{pref});
1271 413 my $prop = { name => 'email', value => $em->{address}, params => $params };
1272 413 _add_label($out, $prop, $em, $ctx);
1273 413 push @$out, $prop;
1274 }
1275 }
1276  
1277 363 ▶sub _unconvert_phones {
Subroutine _unconvert_phones: called CC=7 SCAR=19.5
1278 363 my ($out, $card, $ctx) = @_;
1279 363 ▶ my $phones = $card->{phones} || return;
Condition100%2/2
$card->{'phones'} || (return)
l!l
203160
MC/DC100%1/1
$card->{'phones'} || (return)
$card->{'phones'}
Truth table100%2/2
A
A= $card->{'phones'}
Aresult
11
00
1280  
1281 203 my %feature_to_type = (
1282 mobile => 'cell', voice => 'voice', fax => 'fax',
1283 video => 'video', pager => 'pager', text => 'text',
1284 textphone => 'textphone',
1285 );
1286  
1287 203 for my $id (sort keys %$phones) {
1288 468 my $ph = $phones->{$id};
1289 468 my $params = { 'prop-id' => [$id] };
1290 468 _add_context_params($params, $ph);
1291 468 _add_pref_params($params, $ph->{pref});
1292  
1293 468 ▶ if ($ph->{features}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ph->{"features"}) 303 165 468
1294 303 for my $feat (sort keys %{$ph->{features}}) {
1295 470 my $type = $feature_to_type{$feat};
1296 470 ▶ push @{$params->{type}}, $type if $type;
Branch: 1 branch
BranchTrueFalseTotal
if $type 469 1 470
1297 }
1298 }
1299  
1300 # TEL is TEXT unless it says it is a URI (RFC 6350 S6.4.1); a URI
1301 # escaped as TEXT is no longer the URI
1302 468 ▶ $params->{value} = ['uri'] if _looks_like_uri($ph->{number});
Branch: 1 branch
BranchTrueFalseTotal
if _looks_like_uri($ph->{"number"}) 198 270 468
1303  
1304 468 my $prop = { name => 'tel', value => $ph->{number}, params => $params };
1305 468 _add_label($out, $prop, $ph, $ctx);
1306 468 push @$out, $prop;
1307 }
1308 }
1309  
1310 362 ▶sub _unconvert_addresses {
Subroutine _unconvert_addresses: called CC=13 SCAR=25.7
1311 362 my ($out, $card, $ctx) = @_;
1312 362 ▶ my $addresses = $card->{addresses} || return;
Condition100%2/2
$card->{'addresses'} || (return)
l!l
202160
MC/DC100%1/1
$card->{'addresses'} || (return)
$card->{'addresses'}
Truth table100%2/2
A
A= $card->{'addresses'}
Aresult
11
00
1313  
1314 202 my %kind_to_idx = (
1315 name => 2, apartment => 1,
1316 locality => 3, region => 4, postcode => 5, country => 6,
1317 );
1318  
1319 202 for my $id (sort keys %$addresses) {
1320 236 my $adr = $addresses->{$id};
1321 236 my $params = { 'prop-id' => [$id] };
1322 236 _add_context_params($params, $adr);
1323 236 _add_pref_params($params, $adr->{pref});
1324  
1325 236 ▶ $params->{label} = [$adr->{full}] if $adr->{full};
Branch: 1 branch
BranchTrueFalseTotal
if $adr->{"full"} 36 200 236
1326 236 ▶ $params->{geo} = [$adr->{coordinates}] if $adr->{coordinates};
Branch: 1 branch
BranchTrueFalseTotal
if $adr->{"coordinates"} 35 201 236
1327 236 ▶ $params->{tz} = [$adr->{timeZone}] if $adr->{timeZone};
Branch: 1 branch
BranchTrueFalseTotal
if $adr->{"timeZone"} 35 201 236
1328 236 ▶ $params->{cc} = [$adr->{countryCode}] if $adr->{countryCode};
Branch: 1 branch
BranchTrueFalseTotal
if $adr->{"countryCode"} 165 71 236
1329  
1330 # Build ADR values (7 standard components)
1331 236 my @values = ('') x 7;
1332  
1333 236 ▶ if (my $components = $adr->{components}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $components = $adr->{"components"}) 236 0 236
1334 236 my %by_idx;
1335 236 for my $c (@$components) {
1336 1232 ▶ next if ($c->{kind} // '') eq 'separator';
Branch: 1 branch
BranchTrueFalseTotal
if ($c->{"kind"} // "") eq "separator" 0 1232 1232
Condition50%1/2
$c->{"kind"} // ""
l!l
12320
MC/DC0%0/1
$c->{"kind"} // ""
$c->{"kind"}
Truth table50%1/2
A
A= $c->{"kind"}
Aresult
11
00
1337 1232 my $idx = $kind_to_idx{$c->{kind}};
1338 1232 ▶ push @{$by_idx{$idx}}, $c->{value} if defined $idx;
Branch: 1 branch
BranchTrueFalseTotal
if defined $idx 1231 1 1232
1339 }
1340 # several values of one component are a list (RFC 6350 S6.3.1)
1341 236 for my $idx (keys %by_idx) {
1342 1230 $values[$idx] = $by_idx{$idx};
1343 }
1344 }
1345  
1346 236 push @$out, { name => 'adr', values => \@values, params => $params };
1347 }
1348 }
1349  
1350 361 ▶sub _unconvert_organizations {
Subroutine _unconvert_organizations: called CC=13 SCAR=25.8
1351 361 my ($out, $card, $ctx) = @_;
1352 361 ▶ my $orgs = $card->{organizations} || return;
Condition100%2/2
$card->{'organizations'} || (return)
l!l
198163
MC/DC100%1/1
$card->{'organizations'} || (return)
$card->{'organizations'}
Truth table100%2/2
A
A= $card->{'organizations'}
Aresult
11
00
1353  
1354 198 for my $id (sort keys %$orgs) {
1355 198 my $org = $orgs->{$id};
1356 198 my $params = { 'prop-id' => [$id] };
1357 198 _add_context_params($params, $org);
1358  
1359 198 ▶ my @values = ($org->{name} // '');
Condition50%1/2
$org->{"name"} // ""
l!l
1980
MC/DC0%0/1
$org->{"name"} // ""
$org->{"name"}
Truth table50%1/2
A
A= $org->{"name"}
Aresult
11
00
1360 198 ▶ if ($org->{units}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($org->{"units"}) 134 64 198
1361 134 for my $unit (@{$org->{units}}) {
1362 233 push @values, $unit->{name};
1363 }
1364 }
1365  
1366 198 ▶ if (defined $org->{sortAs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $org->{"sortAs"}) 67 131 198
1367 67 my @sa = ($org->{sortAs});
1368 67 ▶ if ($org->{units}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($org->{"units"}) 67 0 67
1369 67 for my $unit (@{$org->{units}}) {
1370 134 ▶ push @sa, $unit->{sortAs} // '';
Condition100%2/2
$unit->{"sortAs"} // ""
l!l
2132
MC/DC100%1/1
$unit->{"sortAs"} // ""
$unit->{"sortAs"}
Truth table100%2/2
A
A= $unit->{"sortAs"}
Aresult
11
00
1371 }
1372 }
1373 67 ▶ pop @sa while @sa && $sa[-1] eq '';
Branch: 1 branch
BranchTrueFalseTotal
@sa and $sa[-1] eq "" and do { pop @sa } 132 67 199
Condition66%2/3
@sa and $sa[-1] eq ""
!ll&&!rl&&r
067132
MC/DC50%1/2
@sa and $sa[-1] eq ""
@sa $sa[-1] eq ""
Truth table66%2/3
A and B
A= @saB= $sa[-1] eq ""
ABresult
0X0
100
111
1374 67 ▶ $params->{'sort-as'} = [join(',', @sa)] if @sa;
Branch: 1 branch
BranchTrueFalseTotal
if @sa 67 0 67
1375 }
1376  
1377 198 push @$out, { name => 'org', values => \@values, params => $params };
1378 }
1379 }
1380  
1381 361 ▶sub _unconvert_titles {
Subroutine _unconvert_titles: called CC=5 SCAR=16.1
1382 361 my ($out, $card, $ctx) = @_;
1383 361 ▶ my $titles = $card->{titles} || return;
Condition100%2/2
$card->{'titles'} || (return)
l!l
167194
MC/DC100%1/1
$card->{'titles'} || (return)
$card->{'titles'}
Truth table100%2/2
A
A= $card->{'titles'}
Aresult
11
00
1384  
1385 167 for my $id (sort keys %$titles) {
1386 236 my $t = $titles->{$id};
1387 236 ▶ my $propname = ($t->{kind} // 'title') eq 'role' ? 'role' : 'title';
Branch: 1 branch
BranchTrueFalseTotal
($t->{'kind'} // 'title') eq 'role' ? : 36 200 236
Condition50%1/2
$t->{"kind"} // "title"
l!l
2360
MC/DC0%0/1
$t->{"kind"} // "title"
$t->{"kind"}
Truth table50%1/2
A
A= $t->{"kind"}
Aresult
11
00
1388 236 my $params = { 'prop-id' => [$id] };
1389 236 push @$out, { name => $propname, value => $t->{name}, params => $params };
1390 }
1391 }
1392  
1393 362 ▶sub _unconvert_anniversaries {
Subroutine _unconvert_anniversaries: called CC=7 SCAR=19.5
1394 362 my ($out, $card, $ctx) = @_;
1395 362 ▶ my $anns = $card->{anniversaries} || return;
Condition100%2/2
$card->{'anniversaries'} || (return)
l!l
168194
MC/DC100%1/1
$card->{'anniversaries'} || (return)
$card->{'anniversaries'}
Truth table100%2/2
A
A= $card->{'anniversaries'}
Aresult
11
00
1396  
1397 168 my %kind_to_prop = (birth => 'bday', wedding => 'anniversary', death => 'deathdate');
1398  
1399 168 for my $id (sort keys %$anns) {
1400 433 my $ann = $anns->{$id};
1401 433 ▶ my $kind = $ann->{kind} // '';
Condition50%1/2
$ann->{"kind"} // ""
l!l
4330
MC/DC0%0/1
$ann->{"kind"} // ""
$ann->{"kind"}
Truth table50%1/2
A
A= $ann->{"kind"}
Aresult
11
00
1402 433 ▶ my $propname = $kind_to_prop{$kind} // 'anniversary';
Condition100%2/2
$kind_to_prop{$kind} // "anniversary"
l!l
36766
MC/DC100%1/1
$kind_to_prop{$kind} // "anniversary"
$kind_to_prop{$kind}
Truth table100%2/2
A
A= $kind_to_prop{$kind}
Aresult
11
00
1403 433 my $params = { 'prop-id' => [$id] };
1404  
1405 433 ▶ my $date = $ann->{date} // '';
Condition50%1/2
$ann->{"date"} // ""
l!l
4330
MC/DC0%0/1
$ann->{"date"} // ""
$ann->{"date"}
Truth table50%1/2
A
A= $ann->{"date"}
Aresult
11
00
1406 # Convert unknown-year dates to vCard 4 format
1407 433 ▶ $date = "--$1$2" if $date =~ /^0000-(\d{2})-(\d{2})/;
Branch: 1 branch
BranchTrueFalseTotal
if $date =~ /^0000-(\d{2})-(\d{2})/ 99 334 433
1408  
1409 433 push @$out, { name => $propname, value => $date, params => $params };
1410 }
1411 }
1412  
1413 361 ▶sub _unconvert_notes {
Subroutine _unconvert_notes: called CC=7 SCAR=19.5
1414 361 my ($out, $card, $ctx) = @_;
1415 361 ▶ my $notes = $card->{notes} || return;
Condition100%2/2
$card->{'notes'} || (return)
l!l
201160
MC/DC100%1/1
$card->{'notes'} || (return)
$card->{'notes'}
Truth table100%2/2
A
A= $card->{'notes'}
Aresult
11
00
1416  
1417 201 for my $id (sort keys %$notes) {
1418 236 my $note = $notes->{$id};
1419 236 my $params = { 'prop-id' => [$id] };
1420  
1421 236 ▶ if ($note->{created}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($note->{"created"}) 67 169 236
1422 67 $params->{created} = [$note->{created}];
1423 }
1424 236 ▶ if ($note->{author}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($note->{"author"}) 68 168 236
1425 68 ▶ $params->{'author-name'} = [$note->{author}{name}] if $note->{author}{name};
Branch: 1 branch
BranchTrueFalseTotal
if $note->{"author"}{"name"} 68 0 68
1426 68 ▶ $params->{author} = [$note->{author}{uri}] if $note->{author}{uri};
Branch: 1 branch
BranchTrueFalseTotal
if $note->{"author"}{"uri"} 2 66 68
1427 }
1428  
1429 236 push @$out, { name => 'note', value => $note->{note}, params => $params };
1430 }
1431 }
1432  
1433 361 ▶sub _unconvert_online_services {
Subroutine _unconvert_online_services: called CC=7 SCAR=19.5
1434 361 my ($out, $card, $ctx) = @_;
1435 361 ▶ my $services = $card->{onlineServices} || return;
Condition100%2/2
$card->{'onlineServices'} || (return)
l!l
139222
MC/DC100%1/1
$card->{'onlineServices'} || (return)
$card->{'onlineServices'}
Truth table100%2/2
A
A= $card->{'onlineServices'}
Aresult
11
00
1436  
1437 139 for my $id (sort keys %$services) {
1438 308 my $svc = $services->{$id};
1439 308 my $params = { 'prop-id' => [$id] };
1440 308 _add_context_params($params, $svc);
1441 308 _add_pref_params($params, $svc->{pref});
1442  
1443 308 ▶ $params->{'service-type'} = [$svc->{service}] if $svc->{service};
Branch: 1 branch
BranchTrueFalseTotal
if $svc->{"service"} 242 66 308
1444 308 ▶ $params->{username} = [$svc->{user}] if $svc->{user};
Branch: 1 branch
BranchTrueFalseTotal
if $svc->{"user"} 141 167 308
1445  
1446 308 ▶ my $uri = $svc->{uri} // $svc->{user} // '';
Condition60%3/5
($svc->{"uri"} // $svc->{"user"}) // ""
l!l
3080
$svc->{"uri"} // $svc->{"user"}
l!l&&r!l&&!r
269390
MC/DC0%0/2
($svc->{"uri"} // $svc->{"user"}) // ""
$svc->{"uri"} $svc->{"user"}
Truth table66%2/3
A // B
A= $svc->{"uri"}B= $svc->{"user"}
ABresult
1X1
011
000
1447 308 push @$out, { name => 'impp', value => $uri, params => $params };
1448 }
1449 }
1450  
1451 363 ▶sub _unconvert_media {
Subroutine _unconvert_media: called CC=8 SCAR=20.9
1452 363 my ($out, $card, $ctx) = @_;
1453 363 ▶ my $media = $card->{media} || return;
Condition100%2/2
$card->{'media'} || (return)
l!l
139224
MC/DC100%1/1
$card->{'media'} || (return)
$card->{'media'}
Truth table100%2/2
A
A= $card->{'media'}
Aresult
11
00
1454  
1455 139 my %kind_to_prop = (photo => 'photo', logo => 'logo', sound => 'sound');
1456  
1457 139 for my $id (sort keys %$media) {
1458 209 my $m = $media->{$id};
1459 # A Media given only by blobId: the caller resolves the blob to a
1460 # data: URI first; this module fetches nothing, and an empty PHOTO
1461 # is no picture at all.
1462 209 ▶ next unless defined $m->{uri} && $m->{uri} ne '';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $m->{"uri"} and $m->{"uri"} ne "" 1 208 209
Condition66%2/3
defined $m->{"uri"} and $m->{"uri"} ne ""
!ll&&!rl&&r
10208
MC/DC50%1/2
defined $m->{"uri"} and $m->{"uri"} ne ""
defined $m->{"uri"} $m->{"uri"} ne ""
Truth table66%2/3
A and B
A= defined $m->{"uri"}B= $m->{"uri"} ne ""
ABresult
0X0
100
111
1463 208 ▶ my $propname = $kind_to_prop{$m->{kind} // 'photo'} // 'photo';
Condition50%2/4
$kind_to_prop{$m->{"kind"} // "photo"} // "photo"
l!l
2080
$m->{"kind"} // "photo"
l!l
2080
MC/DC0%0/1
$kind_to_prop{$m->{"kind"} // "photo"} // "photo"
$kind_to_prop{$m->{"kind"} // "photo"}
MC/DC0%0/1
$m->{"kind"} // "photo"
$m->{"kind"}
Truth table50%1/2
A
A= $kind_to_prop{$m->{"kind"} // "photo"}
Aresult
11
00
Truth table50%1/2
A
A= $m->{"kind"}
Aresult
11
00
1464 208 my $params = { 'prop-id' => [$id] };
1465 208 _add_pref_params($params, $m->{pref});
1466 208 ▶ $params->{mediatype} = [$m->{mediaType}] if $m->{mediaType};
Branch: 1 branch
BranchTrueFalseTotal
if $m->{"mediaType"} 75 133 208
1467 208 push @$out, { name => $propname, value => $m->{uri}, params => $params };
1468 }
1469 }
1470  
1471 362 ▶sub _unconvert_links {
Subroutine _unconvert_links: called CC=7 SCAR=19.5
1472 362 my ($out, $card, $ctx) = @_;
1473 362 ▶ my $links = $card->{links} || return;
Condition100%2/2
$card->{'links'} || (return)
l!l
169193
MC/DC100%1/1
$card->{'links'} || (return)
$card->{'links'}
Truth table100%2/2
A
A= $card->{'links'}
Aresult
11
00
1474  
1475 169 for my $id (sort keys %$links) {
1476 334 my $link = $links->{$id};
1477 334 ▶ my $kind = $link->{kind} // '';
Condition100%2/2
$link->{"kind"} // ""
l!l
133201
MC/DC100%1/1
$link->{"kind"} // ""
$link->{"kind"}
Truth table100%2/2
A
A= $link->{"kind"}
Aresult
11
00
1478 334 ▶ my $propname = $kind eq 'freeBusy' ? 'fburl'
Branch: 2 branches
BranchTrueFalseTotal
$kind eq 'freeBusy' ? : 99 235 334
$kind eq 'contact' ? : 34 201 235
1479 : $kind eq 'contact' ? 'contact-uri'
1480 : 'url';
1481 334 my $params = { 'prop-id' => [$id] };
1482 334 _add_context_params($params, $link);
1483 334 _add_pref_params($params, $link->{pref});
1484 334 ▶ $params->{mediatype} = [$link->{mediaType}] if $link->{mediaType};
Branch: 1 branch
BranchTrueFalseTotal
if $link->{"mediaType"} 67 267 334
1485 334 push @$out, { name => $propname, value => $link->{uri}, params => $params };
1486 }
1487 }
1488  
1489 1083 ▶sub _unconvert_resource {
Subroutine _unconvert_resource: called CC=4 SCAR=13.9
1490 1083 my ($out, $ctx, $card, $key, $propname) = @_;
1491 1083 ▶ my $resources = $card->{$key} || return;
Condition100%2/2
$card->{$key} || (return)
l!l
237846
MC/DC100%1/1
$card->{$key} || (return)
$card->{$key}
Truth table100%2/2
A
A= $card->{$key}
Aresult
11
00
1492  
1493 237 for my $id (sort keys %$resources) {
1494 308 my $r = $resources->{$id};
1495 308 my $params = { 'prop-id' => [$id] };
1496 308 _add_context_params($params, $r);
1497 308 _add_pref_params($params, $r->{pref});
1498 308 ▶ $params->{mediatype} = [$r->{mediaType}] if $r->{mediaType};
Branch: 1 branch
BranchTrueFalseTotal
if $r->{"mediaType"} 69 239 308
1499 308 push @$out, { name => $propname, value => $r->{uri}, params => $params };
1500 }
1501 }
1502  
1503 361 ▶sub _unconvert_calendars {
Subroutine _unconvert_calendars: called CC=1 SCAR=0.0
1504 361 _unconvert_resource($_[0], $_[2], $_[1], 'calendars', 'caluri');
1505 }
1506  
1507 361 ▶sub _unconvert_scheduling_addresses {
Subroutine _unconvert_scheduling_addresses: called CC=1 SCAR=0.0
1508 361 _unconvert_resource($_[0], $_[2], $_[1], 'schedulingAddresses', 'caladruri');
1509 }
1510  
1511 361 ▶sub _unconvert_crypto_keys {
Subroutine _unconvert_crypto_keys: called CC=1 SCAR=0.0
1512 361 _unconvert_resource($_[0], $_[2], $_[1], 'cryptoKeys', 'key');
1513 }
1514  
1515 361 ▶sub _unconvert_directories {
Subroutine _unconvert_directories: called CC=6 SCAR=18.0
1516 361 my ($out, $card, $ctx) = @_;
1517 361 ▶ my $dirs = $card->{directories} || return;
Condition100%2/2
$card->{'directories'} || (return)
l!l
69292
MC/DC100%1/1
$card->{'directories'} || (return)
$card->{'directories'}
Truth table100%2/2
A
A= $card->{'directories'}
Aresult
11
00
1518  
1519 69 for my $id (sort keys %$dirs) {
1520 135 my $dir = $dirs->{$id};
1521 135 ▶ my $kind = $dir->{kind} // '';
Condition50%1/2
$dir->{"kind"} // ""
l!l
1350
MC/DC0%0/1
$dir->{"kind"} // ""
$dir->{"kind"}
Truth table50%1/2
A
A= $dir->{"kind"}
Aresult
11
00
1522 135 ▶ my $propname = $kind eq 'entry' ? 'source' : 'org-directory';
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'entry' ? : 68 67 135
1523 135 my $params = { 'prop-id' => [$id] };
1524 135 _add_context_params($params, $dir);
1525 135 _add_pref_params($params, $dir->{pref});
1526 135 ▶ $params->{mediatype} = [$dir->{mediaType}] if $dir->{mediaType};
Branch: 1 branch
BranchTrueFalseTotal
if $dir->{"mediaType"} 0 135 135
1527 135 push @$out, { name => $propname, value => $dir->{uri}, params => $params };
1528 }
1529 }
1530  
1531 361 ▶sub _unconvert_preferred_languages {
Subroutine _unconvert_preferred_languages: called CC=3 SCAR=11.0
1532 361 my ($out, $card, $ctx) = @_;
1533 361 ▶ my $langs = $card->{preferredLanguages} || return;
Condition100%2/2
$card->{'preferredLanguages'} || (return)
l!l
99262
MC/DC100%1/1
$card->{'preferredLanguages'} || (return)
$card->{'preferredLanguages'}
Truth table100%2/2
A
A= $card->{'preferredLanguages'}
Aresult
11
00
1534  
1535 99 for my $id (sort keys %$langs) {
1536 230 my $lp = $langs->{$id};
1537 230 my $params = { 'prop-id' => [$id] };
1538 230 _add_context_params($params, $lp);
1539 230 _add_pref_params($params, $lp->{pref});
1540 230 push @$out, { name => 'lang', value => $lp->{language}, params => $params };
1541 }
1542 }
1543  
1544 361 ▶sub _unconvert_personal_info {
Subroutine _unconvert_personal_info: called CC=7 SCAR=19.6
1545 361 my ($out, $card, $ctx) = @_;
1546 361 ▶ my $info = $card->{personalInfo} || return;
Condition100%2/2
$card->{'personalInfo'} || (return)
l!l
36325
MC/DC100%1/1
$card->{'personalInfo'} || (return)
$card->{'personalInfo'}
Truth table100%2/2
A
A= $card->{'personalInfo'}
Aresult
11
00
1547  
1548 36 my %level_map = (low => 'beginner', medium => 'average', high => 'expert');
1549  
1550 36 for my $id (sort keys %$info) {
1551 200 my $pi = $info->{$id};
1552 200 ▶ my $propname = $pi->{kind} // 'expertise';
Condition50%1/2
$pi->{"kind"} // "expertise"
l!l
2000
MC/DC0%0/1
$pi->{"kind"} // "expertise"
$pi->{"kind"}
Truth table50%1/2
A
A= $pi->{"kind"}
Aresult
11
00
1553 200 my $params = { 'prop-id' => [$id] };
1554 200 ▶ if ($pi->{level}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pi->{"level"}) 199 1 200
1555 199 ▶ $params->{level} = [$level_map{$pi->{level}} // $pi->{level}];
Condition33%1/3
$level_map{$pi->{"level"}} // $pi->{"level"}
l!l&&r!l&&!r
19900
MC/DC0%0/2
$level_map{$pi->{"level"}} // $pi->{"level"}
$level_map{$pi->{"level"}} $pi->{"level"}
Truth table33%1/3
A // B
A= $level_map{$pi->{"level"}}B= $pi->{"level"}
ABresult
1X1
011
000
1556 }
1557 200 ▶ if (defined $pi->{listAs}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pi->{"listAs"}) 196 4 200
1558 196 $params->{index} = [$pi->{listAs}];
1559 }
1560 200 push @$out, { name => $propname, value => $pi->{value}, params => $params };
1561 }
1562 }
1563  
1564 361 ▶sub _unconvert_keywords {
Subroutine _unconvert_keywords: called CC=4 SCAR=14.6
1565 361 my ($out, $card, $ctx) = @_;
1566 361 ▶ my $keywords = $card->{keywords} || return;
Condition100%2/2
$card->{'keywords'} || (return)
l!l
134227
MC/DC100%1/1
$card->{'keywords'} || (return)
$card->{'keywords'}
Truth table100%2/2
A
A= $card->{'keywords'}
Aresult
11
00
1567 134 ▶ return unless ref $keywords eq 'HASH' && %$keywords;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $keywords eq "HASH" and %$keywords 0 134 134
Condition33%1/3
ref $keywords eq "HASH" and %$keywords
!ll&&!rl&&r
00134
MC/DC0%0/2
ref $keywords eq "HASH" and %$keywords
ref $keywords eq "HASH" %$keywords
Truth table33%1/3
A and B
A= ref $keywords eq "HASH"B= %$keywords
ABresult
0X0
100
111
1568 # a list: one value, comma-separated (RFC 6350 S6.7.1)
1569 134 push @$out, { name => 'categories', values => [[ sort keys %$keywords ]], params => {} };
1570 }
1571  
1572 361 ▶sub _unconvert_members {
Subroutine _unconvert_members: called CC=3 SCAR=11.0
1573 361 my ($out, $card, $ctx) = @_;
1574 361 ▶ my $members = $card->{members} || return;
Condition100%2/2
$card->{'members'} || (return)
l!l
68293
MC/DC100%1/1
$card->{'members'} || (return)
$card->{'members'}
Truth table100%2/2
A
A= $card->{'members'}
Aresult
11
00
1575 68 for my $uid (sort keys %$members) {
1576 165 push @$out, { name => 'member', value => $uid, params => {} };
1577 }
1578 }
1579  
1580 2 ▶sub _unconvert_members_apple {
Subroutine _unconvert_members_apple: called CC=4 SCAR=14.2
1581 2 my ($out, $card, $ctx) = @_;
1582 2 ▶ my $members = $card->{members} || return;
Condition50%1/2
$card->{'members'} || (return)
l!l
20
MC/DC0%0/1
$card->{'members'} || (return)
$card->{'members'}
Truth table50%1/2
A
A= $card->{'members'}
Aresult
11
00
1583 2 for my $uid (sort keys %$members) {
1584 # Apple uses urn:uuid: prefix for member UIDs
1585 2 my $val = $uid;
1586 2 ▶ $val = "urn:uuid:$val" unless $val =~ /^urn:uuid:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^urn:uuid:/ 0 2 2
1587 2 push @$out, { name => 'x-addressbookserver-member', value => $val, params => {} };
1588 }
1589 }
1590  
1591 362 ▶sub _unconvert_related {
Subroutine _unconvert_related: called CC=7 SCAR=19.5
1592 362 my ($out, $card, $ctx) = @_;
1593 362 ▶ my $related = $card->{relatedTo} || return;
Condition100%2/2
$card->{'relatedTo'} || (return)
l!l
136226
MC/DC100%1/1
$card->{'relatedTo'} || (return)
$card->{'relatedTo'}
Truth table100%2/2
A
A= $card->{'relatedTo'}
Aresult
11
00
1594  
1595 136 for my $uid (sort keys %$related) {
1596 270 my $rel = $related->{$uid};
1597 270 my $params = {};
1598 270 ▶ if ($rel->{relation} && ref $rel->{relation} eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rel->{"relation"} and ref $rel->{"relation"} eq "HASH") 200 70 270
Condition66%2/3
$rel->{"relation"} and ref $rel->{"relation"} eq "HASH"
!ll&&!rl&&r
700200
MC/DC50%1/2
$rel->{"relation"} and ref $rel->{"relation"} eq "HASH"
$rel->{"relation"} ref $rel->{"relation"} eq "HASH"
Truth table66%2/3
A and B
A= $rel->{"relation"}B= ref $rel->{"relation"} eq "HASH"
ABresult
0X0
100
111
1599 200 my @types = sort keys %{$rel->{relation}};
1600 200 ▶ $params->{type} = \@types if @types;
Branch: 1 branch
BranchTrueFalseTotal
if @types 200 0 200
1601 }
1602 # RELATED is a URI unless VALUE=text says otherwise (RFC 6350 S6.6.6)
1603 270 ▶ $params->{value} = ['text'] unless _looks_like_uri($uid);
Branch: 1 branch
BranchTrueFalseTotal
unless _looks_like_uri($uid) 166 104 270
1604 270 push @$out, { name => 'related', value => $uid, params => $params };
1605 }
1606 }
1607  
1608 361 ▶sub _unconvert_speak_to_as {
Subroutine _unconvert_speak_to_as: called CC=5 SCAR=16.1
1609 361 my ($out, $card, $ctx) = @_;
1610 361 ▶ my $sta = $card->{speakToAs} || return;
Condition100%2/2
$card->{'speakToAs'} || (return)
l!l
68293
MC/DC100%1/1
$card->{'speakToAs'} || (return)
$card->{'speakToAs'}
Truth table100%2/2
A
A= $card->{'speakToAs'}
Aresult
11
00
1611  
1612 68 ▶ if ($sta->{grammaticalGender}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sta->{"grammaticalGender"}) 67 1 68
1613 67 push @$out, { name => 'gramgender', value => $sta->{grammaticalGender}, params => {} };
1614 }
1615  
1616 68 ▶ if ($sta->{pronouns}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sta->{"pronouns"}) 68 0 68
1617 68 for my $id (sort keys %{$sta->{pronouns}}) {
1618 102 my $p = $sta->{pronouns}{$id};
1619 102 my $params = { 'prop-id' => [$id] };
1620 102 _add_context_params($params, $p);
1621 102 _add_pref_params($params, $p->{pref});
1622 102 push @$out, { name => 'pronouns', value => $p->{pronouns}, params => $params };
1623 }
1624 }
1625 }
1626  
1627 ###############################################################################
1628 # PATCH: Apply JSContact changes to an existing vCard with minimal disruption
1629 ###############################################################################
1630  
1631 # top-level Card property => the sub that patches it (filled in below)
1632 20 my %PATCHERS;
1633  
1634 # patch_vcard($original, $old_card, $new_card) applies the change from
1635 # $old_card to $new_card to the original vCard, line by line: a line for
1636 # an entry the change does not touch keeps its bytes, its group, its
1637 # folding and its place, and so does every line JSContact cannot say
1638 # anything about. Returns $original itself when nothing changed; else
1639 # the patched vCard, as bytes if $original was bytes (as read from a
1640 # CardDAV server) and as characters if it was characters. An original
1641 # that cannot be parsed gives the whole Card written afresh.
1642 47 ▶sub patch_vcard {
Subroutine patch_vcard: called CC=4 SCAR=14.0
Pod undocumented
1643 47 my ($original_vcard, $old_card, $new_card) = @_;
1644  
1645 # Determine which top-level JSContact properties changed
1646 47 my %changed = _diff_cards($old_card, $new_card);
1647 47 ▶ return $original_vcard unless %changed;
Branch: 1 branch
BranchTrueFalseTotal
unless %changed 21 26 47
1648  
1649 26 ▶ my $out = _patch_vcard($original_vcard, $new_card, \%changed)
Condition33%1/3
_patch_vcard($original_vcard, $new_card, \%changed) // jscontact_to_vcard($new_card)
l!l&&r!l&&!r
2600
MC/DC0%0/2
_patch_vcard($original_vcard, $new_card, \%changed) // jscontact_to_vcard($new_card)
_patch_vcard($original_vcard, $new_card, \%changed) jscontact_to_vcard($new_card)
Truth table33%1/3
A // B
A= _patch_vcard($original_vcard, $new_card, \%changed)B= jscontact_to_vcard($new_card)
ABresult
1X1
011
000
1650 // jscontact_to_vcard($new_card);
1651 26 ▶ return utf8::is_utf8($original_vcard) ? $out : encode_utf8($out);
Branch: 1 branch
BranchTrueFalseTotal
utf8::is_utf8($original_vcard) ? : 1 25 26
1652 }
1653  
1654 # The engine behind patch_vcard and jscontact_to_vcard(original => ...).
1655 # Reads $original (bytes or characters) itself, so the Card it diffs
1656 # against is exactly what the original says, with the vCard properties
1657 # each entry came from. $only limits the patch to those top-level Card
1658 # properties. Returns characters, or undef if the original cannot be
1659 # patched (it does not parse, or its lines cannot be matched to what
1660 # Text::VCardFast parsed).
1661 54 ▶sub _patch_vcard {
Subroutine _patch_vcard: called CC=11 SCAR=24.0
1662 54 my ($original, $new_card, $only) = @_;
1663  
1664 54 my $text = _decode_input($original);
1665 54 my ($old_card, $ctx, $props) = _read_vcard($text);
1666 54 ▶ return undef unless $old_card;
Branch: 1 branch
BranchTrueFalseTotal
unless $old_card 3 51 54
1667  
1668 51 ▶ my $P = _split_vcard($text, $props) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $P = _split_vcard($text, $props) or return undef 51 0 51
1669 51 $P->{ctx} = $ctx;
1670 51 $P->{props} = $props;
1671 # vCard 3 (RFC 2426) or 2.1: lines patched in must be valid there
1672 51 ▶ $P->{v3} = ((_first_value($props, 'version') // '4.0') =~ /^[23]\./) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
(_first_value($props, 'version') // '4.0') =~ /^[23]\./ ? : 34 17 51
Condition50%1/2
_first_value($props, "version") // "4.0"
l!l
510
MC/DC0%0/1
_first_value($props, "version") // "4.0"
_first_value($props, "version")
Truth table50%1/2
A
A= _first_value($props, "version")
Aresult
11
00
1673  
1674 51 ▶ my %changed = $only ? %$only : _diff_cards($old_card, $new_card);
Branch: 1 branch
BranchTrueFalseTotal
$only ? : 26 25 51
1675 51 for my $key (sort keys %changed) {
1676 36 ▶ my $patcher = $PATCHERS{$key} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $patcher = $PATCHERS{$key} or next 35 1 36
1677 35 $patcher->($P, $key, $old_card, $new_card);
1678 }
1679 51 _drop_orphan_groups($P);
1680  
1681 return join '', map {
1682 1526 ▶ (@{$_->{before} // []}, ($_->{deleted} ? () : $_->{raw}), @{$_->{after} // []})
Branch: 1 branch
BranchTrueFalseTotal
$_->{'deleted'} ? : 5 1521 1526
Condition100%4/4
$_->{"before"} // []
l!l
21524
$_->{"after"} // []
l!l
71519
MC/DC100%1/1
$_->{"before"} // []
$_->{"before"}
MC/DC100%1/1
$_->{"after"} // []
$_->{"after"}
Truth table100%2/2
A
A= $_->{"before"}
Aresult
11
00
Truth table100%2/2
A
A= $_->{"after"}
Aresult
11
00
1683 51 } @{$P->{lines}};
1684 }
1685  
1686 # --- The original, as lines ----------------------------------------------
1687  
1688 # Split a vCard into content lines, each keeping its raw text (folding and
1689 # line ending included), and match each property line of the first vCard
1690 # to the property Text::VCardFast parsed from it
1691 51 ▶sub _split_vcard {
Subroutine _split_vcard: called CC=21 SCAR=31.4
1692 51 my ($text, $props) = @_;
1693 51 ▶ return undef unless $text =~ /\n/;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\n/ 0 51 51
1694 51 ▶ my $eol = $text =~ /\r\n/ ? "\r\n" : "\n";
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\r\n/ ? : 4 47 51
1695  
1696 # one match per logical line (a physical line and its folded
1697 # continuations): a split at every newline was most of the time a card
1698 # with a large folded PHOTO took
1699 51 my @lines;
1700 51 pos($text) = 0;
1701 51 ▶ while (pos($text) < length $text) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $text < length $text) 1526 51 1577
1702 1526 ▶ $text =~ /\G([^\n]*(?:\n|\z)(?:[ \t][^\n]*(?:\n|\z))*)/gc or last;
Branch: 1 branch
BranchTrueFalseTotal
$text =~ /\G([^\n]*(?:\n|\z)(?:[ \t][^\n]*(?:\n|\z))*)/cg or last 1526 0 1526
1703 1526 push @lines, { raw => $1 };
1704 }
1705  
1706 51 my ($depth, $end, %by_name) = (0);
1707 51 for my $i (0..$#lines) {
1708 1526 my $line = $lines[$i];
1709 1526 $line->{index} = $i;
1710 1526 (my $u = $line->{raw}) =~ s/\r?\n[ \t]//g;
1711 1526 $u =~ s/\r?\n\z//;
1712 1526 $line->{text} = $u;
1713 1526 ▶ next if $u =~ /^\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $u =~ /^\s*$/ 0 1526 1526
1714 1526 ▶ if ($u =~ /^BEGIN:/i) { $depth++; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($u =~ /^BEGIN:/i) 51 1475 1526
1715 1475 ▶ if ($u =~ /^END:/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($u =~ /^END:/i) 51 1424 1475
1716 51 ▶ $end //= $i if $depth == 1;
Branch: 1 branch
BranchTrueFalseTotal
if $depth == 1 51 0 51
Condition33%1/3
$end //= $i
l!l&&r!l&&!r
0510
MC/DC0%0/2
$end //= $i
$end $i
Truth table33%1/3
A //= B
A= $endB= $i
ABresult
1X1
011
000
1717 51 $depth--;
1718 51 next;
1719 }
1720 1424 ▶ next unless $depth == 1 && !defined $end;
Branch: 1 branch
BranchTrueFalseTotal
unless $depth == 1 and not defined $end 0 1424 1424
Condition33%1/3
$depth == 1 and not defined $end
!ll&&!rl&&r
001424
MC/DC0%0/2
$depth == 1 and not defined $end
$depth == 1 not defined $end
Truth table33%1/3
A and not(B)
A= $depth == 1B= not defined $end
ABresult
0X0
100
111
1721 1424 my ($name) = $u =~ /^([^;:]*)/;
1722 1424 my $lname = lc $name;
1723 # as Text::VCardFast: the group is everything before the last dot
1724 1424 ▶ if ($lname =~ s/^(.+)\.(.*?)$/$2/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lname =~ s/^(.+)\.(.*?)$/$2/) 509 915 1424
1725 509 ($line->{group}) = $name =~ /^(.+)\./;
1726 }
1727 1424 $line->{name} = $lname;
1728 1424 push @{$by_name{$lname}}, $line;
1729 }
1730 51 ▶ return undef unless defined $end;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $end 0 51 51
1731  
1732 51 my %line_of;
1733 51 for my $name (keys %$props) {
1734 995 my $items = $props->{$name};
1735 995 ▶ my $ls = $by_name{$name} // [];
Condition50%1/2
$by_name{$name} // []
l!l
9950
MC/DC0%0/1
$by_name{$name} // []
$by_name{$name}
Truth table50%1/2
A
A= $by_name{$name}
Aresult
11
00
1736 995 ▶ return undef unless @$ls == @$items;
Branch: 1 branch
BranchTrueFalseTotal
unless @$ls == @$items 0 995 995
1737 995 for my $i (0..$#$items) {
1738 1424 $line_of{refaddr $items->[$i]} = $ls->[$i];
1739 1424 $ls->[$i]{item} = $items->[$i];
1740 }
1741 }
1742 51 for my $name (keys %by_name) {
1743 995 ▶ return undef unless $props->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless $props->{$name} 0 995 995
1744 }
1745  
1746 return {
1747 51 lines => \@lines,
1748 eol => $eol,
1749 end => $lines[$end],
1750 by_name => \%by_name,
1751 line_of => \%line_of,
1752 };
1753 }
1754  
1755 70 ▶sub _lines_of_items {
Subroutine _lines_of_items: called CC=3 SCAR=11.6
1756 70 my ($P, $items) = @_;
1757 28 return sort { $a->{index} <=> $b->{index} }
1758 92 ▶ grep { defined && !$_->{deleted} }
Condition33%1/3
defined $_ && !$_->{'deleted'}
!ll&&!rl&&r
0092
MC/DC0%0/2
defined $_ && !$_->{'deleted'}
defined $_ !$_->{'deleted'}
Truth table33%1/3
A && not(B)
A= defined $_B= !$_->{'deleted'}
ABresult
0X0
100
111
1759 70 ▶ map { $P->{line_of}{refaddr $_} } @{$items // []};
Condition50%1/2
$items // []
l!l
700
MC/DC0%0/1
$items // []
$items
Truth table50%1/2
A
A= $items
Aresult
11
00
1760 }
1761  
1762 13 ▶sub _lines_named {
Subroutine _lines_named: called CC=2 SCAR=6.9
1763 13 my ($P, @names) = @_;
1764 7 return sort { $a->{index} <=> $b->{index} }
1765 17 grep { !$_->{deleted} }
1766 13 ▶ map { @{$P->{by_name}{$_} // []} } @names;
Condition100%2/2
$P->{"by_name"}{$_} // []
l!l
125
MC/DC100%1/1
$P->{"by_name"}{$_} // []
$P->{"by_name"}{$_}
Truth table100%2/2
A
A= $P->{"by_name"}{$_}
Aresult
11
00
1767 }
1768  
1769 6 ▶sub _group_lines {
Subroutine _group_lines: called CC=3 SCAR=11.0
1770 6 my ($P, $group) = @_;
1771 330 ▶ return grep { defined $_->{group} && lc $_->{group} eq lc $group && !$_->{deleted} }
Condition100%6/6
defined $_->{'group'} && lc $_->{'group'} eq lc $group && !$_->{'deleted'}
!ll&&!rl&&r
318210
defined $_->{"group"} and lc $_->{"group"} eq lc $group
!ll&&!rl&&r
17414412
MC/DC100%3/3
defined $_->{'group'} && lc $_->{'group'} eq lc $group && !$_->{'deleted'}
defined $_->{"group"} lc $_->{"group"} eq lc $group !$_->{'deleted'}
Truth table100%4/4
A and B && not(C)
A= defined $_->{"group"}B= lc $_->{"group"} eq lc $groupC= !$_->{'deleted'}
ABCresult
0XX0
10X0
1100
1111
1772 6 @{$P->{lines}};
1773 }
1774  
1775 # --- Editing lines -------------------------------------------------------
1776  
1777 25 ▶sub _set_line {
Subroutine _set_line: called CC=2 SCAR=6.9
1778 25 my ($P, $line, $text) = @_;
1779 25 ▶ return if $text eq $line->{text};
Branch: 1 branch
BranchTrueFalseTotal
if $text eq $line->{"text"} 1 24 25
1780 24 $line->{text} = $text;
1781 24 $line->{raw} = _fold($text, $P->{eol});
1782 }
1783  
1784 5 ▶sub _delete_line {
Subroutine _delete_line: called CC=3 SCAR=11.0
1785 5 my ($P, $line) = @_;
1786 5 ▶ return unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 5 5
1787 5 $line->{deleted} = 1;
1788 5 ▶ $P->{touched_groups}{lc $line->{group}} = 1 if defined $line->{group};
Branch: 1 branch
BranchTrueFalseTotal
if defined $line->{"group"} 4 1 5
1789 }
1790  
1791 # Insert content lines after $anchor (and the lines of its group that
1792 # follow it), or before END:VCARD
1793 8 ▶sub _insert {
Subroutine _insert: called CC=7 SCAR=19.5
1794 8 my ($P, $anchor, @texts) = @_;
1795 8 my $at = $P->{end};
1796 8 my $where = 'before';
1797 8 ▶ if ($anchor) {
Branch: 1 branch
BranchTrueFalseTotal
if ($anchor) 6 2 8
1798 6 my $i = $anchor->{index};
1799 6 ▶ my $g = $anchor->{group} // $anchor->{new_group};
Condition66%2/3
$anchor->{"group"} // $anchor->{"new_group"}
l!l&&r!l&&!r
402
MC/DC50%1/2
$anchor->{"group"} // $anchor->{"new_group"}
$anchor->{"group"} $anchor->{"new_group"}
Truth table66%2/3
A // B
A= $anchor->{"group"}B= $anchor->{"new_group"}
ABresult
1X1
011
000
1800 6 my $lines = $P->{lines};
1801 6 ▶ while (defined $g && $i < $#$lines && defined $lines->[$i+1]{group}
Branch: 1 branch
BranchTrueFalseTotal
if (defined $g and $i < $lines->$#* and defined $$lines[$i + 1]{"group"} and lc $$lines[$i + 1]{"group"} eq lc $g) 4 6 10
Condition88%8/9
defined $g and $i < $lines->$#* and defined $$lines[$i + 1]{"group"} and lc $$lines[$i + 1]{"group"} eq lc $g
!ll&&!rl&&r
514
defined $g and $i < $lines->$#* and defined $$lines[$i + 1]{"group"}
!ll&&!rl&&r
235
defined $g and $i < $lines->$#*
!ll&&!rl&&r
208
MC/DC75%3/4
defined $g and $i < $lines->$#* and defined $$lines[$i + 1]{"group"} and lc $$lines[$i + 1]{"group"} eq lc $g
defined $g $i < $lines->$#* defined $$lines[$i + 1]{"group"} lc $$lines[$i + 1]{"group"} eq lc $g
Truth table80%4/5
A and B and C and D
A= defined $gB= $i < $lines->$#*C= defined $$lines[$i + 1]{"group"}D= lc $$lines[$i + 1]{"group"} eq lc $g
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1802 && lc $lines->[$i+1]{group} eq lc $g) {
1803 4 $i++;
1804 }
1805 6 ($at, $where) = ($lines->[$i], 'after');
1806 }
1807 8 push @{$at->{$where}}, map { _fold($_, $P->{eol}) } @texts;
1808 }
1809  
1810 # A group left with nothing but annotations (a label, an Apple country
1811 # code, a GEO or TZ for an address) once its property is deleted goes too
1812 20 my %ANNOTATION = map { $_ => 1 } qw(x-ablabel x-abadr geo tz);
1813  
1814 51 ▶sub _drop_orphan_groups {
Subroutine _drop_orphan_groups: called CC=5 SCAR=16.1
1815 51 my ($P) = @_;
1816 51 ▶ for my $g (sort keys %{$P->{touched_groups} // {}}) {
Condition100%2/2
$P->{"touched_groups"} // {}
l!l
150
MC/DC100%1/1
$P->{"touched_groups"} // {}
$P->{"touched_groups"}
Truth table100%2/2
A
A= $P->{"touched_groups"}
Aresult
11
00
1817 2 my @live = _group_lines($P, $g);
1818 2 ▶ next if grep { !$ANNOTATION{$_->{name}} } @live;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $ANNOTATION{$_->{"name"}};} @live 0 2 2
1819 2 _delete_line($P, $_) for @live;
1820 }
1821 }
1822  
1823 # --- Writing an entry into a line ----------------------------------------
1824  
1825 # A content line split into its name (group included, as written), its
1826 # parameters (each as written, with its values decoded) and its value
1827 25 ▶sub _parse_line {
Subroutine _parse_line: called CC=6 SCAR=19.8
1828 25 my ($text) = @_;
1829 25 $text =~ /\G([^;:]*)/gc;
1830 25 my $name = $1;
1831 25 my @tokens;
1832 25 ▶ while ($text =~ /\G(;([^;:=]*)(?:=((?:"[^"]*"|[^";:])*))?)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ /\G(;([^;:=]*)(?:=((?:"[^"]*"|[^";:])*))?)/cg) 28 25 53
1833 28 my ($raw, $pname, $pval) = ($1, $2, $3);
1834 28 my $tok = { raw => $raw, rawname => $pname };
1835 28 ▶ if (defined $pval) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pval) { } 28 0 28
1836 28 $tok->{name} = lc $pname;
1837 $tok->{values} = [ map {
1838 28 my $r = $_;
1839 29 (my $v = $r) =~ s/^"(.*)"$/$1/s;
1840 0 ▶ $v =~ s/\^([n^'])/$1 eq 'n' ? "\n" : $1 eq '^' ? '^' : '"'/ge;
Branch: 2 branches
BranchTrueFalseTotal
$1 eq 'n' ? : 0 0 0
$1 eq '^' ? : 0 0 0
1841 29 { raw => $r, val => $v };
1842 } $pval =~ /((?:"[^"]*"|[^,"])+)/g ];
1843 }
1844 else {
1845 # vCard 2.1: a bare TYPE value
1846 0 $tok->{name} = 'type';
1847 0 $tok->{bare} = 1;
1848 0 $tok->{values} = [ { raw => $pname, val => $pname } ];
1849 }
1850 28 push @tokens, $tok;
1851 }
1852 25 ▶ return undef unless $text =~ /\G:/gc;
Branch: 1 branch
BranchTrueFalseTotal
unless $text =~ /\G:/cg 0 25 25
1853 25 return { name => $name, tokens => \@tokens, value => substr($text, pos $text) };
1854 }
1855  
1856 # The parameters the reader interprets. Any other parameter on a line is
1857 # kept as it is; so is a TYPE value the reader does not interpret.
1858 620 my %KNOWN_PARAM = map { $_ => 1 } qw(
1859 prop-id value type pref label geo tz cc mediatype service-type username
1860 x-service-type x-user level index created author author-name sort-as
1861 encoding
1862 20 ), map { "x-$_" } qw(label geo tz cc mediatype sort-as level index created
1863 author author-name);
1864 20 my %KNOWN_TYPE = map { $_ => 1 } qw(home work pref cell voice fax video pager
1865 text textphone);
1866 # properties whose every TYPE value is the reader's (a media format, a
1867 # relation)
1868 20 my %ALL_TYPES_KNOWN = map { $_ => 1 } qw(photo logo sound related);
1869  
1870 44 ▶sub _param_key {
Subroutine _param_key: called CC=2 SCAR=6.9
1871 44 my ($name, $val) = @_;
1872 44 $name = lc $name;
1873 44 ▶ return "$name\0" . ($name =~ /^(?:type|value|encoding)$/ ? lc $val : $val);
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /^(?:type|value|encoding)$/ ? : 37 7 44
1874 }
1875  
1876 # The line for $prop written over the original $line of the same name:
1877 # the original's name, group and parameters stay as written, except the
1878 # known parameters the new property no longer has
1879 23 ▶sub _merged_text {
Subroutine _merged_text: called CC=23 SCAR=31.6
1880 23 my ($P, $line, $prop) = @_;
1881 23 ▶ my $parsed = _parse_line($line->{text}) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $parsed = _parse_line($line->{"text"}) or return undef 23 0 23
1882 23 ▶ my $params = $prop->{params} // {};
Condition50%1/2
$prop->{"params"} // {}
l!l
230
MC/DC0%0/1
$prop->{"params"} // {}
$prop->{"params"}
Truth table50%1/2
A
A= $prop->{"params"}
Aresult
11
00
1883 23 my $lname = lc $prop->{name};
1884  
1885 23 my (@want, %want);
1886 23 for my $n (_param_order($params)) {
1887 14 my $vals = $params->{$n};
1888 14 ▶ for my $v (ref $vals eq 'ARRAY' ? @$vals : ($vals)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $vals eq 'ARRAY' ? : 14 0 14
1889 17 ▶ next unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 17 17
1890 17 my $k = _param_key($n, _clean_vcard_text($v));
1891 17 push @want, [$n, $v, $k];
1892 17 $want{$k}++;
1893 }
1894 }
1895  
1896 23 my $out = $parsed->{name};
1897 23 ▶ $out = "$line->{new_group}.$out" if $line->{new_group};
Branch: 1 branch
BranchTrueFalseTotal
if $line->{"new_group"} 1 22 23
1898 23 for my $tok (@{$parsed->{tokens}}) {
1899 28 my $n = $tok->{name};
1900 my $keep_all = !$KNOWN_PARAM{$n}
1901 # a VALUE=date (say) stays unless the new value says otherwise
1902 || ($n eq 'value' && !$params->{value}
1903 28 ▶ && !grep { $_->{val} =~ /^(?:uri|text)$/i } @{$tok->{values}});
Condition77%7/9
!$KNOWN_PARAM{$n} || $n eq 'value' && !$params->{'value'} && !grep({$_->{'val'} =~ /^(?:uri|text)$/i;} @{$$tok{'values'};})
l!l&&r!l&&!r
1126
$n eq "value" and not $params->{"value"} and not grep({$_->{"val"} =~ /^(?:uri|text)$/i;} @{$$tok{"values"};})
!ll&&!rl&&r
2601
$n eq "value" and not $params->{"value"}
!ll&&!rl&&r
2601
MC/DC50%2/4
!$KNOWN_PARAM{$n} || $n eq 'value' && !$params->{'value'} && !grep({$_->{'val'} =~ /^(?:uri|text)$/i;} @{$$tok{'values'};})
!$KNOWN_PARAM{$n} $n eq "value" not $params->{"value"} not grep({$_->{"val"} =~ /^(?:uri|text)$/i;} @{$$tok{"values"};})
Truth table60%3/5
not(A) || B and not(C) and not(D)
A= !$KNOWN_PARAM{$n}B= $n eq "value"C= not $params->{"value"}D= not grep({$_->{"val"} =~ /^(?:uri|text)$/i;} @{$$tok{"values"};})
ABCDresult
1XXX1
01111
00XX0
010X0
01100
1904 28 ▶ if ($keep_all) { $out .= $tok->{raw}; next }
Branch: 1 branch
BranchTrueFalseTotal
if ($keep_all) 2 26 28
1905 26 my (@kept, $dropped);
1906 26 for my $v (@{$tok->{values}}) {
1907 27 my $k = _param_key($n, $v->{val});
1908 27 ▶ if ($want{$k}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($want{$k}) { } 15 12 27
elsif ($n eq 'type' and not $ALL_TYPES_KNOWN{$lname} and not $KNOWN_TYPE{lc $v->{'val'}}) { } 9 3 12
1909 15 $want{$k}--;
1910 15 push @kept, $v;
1911 }
1912 ▶ elsif ($n eq 'type' && !$ALL_TYPES_KNOWN{$lname} && !$KNOWN_TYPE{lc $v->{val}}) {
Condition100%6/6
$n eq "type" and not $ALL_TYPES_KNOWN{$lname} and not $KNOWN_TYPE{lc $v->{"val"}}
!ll&&!rl&&r
219
$n eq "type" and not $ALL_TYPES_KNOWN{$lname}
!ll&&!rl&&r
1110
MC/DC100%3/3
$n eq "type" and not $ALL_TYPES_KNOWN{$lname} and not $KNOWN_TYPE{lc $v->{"val"}}
$n eq "type" not $ALL_TYPES_KNOWN{$lname} not $KNOWN_TYPE{lc $v->{"val"}}
Truth table100%4/4
A and not(B) and not(C)
A= $n eq "type"B= not $ALL_TYPES_KNOWN{$lname}C= not $KNOWN_TYPE{lc $v->{"val"}}
ABCresult
0XX0
10X0
1100
1111
1913 9 push @kept, $v;
1914 }
1915 else {
1916 3 $dropped = 1;
1917 }
1918 }
1919 26 ▶ if (!$dropped) { $out .= $tok->{raw} }
Branch: 2 branches
BranchTrueFalseTotal
if (not $dropped) { } 23 3 26
elsif (@kept) { } 1 2 3
1920 elsif (@kept) {
1921 0 $out .= $tok->{bare} ? join('', map { ";$_->{raw}" } @kept)
1922 1 ▶ : ";$tok->{rawname}=" . join(',', map { $_->{raw} } @kept);
Branch: 1 branch
BranchTrueFalseTotal
$tok->{'bare'} ? : 0 1 1
1923 }
1924 }
1925 23 for my $w (@want) {
1926 17 my ($n, $v, $k) = @$w;
1927 17 ▶ next unless $want{$k};
Branch: 1 branch
BranchTrueFalseTotal
unless $want{$k} 15 2 17
1928 2 $want{$k}--;
1929 2 $out .= ';' . uc($n) . '=' . _param_value($v);
1930 }
1931 23 return $out . ':' . _value_text($prop);
1932 }
1933  
1934 # Write $prop over the original $line: merged when it is the same vCard
1935 # property, else replaced, keeping the line's group (and so its label)
1936 23 ▶sub _replace_line {
Subroutine _replace_line: called CC=5 SCAR=23.1
1937 23 my ($P, $line, $prop) = @_;
1938 23 my $text;
1939 23 ▶ $text = _merged_text($P, $line, $prop) if lc $prop->{name} eq $line->{name};
Branch: 1 branch
BranchTrueFalseTotal
if lc $prop->{"name"} eq $line->{"name"} 23 0 23
1940 23 ▶ unless (defined $text) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $text) 0 23 23
1941 0 ▶ $prop->{group} //= $line->{group} // $line->{new_group};
Condition0%0/6
$prop->{"group"} //= $line->{"group"} // $line->{"new_group"}
l!l&&r!l&&!r
000
$line->{"group"} // $line->{"new_group"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$prop->{"group"} //= $line->{"group"} // $line->{"new_group"}
$prop->{"group"} $line->{"group"} $line->{"new_group"}
Truth table0%0/4
A //= B // C
A= $prop->{"group"}B= $line->{"group"}C= $line->{"new_group"}
ABCresult
1XX1
01X1
0011
0000
1942 0 $text = _prop_line($prop);
1943 }
1944 23 _set_line($P, $line, $text);
1945 }
1946  
1947 # A vCard property that has an Apple or older form keeps the form the
1948 # original used
1949 20 my %KEEP_NAME = (
1950 'x-abdate' => { anniversary => 1 },
1951 'x-socialprofile' => { impp => 1 },
1952 'socialprofile' => { impp => 1 },
1953 );
1954  
1955 # Make a generated property fit the original: its property name, and the
1956 # vCard 3 syntax if the original is vCard 3
1957 31 ▶sub _fit {
Subroutine _fit: called CC=5 SCAR=17.2
1958 31 my ($P, $prop, $line) = @_;
1959 $prop->{name} = $line->{name}
1960 31 ▶ if $line && $KEEP_NAME{$line->{name}} && $KEEP_NAME{$line->{name}}{lc $prop->{name}};
Branch: 1 branch
BranchTrueFalseTotal
if $line and $KEEP_NAME{$line->{"name"}} and $KEEP_NAME{$line->{"name"}}{lc $prop->{"name"}} 0 31 31
Condition50%3/6
$line and $KEEP_NAME{$line->{"name"}} and $KEEP_NAME{$line->{"name"}}{lc $prop->{"name"}}
!ll&&!rl&&r
3100
$line and $KEEP_NAME{$line->{"name"}}
!ll&&!rl&&r
8230
MC/DC0%0/3
$line and $KEEP_NAME{$line->{"name"}} and $KEEP_NAME{$line->{"name"}}{lc $prop->{"name"}}
$line $KEEP_NAME{$line->{"name"}} $KEEP_NAME{$line->{"name"}}{lc $prop->{"name"}}
Truth table50%2/4
A and B and C
A= $lineB= $KEEP_NAME{$line->{"name"}}C= $KEEP_NAME{$line->{"name"}}{lc $prop->{"name"}}
ABCresult
0XX0
10X0
1100
1111
1961 31 ▶ _to_v3($prop) if $P->{v3};
Branch: 1 branch
BranchTrueFalseTotal
if $P->{"v3"} 26 5 31
1962 31 return $prop;
1963 }
1964  
1965 # vCard 4 parameters with no vCard 3 form keep their value as X- (RFC
1966 # 2426 S4 allows x-name parameters); the reader reads them back
1967 20 my @V4_ONLY_PARAM = qw(label geo tz cc mediatype sort-as level index created
1968 author author-name);
1969  
1970 # Rewrite a property as the vCard 4 writer made it into vCard 3 (RFC 2426)
1971 # syntax: no PROP-ID; PREF=1 becomes TYPE=pref; an inline data: URI is
1972 # ENCODING=b with the format as TYPE; RFC 9554 SERVICE-TYPE and USERNAME
1973 # are the X-SERVICE-TYPE and X-USER that Apple writes; KIND and MEMBER
1974 # are Apple's X-ADDRESSBOOKSERVER-*; a date without a year uses Apple's
1975 # 1604. A property defined only for vCard 4 (RELATED, GENDER, LANG, ...)
1976 # keeps its name: RFC 2425 allows any registered name, and a vCard 3
1977 # reader skips one it does not know.
1978 26 ▶sub _to_v3 {
Subroutine _to_v3: called CC=30 SCAR=40.2
1979 26 my ($prop) = @_;
1980 26 ▶ my $p = $prop->{params} //= {};
Condition50%1/2
$prop->{"params"} //= {}
l!l
260
MC/DC0%0/1
$prop->{"params"} //= {}
$prop->{"params"}
Truth table50%1/2
A
A= $prop->{"params"}
Aresult
11
00
1981 26 my $name = lc $prop->{name};
1982  
1983 26 delete $p->{'prop-id'};
1984 26 ▶ if (my $pref = delete $p->{pref}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pref = delete $p->{"pref"}) 4 22 26
1985 4 push @{$p->{type}}, 'pref'
1986 4 ▶ if grep { defined $_ && looks_like_number($_) && $_ == 1 } ref $pref ? @$pref : ($pref);
Branch: 2 branches
BranchTrueFalseTotal
if grep {defined $_ and &looks_like_number($_) and $_ == 1;} ref $pref ? @$pref : $pref 4 0 4
ref $pref ? : 4 0 4
Condition33%2/6
defined $_ && &looks_like_number($_) && $_ == 1
!ll&&!rl&&r
004
defined $_ and &looks_like_number($_)
!ll&&!rl&&r
004
MC/DC0%0/3
defined $_ && &looks_like_number($_) && $_ == 1
defined $_ &looks_like_number($_) $_ == 1
Truth table25%1/4
A and B && C
A= defined $_B= &looks_like_number($_)C= $_ == 1
ABCresult
0XX0
10X0
1100
1111
1987 }
1988  
1989 26 ▶ if ($name =~ /^(?:photo|logo|sound)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^(?:photo|logo|sound)$/) 1 25 26
1990 1 my $mt = delete $p->{mediatype};
1991 1 ▶ if (($prop->{value} // '') =~ m{^data:([^;,]*)(?:;[^;,]*)*;base64,(.*)\z}si) {
Branch: 1 branch
BranchTrueFalseTotal
if (($prop->{'value'} // '') =~ /^data:([^;,]*)(?:;[^;,]*)*;base64,(.*)\z/is) { } 1 0 1
Condition50%1/2
$prop->{"value"} // ""
l!l
10
MC/DC0%0/1
$prop->{"value"} // ""
$prop->{"value"}
Truth table50%1/2
A
A= $prop->{"value"}
Aresult
11
00
1992 1 my ($type, $b64) = ($1, $2);
1993 1 ▶ $type = $mt->[0] if $type eq '' && $mt;
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "" and $mt 0 1 1
Condition33%1/3
$type eq "" and $mt
!ll&&!rl&&r
100
MC/DC0%0/2
$type eq "" and $mt
$type eq "" $mt
Truth table33%1/3
A and B
A= $type eq ""B= $mt
ABresult
0X0
100
111
1994 1 ▶ $type =~ s{.*/}{} if defined $type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $type 1 0 1
1995 1 $p->{encoding} = ['b'];
1996 1 ▶ $p->{type} = [$type] if defined $type && $type ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $type and $type ne "" 1 0 1
Condition33%1/3
defined $type and $type ne ""
!ll&&!rl&&r
001
MC/DC0%0/2
defined $type and $type ne ""
defined $type $type ne ""
Truth table33%1/3
A and B
A= defined $typeB= $type ne ""
ABresult
0X0
100
111
1997 1 $prop->{value} = $b64;
1998 }
1999 else {
2000 0 $p->{value} = ['uri'];
2001 }
2002 }
2003 26 ▶ $p->{type} = [ map { uc } @{$p->{type}} ] if $p->{type};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"type"} 10 16 26
2004  
2005 26 ▶ $p->{'x-service-type'} = delete $p->{'service-type'} if $p->{'service-type'};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"service-type"} 0 26 26
2006 26 ▶ $p->{'x-user'} = delete $p->{username} if $p->{username};
Branch: 1 branch
BranchTrueFalseTotal
if $p->{"username"} 0 26 26
2007 26 for my $n (@V4_ONLY_PARAM) {
2008 286 ▶ $p->{"x-$n"} = delete $p->{$n} if exists $p->{$n};
Branch: 1 branch
BranchTrueFalseTotal
if exists $p->{$n} 0 286 286
2009 }
2010  
2011 26 ▶ if ($name eq 'tel' && $p->{value}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($name eq 'tel' and $p->{'value'}) { } 1 25 26
elsif ($name eq 'related' and $p->{'value'}) { } 1 48 49
Condition100%3/3
$name eq "tel" and $p->{"value"}
!ll&&!rl&&r
2411
MC/DC100%2/2
$name eq "tel" and $p->{"value"}
$name eq "tel" $p->{"value"}
Truth table100%3/3
A and B
A= $name eq "tel"B= $p->{"value"}
ABresult
0X0
100
111
2012 # a vCard 3 TEL is a phone number, not a URI (RFC 2426 S3.3.1)
2013 1 delete $p->{value};
2014 1 $prop->{value} =~ s/^tel://i;
2015 }
2016 ▶ elsif ($name eq 'related' && $p->{value}) {
Condition66%2/3
$name eq "related" and $p->{"value"}
!ll&&!rl&&r
2401
MC/DC50%1/2
$name eq "related" and $p->{"value"}
$name eq "related" $p->{"value"}
Truth table66%2/3
A and B
A= $name eq "related"B= $p->{"value"}
ABresult
0X0
100
111
2017 # vCard 3 has no RELATED; RFC 2425 values are text unless they say
2018 # otherwise, so the vCard 4 VALUE=text is not needed
2019 1 delete $p->{value};
2020 }
2021  
2022 26 ▶ if ($name eq 'kind') {
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq 'kind') { } 0 26 26
elsif ($name eq 'member') { } 0 26 26
elsif ($name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/ and ($prop->{'value'} // '') =~ /^--(\d\d)(\d\d)$/) { } 1 50 51
2023 0 $prop->{name} = 'x-addressbookserver-kind';
2024 }
2025 elsif ($name eq 'member') {
2026 0 $prop->{name} = 'x-addressbookserver-member';
2027 0 ▶ $prop->{value} = "urn:uuid:$prop->{value}" unless $prop->{value} =~ /^urn:uuid:/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $prop->{"value"} =~ /^urn:uuid:/i 0 0 0
2028 }
2029 ▶ elsif ($name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/
Condition60%3/5
$name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/ and ($prop->{"value"} // "") =~ /^--(\d\d)(\d\d)$/
!ll&&!rl&&r
2501
$prop->{"value"} // ""
l!l
10
MC/DC50%1/2
$name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/ and ($prop->{"value"} // "") =~ /^--(\d\d)(\d\d)$/
$name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/ $prop->{"value"}
Truth table66%2/3
A and B
A= $name =~ /^(?:bday|anniversary|deathdate|x-abdate)$/B= $prop->{"value"}
ABresult
0X0
100
111
2030 && ($prop->{value} // '') =~ /^--(\d\d)(\d\d)$/) {
2031 1 $prop->{value} = "1604-$1-$2";
2032 }
2033 26 return $prop;
2034 }
2035  
2036 # --- Labels ---------------------------------------------------------------
2037  
2038 2 ▶sub _label_text {
Subroutine _label_text: called CC=1 SCAR=0.0
2039 2 my ($group, $label) = @_;
2040 2 return "$group.X-ABLabel:" . _escape_text($label);
2041 }
2042  
2043 # Give $line the label $new (none if undef or empty): the X-ABLabel of its
2044 # group is rewritten, added (with a new group if the line has none) or
2045 # deleted
2046 2 ▶sub _set_label {
Subroutine _set_label: called CC=13 SCAR=29.4
2047 2 my ($P, $line, $prop, $new) = @_;
2048 2 ▶ $new = undef if defined $new && (ref $new || $new eq '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $new and ref $new || $new eq "" 0 2 2
Condition33%2/6
defined $new and ref $new || $new eq ""
!ll&&!rl&&r
020
ref $new or $new eq ""
l!l&&r!l&&!r
002
MC/DC0%0/3
defined $new and ref $new || $new eq ""
defined $new ref $new $new eq ""
Truth table25%1/4
A and B or C
A= defined $newB= ref $newC= $new eq ""
ABCresult
0XX0
1000
11X1
1011
2049 2 ▶ my $group = $line->{group} // $line->{new_group};
Condition66%2/3
$line->{"group"} // $line->{"new_group"}
l!l&&r!l&&!r
101
MC/DC50%1/2
$line->{"group"} // $line->{"new_group"}
$line->{"group"} $line->{"new_group"}
Truth table66%2/3
A // B
A= $line->{"group"}B= $line->{"new_group"}
ABresult
1X1
011
000
2050 my ($label_line) = defined $group
2051 2 ▶ ? grep { $_->{name} && $_->{name} eq 'x-ablabel' } _group_lines($P, $group) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $group ? : 1 1 2
Condition66%2/3
$_->{'name'} && $_->{'name'} eq 'x-ablabel'
!ll&&!rl&&r
011
MC/DC50%1/2
$_->{'name'} && $_->{'name'} eq 'x-ablabel'
$_->{'name'} $_->{'name'} eq 'x-ablabel'
Truth table66%2/3
A && B
A= $_->{'name'}B= $_->{'name'} eq 'x-ablabel'
ABresult
0X0
100
111
2052 2 ▶ if (!defined $new) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $new) 0 2 2
2053 0 ▶ _delete_line($P, $label_line) if $label_line;
Branch: 1 branch
BranchTrueFalseTotal
if $label_line 0 0 0
2054 0 return;
2055 }
2056 2 ▶ if ($label_line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($label_line) 1 1 2
2057 1 my $pl = _parse_line($label_line->{text});
2058 0 ▶ my $head = $pl ? $pl->{name} . join('', map { $_->{raw} } @{$pl->{tokens}})
Branch: 1 branch
BranchTrueFalseTotal
$pl ? : 1 0 1
2059 : "$group.X-ABLabel";
2060 1 _set_line($P, $label_line, "$head:" . _escape_text($new));
2061 1 return;
2062 }
2063 1 ▶ unless (defined $group) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $group) 1 0 1
2064 1 $group = _alloc_group($P->{ctx});
2065 1 $line->{new_group} = $group;
2066 }
2067 1 ▶ $prop->{group} = $group if $prop;
Branch: 1 branch
BranchTrueFalseTotal
if $prop 1 0 1
2068 1 push @{$line->{after}}, _fold(_label_text($group, $new), $P->{eol});
2069 }
2070  
2071 14 ▶sub _patch_label {
Subroutine _patch_label: called CC=10 SCAR=23.4
2072 14 my ($P, $line, $prop, $old, $new) = @_;
2073 14 ▶ $_ = (defined $_ && !ref $_ && $_ ne '') ? $_ : undef for my ($o, $n) = ($old, $new);
Branch: 1 branch
BranchTrueFalseTotal
defined $_ && !ref($_) && $_ ne '' ? : 13 15 28
Condition66%4/6
defined $_ and not ref $_ and $_ ne ""
!ll&&!rl&&r
15013
defined $_ and not ref $_
!ll&&!rl&&r
15013
MC/DC33%1/3
defined $_ and not ref $_ and $_ ne ""
defined $_ not ref $_ $_ ne ""
Truth table50%2/4
A and not(B) and C
A= defined $_B= not ref $_C= $_ ne ""
ABCresult
0XX0
10X0
1100
1111
2074 14 ▶ return if !defined $o && !defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o or defined $n 7 7 14
Condition100%3/3
defined $o or defined $n
l!l&&r!l&&!r
617
MC/DC100%2/2
defined $o or defined $n
defined $o defined $n
Truth table100%3/3
A or B
A= defined $oB= defined $n
ABresult
1X1
011
000
2075 7 ▶ return if defined $o && defined $n && $o eq $n;
Branch: 1 branch
BranchTrueFalseTotal
if defined $o and defined $n and $o eq $n 6 1 7
Condition66%4/6
defined $o and defined $n and $o eq $n
!ll&&!rl&&r
106
defined $o and defined $n
!ll&&!rl&&r
106
MC/DC33%1/3
defined $o and defined $n and $o eq $n
defined $o defined $n $o eq $n
Truth table50%2/4
A and B and C
A= defined $oB= defined $nC= $o eq $n
ABCresult
0XX0
10X0
1100
1111
2076 1 _set_label($P, $line, $prop, $n);
2077 }
2078  
2079 # --- Patchers, one per top-level Card property ----------------------------
2080  
2081 62 ▶sub _same {
Subroutine _same: called CC=1 SCAR=0.0
2082 62 my ($x, $y) = @_;
2083 62 state $json = JSON->new->canonical->allow_nonref->allow_blessed->convert_blessed;
2084 62 return $json->encode([$x]) eq $json->encode([$y]);
2085 }
2086  
2087 # The maps whose entries the reader labels from an X-ABLabel
2088 20 my %LABELLED = map { $_ => 1 } qw(emails phones addresses onlineServices media
2089 links calendars schedulingAddresses cryptoKeys directories);
2090  
2091 20 my %MAP_WRITERS = (
2092 nicknames => \&_unconvert_nicknames,
2093 emails => \&_unconvert_emails,
2094 phones => \&_unconvert_phones,
2095 addresses => \&_unconvert_addresses,
2096 organizations => \&_unconvert_organizations,
2097 titles => \&_unconvert_titles,
2098 anniversaries => \&_unconvert_anniversaries,
2099 notes => \&_unconvert_notes,
2100 onlineServices => \&_unconvert_online_services,
2101 media => \&_unconvert_media,
2102 links => \&_unconvert_links,
2103 calendars => \&_unconvert_calendars,
2104 schedulingAddresses => \&_unconvert_scheduling_addresses,
2105 cryptoKeys => \&_unconvert_crypto_keys,
2106 directories => \&_unconvert_directories,
2107 preferredLanguages => \&_unconvert_preferred_languages,
2108 personalInfo => \&_unconvert_personal_info,
2109 );
2110  
2111 # Patch a map of the Card entry by entry: an unchanged entry keeps its
2112 # line; a changed one is written over its line; a removed one loses its
2113 # line; a new one is added after the lines of the same map. $gen makes
2114 # the vCard property of one entry (with no label: labels are patched
2115 # here); $hook may adjust it against the original line first.
2116 22 ▶sub _patch_map {
Subroutine _patch_map: called CC=21 SCAR=30.8
2117 22 my ($P, $key, $old, $new, $gen, $hook) = @_;
2118 22 ▶ $old = {} unless ref $old eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $old eq "HASH" 1 21 22
2119 22 ▶ $new = {} unless ref $new eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $new eq "HASH" 1 21 22
2120 22 ▶ my $sources = $P->{ctx}{sources}{$key} // {};
Condition100%2/2
$P->{"ctx"}{"sources"}{$key} // {}
l!l
211
MC/DC100%1/1
$P->{"ctx"}{"sources"}{$key} // {}
$P->{"ctx"}{"sources"}{$key}
Truth table100%2/2
A
A= $P->{"ctx"}{"sources"}{$key}
Aresult
11
00
2121 22 my $labelled = $LABELLED{$key};
2122  
2123 22 my @all = _lines_of_items($P, [ map { @$_ } values %$sources ]);
2124 22 my $anchor = $all[-1];
2125  
2126 22 my %placed;
2127 22 for my $id (sort keys %$old) {
2128 44 my @lines = _lines_of_items($P, $sources->{$id});
2129 44 ▶ next unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 44 44
2130 44 $placed{$id} = 1;
2131 44 my $entry = $new->{$id};
2132 44 ▶ if (ref $entry ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $entry ne "HASH") 3 41 44
2133 3 _delete_line($P, $_) for @lines;
2134 3 next;
2135 }
2136 41 ▶ next if _same($old->{$id}, $entry);
Branch: 1 branch
BranchTrueFalseTotal
if _same($old->{$id}, $entry) 26 15 41
2137 15 my $line = pop @lines;
2138 15 _delete_line($P, $_) for @lines;
2139 15 my ($prop) = $gen->($id, $entry);
2140 15 ▶ unless ($prop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prop) 0 15 15
2141 0 _delete_line($P, $line);
2142 0 next;
2143 }
2144 15 ▶ $hook->($P, $line, $prop, $old->{$id}, $entry) if $hook;
Branch: 1 branch
BranchTrueFalseTotal
if $hook 1 14 15
2145 15 _fit($P, $prop, $line);
2146 15 ▶ _patch_label($P, $line, $prop, $old->{$id}{label}, $entry->{label}) if $labelled;
Branch: 1 branch
BranchTrueFalseTotal
if $labelled 14 1 15
2147 15 _replace_line($P, $line, $prop);
2148 }
2149  
2150 22 for my $id (sort keys %$new) {
2151 45 ▶ next if $placed{$id};
Branch: 1 branch
BranchTrueFalseTotal
if $placed{$id} 41 4 45
2152 4 my $entry = $new->{$id};
2153 4 ▶ next unless ref $entry eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry eq "HASH" 0 4 4
2154 4 ▶ my ($prop) = $gen->($id, $entry) or next;
Branch: 1 branch
BranchTrueFalseTotal
my($prop) = &$gen($id, $entry) or next 4 0 4
2155 4 _fit($P, $prop);
2156 4 ▶ my $label = $labelled ? $entry->{label} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$labelled ? : 4 0 4
2157 4 my @texts;
2158 4 ▶ if (defined $label && !ref $label && $label ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $label and not ref $label and $label ne '') { } 1 3 4
Condition66%4/6
defined $label and not ref $label and $label ne ""
!ll&&!rl&&r
301
defined $label and not ref $label
!ll&&!rl&&r
301
MC/DC33%1/3
defined $label and not ref $label and $label ne ""
defined $label not ref $label $label ne ""
Truth table50%2/4
A and not(B) and C
A= defined $labelB= not ref $labelC= $label ne ""
ABCresult
0XX0
10X0
1100
1111
2159 1 $prop->{group} = _alloc_group($P->{ctx});
2160 1 @texts = (_prop_line($prop), _label_text($prop->{group}, $label));
2161 }
2162 else {
2163 3 @texts = (_prop_line($prop));
2164 }
2165 4 _insert($P, $anchor, @texts);
2166 }
2167 }
2168  
2169 # One entry of a map as the writer makes it, without its label
2170 22 ▶sub _map_writer {
Subroutine _map_writer: called CC=1 SCAR=0.0
2171 22 my ($P, $key) = @_;
2172 22 my $ctx = $P->{ctx};
2173 19 ▶ return sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2174 19 my ($id, $entry) = @_;
2175 19 my @out;
2176 19 local $ctx->{no_labels} = 1;
2177 19 $MAP_WRITERS{$key}->(\@out, { $key => { $id => $entry } }, $ctx);
2178 19 return @out;
2179 22 };
2180 }
2181  
2182 # An Apple address keeps its country code, coordinates and time zone in
2183 # lines of its group (X-ABADR, GEO, TZ) rather than in parameters: those
2184 # lines are updated instead
2185 1 ▶sub _address_siblings {
Subroutine _address_siblings: called CC=14 SCAR=31.0
2186 1 my ($P, $line, $prop, $old, $new) = @_;
2187 1 ▶ my $group = $line->{group} // return;
Condition50%1/2
$line->{"group"} // (return)
l!l
10
MC/DC0%0/1
$line->{"group"} // (return)
$line->{"group"}
Truth table50%1/2
A
A= $line->{"group"}
Aresult
11
00
2188 1 ▶ my $item = $line->{item} // {};
Condition50%1/2
$line->{"item"} // {}
l!l
10
MC/DC0%0/1
$line->{"item"} // {}
$line->{"item"}
Truth table50%1/2
A
A= $line->{"item"}
Aresult
11
00
2189 1 for my $spec ([cc => 'x-abadr', 'countryCode'], [geo => 'geo', 'coordinates'],
2190 [tz => 'tz', 'timeZone']) {
2191 3 my ($param, $sib_name, $field) = @$spec;
2192 3 ▶ next if defined $item->{params}{$param};
Branch: 1 branch
BranchTrueFalseTotal
if defined $item->{"params"}{$param} 0 3 3
2193 3 ▶ my ($sib) = grep { $_->{name} && $_->{name} eq $sib_name } _group_lines($P, $group);
Condition66%2/3
$_->{'name'} && $_->{'name'} eq $sib_name
!ll&&!rl&&r
051
MC/DC50%1/2
$_->{'name'} && $_->{'name'} eq $sib_name
$_->{'name'} $_->{'name'} eq $sib_name
Truth table66%2/3
A && B
A= $_->{'name'}B= $_->{'name'} eq $sib_name
ABresult
0X0
100
111
2194 3 ▶ next unless $sib;
Branch: 1 branch
BranchTrueFalseTotal
unless $sib 2 1 3
2195 1 delete $prop->{params}{$param};
2196 1 ▶ next if _same($old->{$field}, $new->{$field});
Branch: 1 branch
BranchTrueFalseTotal
if _same($old->{$field}, $new->{$field}) 0 1 1
2197 1 my $val = $new->{$field};
2198 1 ▶ if (!defined $val || ref $val || $val eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $val or ref $val or $val eq "") 0 1 1
Condition33%2/6
not defined $val or ref $val or $val eq ""
l!l&&r!l&&!r
001
not defined $val or ref $val
l!l&&r!l&&!r
001
MC/DC0%0/3
not defined $val or ref $val or $val eq ""
not defined $val ref $val $val eq ""
Truth table25%1/4
not(A) or B or C
A= not defined $valB= ref $valC= $val eq ""
ABCresult
1XX1
01X1
0011
0000
2199 0 _delete_line($P, $sib);
2200 0 next;
2201 }
2202 1 ▶ my $pl = _parse_line($sib->{text}) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $pl = _parse_line($sib->{"text"}) or next 1 0 1
2203 0 my $head = $pl->{name} . join('', map { $_->{raw} } @{$pl->{tokens}});
2204 1 ▶ my $text = $sib_name eq 'geo' && $val =~ /^[a-z][a-z0-9+.-]*:/i ? _uri_text($val) : _escape_text($val);
Branch: 1 branch
BranchTrueFalseTotal
$sib_name eq 'geo' && $val =~ /^[a-z][a-z0-9+.-]*:/i ? : 0 1 1
Condition33%1/3
$sib_name eq "geo" and $val =~ /^[a-z][a-z0-9+.-]*:/i
!ll&&!rl&&r
100
MC/DC0%0/2
$sib_name eq "geo" and $val =~ /^[a-z][a-z0-9+.-]*:/i
$sib_name eq "geo" $val =~ /^[a-z][a-z0-9+.-]*:/i
Truth table33%1/3
A and B
A= $sib_name eq "geo"B= $val =~ /^[a-z][a-z0-9+.-]*:/i
ABresult
0X0
100
111
2205 1 _set_line($P, $sib, "$head:$text");
2206 }
2207 }
2208  
2209 # One vCard property, by name: the first line is written over, or every
2210 # line deleted, or one added before END:VCARD. With $all, the other lines
2211 # of that name go too (the reader merges them all).
2212 9 ▶sub _patch_single {
Subroutine _patch_single: called CC=6 SCAR=19.1
2213 9 my ($P, $names, $prop, $all) = @_;
2214 9 my @lines = _lines_named($P, @$names);
2215 9 ▶ unless ($prop) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($prop) 0 9 9
2216 0 _delete_line($P, $_) for @lines;
2217 0 return;
2218 }
2219 9 my $first = shift @lines;
2220 9 _fit($P, $prop, $first);
2221 9 ▶ if ($first) {
Branch: 1 branch
BranchTrueFalseTotal
if ($first) { } 8 1 9
2222 8 _replace_line($P, $first, $prop);
2223 0 ▶ if ($all) { _delete_line($P, $_) for @lines }
Branch: 1 branch
BranchTrueFalseTotal
if ($all) 0 8 8
2224 }
2225 else {
2226 1 _insert($P, undef, _prop_line($prop));
2227 }
2228 }
2229  
2230 8 ▶sub _patch_name {
Subroutine _patch_name: called CC=8 SCAR=23.9
2231 8 my ($P, $key, $old_card, $new_card) = @_;
2232 8 ▶ my $o = ref $old_card->{name} eq 'HASH' ? $old_card->{name} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $old_card->{'name'} eq 'HASH' ? : 8 0 8
2233 8 ▶ my $n = ref $new_card->{name} eq 'HASH' ? $new_card->{name} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $new_card->{'name'} eq 'HASH' ? : 8 0 8
2234  
2235 8 my @gen;
2236 8 _unconvert_name(\@gen, { name => $n }, $P->{ctx});
2237 8 my ($fn) = grep { $_->{name} eq 'fn' } @gen;
2238 8 my ($nn) = grep { $_->{name} eq 'n' } @gen;
2239  
2240 # FN is the full name, or made from the components when there is none
2241 _patch_single($P, ['fn'], $fn)
2242 if !_same($o->{full}, $n->{full})
2243 8 ▶ || (!defined $n->{full} && !_same($o->{components}, $n->{components}));
Branch: 1 branch
BranchTrueFalseTotal
unless _same($o->{"full"}, $n->{"full"}) and defined $n->{"full"} || _same($o->{"components"}, $n->{"components"}) 8 0 8
Condition16%1/6
_same($o->{"full"}, $n->{"full"}) and defined $n->{"full"} || _same($o->{"components"}, $n->{"components"})
!ll&&!rl&&r
800
defined $n->{"full"} or _same($o->{"components"}, $n->{"components"})
l!l&&r!l&&!r
000
MC/DC0%0/3
_same($o->{"full"}, $n->{"full"}) and defined $n->{"full"} || _same($o->{"components"}, $n->{"components"})
_same($o->{"full"}, $n->{"full"}) defined $n->{"full"} _same($o->{"components"}, $n->{"components"})
Truth table25%1/4
A and B or C
A= _same($o->{"full"}, $n->{"full"})B= defined $n->{"full"}C= _same($o->{"components"}, $n->{"components"})
ABCresult
0XX0
1000
11X1
1011
2244 _patch_single($P, ['n'], $nn)
2245 8 ▶ unless _same([$o->{components}, $o->{sortAs}], [$n->{components}, $n->{sortAs}]);
Branch: 1 branch
BranchTrueFalseTotal
unless _same([$o->{"components"}, $o->{"sortAs"}], [$n->{"components"}, $n->{"sortAs"}]) 0 8 8
2246  
2247 # TODO(registry): phoneticComponents are read from X-PHONETIC-*-NAME but
2248 # the writer has no form for them yet (_unconvert_name), so a change to
2249 # them is not patched; the X-PHONETIC-* lines are left as they are.
2250 8 ▶ _patch_single($P, [qw(x-phonetic-first-name x-phonetic-middle-name x-phonetic-last-name)], undef)
Branch: 1 branch
BranchTrueFalseTotal
unless %$n 0 8 8
2251 unless %$n;
2252 }
2253  
2254 0 ▶sub _patch_keywords {
Subroutine _patch_keywords: not called CC=1 SCAR=6.9
2255 0 my ($P, $key, $old_card, $new_card) = @_;
2256 0 my @gen;
2257 0 _unconvert_keywords(\@gen, $new_card, $P->{ctx});
2258 0 _patch_single($P, ['categories'], $gen[0], 1);
2259 }
2260  
2261 2 ▶sub _patch_members {
Subroutine _patch_members: called CC=12 SCAR=26.7
2262 2 my ($P, $key, $old_card, $new_card) = @_;
2263 2 ▶ my $o = ref $old_card->{members} eq 'HASH' ? $old_card->{members} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $old_card->{'members'} eq 'HASH' ? : 2 0 2
2264 2 ▶ my $n = ref $new_card->{members} eq 'HASH' ? $new_card->{members} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $new_card->{'members'} eq 'HASH' ? : 2 0 2
2265 2 ▶ my $sources = $P->{ctx}{sources}{members} // {};
Condition50%1/2
$P->{"ctx"}{"sources"}{"members"} // {}
l!l
20
MC/DC0%0/1
$P->{"ctx"}{"sources"}{"members"} // {}
$P->{"ctx"}{"sources"}{"members"}
Truth table50%1/2
A
A= $P->{"ctx"}{"sources"}{"members"}
Aresult
11
00
2266 2 ▶ my $apple = $P->{v3} || _lines_named($P, 'x-addressbookserver-member');
Condition33%1/3
$P->{'v3'} || _lines_named($P, 'x-addressbookserver-member')
l!l&&r!l&&!r
200
MC/DC0%0/2
$P->{'v3'} || _lines_named($P, 'x-addressbookserver-member')
$P->{'v3'} _lines_named($P, 'x-addressbookserver-member')
Truth table33%1/3
A || B
A= $P->{'v3'}B= _lines_named($P, 'x-addressbookserver-member')
ABresult
1X1
011
000
2267 2 my @all = _lines_named($P, qw(member x-addressbookserver-member));
2268 2 my $anchor = $all[-1];
2269  
2270 2 for my $m (sort keys %$o) {
2271 5 ▶ next if $n->{$m};
Branch: 1 branch
BranchTrueFalseTotal
if $n->{$m} 5 0 5
2272 0 _delete_line($P, $_) for _lines_of_items($P, $sources->{$m});
2273 }
2274 2 for my $m (sort keys %$n) {
2275 7 ▶ next if $o->{$m} || !$n->{$m};
Branch: 1 branch
BranchTrueFalseTotal
if $o->{$m} or not $n->{$m} 5 2 7
Condition66%2/3
$o->{$m} or not $n->{$m}
l!l&&r!l&&!r
502
MC/DC50%1/2
$o->{$m} or not $n->{$m}
$o->{$m} not $n->{$m}
Truth table66%2/3
A or not(B)
A= $o->{$m}B= not $n->{$m}
ABresult
1X1
011
000
2276 2 my @gen;
2277 2 ▶ my $writer = $apple ? \&_unconvert_members_apple : \&_unconvert_members;
Branch: 1 branch
BranchTrueFalseTotal
$apple ? : 2 0 2
2278 2 $writer->(\@gen, { members => { $m => JSON::true } }, $P->{ctx});
2279 2 _insert($P, $anchor, map { _prop_line(_fit($P, $_)) } @gen);
2280 }
2281 }
2282  
2283 # The X-ABLabel that says a relation to Apple (the reader maps it back)
2284 20 my %RELATION_LABEL = (spouse => 'Spouse', parent => 'Parent', child => 'Child',
2285 friend => 'Friend');
2286  
2287 2 ▶sub _patch_related {
Subroutine _patch_related: called CC=18 SCAR=31.8
2288 2 my ($P, $key, $old_card, $new_card) = @_;
2289 2 ▶ my $o = ref $old_card->{relatedTo} eq 'HASH' ? $old_card->{relatedTo} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $old_card->{'relatedTo'} eq 'HASH' ? : 2 0 2
2290 2 ▶ my $n = ref $new_card->{relatedTo} eq 'HASH' ? $new_card->{relatedTo} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $new_card->{'relatedTo'} eq 'HASH' ? : 2 0 2
2291 2 ▶ my $sources = $P->{ctx}{sources}{relatedTo} // {};
Condition50%1/2
$P->{"ctx"}{"sources"}{"relatedTo"} // {}
l!l
20
MC/DC0%0/1
$P->{"ctx"}{"sources"}{"relatedTo"} // {}
$P->{"ctx"}{"sources"}{"relatedTo"}
Truth table50%1/2
A
A= $P->{"ctx"}{"sources"}{"relatedTo"}
Aresult
11
00
2292 2 my @all = _lines_named($P, qw(related x-abrelatednames));
2293 2 my $anchor = $all[-1];
2294 1 ▶ my $gen = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
2295 1 my ($k, $rel) = @_;
2296 1 my @out;
2297 1 _unconvert_related(\@out, { relatedTo => { $k => $rel } }, $P->{ctx});
2298 1 return $out[0];
2299 2 };
2300  
2301 2 my %placed;
2302 2 for my $k (sort keys %$o) {
2303 4 my @lines = _lines_of_items($P, $sources->{$k});
2304 4 ▶ next unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 4 4
2305 4 $placed{$k} = 1;
2306 4 ▶ if (ref $n->{$k} ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $n->{$k} ne "HASH") 0 4 4
2307 0 _delete_line($P, $_) for @lines;
2308 0 next;
2309 }
2310 4 ▶ next if _same($o->{$k}, $n->{$k});
Branch: 1 branch
BranchTrueFalseTotal
if _same($o->{$k}, $n->{$k}) 3 1 4
2311 1 my $line = pop @lines;
2312 1 _delete_line($P, $_) for @lines;
2313 1 ▶ if ($line->{name} eq 'x-abrelatednames') {
Branch: 1 branch
BranchTrueFalseTotal
if ($line->{"name"} eq "x-abrelatednames") 1 0 1
2314 # Apple says the relation in the label
2315 1 ▶ my @rel = grep { $n->{$k}{relation}{$_} } sort keys %{$n->{$k}{relation} // {}};
Condition50%1/2
$n->{$k}{"relation"} // {}
l!l
10
MC/DC0%0/1
$n->{$k}{"relation"} // {}
$n->{$k}{"relation"}
Truth table50%1/2
A
A= $n->{$k}{"relation"}
Aresult
11
00
2316 0 ▶ if (!@rel) { _set_label($P, $line, undef, undef); next }
Branch: 1 branch
BranchTrueFalseTotal
unless (@rel) 0 1 1
2317 1 ▶ if (@rel == 1 && $RELATION_LABEL{$rel[0]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rel == 1 and $RELATION_LABEL{$rel[0]}) 1 0 1
Condition33%1/3
@rel == 1 and $RELATION_LABEL{$rel[0]}
!ll&&!rl&&r
001
MC/DC0%0/2
@rel == 1 and $RELATION_LABEL{$rel[0]}
@rel == 1 $RELATION_LABEL{$rel[0]}
Truth table33%1/3
A and B
A= @rel == 1B= $RELATION_LABEL{$rel[0]}
ABresult
0X0
100
111
2318 1 _set_label($P, $line, undef, "_\$!<$RELATION_LABEL{$rel[0]}>!\$_");
2319 1 next;
2320 }
2321 }
2322 0 _replace_line($P, $line, _fit($P, $gen->($k, $n->{$k}), $line));
2323 }
2324 2 for my $k (sort keys %$n) {
2325 5 ▶ next if $placed{$k} || ref $n->{$k} ne 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if $placed{$k} or ref $n->{$k} ne "HASH" 4 1 5
Condition66%2/3
$placed{$k} or ref $n->{$k} ne "HASH"
l!l&&r!l&&!r
401
MC/DC50%1/2
$placed{$k} or ref $n->{$k} ne "HASH"
$placed{$k} ref $n->{$k} ne "HASH"
Truth table66%2/3
A or B
A= $placed{$k}B= ref $n->{$k} ne "HASH"
ABresult
1X1
011
000
2326 1 _insert($P, $anchor, _prop_line(_fit($P, $gen->($k, $n->{$k}))));
2327 }
2328 }
2329  
2330 0 ▶sub _patch_speak_to_as {
Subroutine _patch_speak_to_as: not called CC=7 SCAR=40.3
2331 0 my ($P, $key, $old_card, $new_card) = @_;
2332 0 ▶ my $o = ref $old_card->{speakToAs} eq 'HASH' ? $old_card->{speakToAs} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $old_card->{'speakToAs'} eq 'HASH' ? : 0 0 0
2333 0 ▶ my $n = ref $new_card->{speakToAs} eq 'HASH' ? $new_card->{speakToAs} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $new_card->{'speakToAs'} eq 'HASH' ? : 0 0 0
2334 0 ▶ unless (_same($o->{grammaticalGender}, $n->{grammaticalGender})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_same($o->{"grammaticalGender"}, $n->{"grammaticalGender"})) 0 0 0
2335 0 my $gg = $n->{grammaticalGender};
2336 0 ▶ _patch_single($P, ['gramgender'],
Branch: 1 branch
BranchTrueFalseTotal
defined $gg && !ref($gg) && $gg ne '' ? : 0 0 0
Condition0%0/6
defined $gg and not ref $gg and $gg ne ""
!ll&&!rl&&r
000
defined $gg and not ref $gg
!ll&&!rl&&r
000
MC/DC0%0/3
defined $gg and not ref $gg and $gg ne ""
defined $gg not ref $gg $gg ne ""
Truth table0%0/4
A and not(B) and C
A= defined $ggB= not ref $ggC= $gg ne ""
ABCresult
0XX0
10X0
1100
1111
2337 defined $gg && !ref $gg && $gg ne '' ? { name => 'gramgender', value => $gg, params => {} } : undef);
2338 }
2339 0 my $ctx = $P->{ctx};
2340 0 ▶ _patch_map($P, 'speakToAs', $o->{pronouns}, $n->{pronouns}, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
2341 0 my ($id, $entry) = @_;
2342 0 my @out;
2343 0 _unconvert_speak_to_as(\@out, { speakToAs => { pronouns => { $id => $entry } } }, $ctx);
2344 0 return @out;
2345 0 });
2346 }
2347  
2348 100 ▶sub _scalar_patcher {
Subroutine _scalar_patcher: called CC=1 SCAR=0.6
2349 100 my ($vname) = @_;
2350 1 ▶ return sub {
Subroutine __ANON__: called CC=4 SCAR=17.1
2351 1 my ($P, $key, $old_card, $new_card) = @_;
2352 1 my $v = $new_card->{$key};
2353 1 ▶ _patch_single($P, [$vname],
Branch: 1 branch
BranchTrueFalseTotal
defined $v && !ref($v) && $v ne '' ? : 1 0 1
Condition33%2/6
defined $v and not ref $v and $v ne ""
!ll&&!rl&&r
001
defined $v and not ref $v
!ll&&!rl&&r
001
MC/DC0%0/3
defined $v and not ref $v and $v ne ""
defined $v not ref $v $v ne ""
Truth table25%1/4
A and not(B) and C
A= defined $vB= not ref $vC= $v ne ""
ABCresult
0XX0
10X0
1100
1111
2354 defined $v && !ref $v && $v ne '' ? { name => $vname, value => $v, params => {} } : undef);
2355 100 };
2356 }
2357  
2358 0 ▶sub _patch_kind {
Subroutine _patch_kind: not called CC=11 SCAR=48.8
2359 0 my ($P, $key, $old_card, $new_card) = @_;
2360 0 my $v = $new_card->{kind};
2361 0 my @names = qw(kind x-addressbookserver-kind);
2362 0 my ($line) = _lines_named($P, @names);
2363 0 ▶ $v = undef if defined $v && (ref $v || $v eq '');
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and ref $v || $v eq "" 0 0 0
Condition0%0/6
defined $v and ref $v || $v eq ""
!ll&&!rl&&r
000
ref $v or $v eq ""
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $v and ref $v || $v eq ""
defined $v ref $v $v eq ""
Truth table0%0/4
A and B or C
A= defined $vB= ref $vC= $v eq ""
ABCresult
0XX0
1000
11X1
1011
2364 # Apple writes no kind for an individual
2365 0 ▶ $v = undef if defined $v && $v eq 'individual' && ($P->{v3} || ($line && $line->{name} ne 'kind'));
Branch: 1 branch
BranchTrueFalseTotal
if defined $v and $v eq "individual" and $P->{"v3"} || $line && $line->{"name"} ne "kind" 0 0 0
Condition0%0/12
defined $v and $v eq "individual" and $P->{"v3"} || $line && $line->{"name"} ne "kind"
!ll&&!rl&&r
000
defined $v and $v eq "individual"
!ll&&!rl&&r
000
$P->{"v3"} or $line and $line->{"name"} ne "kind"
l!l&&r!l&&!r
000
$line and $line->{"name"} ne "kind"
!ll&&!rl&&r
000
MC/DC0%0/5
defined $v and $v eq "individual" and $P->{"v3"} || $line && $line->{"name"} ne "kind"
defined $v $v eq "individual" $P->{"v3"} $line $line->{"name"} ne "kind"
Truth table0%0/6
A and B and C or D and E
A= defined $vB= $v eq "individual"C= $P->{"v3"}D= $lineE= $line->{"name"} ne "kind"
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
2366 0 ▶ my $name = $line ? $line->{name} : 'kind';
Branch: 1 branch
BranchTrueFalseTotal
$line ? : 0 0 0
2367 0 ▶ _patch_single($P, \@names, defined $v ? { name => $name, value => $v, params => {} } : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $v ? : 0 0 0
2368 }
2369  
2370 %PATCHERS = (
2371 name => \&_patch_name,
2372 keywords => \&_patch_keywords,
2373 members => \&_patch_members,
2374 relatedTo => \&_patch_related,
2375 speakToAs => \&_patch_speak_to_as,
2376 kind => \&_patch_kind,
2377 uid => _scalar_patcher('uid'),
2378 prodId => _scalar_patcher('prodid'),
2379 updated => _scalar_patcher('rev'),
2380 created => _scalar_patcher('created'),
2381 language => _scalar_patcher('language'),
2382 (map {
2383 20 my $key = $_;
2384 22 ▶ ($key => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
2385 22 my ($P, $k, $old_card, $new_card) = @_;
2386 22 ▶ _patch_map($P, $key, $old_card->{$key}, $new_card->{$key}, _map_writer($P, $key),
Branch: 1 branch
BranchTrueFalseTotal
$key eq 'addresses' ? : 2 20 22
2387 $key eq 'addresses' ? \&_address_siblings : undef);
2388 340 });
2389 } keys %MAP_WRITERS),
2390 );
2391  
2392 ###############################################################################
2393 # WRITING: content lines, byte for byte the same for the same input
2394 ###############################################################################
2395  
2396 # Text::VCardFast's hash2vcard writes the parameters of a property in
2397 # Perl's hash order, so the same Card gave different bytes on every run
2398 # (and a CardDAV server a new ETag for every unchanged save). The writer
2399 # here keeps its property order but orders everything it writes.
2400  
2401 # properties in this order first (Text::VCardFast's), then by name
2402 20 my @PROP_ORDER = qw(version fn n nickname lang gender org title role bday
2403 anniversary email tel adr url impp);
2404 20 my %PROP_ORDER = map { $PROP_ORDER[$_] => $_ + 1 } 0..$#PROP_ORDER;
2405  
2406 # parameters in this order first, then by name
2407 20 my @PARAM_ORDER = qw(prop-id value type pref);
2408 20 my %PARAM_ORDER = map { $PARAM_ORDER[$_] => $_ + 1 } 0..$#PARAM_ORDER;
2409  
2410 # A whole vCard from a list of properties; items of one group are written
2411 # together, after the first of them
2412 361 ▶sub _write_vcard {
Subroutine _write_vcard: called CC=14 SCAR=26.5
2413 361 my ($props, $eol) = @_;
2414 361 ▶ $eol //= "\n";
Condition50%1/2
$eol //= "\n"
l!l
0361
MC/DC0%0/1
$eol //= "\n"
$eol
Truth table50%1/2
A
A= $eol
Aresult
11
00
2415  
2416 361 my %by_name;
2417 361 push @{$by_name{lc $_->{name}}}, $_ for @$props;
2418 my @names = sort {
2419 361 ▶ ($PROP_ORDER{$a} // 1000) <=> ($PROP_ORDER{$b} // 1000) || $a cmp $b
Condition85%6/7
($PROP_ORDER{$a} // 1000) <=> ($PROP_ORDER{$b} // 1000) || $a cmp $b
l!l&&r!l&&!r
1075856110
$PROP_ORDER{$a} // 1000
l!l
89847385
$PROP_ORDER{$b} // 1000
l!l
83418028
MC/DC0%0/2
($PROP_ORDER{$a} // 1000) <=> ($PROP_ORDER{$b} // 1000) || $a cmp $b
$PROP_ORDER{$a} $a cmp $b
MC/DC100%1/1
$PROP_ORDER{$b} // 1000
$PROP_ORDER{$b}
Truth table66%2/3
A || B
A= $PROP_ORDER{$a}B= $a cmp $b
ABresult
1X1
011
000
Truth table100%2/2
A
A= $PROP_ORDER{$b}
Aresult
11
00
2420 } keys %by_name;
2421  
2422 361 my %groups;
2423 361 for my $prop (@$props) {
2424 6766 ▶ push @{$groups{lc $prop->{group}}}, $prop if defined $prop->{group} && $prop->{group} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $prop->{"group"} and $prop->{"group"} ne "" 332 6434 6766
Condition66%2/3
defined $prop->{"group"} and $prop->{"group"} ne ""
!ll&&!rl&&r
64340332
MC/DC50%1/2
defined $prop->{"group"} and $prop->{"group"} ne ""
defined $prop->{"group"} $prop->{"group"} ne ""
Truth table66%2/3
A and B
A= defined $prop->{"group"}B= $prop->{"group"} ne ""
ABresult
0X0
100
111
2425 }
2426  
2427 361 my (%done, @out);
2428 361 for my $name (@names) {
2429 5267 for my $prop (@{$by_name{$name}}) {
2430 6766 ▶ next if $done{$prop}++;
Branch: 1 branch
BranchTrueFalseTotal
if $done{$prop}++ 166 6600 6766
2431 6600 push @out, $prop;
2432 6600 ▶ if (defined $prop->{group} && $prop->{group} ne '') {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prop->{"group"} and $prop->{"group"} ne "") 166 6434 6600
Condition66%2/3
defined $prop->{"group"} and $prop->{"group"} ne ""
!ll&&!rl&&r
64340166
MC/DC50%1/2
defined $prop->{"group"} and $prop->{"group"} ne ""
defined $prop->{"group"} $prop->{"group"} ne ""
Truth table66%2/3
A and B
A= defined $prop->{"group"}B= $prop->{"group"} ne ""
ABresult
0X0
100
111
2433 166 push @out, grep { !$done{$_}++ } @{$groups{lc $prop->{group}}};
2434 }
2435 }
2436 }
2437  
2438 361 return join '', "BEGIN:VCARD$eol", (map { _fold(_prop_line($_), $eol) } @out), "END:VCARD$eol";
2439 }
2440  
2441 # One property as an unfolded content line, cleaned of control characters
2442 6772 ▶sub _prop_line {
Subroutine _prop_line: called CC=3 SCAR=11.0
2443 6772 my ($prop) = @_;
2444 6772 my $line = '';
2445 6772 ▶ $line .= "$prop->{group}." if defined $prop->{group} && $prop->{group} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $prop->{"group"} and $prop->{"group"} ne "" 333 6439 6772
Condition66%2/3
defined $prop->{"group"} and $prop->{"group"} ne ""
!ll&&!rl&&r
64390333
MC/DC50%1/2
defined $prop->{"group"} and $prop->{"group"} ne ""
defined $prop->{"group"} $prop->{"group"} ne ""
Truth table66%2/3
A and B
A= defined $prop->{"group"}B= $prop->{"group"} ne ""
ABresult
0X0
100
111
2446 6772 $line .= uc $prop->{name};
2447 6772 $line .= _params_text($prop->{params});
2448 6772 return $line . ':' . _value_text($prop);
2449 }
2450  
2451 6772 ▶sub _params_text {
Subroutine _params_text: called CC=7 SCAR=19.6
2452 6772 my ($params) = @_;
2453 6772 ▶ return '' unless $params && %$params;
Branch: 1 branch
BranchTrueFalseTotal
unless $params and %$params 2275 4497 6772
Condition100%3/3
$params and %$params
!ll&&!rl&&r
118310924497
MC/DC100%2/2
$params and %$params
$params %$params
Truth table100%3/3
A and B
A= $paramsB= %$params
ABresult
0X0
100
111
2454 4497 my $text = '';
2455 4497 for my $name (_param_order($params)) {
2456 8462 my $vals = $params->{$name};
2457 8462 ▶ for my $val (ref $vals eq 'ARRAY' ? @$vals : ($vals)) {
Branch: 1 branch
BranchTrueFalseTotal
ref $vals eq 'ARRAY' ? : 8462 0 8462
2458 8831 ▶ next unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 8831 8831
2459 8831 $text .= ';' . uc($name) . '=' . _param_value($val);
2460 }
2461 }
2462 4497 return $text;
2463 }
2464  
2465 4520 ▶sub _param_order {
Subroutine _param_order: called CC=4 SCAR=13.9
2466 4520 my ($params) = @_;
2467 return sort {
2468 4520 ▶ ($PARAM_ORDER{$a} // 1000) <=> ($PARAM_ORDER{$b} // 1000) || $a cmp $b
Condition85%6/7
($PARAM_ORDER{$a} // 1000) <=> ($PARAM_ORDER{$b} // 1000) || $a cmp $b
l!l&&r!l&&!r
45005750
$PARAM_ORDER{$a} // 1000
l!l
38031272
$PARAM_ORDER{$b} // 1000
l!l
35971478
MC/DC0%0/2
($PARAM_ORDER{$a} // 1000) <=> ($PARAM_ORDER{$b} // 1000) || $a cmp $b
$PARAM_ORDER{$a} $a cmp $b
MC/DC100%1/1
$PARAM_ORDER{$b} // 1000
$PARAM_ORDER{$b}
Truth table66%2/3
A || B
A= $PARAM_ORDER{$a}B= $a cmp $b
ABresult
1X1
011
000
Truth table100%2/2
A
A= $PARAM_ORDER{$b}
Aresult
11
00
2469 } keys %$params;
2470 }
2471  
2472 # RFC 6868 caret encoding; quoted when it holds a separator or space
2473 8833 ▶sub _param_value {
Subroutine _param_value: called CC=4 SCAR=14.3
2474 8833 my ($val) = @_;
2475 8833 $val = _clean_vcard_text($val);
2476 # a backslash has no meaning in a parameter (RFC 6350, 6868), but
2477 # Text::VCardFast reads \; \: \" as escapes: "a \" swallowed what
2478 # followed, and the card no longer parsed
2479 8833 $val =~ s/\\//g;
2480 8833 ▶ $val =~ s/([\n^"])/$1 eq "\n" ? '^n' : $1 eq '^' ? '^^' : "^'"/ge;
Branch: 2 branches
BranchTrueFalseTotal
$1 eq "\n" ? : 41 0 41
$1 eq '^' ? : 0 0 0
2481 8833 ▶ $val = qq{"$val"} if $val =~ /[;:,\s]/;
Branch: 1 branch
BranchTrueFalseTotal
if $val =~ /[;:,\s]/ 212 8621 8833
2482 8833 return $val;
2483 }
2484  
2485 # RFC 6350 S3.4: TEXT escapes BACKSLASH, COMMA, SEMICOLON and NEWLINE
2486 7672 ▶sub _escape_text {
Subroutine _escape_text: called CC=3 SCAR=11.0
2487 7672 my ($val) = @_;
2488 7672 ▶ die "Text::JSContact: a vCard value must be a string, not $val\n" if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 1 7671 7672
2489 7671 ▶ $val = _clean_vcard_text($val // '');
Condition50%1/2
$val // ""
l!l
76710
MC/DC0%0/1
$val // ""
$val
Truth table50%1/2
A
A= $val
Aresult
11
00
2490 7671 $val =~ s/([\\,;])/\\$1/g;
2491 7671 $val =~ s/\n/\\n/g;
2492 7671 return $val;
2493 }
2494  
2495 # A structured value is a list of components, each a string or a list
2496 # of strings (written comma-separated)
2497 6795 ▶sub _value_text {
Subroutine _value_text: called CC=4 SCAR=13.9
2498 6795 my ($prop) = @_;
2499 6795 ▶ if (my $values = $prop->{values}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $values = $prop->{"values"}) 834 5961 6795
2500 return join ';', map {
2501 834 ▶ ref $_ eq 'ARRAY' ? join(',', map { _escape_text($_) } @$_) : _escape_text($_)
Branch: 1 branch
BranchTrueFalseTotal
ref $_ eq 'ARRAY' ? : 2266 950 3216
2502 } @$values;
2503 }
2504 5961 ▶ return _uri_text($prop->{value}) if _is_uri_value($prop);
Branch: 1 branch
BranchTrueFalseTotal
if _is_uri_value($prop) 1904 4057 5961
2505 4057 return _escape_text($prop->{value});
2506 }
2507  
2508 # Fold a content line at 75 octets (RFC 6350 S3.2), never inside a
2509 # character or between a backslash and the character it escapes
2510 6797 ▶sub _fold {
Subroutine _fold: called CC=13 SCAR=25.7
2511 6797 my ($line, $eol) = @_;
2512 6797 my @out;
2513 6797 ▶ if ($line !~ /[^\x00-\x7f]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $line =~ /[^\x00-\x7f]/) { } 6521 276 6797
2514 # one character, one octet
2515 6521 my $pos = 0;
2516 6521 ▶ while ($pos < length $line) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos < length $line) 7166 6521 13687
2517 7166 ▶ my $max = @out ? 74 : 75;
Branch: 1 branch
BranchTrueFalseTotal
@out ? : 645 6521 7166
2518 7166 my $chunk = substr($line, $pos, $max);
2519 # an odd run of backslashes at the end splits an escape
2520 7166 ▶ chop $chunk if length $chunk == $max && $pos + $max < length $line
Branch: 1 branch
BranchTrueFalseTotal
if length $chunk == $max and $pos + $max < length $line and $chunk =~ /(?<!\\)(?:\\\\)*\\\z/ 0 7166 7166
Condition83%5/6
length $chunk == $max and $pos + $max < length $line and $chunk =~ /(?<!\\)(?:\\\\)*\\\z/
!ll&&!rl&&r
65216450
length $chunk == $max and $pos + $max < length $line
!ll&&!rl&&r
642497645
MC/DC0%0/3
length $chunk == $max and $pos + $max < length $line and $chunk =~ /(?<!\\)(?:\\\\)*\\\z/
length $chunk == $max $pos + $max < length $line $chunk =~ /(?<!\\)(?:\\\\)*\\\z/
Truth table75%3/4
A and B and C
A= length $chunk == $maxB= $pos + $max < length $lineC= $chunk =~ /(?<!\\)(?:\\\\)*\\\z/
ABCresult
0XX0
10X0
1100
1111
2521 && $chunk =~ /(?<!\\)(?:\\\\)*\\\z/;
2522 7166 push @out, $chunk;
2523 7166 $pos += length $chunk;
2524 }
2525 }
2526 else {
2527 276 my $cur = '';
2528 276 my $len = 0;
2529 276 for my $ch ($line =~ /(\\.|.)/sg) {
2530 10280 ▶ my $l = $ch =~ /[^\x00-\x7f]/ ? length(encode_utf8($ch)) : length $ch;
Branch: 1 branch
BranchTrueFalseTotal
$ch =~ /[^\x00-\x7f]/ ? : 719 9561 10280
2531 10280 ▶ my $max = @out ? 74 : 75;
Branch: 1 branch
BranchTrueFalseTotal
@out ? : 364 9916 10280
2532 10280 ▶ if ($len + $l > $max && $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len + $l > $max and $len) 33 10247 10280
Condition66%2/3
$len + $l > $max and $len
!ll&&!rl&&r
10247033
MC/DC50%1/2
$len + $l > $max and $len
$len + $l > $max $len
Truth table66%2/3
A and B
A= $len + $l > $maxB= $len
ABresult
0X0
100
111
2533 33 push @out, $cur;
2534 33 $cur = '';
2535 33 $len = 0;
2536 }
2537 10280 $cur .= $ch;
2538 10280 $len += $l;
2539 }
2540 276 push @out, $cur;
2541 }
2542 6797 ▶ push @out, '' unless @out;
Branch: 1 branch
BranchTrueFalseTotal
unless @out 0 6797 6797
2543 6797 return join("$eol ", @out) . $eol;
2544 }
2545  
2546 # RFC 6350 S3.4 escapes COMMA, SEMICOLON, BACKSLASH and NEWLINE in TEXT
2547 # values only; a URI value is written as it is. Escaping every value
2548 # turns "data:image/png;base64,..." into "data:image/png\;base64\,...",
2549 # no longer a URI (Cyrus then stores it as TEXT with a nonsense media
2550 # type). These are the properties whose default value type is URI
2551 # (RFC 6350 S6, RFC 6715, RFC 8605, RFC 9554), plus Apple's member
2552 # property.
2553 20 my %URI_DEFAULT = map { $_ => 1 } qw(
2554 source photo impp geo logo member sound uid url key fburl caladruri
2555 caluri related org-directory contact-uri socialprofile
2556 x-addressbookserver-member
2557 );
2558  
2559 5961 ▶sub _is_uri_value {
Subroutine _is_uri_value: called CC=11 SCAR=24.5
2560 5961 my ($prop) = @_;
2561 5961 ▶ return 0 if exists $prop->{values};
Branch: 1 branch
BranchTrueFalseTotal
if exists $prop->{"values"} 0 5961 5961
2562 5961 my $val = $prop->{value};
2563 5961 ▶ return 0 if !defined $val || ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $val or ref $val 1 5960 5961
Condition66%2/3
not defined $val or ref $val
l!l&&r!l&&!r
015960
MC/DC50%1/2
not defined $val or ref $val
not defined $val ref $val
Truth table66%2/3
not(A) or B
A= not defined $valB= ref $val
ABresult
1X1
011
000
2564 # only something that is a URI: a bare IMPP username, a vCard 3 GEO
2565 # or a non-URI UID stays TEXT
2566 5960 ▶ return 0 unless $val =~ /^[a-z][a-z0-9+.-]*:/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $val =~ /^[a-z][a-z0-9+.-]*:/i 4056 1904 5960
2567 1904 ▶ my $params = $prop->{params} // {};
Condition100%2/2
$prop->{"params"} // {}
l!l
1722182
MC/DC100%1/1
$prop->{"params"} // {}
$prop->{"params"}
Truth table100%2/2
A
A= $prop->{"params"}
Aresult
11
00
2568 1904 ▶ return 0 if $params->{encoding};
Branch: 1 branch
BranchTrueFalseTotal
if $params->{"encoding"} 0 1904 1904
2569 1904 ▶ my @vt = map { lc } grep { defined } map { ref $_ ? @$_ : $_ } $params->{value} // ();
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 197 0 197
Condition66%2/3
$params->{"value"} // ()
l!l&&r!l&&!r
19717070
MC/DC0%0/2
$params->{"value"} // ()
$params->{"value"} ()
Truth table66%2/3
A // B
A= $params->{"value"}B= ()
ABresult
1X1
011
000
2570 1904 ▶ return $vt[0] eq 'uri' if @vt;
Branch: 1 branch
BranchTrueFalseTotal
if @vt 197 1707 1904
2571 1707 ▶ return $URI_DEFAULT{lc $prop->{name}} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$URI_DEFAULT{lc $prop->{'name'}} ? : 1707 0 1707
2572 }
2573  
2574 # A value that is a URI: a scheme, a colon, and no white space
2575 738 ▶sub _looks_like_uri {
Subroutine _looks_like_uri: called CC=3 SCAR=12.5
2576 738 my ($val) = @_;
2577 738 ▶ return defined $val && !ref $val && $val =~ /^[a-z][a-z0-9+.-]*:\S+\z/i;
Condition50%3/6
defined $val && !ref($val) && $val =~ /^[a-z][a-z0-9+.-]*:\S+\z/i
!ll&&!rl&&r
0436302
defined $val and not ref $val
!ll&&!rl&&r
00738
MC/DC33%1/3
defined $val && !ref($val) && $val =~ /^[a-z][a-z0-9+.-]*:\S+\z/i
defined $val not ref $val $val =~ /^[a-z][a-z0-9+.-]*:\S+\z/i
Truth table50%2/4
A and not(B) && C
A= defined $valB= not ref $valC= $val =~ /^[a-z][a-z0-9+.-]*:\S+\z/i
ABCresult
0XX0
10X0
1100
1111
2578 }
2579  
2580 1904 ▶sub _uri_text {
Subroutine _uri_text: called CC=1 SCAR=0.0
2581 1904 my ($val) = @_;
2582 1904 $val = _clean_vcard_text($val);
2583 # A URI has neither, and written raw a LF would end the content line,
2584 # so percent-encode them; it also means a reader's TEXT unescaping
2585 # (Text::VCardFast's applies to every value) leaves the URI alone.
2586 1904 $val =~ s/\\/%5C/g;
2587 1904 $val =~ s/\n/%0A/g;
2588 1904 return $val;
2589 }
2590  
2591 # A CR from the card data could end the content line early and forge
2592 # extra properties. vCard allows no control characters but HTAB, so turn
2593 # CR into LF (escaped as \n, or ^n in a parameter) and drop the rest.
2594 18425 ▶sub _clean_vcard_text {
Subroutine _clean_vcard_text: called CC=4 SCAR=15.5
2595 18425 my ($val) = @_;
2596 0 ▶ return [ map { _clean_vcard_text($_) } @$val ] if ref $val eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $val eq "ARRAY" 0 18425 18425
2597 18425 ▶ return $val if !defined $val || ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $val or ref $val 0 18425 18425
Condition33%1/3
not defined $val or ref $val
l!l&&r!l&&!r
0018425
MC/DC0%0/2
not defined $val or ref $val
not defined $val ref $val
Truth table33%1/3
not(A) or B
A= not defined $valB= ref $val
ABresult
1X1
011
000
2598 18425 $val =~ s/\r\n?/\n/g;
2599 18425 $val =~ s/[\x00-\x08\x0b-\x1f\x7f]//g;
2600 18425 return $val;
2601 }
2602  
2603 # Compare two JSContact cards and return hash of changed property names
2604 72 ▶sub _diff_cards {
Subroutine _diff_cards: called CC=9 SCAR=22.0
2605 72 my ($old, $new) = @_;
2606 72 my $json = JSON->new->canonical->utf8;
2607  
2608 72 my %changed;
2609  
2610 # Check all keys in both old and new
2611 my %all_keys;
2612 72 $all_keys{$_} = 1 for keys %$old, keys %$new;
2613  
2614 # Skip internal/metadata keys
2615 72 delete $all_keys{$_} for qw(@type version CPath href _raw meta);
2616  
2617 72 for my $key (keys %all_keys) {
2618 858 my $old_val = $old->{$key};
2619 858 my $new_val = $new->{$key};
2620  
2621 # Both undef/missing -> no change
2622 858 ▶ next if !defined $old_val && !defined $new_val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $old_val or defined $new_val 0 858 858
Condition66%2/3
defined $old_val or defined $new_val
l!l&&r!l&&!r
85530
MC/DC0%0/2
defined $old_val or defined $new_val
defined $old_val defined $new_val
Truth table66%2/3
A or B
A= defined $old_valB= defined $new_val
ABresult
1X1
011
000
2623  
2624 # One exists, other doesn't -> changed
2625 858 ▶ if (!defined $old_val || !defined $new_val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $old_val and defined $new_val) 4 854 858
Condition100%3/3
defined $old_val and defined $new_val
!ll&&!rl&&r
31854
MC/DC100%2/2
defined $old_val and defined $new_val
defined $old_val defined $new_val
Truth table100%3/3
A and B
A= defined $old_valB= defined $new_val
ABresult
0X0
100
111
2626 4 $changed{$key} = 1;
2627 4 next;
2628 }
2629  
2630 # Compare via canonical JSON encoding
2631 854 my $old_json = $json->encode([$old_val]);
2632 854 my $new_json = $json->encode([$new_val]);
2633 854 ▶ $changed{$key} = 1 if $old_json ne $new_json;
Branch: 1 branch
BranchTrueFalseTotal
if $old_json ne $new_json 32 822 854
2634 }
2635  
2636 72 return %changed;
2637 }
2638  
2639 20 1;
2640  
2641 __END__
↑ Summary