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

Diff of /markup/html/whatpm/t/HTML-tree.t

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by wakaba, Mon Apr 30 14:12:02 2007 UTC revision 1.28 by wakaba, Sun Apr 13 10:36:41 2008 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2  use strict;  use strict;
3    
4    my $DEBUG = $ENV{DEBUG};
5    
6  my $dir_name;  my $dir_name;
7    my $test_dir_name;
8  BEGIN {  BEGIN {
9    my $test_dir_name = 't/';    $test_dir_name = 't/';
10    $dir_name = 't/tree-construction/';    $dir_name = 't/tree-construction/';
11    my $skip = "You don't have make command";    my $skip = "You don't have make command";
12    eval q{    eval q{
# Line 19  BEGIN { Line 22  BEGIN {
22  }  }
23    
24  use Test;  use Test;
25  BEGIN { plan tests => 67 }  BEGIN { plan tests => 1920 }
26    
27  use Data::Dumper;  use Data::Dumper;
28  $Data::Dumper::Useqq = 1;  $Data::Dumper::Useqq = 1;
# Line 29  sub Data::Dumper::qquote { Line 32  sub Data::Dumper::qquote {
32    return q<qq'> . $s . q<'>;    return q<qq'> . $s . q<'>;
33  } # Data::Dumper::qquote  } # Data::Dumper::qquote
34    
35  for my $file_name (qw[  
36                        tests1.dat  if ($DEBUG) {
37                        tests2.dat    my $not_found = {%{$Whatpm::HTML::Debug::cp or {}}};
38                        tests3.dat    $Whatpm::HTML::Debug::cp_pass = sub {
39                        tests4.dat      my $id = shift;
40        delete $not_found->{$id};
41      };
42    
43      END {
44        for my $id (sort {$a <=> $b || $a cmp $b} keys %$not_found) {
45          print "# checkpoint $id is not reached\n";
46        }
47      }
48    }
49    
50    for my $file_name (grep {$_} split /\s+/, qq[
51                          ${test_dir_name}tokenizer-test-2.dat
52                          ${dir_name}tests1.dat
53                          ${dir_name}tests2.dat
54                          ${dir_name}tests3.dat
55                          ${dir_name}tests4.dat
56                          ${dir_name}tests5.dat
57                          ${dir_name}tests6.dat
58                          ${dir_name}tests7.dat
59                          ${test_dir_name}tree-test-1.dat
60                          ${test_dir_name}tree-test-2.dat
61                       ]) {                       ]) {
62    open my $file, '<', $dir_name.$file_name    open my $file, '<', $file_name
63      or die "$0: $dir_name$file_name: $!";      or die "$0: $file_name: $!";
64      print "# $file_name\n";
65    
66    my $test;    my $test;
67    my $mode = 'data';    my $mode = 'data';
68      my $escaped;
69    while (<$file>) {    while (<$file>) {
70      s/\x0D\x0A/\x0A/;      s/\x0D\x0A/\x0A/;
71      if (/^#data$/) {      if (/^#data$/) {
72        undef $test;        undef $test;
73        $test->{data} = '';        $test->{data} = '';
74        $mode = 'data';        $mode = 'data';
75          undef $escaped;
76        } elsif (/^#data escaped$/) {
77          undef $test;
78          $test->{data} = '';
79          $mode = 'data';
80          $escaped = 1;
81      } elsif (/^#errors$/) {      } elsif (/^#errors$/) {
82        $test->{errors} = [];        $test->{errors} = [];
83        $mode = 'errors';        $mode = 'errors';
84        $test->{data} =~ s/\x0D?\x0A\z//;              $test->{data} =~ s/\x0D?\x0A\z//;      
85          $test->{data} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
86          $test->{data} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
87          undef $escaped;
88      } elsif (/^#document$/) {      } elsif (/^#document$/) {
89        $test->{document} = '';        $test->{document} = '';
90        $mode = 'document';        $mode = 'document';
91      } elsif (/^$/) {        undef $escaped;
92        test ($test) if $test->{errors};      } elsif (/^#document escaped$/) {
93          $test->{document} = '';
94          $mode = 'document';
95          $escaped = 1;
96        } elsif (/^#document-fragment$/) {
97          $test->{element} = '';
98          $mode = 'element';
99          undef $escaped;
100        } elsif (/^#document-fragment (\S+)$/) {
101          $test->{document} = '';
102          $mode = 'document';
103          $test->{element} = $1;
104          undef $escaped;
105        } elsif (/^#document-fragment (\S+) escaped$/) {
106          $test->{document} = '';
107          $mode = 'document';
108          $test->{element} = $1;
109          $escaped = 1;
110        } elsif (defined $test->{document} and /^$/) {
111          $test->{document} =~ s/\\u([0-9A-Fa-f]{4})/chr hex $1/ge if $escaped;
112          $test->{document} =~ s/\\U([0-9A-Fa-f]{8})/chr hex $1/ge if $escaped;
113          test ($test);
114        undef $test;        undef $test;
115      } else {      } else {
116        if ($mode eq 'data' or $mode eq 'document') {        if ($mode eq 'data' or $mode eq 'document') {
117          $test->{$mode} .= $_;          $test->{$mode} .= $_;
118          } elsif ($mode eq 'element') {
119            tr/\x0D\x0A//d;
120            $test->{$mode} .= $_;
121        } elsif ($mode eq 'errors') {        } elsif ($mode eq 'errors') {
122          tr/\x0D\x0A//d;          tr/\x0D\x0A//d;
123          push @{$test->{errors}}, $_;          push @{$test->{errors}}, $_;
# Line 68  for my $file_name (qw[ Line 127  for my $file_name (qw[
127    test ($test) if $test->{errors};    test ($test) if $test->{errors};
128  }  }
129    
130  use What::HTML;  use Whatpm::HTML;
131    use Whatpm::NanoDOM;
132    
133  sub test ($) {  sub test ($) {
134    my $test = shift;    my $test = shift;
135    
136    my $s = $test->{data};    my $doc = Whatpm::NanoDOM::Document->new;
   
   my $p = What::HTML->new;  
   my $i = 0;  
   $p->{set_next_input_character} = sub {  
     my $self = shift;  
     $self->{next_input_character} = -1 and return if $i >= length $s;  
     $self->{next_input_character} = ord substr $s, $i++, 1;  
       
     if ($self->{next_input_character} == 0x000D) { # CR  
       if ($i >= length $s) {  
         #  
       } else {  
         my $next_char = ord substr $s, $i++, 1;  
         if ($next_char == 0x000A) { # LF  
           #  
         } else {  
           push @{$self->{char}}, $next_char;  
         }  
       }  
       $self->{next_input_character} = 0x000A; # LF # MUST  
     } elsif ($self->{next_input_character} > 0x10FFFF) {  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     } elsif ($self->{next_input_character} == 0x0000) { # NULL  
       $self->{next_input_character} = 0xFFFD; # REPLACEMENT CHARACTER # MUST  
     }  
   };  
     
137    my @errors;    my @errors;
   $p->{parse_error} = sub {  
     my $msg = shift;  
     push @errors, $msg;  
   };  
138        
139    $SIG{INT} = sub {    $SIG{INT} = sub {
140      print scalar serialize ($p->{document});      print scalar serialize ($doc);
141      exit;      exit;
142    };    };
     
   $p->_initialize_tokenizer;  
   $p->_initialize_tree_constructor;  
   $p->_construct_tree;  
   $p->_terminate_tree_constructor;  
143    
144      my $onerror = sub {
145        my %opt = @_;
146        push @errors, join ':', $opt{line}, $opt{column}, $opt{type};
147      };
148      my $result;
149      unless (defined $test->{element}) {
150        Whatpm::HTML->parse_string ($test->{data} => $doc, $onerror);
151        $result = serialize ($doc);
152      } else {
153        my $el = $doc->create_element_ns
154          ('http://www.w3.org/1999/xhtml', [undef, $test->{element}]);
155        Whatpm::HTML->set_inner_html ($el, $test->{data}, $onerror);
156        $result = serialize ($el);
157      }
158        
159    ok scalar @errors, scalar @{$test->{errors}},    ok scalar @errors, scalar @{$test->{errors}},
160      'Parse error: ' . $test->{data} . '; ' .      'Parse error: ' . Data::Dumper::qquote ($test->{data}) . '; ' .
161      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});      join (', ', @errors) . ';' . join (', ', @{$test->{errors}});
162    
163    my $doc = $p->{document};    ok $result, $test->{document},
164    my $doc_s = serialize ($doc);        'Document tree: ' . Data::Dumper::qquote ($test->{data});
   ok $doc_s, $test->{document}, 'Document tree: ' . $test->{data};  
165  } # test  } # test
166    
167  sub serialize ($) {  sub serialize ($) {
# Line 137  sub serialize ($) { Line 175  sub serialize ($) {
175      if ($nt == $child->[0]->ELEMENT_NODE) {      if ($nt == $child->[0]->ELEMENT_NODE) {
176        $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?        $r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case?
177    
178        for my $attr (sort {$a->[1] cmp $b->[1]} map { [$_->name, $_->value] }        for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] }
179                      @{$child->[0]->attributes}) {                      @{$child->[0]->attributes}) {
180          $r .= '| ' . $child->[1] . '  ' . $attr->[0] . '="'; ## ISSUE: case?          $r .= '| ' . $child->[1] . '  ' . $attr->[0] . '="'; ## ISSUE: case?
181          $r .= $attr->[1] . '"' . "\x0A";          $r .= $attr->[1] . '"' . "\x0A";
# Line 150  sub serialize ($) { Line 188  sub serialize ($) {
188      } elsif ($nt == $child->[0]->COMMENT_NODE) {      } elsif ($nt == $child->[0]->COMMENT_NODE) {
189        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";        $r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A";
190      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {      } elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) {
191        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A";        $r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name;
192          my $pubid = $child->[0]->public_id;
193          $r .= ' PUBLIC "' . $pubid . '"' if length $pubid;
194          my $sysid = $child->[0]->system_id;
195          $r .= ' SYSTEM' if not length $pubid and length $sysid;
196          $r .= ' "' . $sysid . '"' if length $sysid;
197          $r .= ">\x0A";
198      } else {      } else {
199        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error        $r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error
200      }      }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.28

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24