blib/lib/IO/HTML.pm

filter: Statementstmt 96.3% 106 / 110 Branchbran 80.3% 82 / 102 Conditioncond 82.5% 33 / 40 MC/DCmcdc 72.7% 16 / 22 Subroutinesub 100% 13 / 13 Podpod 100% 5 / 5 totaltotal 87.3% 255 / 292 CC 67 SCAR 27.5
CC
67
Cov
88%
find_charset_in
26.1
sniff_encoding
18.2
_get_attribute
9.9
CRAP 15.7
↑ Summary
1 #---------------------------------------------------------------------
2 package IO::HTML;
3 #
4 # Copyright 2020 Christopher J. Madsen
5 #
6 # Author: Christopher J. Madsen <perl@cjmweb.net>
7 # Created: 14 Jan 2012
8 #
9 # This program is free software; you can redistribute it and/or modify
10 # it under the same terms as Perl itself.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See either the
15 # GNU General Public License or the Artistic License for more details.
16 #
17 # ABSTRACT: Open an HTML file with automatic charset detection
18 #---------------------------------------------------------------------
19  
20 4 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 4 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Encode 2.10 qw(decode find_encoding); # need utf-8-strict encoding
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 4 our $VERSION = '1.004';
29 # This file is part of IO-HTML 1.004 (September 26, 2020)
30  
31  
32 4 ▶our $bytes_to_check ||= 1024;
Condition50%1/2
our $bytes_to_check ||= 1024
l!l
04
MC/DC0%0/1
our $bytes_to_check ||= 1024
our $bytes_to_check
Truth table50%1/2
A
A= our $bytes_to_check
Aresult
11
00
33 4 ▶our $default_encoding ||= 'cp1252';
Condition50%1/2
our $default_encoding ||= "cp1252"
l!l
04
MC/DC0%0/1
our $default_encoding ||= "cp1252"
our $default_encoding
Truth table50%1/2
A
A= our $default_encoding
Aresult
11
00
34  
35 4 our @EXPORT = qw(html_file);
36 4 our @EXPORT_OK = qw(find_charset_in html_file_and_encoding html_outfile
37 sniff_encoding);
38  
39 4 our %EXPORT_TAGS = (
40 rw => [qw( html_file html_file_and_encoding html_outfile )],
41 all => [ @EXPORT, @EXPORT_OK ],
42 );
43  
44 #=====================================================================
45  
46  
47 sub html_file
48 18 ▶{
Subroutine html_file: called CC=1 SCAR=0.0
Pod documented
49 18 (&html_file_and_encoding)[0]; # return just the filehandle
50 } # end html_file
51  
52  
53 # Note: I made html_file and html_file_and_encoding separate functions
54 # (instead of making html_file context-sensitive) because I wanted to
55 # use html_file in function calls (i.e. list context) without having
56 # to write "scalar html_file" all the time.
57  
58 sub html_file_and_encoding
59 36 ▶{
Subroutine html_file_and_encoding: called CC=7 SCAR=19.7
Pod documented
60 36 my ($filename, $options) = @_;
61  
62 36 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
828
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
63  
64 36 ▶ open(my $in, '<:raw', $filename) or croak "Failed to open $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $in, "<:raw", $filename or croak("Failed to open ${filename}: $!") 36 0 36
65  
66  
67 36 my ($encoding, $bom) = sniff_encoding($in, $filename, $options);
68  
69 36 ▶ if (not defined $encoding) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $encoding) 8 28 36
70 8 ▶ croak "No default encoding specified"
Branch: 1 branch
BranchTrueFalseTotal
unless defined($encoding = $IO::HTML::default_encoding) 0 8 8
71 unless defined($encoding = $default_encoding);
72 8 ▶ $encoding = find_encoding($encoding) if $options->{encoding};
Branch: 1 branch
BranchTrueFalseTotal
if $options->{"encoding"} 0 8 8
73 } # end if we didn't find an encoding
74  
75 binmode $in, sprintf(":encoding(%s):crlf",
76 36 ▶ $options->{encoding} ? $encoding->name : $encoding);
Branch: 1 branch
BranchTrueFalseTotal
$options->{'encoding'} ? : 6 30 36
77  
78 36 return ($in, $encoding, $bom);
79 } # end html_file_and_encoding
80 #---------------------------------------------------------------------
81  
82  
83 sub html_outfile
84 6 ▶{
Subroutine html_outfile: called CC=6 SCAR=19.3
Pod documented
85 6 my ($filename, $encoding, $bom) = @_;
86  
87 6 ▶ if (not defined $encoding) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $encoding) { } 0 6 6
elsif (ref $encoding) { } 1 5 6
88 0 ▶ croak "No default encoding specified"
Branch: 1 branch
BranchTrueFalseTotal
unless defined($encoding = $IO::HTML::default_encoding) 0 0 0
89 unless defined($encoding = $default_encoding);
90 } # end if we didn't find an encoding
91 elsif (ref $encoding) {
92 1 $encoding = $encoding->name;
93 }
94  
95 6 ▶ open(my $out, ">:encoding($encoding)", $filename)
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">:encoding($encoding)", $filename or croak("Failed to open ${filename}: $!") 6 0 6
96 or croak "Failed to open $filename: $!";
97  
98 6 ▶ print $out "\x{FeFF}" if $bom;
Branch: 1 branch
BranchTrueFalseTotal
if $bom 3 3 6
99  
100 6 return $out;
101 } # end html_outfile
102 #---------------------------------------------------------------------
103  
104  
105 sub sniff_encoding
106 72 ▶{
Subroutine sniff_encoding: called CC=18 SCAR=29.0
Pod documented
107 72 my ($in, $filename, $options) = @_;
108  
109 72 ▶ $filename = 'file' unless defined $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $filename 36 36 72
110 72 ▶ $options ||= {};
Condition100%2/2
$options ||= {}
l!l
5814
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
111  
112 72 my $pos = tell $in;
113 72 ▶ croak "Could not seek $filename: $!" if $pos < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 0 72 72
114  
115 72 ▶ croak "Could not read $filename: $!"
Branch: 1 branch
BranchTrueFalseTotal
unless defined read($in, my $buf, $IO::HTML::bytes_to_check) 0 72 72
116 unless defined read $in, my($buf), $bytes_to_check;
117  
118 72 ▶ seek $in, $pos, 0 or croak "Could not seek $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $in, $pos, 0 or croak("Could not seek ${filename}: $!") 72 0 72
119  
120  
121 # Check for BOM:
122 72 my $bom;
123 72 my $encoding = do {
124 72 ▶ if ($buf =~ /^\xFe\xFF/) {
Branch: 3 branches
BranchTrueFalseTotal
if ($buf =~ /^\xFe\xFF/) { } 4 68 72
elsif ($buf =~ /^\xFF\xFe/) { } 8 60 68
elsif ($buf =~ /^\xEF\xBB\xBF/) { } 4 56 60
125 4 $bom = 2;
126 4 'UTF-16BE';
127 } elsif ($buf =~ /^\xFF\xFe/) {
128 8 $bom = 2;
129 8 'UTF-16LE';
130 } elsif ($buf =~ /^\xEF\xBB\xBF/) {
131 4 $bom = 3;
132 4 'utf-8-strict';
133 } else {
134 56 find_charset_in($buf, $options); # check for <meta charset>
135 }
136 }; # end $encoding
137  
138 72 ▶ if ($bom) {
Branch: 2 branches
BranchTrueFalseTotal
if ($bom) { } 16 56 72
elsif (not defined $encoding) { } 28 28 56
139 16 ▶ seek $in, $bom, 1 or croak "Could not seek $filename: $!";
Branch: 1 branch
BranchTrueFalseTotal
seek $in, $bom, 1 or croak("Could not seek ${filename}: $!") 16 0 16
140 16 $bom = 1;
141 }
142 elsif (not defined $encoding) { # try decoding as UTF-8
143 28 my $test = decode('utf-8-strict', $buf, Encode::FB_QUIET);
144 28 ▶ if ($buf =~ /^(?: # nothing left over
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ /^(?: # nothing left over | [\xC2-\xDF] # incomplete 2-byte char | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char )\z/x and $test =~ /[^\x00-\x7F]/) 12 16 28
Condition100%3/3
$buf =~ /^(?: # nothing left over | [\xC2-\xDF] # incomplete 2-byte char | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char )\z/x and $test =~ /[^\x00-\x7F]/
!ll&&!rl&&r
41212
MC/DC100%2/2
$buf =~ /^(?: # nothing left over | [\xC2-\xDF] # incomplete 2-byte char | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char )\z/x and $test =~ /[^\x00-\x7F]/
$buf =~ /^(?: # nothing left over | [\xC2-\xDF] # incomplete 2-byte char | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char )\z/x $test =~ /[^\x00-\x7F]/
Truth table100%3/3
A and B
A= $buf =~ /^(?: # nothing left over | [\xC2-\xDF] # incomplete 2-byte char | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char )\z/xB= $test =~ /[^\x00-\x7F]/
ABresult
0X0
100
111
145 | [\xC2-\xDF] # incomplete 2-byte char
146 | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char
147 | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char
148 )\z/x and $test =~ /[^\x00-\x7F]/) {
149 12 $encoding = 'utf-8-strict';
150 } # end if valid UTF-8 with at least one multi-byte character:
151 } # end if testing for UTF-8
152  
153 72 ▶ if (defined $encoding and $options->{encoding} and not ref $encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding and $options->{"encoding"} and not ref $encoding) 9 63 72
Condition100%6/6
defined $encoding and $options->{"encoding"} and not ref $encoding
!ll&&!rl&&r
52119
defined $encoding and $options->{"encoding"}
!ll&&!rl&&r
163620
MC/DC100%3/3
defined $encoding and $options->{"encoding"} and not ref $encoding
defined $encoding $options->{"encoding"} not ref $encoding
Truth table100%4/4
A and B and not(C)
A= defined $encodingB= $options->{"encoding"}C= not ref $encoding
ABCresult
0XX0
10X0
1100
1111
154 9 $encoding = find_encoding($encoding);
155 } # end if $encoding is a string and we want an object
156  
157 72 ▶ return wantarray ? ($encoding, $bom) : $encoding;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 72 0 72
158 } # end sniff_encoding
159  
160 #=====================================================================
161 # Based on HTML5 8.2.2.2 Determining the character encoding:
162  
163 # Get attribute from current position of $_
164 sub _get_attribute
165 286 ▶{
Subroutine _get_attribute: called CC=9 SCAR=23.0
166 286 m!\G[\x09\x0A\x0C\x0D /]+!gc; # skip whitespace or /
167  
168 286 ▶ return if /\G>/gc or not /\G(=?[^\x09\x0A\x0C\x0D =]*)/gc;
Branch: 1 branch
BranchTrueFalseTotal
if /\G>/cg or not /\G(=?[^\x09\x0A\x0C\x0D =]*)/cg 192 94 286
Condition100%3/3
/\G>/cg or not /\G(=?[^\x09\x0A\x0C\x0D =]*)/cg
l!l&&r!l&&!r
184894
MC/DC100%2/2
/\G>/cg or not /\G(=?[^\x09\x0A\x0C\x0D =]*)/cg
/\G>/cg not /\G(=?[^\x09\x0A\x0C\x0D =]*)/cg
Truth table100%3/3
A or not(B)
A= /\G>/cgB= not /\G(=?[^\x09\x0A\x0C\x0D =]*)/cg
ABresult
1X1
011
000
169  
170 94 my ($name, $value) = (lc $1, '');
171  
172 94 ▶ if (/\G[\x09\x0A\x0C\x0D ]*=[\x09\x0A\x0C\x0D ]*/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G[\x09\x0A\x0C\x0D ]*=[\x09\x0A\x0C\x0D ]*/cg) 84 10 94
173 84 ▶ if (/\G"/gc) {
Branch: 2 branches
BranchTrueFalseTotal
if (/\G"/cg) { } 72 12 84
elsif (/\G'/cg) { } 0 12 12
174 # Double-quoted attribute value
175 72 /\G([^"]*)("?)/gc;
176 72 ▶ return unless $2; # Incomplete attribute (missing closing quote)
Branch: 1 branch
BranchTrueFalseTotal
unless $2 9 63 72
177 63 $value = lc $1;
178 } elsif (/\G'/gc) {
179 # Single-quoted attribute value
180 0 /\G([^']*)('?)/gc;
181 0 ▶ return unless $2; # Incomplete attribute (missing closing quote)
Branch: 1 branch
BranchTrueFalseTotal
unless $2 0 0 0
182 0 $value = lc $1;
183 } else {
184 # Unquoted attribute value
185 12 /\G([^\x09\x0A\x0C\x0D >]*)/gc;
186 12 $value = lc $1;
187 }
188 } # end if attribute has value
189  
190 85 ▶ return wantarray ? ($name, $value) : 1;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 85 0 85
191 } # end _get_attribute
192  
193 # Examine a meta value for a charset:
194 sub _get_charset_from_meta
195 19 ▶{
Subroutine _get_charset_from_meta: called CC=6 SCAR=20.3
196 19 for (shift) {
197 19 ▶ while (/charset[\x09\x0A\x0C\x0D ]*=[\x09\x0A\x0C\x0D ]*/ig) {
Branch: 1 branch
BranchTrueFalseTotal
if (/charset[\x09\x0A\x0C\x0D ]*=[\x09\x0A\x0C\x0D ]*/gi) 16 3 19
198 16 ▶ return $1 if (/\G"([^"]*)"/gc or
Branch: 1 branch
BranchTrueFalseTotal
if /\G"([^"]*)"/cg or /\G'([^']*)'/cg or /\G(?!['"])([^\x09\x0A\x0C\x0D ;]+)/cg 16 0 16
Condition33%2/6
/\G"([^"]*)"/cg or /\G'([^']*)'/cg or /\G(?!['"])([^\x09\x0A\x0C\x0D ;]+)/cg
l!l&&r!l&&!r
0160
/\G"([^"]*)"/cg or /\G'([^']*)'/cg
l!l&&r!l&&!r
0016
MC/DC0%0/3
/\G"([^"]*)"/cg or /\G'([^']*)'/cg or /\G(?!['"])([^\x09\x0A\x0C\x0D ;]+)/cg
/\G"([^"]*)"/cg /\G'([^']*)'/cg /\G(?!['"])([^\x09\x0A\x0C\x0D ;]+)/cg
Truth table25%1/4
A or B or C
A= /\G"([^"]*)"/cgB= /\G'([^']*)'/cgC= /\G(?!['"])([^\x09\x0A\x0C\x0D ;]+)/cg
ABCresult
1XX1
01X1
0011
0000
199 /\G'([^']*)'/gc or
200 /\G(?!['"])([^\x09\x0A\x0C\x0D ;]+)/gc);
201 }
202 } # end for value
203  
204 3 return undef;
205 } # end _get_charset_from_meta
206 #---------------------------------------------------------------------
207  
208  
209 sub find_charset_in
210 81 ▶{
Subroutine find_charset_in: called CC=26 SCAR=32.6
Pod documented
211 81 for (shift) {
212 81 ▶ my $options = shift || {};
Condition100%2/2
shift() || {}
l!l
5823
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
213 # search only the first $bytes_to_check bytes (default 1024)
214 81 ▶ my $stop = length > $bytes_to_check ? $bytes_to_check : length;
Branch: 1 branch
BranchTrueFalseTotal
length $_ > $IO::HTML::bytes_to_check ? : 3 78 81
215  
216 my $expect_pragma = (defined $options->{need_pragma}
217 81 ▶ ? $options->{need_pragma} : 1);
Branch: 1 branch
BranchTrueFalseTotal
defined $options->{'need_pragma'} ? : 13 68 81
218  
219 81 pos() = 0;
220 81 ▶ while (pos() < $stop) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $_ < $stop) 211 33 244
221 211 ▶ if (/\G<!--.*?(?<=--)>/sgc) {
Branch: 5 branches
BranchTrueFalseTotal
if (/\G<!--.*?(?<=--)>/cgs) { } 7 204 211
elsif (m[\G<meta(?=[\x09\x0A\x0C\x0D /])]cig) { } 63 141 204
elsif (m[\G</?[a-zA-Z][^\x09\x0A\x0C\x0D >]*]cg) { } 138 3 141
elsif (m[\G<[!/?][^>]*]cg) { } 0 3 3
elsif (/\G</cg) { } 0 3 3
222 } # Skip comment
223 elsif (m!\G<meta(?=[\x09\x0A\x0C\x0D /])!gic) {
224 63 my ($got_pragma, $need_pragma, $charset);
225  
226 63 ▶ while (my ($name, $value) = &_get_attribute) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &_get_attribute) 85 63 148
227 85 ▶ if ($name eq 'http-equiv' and $value eq 'content-type') {
Branch: 3 branches
BranchTrueFalseTotal
if ($name eq 'http-equiv' and $value eq 'content-type') { } 5 80 85
elsif ($name eq 'content' and not defined $charset) { } 19 61 80
elsif ($name eq 'charset') { } 38 23 61
Condition100%3/3
$name eq "http-equiv" and $value eq "content-type"
!ll&&!rl&&r
70105
MC/DC100%2/2
$name eq "http-equiv" and $value eq "content-type"
$name eq "http-equiv" $value eq "content-type"
Truth table100%3/3
A and B
A= $name eq "http-equiv"B= $value eq "content-type"
ABresult
0X0
100
111
228 5 $got_pragma = 1;
229 ▶ } elsif ($name eq 'content' and not defined $charset) {
Condition66%2/3
$name eq "content" and not defined $charset
!ll&&!rl&&r
61019
MC/DC50%1/2
$name eq "content" and not defined $charset
$name eq "content" not defined $charset
Truth table66%2/3
A and not(B)
A= $name eq "content"B= not defined $charset
ABresult
0X0
100
111
230 19 ▶ $need_pragma = $expect_pragma
Branch: 1 branch
BranchTrueFalseTotal
if defined($charset = _get_charset_from_meta($value)) 16 3 19
231 if defined($charset = _get_charset_from_meta($value));
232 } elsif ($name eq 'charset') {
233 38 $charset = $value;
234 38 $need_pragma = 0;
235 }
236 } # end while more attributes in this <meta> tag
237  
238 63 ▶ if (defined $need_pragma and (not $need_pragma or $got_pragma)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $need_pragma and !$need_pragma || $got_pragma) 49 14 63
Condition100%6/6
defined $need_pragma and !$need_pragma || $got_pragma
!ll&&!rl&&r
9549
not $need_pragma or $got_pragma
l!l&&r!l&&!r
4725
MC/DC100%3/3
defined $need_pragma and !$need_pragma || $got_pragma
defined $need_pragma not $need_pragma $got_pragma
Truth table100%4/4
A and not(B) or C
A= defined $need_pragmaB= not $need_pragmaC= $got_pragma
ABCresult
0XX0
1000
11X1
1011
239 49 ▶ $charset = 'UTF-8' if $charset =~ /^utf-?16/;
Branch: 1 branch
BranchTrueFalseTotal
if $charset =~ /^utf-?16/ 9 40 49
240 49 ▶ $charset = 'cp1252' if $charset eq 'iso-8859-1'; # people lie
Branch: 1 branch
BranchTrueFalseTotal
if $charset eq "iso-8859-1" 4 45 49
241 49 ▶ if (my $encoding = find_encoding($charset)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $encoding = &find_encoding($charset)) 48 1 49
242 48 ▶ return $options->{encoding} ? $encoding : $encoding->name;
Branch: 1 branch
BranchTrueFalseTotal
$options->{'encoding'} ? : 12 36 48
243 } # end if charset is a recognized encoding
244 } # end if found charset
245 } # end elsif <meta
246 elsif (m!\G</?[a-zA-Z][^\x09\x0A\x0C\x0D >]*!gc) {
247 138 ▶ 1 while &_get_attribute;
Branch: 1 branch
BranchTrueFalseTotal
&_get_attribute and do { '???' } 0 138 138
248 } # end elsif some other tag
249 elsif (m{\G<[!/?][^>]*}gc) {
250 } # skip unwanted things
251 elsif (m/\G</gc) {
252 } # skip < that doesn't open anything we recognize
253  
254 # Advance to the next <:
255 163 m/\G[^<]+/gc;
256 } # end while not at search boundary
257 } # end for string
258  
259 33 return undef; # Couldn't find a charset
260 } # end find_charset_in
261 #---------------------------------------------------------------------
262  
263  
264 # Shortcuts for people who don't like exported functions:
265 4 *file = \&html_file;
266 4 *file_and_encoding = \&html_file_and_encoding;
267 4 *outfile = \&html_outfile;
268  
269 #=====================================================================
270 # Package Return Value:
271  
272 4 1;
273  
274 __END__
↑ Summary