blib/lib/HTML/Encoding.pm

filter: Statementstmt 29.0% 66 / 227 Branchbran 13.6% 23 / 168 Conditioncond 21.3% 13 / 61 MC/DCmcdc 2.7% 1 / 36 Subroutinesub 42.8% 9 / 21 Podpod 100% 9 / 9 totaltotal 23.1% 121 / 522 CC 116 SCAR 47.2
CC
116
Cov
22%
encoding_from_meta_element
182.0
encoding_from_http_message
159.7
encoding_from_html_document
146.2
CRAP 112.1
↑ Summary
1 package HTML::Encoding;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 3 ▶use HTML::Parser qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use HTTP::Headers::Util qw(split_header_words);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Encode qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
8
9 3 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
10
11 3 our $VERSION = '0.61';
12
13 3 our @EXPORT_OK =
14 qw/
15 &encoding_from_meta_element
16 &xml_declaration_from_octets
17 &encoding_from_first_chars
18 &encoding_from_xml_declaration
19 &encoding_from_byte_order_mark
20 &encoding_from_content_type
21 &encoding_from_xml_document
22 &encoding_from_html_document
23 &encoding_from_http_message
24 /;
25
26 3 our $DEFAULT_ENCODINGS = [qw/
27 ISO-8859-1
28 UTF-16LE
29 UTF-16BE
30 UTF-32LE
31 UTF-32BE
32 UTF-8
33 /];
34
35 3 our %MAP =
36 (
37 BM => "\x{FEFF}",
38 CR => "\x{000D}",
39 LF => "\x{000A}",
40 SP => "\x{0020}",
41 TB => "\x{0009}",
42 QS => "\x{003F}",
43 NL => "\x{0085}",
44 LS => "\x{2028}",
45 LT => "<", # fixme
46 GT => ">", # fixme
47 );
48
49 sub _my_encode
50 0 ▶{
Subroutine _my_encode: not called CC=2 SCAR=17.9
51 0 my $seq;
52
53 eval
54 0 {
55 0 $seq = Encode::encode($_[0],
56 $_[1],
57 $_[2]);
58 };
59
60 0 ▶ return $seq unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
61 0 return;
62 }
63
64 sub _my_decode
65 0 ▶{
Subroutine _my_decode: not called CC=2 SCAR=17.9
66 0 my $str;
67
68 eval
69 0 {
70 0 $str = Encode::decode($_[0],
71 $_[1],
72 $_[2]);
73 };
74
75 0 ▶ return $str unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
76 0 return;
77 }
78
79 sub _make_character_map
80 0 ▶{
Subroutine _make_character_map: not called CC=3 SCAR=24.8
81 0 my $encoding = shift;
82 0 my %data;
83
84 0 foreach my $sym (keys %MAP)
85 {
86 0 my $seq = _my_encode($encoding, "$MAP{$sym}", Encode::FB_CROAK);
87 0 ▶ $data{$sym} = $seq if defined $seq;
Branch: 1 branch
BranchTrueFalseTotal
if defined $seq 0 0 0
88 }
89
90 0 \%data;
91 }
92
93 # cache for U+XXXX octet sequences
94 3 our %CHARACTER_MAP_CACHE = ();
95
96 sub _get_character_map
97 0 ▶{
Subroutine _get_character_map: not called CC=2 SCAR=17.9
98 0 my $encoding = shift;
99
100 # read from cache
101 return $CHARACTER_MAP_CACHE{$encoding}
102 0 ▶ if exists $CHARACTER_MAP_CACHE{$encoding};
Branch: 1 branch
BranchTrueFalseTotal
if exists $HTML::Encoding::CHARACTER_MAP_CACHE{$encoding} 0 0 0
103
104 # new cache entry
105 0 my $map = _make_character_map($encoding);
106 0 $CHARACTER_MAP_CACHE{$encoding} = $map;
107
108 # return new entry
109 0 return $map;
110 }
111
112 sub encoding_from_meta_element
113 0 ▶{
Subroutine encoding_from_meta_element: not called CC=13 SCAR=52.0
Pod documented
114 0 my $text = shift;
115 0 my $enco = shift;
116
117 0 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
118 0 ▶ return unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 0 0
119
120 0 ▶ return unless defined $enco;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $enco 0 0 0
121 0 ▶ return unless length $enco;
Branch: 1 branch
BranchTrueFalseTotal
unless length $enco 0 0 0
122
123 0 my $pars = HTML::Parser->new
124 (
125 api_version => 3,
126 @_
127 );
128
129 0 my $meta = [];
130 0 my $leng = length $text;
131 0 my $size = 8192;
132 0 my $data = '';
133 0 my $utf8 = '';
134 0 my $i = 0;
135
136 # todo: should finish when <body> or logically body//*
137
138 0 $pars->report_tags(qw/meta head/);
139 0 $pars->handler(start => $meta, "tagname,attr");
140 $pars->handler
141 (
142 0 ▶ end => sub { $_[0]->eof if $_[1] eq "head" },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] eq "head" 0 0 0
143 0 "self,tagname"
144 );
145
146 $pars->parse(sub
147 0 ▶ {
Subroutine __ANON__: not called CC=2 SCAR=17.9
148 0 ▶ return if $i > $leng;
Branch: 1 branch
BranchTrueFalseTotal
if $i > $leng 0 0 0
149 0 $data .= substr $text, $i, $size;
150 0 $i += $size;
151 0 _my_decode($enco, $data, Encode::FB_QUIET);
152 0 });
153
154 0 my @resu;
155
156 0 foreach (grep { $_->[0] eq "meta" } @$meta)
157 {
158 0 my %hash = %{$_->[1]};
159 0 ▶ next unless defined $hash{'content'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hash{"content"} 0 0 0
160 0 ▶ next unless exists $hash{'http-equiv'};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash{"http-equiv"} 0 0 0
161 0 ▶ next unless lc $hash{'http-equiv'} eq "content-type";
Branch: 1 branch
BranchTrueFalseTotal
unless lc $hash{"http-equiv"} eq "content-type" 0 0 0
162 0 my $char = encoding_from_content_type($hash{'content'});
163 0 ▶ push @resu, $char if defined $char and length $char;
Branch: 1 branch
BranchTrueFalseTotal
if defined $char and length $char 0 0 0
Condition0%0/3
defined $char and length $char
!ll&&!rl&&r
000
MC/DC0%0/2
defined $char and length $char
defined $char length $char
Truth table0%0/3
A and B
A= defined $charB= length $char
ABresult
0X0
100
111
164 }
165
166 0 ▶ return unless @resu;
Branch: 1 branch
BranchTrueFalseTotal
unless @resu 0 0 0
167 0 ▶ return wantarray ? @resu : $resu[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
168 }
169
170 sub xml_declaration_from_octets
171 0 ▶{
Subroutine xml_declaration_from_octets: not called CC=11 SCAR=48.8
Pod documented
172 0 my $text = shift;
173 0 my %o = @_;
174 0 ▶ my $encodings = $o{encodings} || $DEFAULT_ENCODINGS;
Condition0%0/3
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
l!l&&r!l&&!r
000
MC/DC0%0/2
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
$o{'encodings'} $HTML::Encoding::DEFAULT_ENCODINGS
Truth table0%0/3
A || B
A= $o{'encodings'}B= $HTML::Encoding::DEFAULT_ENCODINGS
ABresult
1X1
011
000
175 0 my %resu;
176
177 0 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
178 0 ▶ return unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 0 0
179
180 0 foreach my $e (@$encodings)
181 {
182 0 my $map = _get_character_map($e);
183
184 # search for >
185 0 my $end = index $text, $map->{GT};
186
187 # search for <?
188 0 my $str = index $text, $map->{LT} . $map->{QS};
189
190 # skip this encoding unless ...
191 0 ▶ next unless $end > 0 and $str >= 0 and $end > $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $end > 0 and $str >= 0 and $end > $str 0 0 0
Condition0%0/6
$end > 0 and $str >= 0 and $end > $str
!ll&&!rl&&r
000
$end > 0 and $str >= 0
!ll&&!rl&&r
000
MC/DC0%0/3
$end > 0 and $str >= 0 and $end > $str
$end > 0 $str >= 0 $end > $str
Truth table0%0/4
A and B and C
A= $end > 0B= $str >= 0C= $end > $str
ABCresult
0XX0
10X0
1100
1111
192
193 # extract tentative XML declaration
194 0 my $decl = substr $text, $str, $end - $str + 1;
195
196 # decode XML declaration
197 0 my $deco = _my_decode($e, $decl, Encode::FB_CROAK);
198
199 # skip encoding if decoding failed
200 0 ▶ next unless defined $deco;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $deco 0 0 0
201
202 0 $resu{$deco}++;
203 }
204
205 # No XML declarations found
206 0 ▶ return unless keys %resu;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %resu 0 0 0
207
208 # sort by number of matches, most match first
209 0 my @sort = sort { $resu{$b} <=> $resu{$a} } keys %resu;
210
211 # in array context return all encodings,
212 # in scalar context return best match.
213 0 ▶ return wantarray ? @sort : $sort[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
214 }
215
216 sub encoding_from_first_chars
217 0 ▶{
Subroutine encoding_from_first_chars: not called CC=11 SCAR=48.8
Pod documented
218 0 my $text = shift;
219 0 my %o = @_;
220 0 ▶ my $encodings = $o{encodings} || $DEFAULT_ENCODINGS;
Condition0%0/3
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
l!l&&r!l&&!r
000
MC/DC0%0/2
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
$o{'encodings'} $HTML::Encoding::DEFAULT_ENCODINGS
Truth table0%0/3
A || B
A= $o{'encodings'}B= $HTML::Encoding::DEFAULT_ENCODINGS
ABresult
1X1
011
000
221 0 ▶ my $whitespace = $o{whitespace} || [qw/CR LF TB SP/];
Condition0%0/2
$o{'whitespace'} || ['CR', 'LF', 'TB', 'SP']
l!l
00
MC/DC0%0/1
$o{'whitespace'} || ['CR', 'LF', 'TB', 'SP']
$o{'whitespace'}
Truth table0%0/2
A
A= $o{'whitespace'}
Aresult
11
00
222
223 0 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
224 0 ▶ return unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 0 0
225
226 0 my %resu;
227 0 foreach my $e (@$encodings)
228 {
229 0 my $m = _get_character_map($e);
230 0 my $i = index $text, $m->{LT};
231 0 ▶ next unless $i >= 0;
Branch: 1 branch
BranchTrueFalseTotal
unless $i >= 0 0 0 0
232 0 my $t = substr $text, 0, $i;
233
234 0 my @y;
235
236 # construct \xXX\xXX string from octets, might make sense to
237 # have this in the map construction process
238 0 push@y,"(?:".join("",map{sprintf"\\x%02x",ord}split//,$m->{$_}).")"
239 0 foreach grep defined, @$whitespace;
240
241 0 my $x = join "|", @y;
242 0 $t =~ s/^($x)+//g;
243
244 0 ▶ $resu{$e} = $i + length $m->{LT} unless length $t;
Branch: 1 branch
BranchTrueFalseTotal
unless length $t 0 0 0
245 }
246
247 # ...
248 0 ▶ return unless keys %resu;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %resu 0 0 0
249
250 # sort by match length, longest match first
251 0 my @sort = sort { $resu{$b} <=> $resu{$a} } keys %resu;
252
253 # in array context return all encodings,
254 # in scalar context return best match.
255 0 ▶ return wantarray ? @sort : $sort[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
256 }
257
258 sub encoding_from_xml_declaration
259 0 ▶{
Subroutine encoding_from_xml_declaration: not called CC=8 SCAR=42.8
Pod documented
260 0 my $decl = shift;
261
262 0 ▶ return unless defined $decl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $decl 0 0 0
263 0 ▶ return unless length $decl;
Branch: 1 branch
BranchTrueFalseTotal
unless length $decl 0 0 0
264
265 # todo: move this to some better place...
266 0 my $ws = qr/[\x09\x85\x20\x0d\x0a\x{2028}]*/;
267
268 # skip if not an XML declaration
269 0 ▶ return unless $decl =~ /^<\?xml$ws/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $decl =~ /^<\?xml$ws/i 0 0 0
270
271 # attempt to extract encoding pseudo attribute
272 0 ▶ return unless $decl =~ /encoding$ws=$ws'([^']+)'/i or
Branch: 1 branch
BranchTrueFalseTotal
unless $decl =~ /encoding$ws=$ws'([^']+)'/i or $decl =~ /encoding$ws=$ws"([^"]+)"/i 0 0 0
Condition0%0/3
$decl =~ /encoding$ws=$ws'([^']+)'/i or $decl =~ /encoding$ws=$ws"([^"]+)"/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$decl =~ /encoding$ws=$ws'([^']+)'/i or $decl =~ /encoding$ws=$ws"([^"]+)"/i
$decl =~ /encoding$ws=$ws'([^']+)'/i $decl =~ /encoding$ws=$ws"([^"]+)"/i
Truth table0%0/3
A or B
A= $decl =~ /encoding$ws=$ws'([^']+)'/iB= $decl =~ /encoding$ws=$ws"([^"]+)"/i
ABresult
1X1
011
000
273 $decl =~ /encoding$ws=$ws"([^"]+)"/i;
274
275 # no encoding pseudo-attribute
276 0 ▶ return unless defined $1;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $1 0 0 0
277 0 my $enco = $1;
278
279 # strip leading/trailing whitespace/quotes
280 0 $enco =~ s/^[\s'"]+|[\s'"]+$//g;
281
282 # collapse white-space
283 0 $enco =~ s/\s+/ /g;
284
285 # treat empty charset as if it were unspecified
286 0 ▶ return unless length $enco;
Branch: 1 branch
BranchTrueFalseTotal
unless length $enco 0 0 0
287
288 0 return $enco;
289 }
290
291 sub encoding_from_byte_order_mark
292 0 ▶{
Subroutine encoding_from_byte_order_mark: not called CC=9 SCAR=45.0
Pod documented
293 0 my $text = shift;
294 0 my %o = @_;
295 0 ▶ my $encodings = $o{encodings} || $DEFAULT_ENCODINGS;
Condition0%0/3
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
l!l&&r!l&&!r
000
MC/DC0%0/2
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
$o{'encodings'} $HTML::Encoding::DEFAULT_ENCODINGS
Truth table0%0/3
A || B
A= $o{'encodings'}B= $HTML::Encoding::DEFAULT_ENCODINGS
ABresult
1X1
011
000
296 0 my %resu;
297
298 0 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
299 0 ▶ return unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 0 0
300
301 0 foreach my $e (@$encodings)
302 {
303 0 my $map = _get_character_map($e);
304 0 my $bom = $map->{BM};
305
306 # encoding cannot encode U+FEFF
307 0 ▶ next unless defined $bom;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bom 0 0 0
308
309 # remember match length
310 0 ▶ $resu{$e} = length $bom if $text =~ /^(\Q$bom\E)/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /^(\Q$bom\E)/ 0 0 0
311 }
312
313 # does not start with BOM
314 0 ▶ return unless keys %resu;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %resu 0 0 0
315
316 # sort by match length, longest match first
317 0 my @sort = sort { $resu{$b} <=> $resu{$a} } keys %resu;
318
319 # in array context return all encodings,
320 # in scalar context return best match.
321 0 ▶ return wantarray ? @sort : $sort[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
322 }
323
324 sub encoding_from_content_type
325 42 ▶{
Subroutine encoding_from_content_type: called CC=9 SCAR=22.3
Pod documented
326 42 my $text = shift;
327
328 # nothing to do...
329 42 ▶ return unless defined $text and length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text and length $text 1 41 42
Condition66%2/3
defined $text and length $text
!ll&&!rl&&r
1041
MC/DC50%1/2
defined $text and length $text
defined $text length $text
Truth table66%2/3
A and B
A= defined $textB= length $text
ABresult
0X0
100
111
330
331 # downgrade Unicode strings
332 41 ▶ $text = Encode::encode_utf8($text) if Encode::is_utf8($text);
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($text) 0 41 41
333
334 # split parameters, only look at the first set
335 41 my %data = @{(split_header_words($text))[0]};
336
337 # extract first charset parameter if any
338 41 my $char;
339 41 foreach my $param (keys %data) {
340 64 ▶ $char = $data{$param} and last if 'charset' eq lc $param;
Branch: 1 branch
BranchTrueFalseTotal
if "charset" eq lc $param 39 25 64
Condition60%3/5
'charset' eq lc $param && (($char = $data{$param}) && (last))
!ll&&!rl&&r
25390
$char = $data{$param} and last
!ll
039
MC/DC0%0/2
'charset' eq lc $param && (($char = $data{$param}) && (last))
'charset' eq lc $param $char = $data{$param}
Truth table33%1/3
A && B
A= 'charset' eq lc $paramB= $char = $data{$param}
ABresult
0X0
100
111
341 }
342
343 # no charset parameter
344 41 ▶ return unless defined $char;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $char 2 39 41
345
346 # there are no special escapes so just remove \s
347 39 $char =~ tr/\\//d;
348
349 # strip leading/trailing whitespace/quotes
350 39 $char =~ s/^[\s'"]+|[\s'"]+$//g;
351
352 # collapse white-space
353 39 $char =~ s/\s+/ /g;
354
355 # treat empty charset as if it were unspecified
356 39 ▶ return unless length $char;
Branch: 1 branch
BranchTrueFalseTotal
unless length $char 0 39 39
357
358 39 return $char
359 }
360
361 sub encoding_from_xml_document
362 0 ▶{
Subroutine encoding_from_xml_document: not called CC=11 SCAR=48.8
Pod documented
363 0 my $text = shift;
364 0 my %o = @_;
365 0 ▶ my $encodings = $o{encodings} || $DEFAULT_ENCODINGS;
Condition0%0/3
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
l!l&&r!l&&!r
000
MC/DC0%0/2
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
$o{'encodings'} $HTML::Encoding::DEFAULT_ENCODINGS
Truth table0%0/3
A || B
A= $o{'encodings'}B= $HTML::Encoding::DEFAULT_ENCODINGS
ABresult
1X1
011
000
366 0 my %resu;
367
368 0 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 0 0
369 0 ▶ return unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 0 0 0
370
371 0 my @boms = encoding_from_byte_order_mark($text, encodings => $encodings);
372
373 # BOM determines encoding
374 0 ▶ return wantarray ? (bom => \@boms) : $boms[0] if @boms;
Branch: 2 branches
BranchTrueFalseTotal
if @boms 0 0 0
wantarray ? : 0 0 0
375
376 # no BOM
377 0 my @decls = xml_declaration_from_octets($text, encodings => $encodings);
378 0 foreach my $decl (@decls)
379 {
380 0 my $enco = encoding_from_xml_declaration($decl);
381 0 ▶ $resu{$enco}++ if defined $enco and length $enco;
Branch: 1 branch
BranchTrueFalseTotal
if defined $enco and length $enco 0 0 0
Condition0%0/3
defined $enco and length $enco
!ll&&!rl&&r
000
MC/DC0%0/2
defined $enco and length $enco
defined $enco length $enco
Truth table0%0/3
A and B
A= defined $encoB= length $enco
ABresult
0X0
100
111
382 }
383
384 0 ▶ return unless keys %resu;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %resu 0 0 0
385 0 my @sort = sort { $resu{$b} <=> $resu{$a} } keys %resu;
386
387 # in array context return all encodings,
388 # in scalar context return best match.
389 0 ▶ return wantarray ? (xml => \@sort) : $sort[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
390 }
391
392 sub encoding_from_html_document
393 2 ▶{
Subroutine encoding_from_html_document: called CC=17 SCAR=49.9
Pod documented
394 2 my $text = shift;
395 2 my %o = @_;
396 2 ▶ my $encodings = $o{encodings} || $DEFAULT_ENCODINGS;
Condition33%1/3
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
l!l&&r!l&&!r
200
MC/DC0%0/2
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
$o{'encodings'} $HTML::Encoding::DEFAULT_ENCODINGS
Truth table33%1/3
A || B
A= $o{'encodings'}B= $HTML::Encoding::DEFAULT_ENCODINGS
ABresult
1X1
011
000
397 2 ▶ my $popts = $o{parser_options} || {};
Condition50%1/2
$o{'parser_options'} || {}
l!l
02
MC/DC0%0/1
$o{'parser_options'} || {}
$o{'parser_options'}
Truth table50%1/2
A
A= $o{'parser_options'}
Aresult
11
00
398 2 ▶ my $xhtml = exists $o{xhtml} ? $o{xhtml} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $o{'xhtml'} ? : 2 0 2
399
400 2 ▶ return unless defined $text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $text 0 2 2
401 2 ▶ return unless length $text;
Branch: 1 branch
BranchTrueFalseTotal
unless length $text 2 0 2
402
403 0 ▶ if ($xhtml)
Branch: 1 branch
BranchTrueFalseTotal
if ($xhtml) { } 0 0 0
404 {
405 0 ▶ my @xml = wantarray
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
406 ? encoding_from_xml_document($text, encodings => $encodings)
407 : scalar encoding_from_xml_document($text, encodings => $encodings);
408
409 return wantarray
410 ? @xml
411 0 ▶ : $xml[0]
Branch: 2 branches
BranchTrueFalseTotal
if @xml and defined $xml[0] 0 0 0
wantarray ? : 0 0 0
Condition0%0/3
@xml and defined $xml[0]
!ll&&!rl&&r
000
MC/DC0%0/2
@xml and defined $xml[0]
@xml defined $xml[0]
Truth table0%0/3
A and B
A= @xmlB= defined $xml[0]
ABresult
0X0
100
111
412 if @xml and defined $xml[0];
413 }
414 else
415 {
416 0 my @boms = encoding_from_byte_order_mark($text, encodings => $encodings);
417
418 # BOM determines encoding
419 0 ▶ return wantarray ? (bom => \@boms) : $boms[0] if @boms;
Branch: 2 branches
BranchTrueFalseTotal
if @boms 0 0 0
wantarray ? : 0 0 0
420 }
421
422 # no BOM
423 0 my @resu;
424
425 # sanity check to exclude e.g. UTF-32
426 0 my @first = encoding_from_first_chars($text, encodings => $encodings);
427
428 # fall back to provided encoding list
429 0 ▶ @first = @$encodings unless @first;
Branch: 1 branch
BranchTrueFalseTotal
unless @first 0 0 0
430
431 0 foreach my $try (@first)
432 {
433 0 push @resu, encoding_from_meta_element($text, $try, %$popts);
434 }
435
436 0 ▶ return unless @resu;
Branch: 1 branch
BranchTrueFalseTotal
unless @resu 0 0 0
437 0 ▶ return wantarray ? (meta => \@resu) : $resu[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
438 }
439
440 sub encoding_from_http_message
441 42 ▶{
Subroutine encoding_from_http_message: called CC=28 SCAR=50.7
Pod documented
442 42 my $mess = shift;
443 42 my %o = @_;
444
445 42 ▶ my $encodings = $o{encodings} || $DEFAULT_ENCODINGS;
Condition33%1/3
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
l!l&&r!l&&!r
0420
MC/DC0%0/2
$o{'encodings'} || $HTML::Encoding::DEFAULT_ENCODINGS
$o{'encodings'} $HTML::Encoding::DEFAULT_ENCODINGS
Truth table33%1/3
A || B
A= $o{'encodings'}B= $HTML::Encoding::DEFAULT_ENCODINGS
ABresult
1X1
011
000
446 42 ▶ my $is_html = $o{is_html} || qr{^text/html$}i;
Condition50%1/2
$o{'is_html'} || qr"^text/html$"i
l!l
042
MC/DC0%0/1
$o{'is_html'} || qr"^text/html$"i
$o{'is_html'}
Truth table50%1/2
A
A= $o{'is_html'}
Aresult
11
00
447 42 ▶ my $is_xml = $o{is_xml} || qr{^.+/(?:.+\+)?xml$}i;
Condition50%1/2
$o{'is_xml'} || qr"^.+/(?:.+\+)?xml$"i
l!l
042
MC/DC0%0/1
$o{'is_xml'} || qr"^.+/(?:.+\+)?xml$"i
$o{'is_xml'}
Truth table50%1/2
A
A= $o{'is_xml'}
Aresult
11
00
448 42 ▶ my $is_t_xml = $o{is_text_xml} || qr{^text/(?:.+\+)?xml$}i;
Condition50%1/2
$o{'is_text_xml'} || qr"^text/(?:.+\+)?xml$"i
l!l
042
MC/DC0%0/1
$o{'is_text_xml'} || qr"^text/(?:.+\+)?xml$"i
$o{'is_text_xml'}
Truth table50%1/2
A
A= $o{'is_text_xml'}
Aresult
11
00
449 42 ▶ my $html_d = $o{html_default} || "ISO-8859-1";
Condition50%1/2
$o{'html_default'} || 'ISO-8859-1'
l!l
042
MC/DC0%0/1
$o{'html_default'} || 'ISO-8859-1'
$o{'html_default'}
Truth table50%1/2
A
A= $o{'html_default'}
Aresult
11
00
450 42 ▶ my $xml_d = $o{xml_default} || "UTF-8";
Condition50%1/2
$o{'xml_default'} || 'UTF-8'
l!l
042
MC/DC0%0/1
$o{'xml_default'} || 'UTF-8'
$o{'xml_default'}
Truth table50%1/2
A
A= $o{'xml_default'}
Aresult
11
00
451 42 my $txml = $o{text_xml_default};
452
453 42 ▶ my $xhtml = exists $o{xhtml} ? $o{xhtml} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $o{'xhtml'} ? : 0 42 42
454 42 ▶ my $default = exists $o{default} ? $o{default} : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $o{'default'} ? : 0 42 42
455
456 42 my $type = $mess->header('Content-Type');
457 42 my $charset = encoding_from_content_type($type);
458
459 42 ▶ if ($mess->content_type =~ $is_xml)
Branch: 1 branch
BranchTrueFalseTotal
if ($mess->content_type =~ /$is_xml/) 0 42 42
460 {
461 0 ▶ return wantarray ? (protocol => $charset) : $charset
Branch: 2 branches
BranchTrueFalseTotal
if defined $charset 0 0 0
wantarray ? : 0 0 0
462 if defined $charset;
463
464 # special case for text/xml at user option
465 0 ▶ return wantarray ? (protocol_default => $txml) : $txml
Branch: 2 branches
BranchTrueFalseTotal
if defined $txml and $mess->content_type =~ /$is_t_xml/ 0 0 0
wantarray ? : 0 0 0
Condition0%0/3
defined $txml and $mess->content_type =~ /$is_t_xml/
!ll&&!rl&&r
000
MC/DC0%0/2
defined $txml and $mess->content_type =~ /$is_t_xml/
defined $txml $mess->content_type =~ /$is_t_xml/
Truth table0%0/3
A and B
A= defined $txmlB= $mess->content_type =~ /$is_t_xml/
ABresult
0X0
100
111
466 if defined $txml and $mess->content_type =~ $is_t_xml;
467
468 0 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 0 0
469 {
470 0 my @xml = encoding_from_xml_document($mess->content, encodings => $encodings);
471 0 ▶ return @xml if @xml;
Branch: 1 branch
BranchTrueFalseTotal
if @xml 0 0 0
472 }
473 else
474 {
475 0 my $xml = scalar encoding_from_xml_document($mess->content, encodings => $encodings);
476 0 ▶ return $xml if defined $xml;
Branch: 1 branch
BranchTrueFalseTotal
if defined $xml 0 0 0
477 }
478
479 0 ▶ return wantarray ? (default => $xml_d) : $xml_d if defined $default;
Branch: 2 branches
BranchTrueFalseTotal
if defined $default 0 0 0
wantarray ? : 0 0 0
480 }
481
482 42 ▶ if ($mess->content_type =~ $is_html)
Branch: 1 branch
BranchTrueFalseTotal
if ($mess->content_type =~ /$is_html/) 41 1 42
483 {
484 41 ▶ return wantarray ? (protocol => $charset) : $charset
Branch: 2 branches
BranchTrueFalseTotal
if defined $charset 39 2 41
wantarray ? : 0 39 39
485 if defined $charset;
486
487 2 ▶ if (wantarray)
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 2 2
488 {
489 0 my @html = encoding_from_html_document($mess->content, encodings => $encodings, xhtml => $xhtml);
490 0 ▶ return @html if @html;
Branch: 1 branch
BranchTrueFalseTotal
if @html 0 0 0
491 }
492 else
493 {
494 2 my $html = scalar encoding_from_html_document($mess->content, encodings => $encodings, xhtml => $xhtml);
495 2 ▶ return $html if defined $html;
Branch: 1 branch
BranchTrueFalseTotal
if defined $html 0 2 2
496 }
497
498 2 ▶ return wantarray ? (default => $html_d) : $html_d if defined $default;
Branch: 2 branches
BranchTrueFalseTotal
if defined $default 2 0 2
wantarray ? : 0 2 2
499 }
500
501 return
502 1 }
503
504 3 1;
505
506 __END__
↑ Summary