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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24