summaryrefslogtreecommitdiff
path: root/t/offset.t
blob: 840728d7c3bafec917b597644ab6fc1773c2584c (plain)
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
use strict;
use HTML::Parser ();
use Test::More tests => 1;

my $HTML = <<'EOT';

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html40/strict.dtd">

<foo bar baz=3>heisan
</foo> <?process>
<!-- comment -->
<xmp>xmp</xmp>

EOT

my $p = HTML::Parser->new(api_version => 3);

my $sum_len = 0;
my $count = 0;
my $err;

$p->handler(default =>
	    sub {
		my($offset, $length, $offset_end, $line, $col, $text) = @_;
		my $copy = $text;
		$copy =~ s/\n/\\n/g;
		substr($copy, 30) = "..." if length($copy) > 32;
		#diag sprintf ">>> %d.%d %s", $line, $col, $copy;
		if ($offset != $sum_len) {
		   diag "offset mismatch $offset vs $sum_len";
		   $err++;
                }
		if ($offset_end != $offset + $length) {
		   diag "offset_end $offset_end wrong";
		   $err++;
                }
		if ($length != length($text)) {
		   diag "length mismatch";
		   $err++;
		}
                if (substr($HTML, $offset, $length) ne $text) {
		   diag "content mismatch";
		   $err++;
		}
		$sum_len += $length;
		$count++;
	    },
	    'offset,length,offset_end,line,column,text');

for (split(//, $HTML)) {
   $p->parse($_);
}
$p->eof;

ok($count > 5 && !$err);