| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | return unless length $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return unless defined $enco; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | return unless length $enco; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | next unless exists $hash{'http-equiv'}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | next unless lc $hash{'http-equiv'} eq "content-type"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | my $char = encoding_from_content_type($hash{'content'}); | |||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | ▶ | push @resu, $char if defined $char and length $char; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $char and length $char
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
| |||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | return unless @resu; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | return wantarray ? @resu : $resu[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my %resu; | |||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ▶ | return unless defined $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | return unless length $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
$end > 0 and $str >= 0 and $end > $str
$end > 0 and $str >= 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $resu{$deco}++; | |||||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||
| 205 | # No XML declarations found | ||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | return unless keys %resu; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | my $whitespace = $o{whitespace} || [qw/CR LF TB SP/]; | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$o{'whitespace'} || ['CR', 'LF', 'TB', 'SP']
MC/DC0%0/1
$o{'whitespace'} || ['CR', 'LF', 'TB', 'SP']
$o{'whitespace'}
Truth table0%0/2
A
A= $o{'whitespace'}
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | ▶ | return unless defined $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | return unless length $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | # ... | ||||||||||||||||||||||||||||||||||||||||||
| 248 | 0 | ▶ | return unless keys %resu; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | return unless length $decl; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | # attempt to extract encoding pseudo attribute | ||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | ▶ | return unless $decl =~ /encoding$ws=$ws'([^']+)'/i or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$decl =~ /encoding$ws=$ws'([^']+)'/i or $decl =~ /encoding$ws=$ws"([^"]+)"/i
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
| |||||||||||||||||||||||||||||||||||||||||||
| 273 | $decl =~ /encoding$ws=$ws"([^"]+)"/i; | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | # no encoding pseudo-attribute | ||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | return unless defined $1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | my %resu; | |||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | ▶ | return unless defined $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | ▶ | return unless length $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||||||||
| 309 | # remember match length | ||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | $resu{$e} = length $bom if $text =~ /^(\Q$bom\E)/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 311 | } | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | # does not start with BOM | ||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ▶ | return unless keys %resu; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $text and length $text
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
| |||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | # downgrade Unicode strings | ||||||||||||||||||||||||||||||||||||||||||
| 332 | 41 | ▶ | $text = Encode::encode_utf8($text) if Encode::is_utf8($text); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition60%3/5
'charset' eq lc $param && (($char = $data{$param}) && (last))
$char = $data{$param} and last
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 341 | } | ||||||||||||||||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||||||||||||||||
| 343 | # no charset parameter | ||||||||||||||||||||||||||||||||||||||||||
| 344 | 41 | ▶ | return unless defined $char; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 366 | 0 | my %resu; | |||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | 0 | ▶ | return unless defined $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 369 | 0 | ▶ | return unless length $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $enco and length $enco
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
| |||||||||||||||||||||||||||||||||||||||||||
| 382 | } | ||||||||||||||||||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||||||||||||||||
| 384 | 0 | ▶ | return unless keys %resu; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | 2 | ▶ | my $popts = $o{parser_options} || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{'parser_options'} || {}
MC/DC0%0/1
$o{'parser_options'} || {}
$o{'parser_options'}
Truth table50%1/2
A
A= $o{'parser_options'}
| |||||||||||||||||||||||||||||||||||||||||||
| 398 | 2 | ▶ | my $xhtml = exists $o{xhtml} ? $o{xhtml} : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | 2 | ▶ | return unless defined $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 401 | 2 | ▶ | return unless length $text; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||||||||||||||||
| 403 | 0 | ▶ | if ($xhtml) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 404 | { | ||||||||||||||||||||||||||||||||||||||||||
| 405 | 0 | ▶ | my @xml = wantarray | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
@xml and defined $xml[0]
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]
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 437 | 0 | ▶ | return wantarray ? (meta => \@resu) : $resu[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 446 | 42 | ▶ | my $is_html = $o{is_html} || qr{^text/html$}i; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{'is_html'} || qr"^text/html$"i
MC/DC0%0/1
$o{'is_html'} || qr"^text/html$"i
$o{'is_html'}
Truth table50%1/2
A
A= $o{'is_html'}
| |||||||||||||||||||||||||||||||||||||||||||
| 447 | 42 | ▶ | my $is_xml = $o{is_xml} || qr{^.+/(?:.+\+)?xml$}i; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{'is_xml'} || qr"^.+/(?:.+\+)?xml$"i
MC/DC0%0/1
$o{'is_xml'} || qr"^.+/(?:.+\+)?xml$"i
$o{'is_xml'}
Truth table50%1/2
A
A= $o{'is_xml'}
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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'}
| |||||||||||||||||||||||||||||||||||||||||||
| 449 | 42 | ▶ | my $html_d = $o{html_default} || "ISO-8859-1"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{'html_default'} || 'ISO-8859-1'
MC/DC0%0/1
$o{'html_default'} || 'ISO-8859-1'
$o{'html_default'}
Truth table50%1/2
A
A= $o{'html_default'}
| |||||||||||||||||||||||||||||||||||||||||||
| 450 | 42 | ▶ | my $xml_d = $o{xml_default} || "UTF-8"; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$o{'xml_default'} || 'UTF-8'
MC/DC0%0/1
$o{'xml_default'} || 'UTF-8'
$o{'xml_default'}
Truth table50%1/2
A
A= $o{'xml_default'}
| |||||||||||||||||||||||||||||||||||||||||||
| 451 | 42 | my $txml = $o{text_xml_default}; | |||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | 42 | ▶ | my $xhtml = exists $o{xhtml} ? $o{xhtml} : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 454 | 42 | ▶ | my $default = exists $o{default} ? $o{default} : 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 460 | { | ||||||||||||||||||||||||||||||||||||||||||
| 461 | 0 | ▶ | return wantarray ? (protocol => $charset) : $charset | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $txml and $mess->content_type =~ /$is_t_xml/
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/
| |||||||||||||||||||||||||||||||||||||||||||
| 466 | if defined $txml and $mess->content_type =~ $is_t_xml; | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | 0 | ▶ | if (wantarray) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 469 | { | ||||||||||||||||||||||||||||||||||||||||||
| 470 | 0 | my @xml = encoding_from_xml_document($mess->content, encodings => $encodings); | |||||||||||||||||||||||||||||||||||||||||
| 471 | 0 | ▶ | return @xml if @xml; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 477 | } | ||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||
| 479 | 0 | ▶ | return wantarray ? (default => $xml_d) : $xml_d if defined $default; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 480 | } | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | 42 | ▶ | if ($mess->content_type =~ $is_html) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 483 | { | ||||||||||||||||||||||||||||||||||||||||||
| 484 | 41 | ▶ | return wantarray ? (protocol => $charset) : $charset | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 485 | if defined $charset; | ||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||
| 487 | 2 | ▶ | if (wantarray) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 488 | { | ||||||||||||||||||||||||||||||||||||||||||
| 489 | 0 | my @html = encoding_from_html_document($mess->content, encodings => $encodings, xhtml => $xhtml); | |||||||||||||||||||||||||||||||||||||||||
| 490 | 0 | ▶ | return @html if @html; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | 2 | ▶ | return wantarray ? (default => $html_d) : $html_d if defined $default; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||||||||||||||||
| 501 | return | ||||||||||||||||||||||||||||||||||||||||||
| 502 | 1 | } | |||||||||||||||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||||||||||||||||
| 504 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||||||||||||||||
| 506 | __END__ | ||||||||||||||||||||||||||||||||||||||||||