/[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.5 by wakaba, Sat Jun 30 08:26:08 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" 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'];  
   }  
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>  
   
 <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";  
     };  
   
     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>];  
         print STDOUT qq[<a href="#node-@{[refaddr $table_el]}">],  
           htescape get_node_path ($table_el);  
         print STDOUT qq[</a></h3>\n];  
           
         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>];  
     }  
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        my $child_nsuri = $child->namespace_uri;    exit;
61        $r .= qq[<li id="$node_id" class="tree-element"><code title="@{[defined $child_nsuri ? $child_nsuri : '']}">] . htescape ($child->tag_name) .  }
62            '</code>'; ## ISSUE: case  
63    sub check_and_print ($$$) {
64        if ($child->has_attributes) {    my ($input, $result, $out) = @_;
65          $r .= '<ul class="attributes">';    my $original_input = $out->input;
66          for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] }    $out->input ($input);
67                        @{$child->attributes}) {  
68            $r .= qq[<li id="$attr->[3]" class="tree-attribute"><code title="@{[defined $_->[2] ? $_->[2] : '']}">] . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case?    $input->generate_info_section ($result);
69            $r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children  
70          }    $input->generate_transfer_sections ($result);
71          $r .= '</ul>';  
72        }    unless (defined $input->{s}) {
73        $result->{conforming_min} = 0;
74        if ($node->has_child_nodes) {      return;
75          $r .= '<ol class="children">';    }
76          unshift @node, @{$child->child_nodes}, '</ol>';  
77        }    my $checker_class = {
78      } elsif ($nt == $child->TEXT_NODE) {      'text/cache-manifest' => 'WebHACC::Language::CacheManifest',
79        $r .= qq'<li id="$node_id" class="tree-text"><q lang="">' . htescape ($child->data) . '</q></li>';      'text/css' => 'WebHACC::Language::CSS',
80      } elsif ($nt == $child->CDATA_SECTION_NODE) {      'text/html' => 'WebHACC::Language::HTML',
81        $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',
82      } elsif ($nt == $child->COMMENT_NODE) {  
83        $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',
84      } elsif ($nt == $child->DOCUMENT_NODE) {      'application/atom+xml' => 'WebHACC::Language::XML',
85        $r .= qq'<li id="$node_id" class="tree-document">Document</li>';      'application/rss+xml' => 'WebHACC::Language::XML',
86        if ($child->has_child_nodes) {      'image/svg+xml' => 'WebHACC::Language::XML',
87          $r .= '<ol>';      'application/xhtml+xml' => 'WebHACC::Language::XML',
88          unshift @node, @{$child->child_nodes}, '</ol>';      'application/xml' => 'WebHACC::Language::XML',
89        }      ## TODO: Should we make all XML MIME Types fall
90      } elsif ($nt == $child->DOCUMENT_TYPE_NODE) {      ## into this category?
91        $r .= qq'<li id="$node_id" class="tree-doctype"><code>&lt;!DOCTYPE&gt;</code><ul class="attributes">';  
92        $r .= qq[<li class="tree-doctype-name">Name = <q>@{[htescape ($child->name)]}</q></li>];      ## NOTE: This type has different model from normal XML types.
93        $r .= qq[<li class="tree-doctype-publicid">Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>];      'application/rdf+xml' => 'WebHACC::Language::XML',
94        $r .= qq[<li class="tree-doctype-systemid">System identifier = <q>@{[htescape ($child->system_id)]}</q></li>];    }->{$input->{media_type}} || 'WebHACC::Language::Default';
95        $r .= '</ul></li>';  
96      } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) {    eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@";
97        $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;
98      } else {    $checker->input ($input);
99        $r .= qq'<li id="$node_id" class="tree-unknown">@{[$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 386  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.5  
changed lines
  Added in v.1.61

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24