| 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
MC/DC0%0/1
our $bytes_to_check ||= 1024
our $bytes_to_check
Truth table50%1/2
A
A= our $bytes_to_check
| |||||||||||||||||||||||||||||||||||||||||||
| 33 | 4 | ▶ | our $default_encoding ||= 'cp1252'; | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
our $default_encoding ||= "cp1252"
MC/DC0%0/1
our $default_encoding ||= "cp1252"
our $default_encoding
Truth table50%1/2
A
A= our $default_encoding
| |||||||||||||||||||||||||||||||||||||||||||
| 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 ||= {}
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | 36 | ▶ | open(my $in, '<:raw', $filename) or croak "Failed to open $filename: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||||||
| 67 | 36 | my ($encoding, $bom) = sniff_encoding($in, $filename, $options); | |||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | 36 | ▶ | if (not defined $encoding) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 8 | ▶ | croak "No default encoding specified" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | unless defined($encoding = $default_encoding); | ||||||||||||||||||||||||||||||||||||||||||
| 72 | 8 | ▶ | $encoding = find_encoding($encoding) if $options->{encoding}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 0 | ▶ | croak "No default encoding specified" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 96 | or croak "Failed to open $filename: $!"; | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 6 | ▶ | print $out "\x{FeFF}" if $bom; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 110 | 72 | ▶ | $options ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$options ||= {}
MC/DC100%1/1
$options ||= {}
$options
Truth table100%2/2
A
A= $options
| |||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | 72 | my $pos = tell $in; | |||||||||||||||||||||||||||||||||||||||||
| 113 | 72 | ▶ | croak "Could not seek $filename: $!" if $pos < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | 72 | ▶ | croak "Could not read $filename: $!" | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||
| 121 | # Check for BOM: | ||||||||||||||||||||||||||||||||||||||||||
| 122 | 72 | my $bom; | |||||||||||||||||||||||||||||||||||||||||
| 123 | 72 | my $encoding = do { | |||||||||||||||||||||||||||||||||||||||||
| 124 | 72 | ▶ | if ($buf =~ /^\xFe\xFF/) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 139 | 16 | ▶ | seek $in, $bom, 1 or croak "Could not seek $filename: $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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]/
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]/
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
defined $encoding and $options->{"encoding"} and not ref $encoding
defined $encoding and $options->{"encoding"}
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
/\G>/cg or not /\G(=?[^\x09\x0A\x0C\x0D =]*)/cg
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 173 | 84 | ▶ | if (/\G"/gc) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 174 | # Double-quoted attribute value | ||||||||||||||||||||||||||||||||||||||||||
| 175 | 72 | /\G([^"]*)("?)/gc; | |||||||||||||||||||||||||||||||||||||||||
| 176 | 72 | ▶ | return unless $2; # Incomplete attribute (missing closing quote) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | 16 | ▶ | return $1 if (/\G"([^"]*)"/gc or | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%2/6
/\G"([^"]*)"/cg or /\G'([^']*)'/cg or /\G(?!['"])([^\x09\x0A\x0C\x0D ;]+)/cg
/\G"([^"]*)"/cg or /\G'([^']*)'/cg
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||
| 216 | my $expect_pragma = (defined $options->{need_pragma} | ||||||||||||||||||||||||||||||||||||||||||
| 217 | 81 | ▶ | ? $options->{need_pragma} : 1); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||
| 219 | 81 | pos() = 0; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 81 | ▶ | while (pos() < $stop) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | 211 | ▶ | if (/\G<!--.*?(?<=--)>/sgc) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 227 | 85 | ▶ | if ($name eq 'http-equiv' and $value eq 'content-type') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition100%3/3
$name eq "http-equiv" and $value eq "content-type"
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"
| |||||||||||||||||||||||||||||||||||||||||||
| 228 | 5 | $got_pragma = 1; | |||||||||||||||||||||||||||||||||||||||||
| 229 | ▶ | } elsif ($name eq 'content' and not defined $charset) { | |||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$name eq "content" and not defined $charset
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
| |||||||||||||||||||||||||||||||||||||||||||
| 230 | 19 | ▶ | $need_pragma = $expect_pragma | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%6/6
defined $need_pragma and !$need_pragma || $got_pragma
not $need_pragma or $got_pragma
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
| |||||||||||||||||||||||||||||||||||||||||||
| 239 | 49 | ▶ | $charset = 'UTF-8' if $charset =~ /^utf-?16/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 240 | 49 | ▶ | $charset = 'cp1252' if $charset eq 'iso-8859-1'; # people lie | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 241 | 49 | ▶ | if (my $encoding = find_encoding($charset)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 242 | 48 | ▶ | return $options->{encoding} ? $encoding : $encoding->name; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||