/[suikacvs]/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.4 by wakaba, Wed Jun 27 14:36:45 2007 UTC revision 1.60 by wakaba, Sat Jul 26 11:27:25 2008 UTC
# Line 2  Line 2 
2  use strict;  use strict;
3    
4  use lib qw[/home/httpd/html/www/markup/html/whatpm  use lib qw[/home/httpd/html/www/markup/html/whatpm
5             /home/wakaba/work/manakai/lib             /home/wakaba/work/manakai2/lib];
            /home/wakaba/public_html/-temp/wiki/lib];  
6  use CGI::Carp qw[fatalsToBrowser];  use CGI::Carp qw[fatalsToBrowser];
 use Scalar::Util qw[refaddr];  
7    
8  use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module    require WebHACC::Input;
   
 sub htescape ($) {  
   my $s = $_[0];  
   $s =~ s/&/&/g;  
   $s =~ s/</&lt;/g;  
   $s =~ s/>/&gt;/g;  
   $s =~ s/"/&quot;/g;  
   $s =~ s!([\x00-\x09\x0B-\x1F\x7F-\x80])!sprintf '<var>U+%04X</var>', ord $1!ge;  
   return $s;  
 } # 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'];  
   }  
9    
10    {
11      require Message::CGI::HTTP;
12      my $http = Message::CGI::HTTP->new;
13    
14      require WebHACC::Output;
15      my $out = WebHACC::Output->new;
16      $out->handle (*STDOUT);
17      $out->set_utf8;
18    
19    if (defined $doc or defined $el) {    if ($http->get_meta_variable ('PATH_INFO') ne '/') {
20      print STDOUT qq[      $out->http_error (404);
21  <div id="document-tree" class="section">      exit;
 <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><a href="#node-@{[refaddr $opt{node}]}">],  
           htescape get_node_path ($opt{node}),  
           "</a></dt>\n<dd>", htescape $opt{type}, "</dd>\n";  
     };  
   
     if ($el) {  
       Whatpm::ContentChecker->check_element ($el, $onerror);  
     } else {  
       Whatpm::ContentChecker->check_document ($doc, $onerror);  
     }  
   
     print STDOUT qq[  
 </dl>  
 </div>  
 ];  
   }  
   
   ## TODO: Show result  
   
   print STDOUT qq[  
 <ul class="navigation" id="nav-items">  
 ];  
   for (@nav) {  
     print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>];  
   }  
   print STDOUT qq[  
 </ul>  
 </body>  
 </html>  
 ];  
   
 exit;  
   
 sub print_source_string ($) {  
   my $s = $_[0];  
   my $i = 1;  
   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";  
22    }    }
   print STDOUT "</ol>";  
 } # print_input_string  
23    
24  sub print_document_tree ($) {    ## TODO: We need real conneg support...
25    my $node = shift;    my $primary_language = 'en';
26    my $r = '<ol class="xoxo">';    if ($ENV{HTTP_ACCEPT_LANGUAGE} =~ /ja/) {
27        $primary_language = 'ja';
28    my @node = ($node);    }
29    while (@node) {    $out->load_text_catalog ($primary_language);
30      my $child = shift @node;    
31      unless (ref $child) {    $out->set_flush;
32        $r .= $child;    $out->http_header;
33        next;    $out->html_header;
34      }    $out->unset_flush;
35    
36      my $node_id = 'node-'.refaddr $child;    require WebHACC::Result;
37      my $nt = $child->node_type;    my $result = WebHACC::Result->new;
38      if ($nt == $child->ELEMENT_NODE) {    $result->{conforming_min} = 1;
39        my $child_nsuri = $child->namespace_uri;    $result->{conforming_max} = 1;
40        $r .= qq[<li id="$node_id" class="tree-element"><code title="@{[defined $child_nsuri ? $child_nsuri : '']}">] . htescape ($child->tag_name) .    $result->output ($out);
41            '</code>'; ## ISSUE: case  
42      require WebHACC::Input;
43        if ($child->has_attributes) {    my $input = WebHACC::Input->get_document ($http => $result => $out);
44          $r .= '<ul class="attributes">';  
45          for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] }    check_and_print ($input => $result => $out);
46                        @{$child->attributes}) {    
47            $r .= qq[<li id="$attr->[3]" class="tree-attribute"><code title="@{[defined $_->[2] ? $_->[2] : '']}">] . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case?    $result->generate_result_section;
48            $r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children  
49          }    $out->nav_list;
50          $r .= '</ul>';  
51        }    exit;
52    }
53        if ($node->has_child_nodes) {  
54          $r .= '<ol class="children">';  sub check_and_print ($$$) {
55          unshift @node, @{$child->child_nodes}, '</ol>';    my ($input, $result, $out) = @_;
56        }    my $original_input = $out->input;
57      } elsif ($nt == $child->TEXT_NODE) {    $out->input ($input);
58        $r .= qq'<li id="$node_id" class="tree-text"><q lang="">' . htescape ($child->data) . '</q></li>';  
59      } elsif ($nt == $child->CDATA_SECTION_NODE) {    $input->generate_info_section ($result);
60        $r .= qq'<li id="$node_id" class="tree-cdata"><code>&lt;[CDATA[</code><q lang="">' . htescape ($child->data) . '</q><code>]]&gt;</code></li>';  
61      } elsif ($nt == $child->COMMENT_NODE) {    $input->generate_transfer_sections ($result);
62        $r .= qq'<li id="$node_id" class="tree-comment"><code>&lt;!--</code><q lang="">' . htescape ($child->data) . '</q><code>--&gt;</code></li>';  
63      } elsif ($nt == $child->DOCUMENT_NODE) {    unless (defined $input->{s}) {
64        $r .= qq'<li id="$node_id" class="tree-document">Document</li>';      $result->{conforming_min} = 0;
65        if ($child->has_child_nodes) {      return;
66          $r .= '<ol>';    }
67          unshift @node, @{$child->child_nodes}, '</ol>';  
68        }    my $checker_class = {
69      } elsif ($nt == $child->DOCUMENT_TYPE_NODE) {      'text/cache-manifest' => 'WebHACC::Language::CacheManifest',
70        $r .= qq'<li id="$node_id" class="tree-doctype"><code>&lt;!DOCTYPE&gt;</code><ul>';      'text/css' => 'WebHACC::Language::CSS',
71        $r .= '<li class="tree-doctype-name">Name = <q>@{[htescape ($child->name)]}</q></li>';      'text/html' => 'WebHACC::Language::HTML',
72        $r .= '<li class="tree-doctype-publicid">Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>';      'text/x-webidl' => 'WebHACC::Language::WebIDL',
73        $r .= '<li class="tree-doctype-systemid">System identifier = <q>@{[htescape ($child->system_id)]}</q></li>';  
74        $r .= '</ul></li>';      'text/xml' => 'WebHACC::Language::XML',
75      } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) {      'application/atom+xml' => 'WebHACC::Language::XML',
76        $r .= qq'<li id="$node_id" class="tree-id"><code>&lt;?@{[htescape ($child->target)]}</code> <q>@{[htescape ($child->data)]}</q><code>?&gt;</code></li>';      'application/rss+xml' => 'WebHACC::Language::XML',
77      } else {      'image/svg+xml' => 'WebHACC::Language::XML',
78        $r .= qq'<li id="$node_id" class="tree-unknown">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error      'application/xhtml+xml' => 'WebHACC::Language::XML',
79      }      'application/xml' => 'WebHACC::Language::XML',
80        ## TODO: Should we make all XML MIME Types fall
81        ## into this category?
82    
83        ## NOTE: This type has different model from normal XML types.
84        'application/rdf+xml' => 'WebHACC::Language::XML',
85      }->{$input->{media_type}} || 'WebHACC::Language::Default';
86    
87      eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@";
88      my $checker = $checker_class->new;
89      $checker->input ($input);
90      $checker->output ($out);
91      $checker->result ($result);
92    
93      ## TODO: A cache manifest MUST be text/cache-manifest
94      ## TODO: WebIDL media type "text/x-webidl"
95    
96      $checker->generate_syntax_error_section;
97      $checker->generate_source_string_section;
98    
99      my @subdoc;
100      $checker->onsubdoc (sub {
101        push @subdoc, shift;
102      });
103    
104      $checker->generate_structure_dump_section;
105      $checker->generate_structure_error_section;
106      $checker->generate_additional_sections;
107    
108      my $id_prefix = 0;
109      for my $_subinput (@subdoc) {
110        my $subinput = WebHACC::Input::Subdocument->new (++$id_prefix);
111        $subinput->{$_} = $_subinput->{$_} for keys %$_subinput;
112        $subinput->{base_uri} = $subinput->{container_node}->base_uri
113            unless defined $subinput->{base_uri};
114        $subinput->{parent_input} = $input;
115    
116        $subinput->start_section ($result);
117        check_and_print ($subinput => $result => $out);
118        $subinput->end_section ($result);
119    }    }
120    
121    $r .= '</ol>';    $out->input ($original_input);
122    print STDOUT $r;  } # check_and_print
 } # 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  
123    
124  =head1 AUTHOR  =head1 AUTHOR
125    
# Line 330  Wakaba <w@suika.fam.cx>. Line 127  Wakaba <w@suika.fam.cx>.
127    
128  =head1 LICENSE  =head1 LICENSE
129    
130  Copyright 2007 Wakaba <w@suika.fam.cx>  Copyright 2007-2008 Wakaba <w@suika.fam.cx>
131    
132  This library is free software; you can redistribute it  This library is free software; you can redistribute it
133  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.4  
changed lines
  Added in v.1.60

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24