1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
|
#escapeChars {return}
#use lib '../lib';
use strict;
use warnings;
use HTML::Copy;
use utf8;
use File::Spec::Functions;
#use Data::Dumper;
use Encode qw(encode_utf8 decode_utf8);
use Test::More tests => 16;
sub read_and_unlink {
my ($path, $htmlcopy) = @_;
open(my $in, "<".$htmlcopy->io_layer(), $path)
or die "Can't open $path.";
my $contents = do {local $/; <$in>};
close $in;
unlink($path);
return $contents;
}
##== prepare linked HTML file
my $linked_html = <<EOT;
<!DOCTYPE html>
<html>
</html>
EOT
my $linked_file_name = "linked$$.html";
open(my $linked_fh, ">", $linked_file_name)
or die "Can't open $linked_file_name.";
print $linked_fh $linked_html;
close $linked_fh;
##== HTML data without charsets
my $source_html_nocharset = <<EOT;
<!DOCTYPE html>
<html>
ああ
<a href="$linked_file_name#anchor"></a>
<frame src="$linked_file_name">
<img src="$linked_file_name">
<script src="$linked_file_name"></script>
<link href="$linked_file_name">
</html>
EOT
my $result_html_nocharset = <<EOT;
<!DOCTYPE html>
<html>
ああ
<a href="../$linked_file_name#anchor"></a>
<frame src="../$linked_file_name">
<img src="../$linked_file_name">
<script src="../$linked_file_name"></script>
<link href="../$linked_file_name">
</html>
EOT
##== write test data
my $sub_dir_name = "sub$$";
mkdir($sub_dir_name);
my $src_file_name = "file$$.html";
my $destination = catfile($sub_dir_name, $src_file_name);
##== Test code with no charsets HTML
open(my $src_fh, ">:utf8", $src_file_name)
or die "Can't open $src_file_name.";
print $src_fh $source_html_nocharset;
close $src_fh;
##=== parse_to UTF-8
my $p = HTML::Copy->new($src_file_name);
my $copy_html = $p->parse_to($destination);
ok($copy_html eq $result_html_nocharset, "parse_to no charset UTF-8");
##=== copty_to UTF8
$p->copy_to($destination);
open(my $in, "<".$p->io_layer(), $destination)
or die "Can't open $destination.";
$copy_html = read_and_unlink($destination, $p);
ok($copy_html eq $result_html_nocharset, "copy_to no charset UTF-8");
##=== write data with shift_jis
open($src_fh, ">:encoding(shiftjis)", $src_file_name)
or die "Can't open $src_file_name.";
print $src_fh $source_html_nocharset;
close $src_fh;
##=== parse_to shift_jis
$p = HTML::Copy->new($src_file_name);
$copy_html = do {
$p->encode_suspects("shiftjis");
$p->parse_to(catfile($sub_dir_name, $src_file_name));
};
ok($copy_html eq $result_html_nocharset, "parse_to no charset shift_jis");
##=== copy_to shift_jis
$copy_html = do {
$p->copy_to($destination);
open(my $in, "<".$p->io_layer, $destination)
or die "Can't open $destination.";
read_and_unlink($destination, $p);
};
ok($copy_html eq $result_html_nocharset, "copy_to no charset shift_jis");
##== HTML with charset uft-8
my $src_html_utf8 = encode_utf8(<<EOT);
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
ああ
<a href="$linked_file_name"></a>
<frame src="$linked_file_name">
<img src="$linked_file_name">
<script src="$linked_file_name"></script>
<link href="$linked_file_name">
</body>
</html>
EOT
my $result_html_utf8 = encode_utf8(<<EOT);
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
</head>
<body>
ああ
<a href="../$linked_file_name"></a>
<frame src="../$linked_file_name">
<img src="../$linked_file_name">
<script src="../$linked_file_name"></script>
<link href="../$linked_file_name">
</body>
</html>
EOT
##== Test code with charset utf-8
open($src_fh, ">:utf8", $src_file_name)
or die "Can't open $src_file_name.";
print $src_fh $src_html_utf8;
close $src_fh;
##=== parse_to
$p = HTML::Copy->new($src_file_name);
$copy_html = $p->parse_to($destination);
ok($copy_html eq $result_html_utf8, "parse_to charset UTF-8");
##=== copy_to
$p->copy_to($destination);
$copy_html = read_and_unlink($destination, $p);
ok($copy_html eq $result_html_utf8, "copy_to charset UTF-8");
##=== copy_to gving a file handle
$copy_html = do {
open $in, "<", \$src_html_utf8;
my $outdata ='';;
my $p = HTML::Copy->new($in);
open my $out, ">", $destination;
$p->destination_path($destination);
$p->copy_to($out);
close $out;
read_and_unlink($destination, $p);
};
ok($copy_html eq decode_utf8($result_html_utf8), "copy_to giving a file handle");
##=== copy_to gving file handles for input and output
$copy_html = do {
open my $in, "<", \$src_html_utf8;
my $outdata;
my $p = HTML::Copy->new($in);
open my $out, ">".$p->io_layer(), \$outdata;
$p->destination_path($destination);
$p->copy_to($out);
Encode::decode($p->encoding, $outdata);
};
ok($copy_html eq decode_utf8($result_html_utf8), "copy_to giving file handles for input and output");
##=== parse_to giving a file handle
$copy_html = do {
open my $in, "<", \$src_html_utf8;
my $p = HTML::Copy->new($in);
$p->parse_to($destination);
};
ok($copy_html eq decode_utf8($result_html_utf8), "copy_to giving file handles for input and output");
##=== copy_to with directory destination
$copy_html = do {
my $p = HTML::Copy->new($src_file_name);
my $destination = $p->copy_to($sub_dir_name);
read_and_unlink($destination, $p);
};
ok($copy_html eq $result_html_utf8, "copy_to with a directory destination");
##== HTML with charset shift_jis
my $src_html_shiftjis = <<EOT;
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=shift_jis">
</head>
ああ
<a href="$linked_file_name"></a>
<frame src="$linked_file_name">
<img src="$linked_file_name">
<script src="$linked_file_name"></script>
<link href="$linked_file_name">
</html>
EOT
my $result_html_shiftjis = <<EOT;
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=shift_jis">
</head>
ああ
<a href="../$linked_file_name"></a>
<frame src="../$linked_file_name">
<img src="../$linked_file_name">
<script src="../$linked_file_name"></script>
<link href="../$linked_file_name">
</html>
EOT
##== Test code with charset shift_jis
open($src_fh, ">:encoding(shiftjis)", $src_file_name)
or die "Can't open $src_file_name.";
print $src_fh $src_html_shiftjis;
close $src_fh;
##=== parse_to
$p = HTML::Copy->new($src_file_name);
$p->encode_suspects("shiftjis");
$copy_html = $p->parse_to($destination);
ok($copy_html eq $result_html_shiftjis, "parse_to no charset shift_jis");
##=== copy_to
$p->copy_to($destination);
$copy_html = read_and_unlink($destination, $p);
ok($copy_html eq $result_html_shiftjis, "copy_to no charset shift_jis");
##== class_methods
$copy_html = HTML::Copy->parse_file($src_file_name, $destination);
ok($copy_html eq $result_html_shiftjis, "parse_file");
HTML::Copy->htmlcopy($src_file_name, $destination);
open($in, "<".$p->io_layer, $destination)
or die "Can't open $destination.";
{local $/; $copy_html = <$in>};
close $in;
ok($copy_html eq $result_html_shiftjis, "htmlcopy");
unlink($destination);
##== Test with base url
my $src_html_base = <<EOT;
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<base href="http://homepage.mac.com/tkurita/scriptfactory/">
</head>
ああ
<a href="$linked_file_name"></a>
<frame src="$linked_file_name">
<img src="$linked_file_name">
<script src="$linked_file_name"></script>
<link href="$linked_file_name">
</html>
EOT
##== Test code with base url
open($src_fh, ">:utf8", $src_file_name)
or die "Can't open $destination.";
print $src_fh $src_html_base;
close $src_fh;
##=== parse_to
$p = HTML::Copy->new($src_file_name);
$copy_html = $p->parse_to($destination);
ok($copy_html eq $src_html_base, "parse_to HTML with base URL");
##=== copy_to
$p->copy_to($destination);
open($in, "<".$p->io_layer, $destination)
or die "Can't open $destination.";
{local $/; $copy_html = <$in>};
close $in;
ok($copy_html eq $src_html_base, "copy_to HTML with base URL");
unlink($destination);
unlink($linked_file_name, $src_file_name, $destination);
rmdir($sub_dir_name);
|