/[suikacvs]/markup/html/whatpm/t/HTML-tokenizer.t
Suika

Contents of /markup/html/whatpm/t/HTML-tokenizer.t

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (hide annotations) (download) (as text)
Wed May 2 13:44:35 2007 UTC (18 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.5: +2 -2 lines
File MIME type: application/x-troff
++ ChangeLog	2 May 2007 13:37:34 -0000
2007-05-02  Wakaba  <wakaba@suika.fam.cx>

	* readme.en.html: TODO section is added.

++ whatpm/t/ChangeLog	2 May 2007 13:44:02 -0000
2007-05-02  Wakaba  <wakaba@suika.fam.cx>

	* .cvsignore: Result files are added.

	* HTML-tree.t: Support for document fragment tests.

	* Makefile: Generate test result files.

	* tokenizer-test-1.test: A new test to ensure that
	characters after end tag are preserved in RCDATA or CDATA
	case.

++ whatpm/Whatpm/ChangeLog	2 May 2007 13:42:17 -0000
2007-05-02  Wakaba  <wakaba@suika.fam.cx>

	* NanoDOM.pm (DOMImplementation): New class.
	(append_child): Weaken the |parent_node| reference.
	(create_element_ns, Element new): Set the |owner_document|
	reference.
	(implementation): New attribute.
	(owner_document, local_name, namespace_uri): New attributes.

	* HTML.pm.src (parse_string): Line and column numbers
	are now provided to error handler.
	(!!!parse-error): Short descriptions are added.
	(_construct_tree): Split into three methods; support
	for innerHTML mode.
	(set_inner_html): New method.

1 wakaba 1.1 #!/usr/bin/perl
2     use strict;
3    
4 wakaba 1.2 my $dir_name;
5 wakaba 1.4 my $test_dir_name;
6 wakaba 1.1 BEGIN {
7 wakaba 1.4 $test_dir_name = 't/';
8 wakaba 1.2 $dir_name = 't/tokenizer/';
9 wakaba 1.1 my $skip = "You don't have JSON module";
10     eval q{
11     use JSON 1.00;
12     $skip = "You don't have make command";
13 wakaba 1.3 system ("cd $test_dir_name; make tokenizer-files") == 0 or die
14 wakaba 1.2 unless -f $dir_name.'test1.test';
15 wakaba 1.1 $skip = '';
16     };
17     if ($skip) {
18     print "1..1\n";
19     print "ok 1 # $skip\n";
20     exit;
21     }
22     $JSON::UnMapping = 1;
23 wakaba 1.2 $JSON::UTF8 = 1;
24 wakaba 1.1 }
25    
26     use Test;
27 wakaba 1.6 BEGIN { plan tests => 72 }
28 wakaba 1.2
29 wakaba 1.1 use Data::Dumper;
30 wakaba 1.2 $Data::Dumper::Useqq = 1;
31     sub Data::Dumper::qquote {
32     my $s = shift;
33     $s =~ s/([^\x20\x21-\x26\x28-\x5B\x5D-\x7E])/sprintf '\x{%02X}', ord $1/ge;
34     return q<qq'> . $s . q<'>;
35     } # Data::Dumper::qquote
36 wakaba 1.1
37 wakaba 1.5 use Whatpm::HTML;
38 wakaba 1.1
39 wakaba 1.4 for my $file_name (grep {$_} split /\s+/, qq[
40     ${dir_name}test1.test
41     ${dir_name}test2.test
42     ${dir_name}contentModelFlags.test
43     ${test_dir_name}tokenizer-test-1.test
44 wakaba 1.1 ]) {
45 wakaba 1.4 open my $file, '<', $file_name
46     or die "$0: $file_name: $!";
47 wakaba 1.1 local $/ = undef;
48     my $js = <$file>;
49     close $file;
50    
51     my $tests = jsonToObj ($js)->{tests};
52     TEST: for my $test (@$tests) {
53     my $s = $test->{input};
54    
55     my $j = 1;
56     while ($j < @{$test->{output}}) {
57     if (ref $test->{output}->[$j - 1] and
58     $test->{output}->[$j - 1]->[0] eq 'Character' and
59     ref $test->{output}->[$j] and
60     $test->{output}->[$j]->[0] eq 'Character') {
61     $test->{output}->[$j - 1]->[1]
62     .= $test->{output}->[$j]->[1];
63     splice @{$test->{output}}, $j, 1;
64     }
65     $j++;
66     }
67    
68 wakaba 1.2 my @cm = @{$test->{contentModelFlags} || ['PCDATA']};
69     my $last_start_tag = $test->{lastStartTag};
70 wakaba 1.1 for my $cm (@cm) {
71 wakaba 1.5 my $p = Whatpm::HTML->new;
72 wakaba 1.1 my $i = 0;
73     $p->{set_next_input_character} = sub {
74     my $self = shift;
75     $self->{next_input_character} = -1 and return if $i >= length $s;
76     $self->{next_input_character} = ord substr $s, $i++, 1;
77 wakaba 1.2
78     if ($self->{next_input_character} == 0x000D) { # CR
79     if ($i >= length $s) {
80     #
81     } else {
82     my $next_char = ord substr $s, $i++, 1;
83     if ($next_char == 0x000A) { # LF
84     #
85     } else {
86     push @{$self->{char}}, $next_char;
87     }
88     }
89     $self->{next_input_character} = 0x000A; # LF # MUST
90     } elsif ($self->{next_input_character} > 0x10FFFF) {
91     $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
92     } elsif ($self->{next_input_character} == 0x0000) { # NULL
93     $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST
94     }
95 wakaba 1.1 };
96    
97     my @token;
98     $p->{parse_error} = sub {
99     push @token, 'ParseError';
100     };
101    
102     $p->_initialize_tokenizer;
103     $p->{content_model_flag} = $cm;
104 wakaba 1.2 $p->{last_emitted_start_tag_name} = $last_start_tag;
105 wakaba 1.1
106     while (1) {
107     my $token = $p->_get_next_token;
108     last if $token->{type} eq 'end-of-file';
109    
110     my $test_token = [
111     {
112     DOCTYPE => 'DOCTYPE',
113     'start tag' => 'StartTag',
114     'end tag' => 'EndTag',
115     comment => 'Comment',
116     character => 'Character',
117     }->{$token->{type}} || $token->{type},
118     ];
119     $test_token->[1] = $token->{name} if defined $token->{name};
120     $test_token->[1] = $token->{tag_name} if defined $token->{tag_name};
121     $test_token->[1] = $token->{data} if defined $token->{data};
122     $test_token->[2] = $token->{error} ? 1 : 0 if $token->{type} eq 'DOCTYPE';
123     $test_token->[2] = {map {$_->{name} => $_->{value}} values %{$token->{attributes}}}
124     if $token->{type} eq 'start tag';
125    
126     if (@token and ref $token[-1] and $token[-1]->[0] eq 'Character' and
127     $test_token->[0] eq 'Character') {
128     $token[-1]->[1] .= $test_token->[1];
129     } else {
130     push @token, $test_token;
131     }
132     }
133    
134     my $expected_dump = Dumper ($test->{output});
135     my $parser_dump = Dumper (\@token);
136 wakaba 1.2 ok $parser_dump, $expected_dump,
137     $test->{description} . ': ' . $test->{input};
138 wakaba 1.1 }
139     }
140     }
141    
142 wakaba 1.6 ## $Date: 2007/05/01 10:47:37 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24