/[pub]/test/html-webhacc/cc.cgi
Suika

Diff of /test/html-webhacc/cc.cgi

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

revision 1.6 by wakaba, Sat Jun 30 14:51:10 2007 UTC revision 1.68 by wakaba, Wed Sep 17 03:56:43 2008 UTC
# Line 1  Line 1 
1  #!/usr/bin/perl  #!/usr/bin/perl
2    # -d:DProf
3  use strict;  use strict;
4    
5  use lib qw[/home/httpd/html/www/markup/html/whatpm  use lib qw[/home/httpd/html/www/markup/html/whatpm
6             /home/wakaba/work/manakai/lib             /home/wakaba/work/manakai2/lib];
            /home/wakaba/public_html/-temp/wiki/lib];  
7  use CGI::Carp qw[fatalsToBrowser];  use CGI::Carp qw[fatalsToBrowser];
 use Scalar::Util qw[refaddr];  
8    
9  use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module    require WebHACC::Input;
10    
11  sub htescape ($) {  {
12    my $s = $_[0];    require Message::CGI::HTTP;
13    $s =~ s/&/&/g;    my $http = Message::CGI::HTTP->new;
14    $s =~ s/</&lt;/g;  
15    $s =~ s/>/&gt;/g;    require WebHACC::Output;
16    $s =~ s/"/&quot;/g;    my $out = WebHACC::Output->new;
17    $s =~ s!([\x00-\x09\x0B-\x1F\x7F-\x80])!sprintf '<var>U+%04X</var>', ord $1!ge;    $out->handle (*STDOUT);
18    return $s;    $out->set_utf8;
 } # htescape  
   
 my $http = SuikaWiki::Input::HTTP->new;  
   
 ## TODO: _charset_  
   
   my $input_format = $http->parameter ('i') || 'text/html';  
   my $inner_html_element = $http->parameter ('e');  
   my $input_uri = 'thismessage:/';  
   
   my $s = $http->parameter ('s');  
   if (length $s > 1000_000) {  
     print STDOUT "Status: 400 Document Too Long\nContent-Type: text/plain; charset=us-ascii\n\nToo long";  
     exit;  
   }  
   
   my @nav;  
   print STDOUT qq[Content-Type: text/html; charset=utf-8  
   
 <!DOCTYPE html>  
 <html lang="en">  
 <head>  
 <title>Web Document Conformance Checker (BETA)</title>  
 <link rel="stylesheet" href="../cc-style.css" type="text/css">  
 </head>  
 <body>  
 <h1>Web Document Conformance Checker (<em>beta</em>)</h1>  
   
 <div id="document-info" class="section">  
 <dl>  
 <dt>Document URI</dt>  
     <dd><code class="URI" lang="">&lt;<a href="@{[htescape $input_uri]}">@{[htescape $input_uri]}</a>&gt;</code></dd>  
 <dt>Internet Media Type</dt>  
     <dd><code class="MIME" lang="en">@{[htescape $input_format]}</code></dd>  
 ]; # no </dl> yet  
   push @nav, ['#document-info' => 'Information'];  
   
   require Message::DOM::DOMImplementation;  
   my $dom = Message::DOM::DOMImplementation->____new;  
   my $doc;  
   my $el;  
   
   if ($input_format eq 'text/html') {  
     require Encode;  
     require Whatpm::HTML;  
       
     $s = Encode::decode ('utf-8', $s);  
   
     print STDOUT qq[  
 <dt>Character Encoding</dt>  
     <dd>(none)</dd>  
 </dl>  
 </div>  
   
 <div id="source-string" class="section">  
 <h2>Document Source</h2>  
 ];  
     push @nav, ['#source-string' => 'Source'];  
     print_source_string (\$s);  
     print STDOUT qq[  
 </div>  
   
 <div id="parse-errors" class="section">  
 <h2>Parse Errors</h2>  
   
 <dl>  
 ];  
   push @nav, ['#parse-errors' => 'Parse Error'];  
   
   my $onerror = sub {  
     my (%opt) = @_;  
     if ($opt{column} > 0) {  
       print STDOUT qq[<dt><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}</dt>\n];  
     } else {  
       $opt{line}--;  
       print STDOUT qq[<dt><a href="#line-$opt{line}">Line $opt{line}</a></dt>\n];  
     }  
     print STDOUT qq[<dd>@{[htescape $opt{type}]}</dd>\n];  
   };  
   
   $doc = $dom->create_document;  
   if (defined $inner_html_element and length $inner_html_element) {  
     $el = $doc->create_element_ns  
         ('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]);  
     Whatpm::HTML->set_inner_html ($el, $s, $onerror);  
   } else {  
     Whatpm::HTML->parse_string ($s => $doc, $onerror);  
   }  
   
   print STDOUT qq[  
 </dl>  
 </div>  
 ];  
   } elsif ($input_format eq 'application/xhtml+xml') {  
     require Message::DOM::XMLParserTemp;  
     require Encode;  
       
     my $t = Encode::decode ('utf-8', $s);  
   
     print STDOUT qq[  
 <dt>Character Encoding</dt>  
     <dd>(none)</dd>  
 </dl>  
 </div>  
   
 <div id="source-string" class="section">  
 <h2>Document Source</h2>  
 ];  
     push @nav, ['#source-string' => 'Source'];  
     print_source_string (\$t);  
     print STDOUT qq[  
 </div>  
   
 <div id="parse-errors" class="section">  
 <h2>Parse Errors</h2>  
   
 <dl>  
 ];  
   push @nav, ['#parse-errors' => 'Parse Error'];  
   
   my $onerror = sub {  
     my $err = shift;  
     my $line = $err->location->line_number;  
     print STDOUT qq[<dt><a href="#line-$line">Line $line</a> column ];  
     print STDOUT $err->location->column_number, "</dt><dd>";  
     print STDOUT htescape $err->text, "</dd>\n";  
     return 1;  
   };  
   
   open my $fh, '<', \$s;  
   $doc = Message::DOM::XMLParserTemp->parse_byte_stream  
       ($fh => $dom, $onerror, charset => 'utf-8');  
   
     print STDOUT qq[  
 </dl>  
 </div>  
 ];  
   } else {  
     print STDOUT qq[  
 </dl>  
 </div>  
   
 <div id="result-summary" class="section">  
 <p><em>Media type <code class="MIME" lang="en">@{[htescape $input_format]}</code> is not supported!</em></p>  
 </div>  
 ];  
     push @nav, ['#result-summary' => 'Result'];  
   }  
19    
20      if ($http->get_meta_variable ('PATH_INFO') ne '/') {
21    if (defined $doc or defined $el) {      $out->http_error (404);
22      print STDOUT qq[      exit;
 <div id="document-tree" class="section">  
 <h2>Document Tree</h2>  
 ];  
     push @nav, ['#document-tree' => 'Tree'];  
   
     print_document_tree ($el || $doc);  
   
     print STDOUT qq[  
 </div>  
   
 <div id="document-errors" class="section">  
 <h2>Document Errors</h2>  
   
 <dl>  
 ];  
     push @nav, ['#document-errors' => 'Document Error'];  
   
     require Whatpm::ContentChecker;  
     my $onerror = sub {  
       my %opt = @_;  
       print STDOUT qq[<dt>] . get_node_link ($opt{node}) .  
           "</dt>\n<dd>", htescape $opt{type}, "</dd>\n";  
     };  
   
     my $elements;  
     if ($el) {  
       $elements = Whatpm::ContentChecker->check_element ($el, $onerror);  
     } else {  
       $elements = Whatpm::ContentChecker->check_document ($doc, $onerror);  
     }  
   
     print STDOUT qq[  
 </dl>  
 </div>  
 ];  
   
     if (@{$elements->{table}}) {  
       require JSON;  
   
       print STDOUT qq[  
 <div id="tables" class="section">  
 <h2>Tables</h2>  
   
 <!--[if IE]><script type="text/javascript" src="../excanvas.js"></script><![endif]-->  
 <script src="../table-script.js" type="text/javascript"></script>  
 <noscript>  
 <p><em>Structure of tables are visualized here if scripting is enabled.</em></p>  
 </noscript>  
 ];  
   
       my $i = 0;  
       for my $table_el (@{$elements->{table}}) {  
         $i++;  
         print STDOUT qq[<div class="section" id="table-$i"><h3>] .  
             get_node_link ($table_el) . q[</h3>];  
           
         my $table = Whatpm::HTMLTable->form_table ($table_el);  
           
         for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption}) {  
           next unless $_;  
           delete $_->{element};  
         }  
           
         for (@{$table->{row_group}}) {  
           next unless $_;  
           next unless $_->{element};  
           $_->{type} = $_->{element}->manakai_local_name;  
           delete $_->{element};  
         }  
           
         for (@{$table->{cell}}) {  
           next unless $_;  
           for (@{$_}) {  
             next unless $_;  
             for (@$_) {  
               $_->{id} = refaddr $_->{element} if defined $_->{element};  
               delete $_->{element};  
             }  
           }  
         }  
           
         print STDOUT '</div><script type="text/javascript">tableToCanvas (';  
         print STDOUT JSON::objToJson ($table);  
         print STDOUT qq[, document.getElementById ('table-$i'));</script>];  
       }  
       
       print STDOUT qq[</div>];  
     }  
   
     if (keys %{$elements->{term}}) {  
       print STDOUT qq[  
 <div id="terms" class="section">  
 <h2>Terms</h2>  
   
 <dl>  
 ];  
       for my $term (sort {$a cmp $b} keys %{$elements->{term}}) {  
         print STDOUT qq[<dt>@{[htescape $term]}</dt>];  
         for (@{$elements->{term}->{$term}}) {  
           print STDOUT qq[<dd>].get_node_link ($_).qq[</dd>];  
         }  
       }  
       print STDOUT qq[</dl></div>];  
     }  
23    }    }
24    
25    ## TODO: Show result    ## TODO: We need real conneg support...
26      my $primary_language = 'en';
27    print STDOUT qq[    if ($ENV{HTTP_ACCEPT_LANGUAGE} =~ /ja/) {
28  <ul class="navigation" id="nav-items">      $primary_language = 'ja';
29  ];    }
30    for (@nav) {    $out->load_text_catalog ($primary_language);
31      print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>];    
32    }    $out->set_flush;
33    print STDOUT qq[    $out->http_header;
34  </ul>    $out->html_header;
35  </body>    $out->unset_flush;
36  </html>  
37  ];    $out->generate_input_section ($http);
38    
39  exit;    my $u = $http->get_parameter ('uri');
40      my $s = $http->get_parameter ('s');
41  sub print_source_string ($) {    if ((not defined $u or not length $u) and
42    my $s = $_[0];        (not defined $s or not length $s)) {
43    my $i = 1;      exit;
   print STDOUT qq[<ol lang="">\n];  
   while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) {  
     print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n";  
     $i++;  
   }  
   if ($$s =~ /\G([^\x0A]+)/gc) {  
     print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n";  
44    }    }
   print STDOUT "</ol>";  
 } # print_input_string  
45    
46  sub print_document_tree ($) {    require WebHACC::Result;
47    my $node = shift;    my $result = WebHACC::Result->new;
48    my $r = '<ol class="xoxo">';    $result->output ($out);
49    
50    my @node = ($node);    require WebHACC::Input;
51    while (@node) {    my $input = WebHACC::Input->get_document ($http => $result => $out);
52      my $child = shift @node;  
53      unless (ref $child) {    check_and_print ($input => $result => $out);
54        $r .= $child;    
55        next;    $out->nav_list;
56      }  
57      exit;
58      my $node_id = 'node-'.refaddr $child;  }
59      my $nt = $child->node_type;  
60      if ($nt == $child->ELEMENT_NODE) {  sub check_and_print ($$$) {
61        my $child_nsuri = $child->namespace_uri;    my ($input, $result, $out) = @_;
62        $r .= qq[<li id="$node_id" class="tree-element"><code title="@{[defined $child_nsuri ? $child_nsuri : '']}">] . htescape ($child->tag_name) .    my $original_input = $out->input;
63            '</code>'; ## ISSUE: case    $out->input ($input);
64    
65        if ($child->has_attributes) {    $input->generate_info_section ($result);
66          $r .= '<ul class="attributes">';  
67          for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] }    $input->generate_transfer_sections ($result);
68                        @{$child->attributes}) {  
69            $r .= qq[<li id="$attr->[3]" class="tree-attribute"><code title="@{[defined $_->[2] ? $_->[2] : '']}">] . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case?    unless (defined $input->{s}) {
70            $r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children      ## NOTE: This is an error of the implementation.
71          }      $result->layer_uncertain ('transfer');
72          $r .= '</ul>';      $result->generate_result_section;
73        }  
74        $out->input ($original_input);
75        if ($node->has_child_nodes) {      return;
76          $r .= '<ol class="children">';    }
77          unshift @node, @{$child->child_nodes}, '</ol></li>';  
78        } else {    my $checker_class = {
79          $r .= '</li>';      'text/cache-manifest' => 'WebHACC::Language::CacheManifest',
80        }      'text/css' => 'WebHACC::Language::CSS',
81      } elsif ($nt == $child->TEXT_NODE) {      'text/x-css-inline' => 'WebHACC::Language::CSSInline',
82        $r .= qq'<li id="$node_id" class="tree-text"><q lang="">' . htescape ($child->data) . '</q></li>';      'text/html' => 'WebHACC::Language::HTML',
83      } elsif ($nt == $child->CDATA_SECTION_NODE) {      'text/x-h2h' => 'WebHACC::Language::H2H',
84        $r .= qq'<li id="$node_id" class="tree-cdata"><code>&lt;[CDATA[</code><q lang="">' . htescape ($child->data) . '</q><code>]]&gt;</code></li>';      'text/x-webidl' => 'WebHACC::Language::WebIDL',
85      } elsif ($nt == $child->COMMENT_NODE) {  
86        $r .= qq'<li id="$node_id" class="tree-comment"><code>&lt;!--</code><q lang="">' . htescape ($child->data) . '</q><code>--&gt;</code></li>';      'text/xml' => 'WebHACC::Language::XML',
87      } elsif ($nt == $child->DOCUMENT_NODE) {      'application/atom+xml' => 'WebHACC::Language::XML',
88        $r .= qq'<li id="$node_id" class="tree-document">Document';      'application/rss+xml' => 'WebHACC::Language::XML',
89        if ($child->has_child_nodes) {      'image/svg+xml' => 'WebHACC::Language::XML',
90          $r .= '<ol>';      'application/xhtml+xml' => 'WebHACC::Language::XML',
91          unshift @node, @{$child->child_nodes}, '</ol></li>';      'application/xml' => 'WebHACC::Language::XML',
92        }      ## TODO: Should we make all XML MIME Types fall
93      } elsif ($nt == $child->DOCUMENT_TYPE_NODE) {      ## into this category?
94        $r .= qq'<li id="$node_id" class="tree-doctype"><code>&lt;!DOCTYPE&gt;</code><ul class="attributes">';  
95        $r .= qq[<li class="tree-doctype-name">Name = <q>@{[htescape ($child->name)]}</q></li>];      ## NOTE: This type has different model from normal XML types.
96        $r .= qq[<li class="tree-doctype-publicid">Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>];      'application/rdf+xml' => 'WebHACC::Language::XML',
97        $r .= qq[<li class="tree-doctype-systemid">System identifier = <q>@{[htescape ($child->system_id)]}</q></li>];    }->{$input->{media_type}} || 'WebHACC::Language::Default';
98        $r .= '</ul></li>';  
99      } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) {    eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@";
100        $r .= qq'<li id="$node_id" class="tree-id"><code>&lt;?@{[htescape ($child->target)]}</code> <q>@{[htescape ($child->data)]}</q><code>?&gt;</code></li>';    my $checker = $checker_class->new;
101      } else {    $checker->input ($input);
102        $r .= qq'<li id="$node_id" class="tree-unknown">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error    $checker->output ($out);
103      }    $checker->result ($result);
104    
105      ## TODO: A cache manifest MUST be text/cache-manifest
106      ## TODO: WebIDL media type "text/x-webidl"
107    
108      $checker->generate_syntax_error_section;
109      $checker->generate_source_string_section;
110    
111      my @subdoc;
112      $checker->onsubdoc (sub {
113        push @subdoc, shift;
114      });
115    
116      $checker->generate_structure_dump_section;
117      $checker->generate_structure_error_section;
118      $checker->generate_additional_sections;
119    
120      my $id_prefix = 0;
121      for my $_subinput (@subdoc) {
122        my $subinput = WebHACC::Input::Subdocument->new (++$id_prefix);
123        $subinput->{$_} = $_subinput->{$_} for keys %$_subinput;
124        $subinput->{base_uri} = $subinput->{container_node}->base_uri
125            unless defined $subinput->{base_uri};
126        $subinput->{parent_input} = $input;
127    
128        my $subresult = WebHACC::Result->new;
129        $subresult->output ($out);
130        $subresult->parent_result ($result);
131    
132        $subinput->start_section ($subresult);
133        check_and_print ($subinput => $subresult => $out);
134        $subinput->end_section ($subresult);
135    }    }
136    
137    $r .= '</ol>';    $result->generate_result_section;
   print STDOUT $r;  
 } # print_document_tree  
   
 sub get_node_path ($) {  
   my $node = shift;  
   my @r;  
   while (defined $node) {  
     my $rs;  
     if ($node->node_type == 1) {  
       $rs = $node->manakai_local_name;  
       $node = $node->parent_node;  
     } elsif ($node->node_type == 2) {  
       $rs = '@' . $node->manakai_local_name;  
       $node = $node->owner_element;  
     } elsif ($node->node_type == 3) {  
       $rs = '"' . $node->data . '"';  
       $node = $node->parent_node;  
     } elsif ($node->node_type == 9) {  
       $rs = '';  
       $node = $node->parent_node;  
     } else {  
       $rs = '#' . $node->node_type;  
       $node = $node->parent_node;  
     }  
     unshift @r, $rs;  
   }  
   return join '/', @r;  
 } # get_node_path  
138    
139  sub get_node_link ($) {    $out->input ($original_input);
140    return qq[<a href="#node-@{[refaddr $_[0]]}">] .  } # check_and_print
       htescape (get_node_path ($_[0])) . qq[</a>];  
 } # get_node_link  
141    
142  =head1 AUTHOR  =head1 AUTHOR
143    
# Line 406  Wakaba <w@suika.fam.cx>. Line 145  Wakaba <w@suika.fam.cx>.
145    
146  =head1 LICENSE  =head1 LICENSE
147    
148  Copyright 2007 Wakaba <w@suika.fam.cx>  Copyright 2007-2008 Wakaba <w@suika.fam.cx>
149    
150  This library is free software; you can redistribute it  This library is free software; you can redistribute it
151  and/or modify it under the same terms as Perl itself.  and/or modify it under the same terms as Perl itself.

Legend:
Removed from v.1.6  
changed lines
  Added in v.1.68

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24