| 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/</</g; |  | 
| 15 | $s =~ s/>/>/g; | require WebHACC::Output; | 
| 16 | $s =~ s/"/"/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; |  | 
|  | } |  | 
| 19 |  |  | 
| 20 | my @nav; | if ($http->get_meta_variable ('PATH_INFO') ne '/') { | 
| 21 | print STDOUT qq[Content-Type: text/html; charset=utf-8 | $out->http_error (404); | 
| 22 |  | exit; | 
|  | <!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=""><<a href="@{[htescape $input_uri]}">@{[htescape $input_uri]}</a>></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']; |  | 
| 23 | } | } | 
| 24 |  |  | 
| 25 |  | ## TODO: We need real conneg support... | 
| 26 | if (defined $doc or defined $el) { | my $primary_language = 'en'; | 
| 27 | print STDOUT qq[ | if ($ENV{HTTP_ACCEPT_LANGUAGE} =~ /ja/) { | 
| 28 | <div id="document-tree" class="section"> | $primary_language = 'ja'; | 
| 29 | <h2>Document Tree</h2> | } | 
| 30 | ]; | $out->load_text_catalog ($primary_language); | 
| 31 | push @nav, ['#document-tree' => 'Tree']; |  | 
| 32 |  | $out->set_flush; | 
| 33 | print_document_tree ($el || $doc); | $out->http_header; | 
| 34 |  | $out->html_header; | 
| 35 | print STDOUT qq[ | $out->unset_flush; | 
| 36 | </div> |  | 
| 37 |  | $out->generate_input_section ($http); | 
| 38 | <div id="document-errors" class="section"> |  | 
| 39 | <h2>Document Errors</h2> | my $u = $http->get_parameter ('uri'); | 
| 40 |  | my $s = $http->get_parameter ('s'); | 
| 41 | <ul> | if ((not defined $u or not length $u) and | 
| 42 | ]; | (not defined $s or not length $s)) { | 
| 43 | push @nav, ['#document-errors' => 'Document Error']; | exit; | 
|  |  |  | 
|  | 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> |  | 
|  | ]; |  | 
| 44 | } | } | 
| 45 |  |  | 
| 46 | ## TODO: Show result | require WebHACC::Result; | 
| 47 |  | my $result = WebHACC::Result->new; | 
| 48 | print STDOUT qq[ | $result->output ($out); | 
| 49 | <ul class="navigation" id="nav-items"> |  | 
| 50 | ]; | require WebHACC::Input; | 
| 51 | for (@nav) { | my $input = WebHACC::Input->get_document ($http => $result => $out); | 
| 52 | print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>]; |  | 
| 53 | } | check_and_print ($input => $result => $out); | 
| 54 | print STDOUT qq[ |  | 
| 55 | </ul> | $out->nav_list; | 
| 56 | </body> |  | 
| 57 | </html> | exit; | 
| 58 | ]; | } | 
| 59 |  |  | 
| 60 | exit; | sub check_and_print ($$$) { | 
| 61 |  | my ($input, $result, $out) = @_; | 
| 62 | sub print_source_string ($) { | my $original_input = $out->input; | 
| 63 | my $s = $_[0]; | $out->input ($input); | 
| 64 | my $i = 1; |  | 
| 65 | print STDOUT qq[<ol lang="">\n]; | $input->generate_info_section ($result); | 
| 66 | while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) { |  | 
| 67 | print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; | $input->generate_transfer_sections ($result); | 
| 68 | $i++; |  | 
| 69 |  | unless (defined $input->{s}) { | 
| 70 |  | ## NOTE: This is an error of the implementation. | 
| 71 |  | $result->layer_uncertain ('transfer'); | 
| 72 |  | $result->generate_result_section; | 
| 73 |  |  | 
| 74 |  | $out->input ($original_input); | 
| 75 |  | return; | 
| 76 |  | } | 
| 77 |  |  | 
| 78 |  | my $checker_class = { | 
| 79 |  | 'text/cache-manifest' => 'WebHACC::Language::CacheManifest', | 
| 80 |  | 'text/css' => 'WebHACC::Language::CSS', | 
| 81 |  | 'text/html' => 'WebHACC::Language::HTML', | 
| 82 |  | 'text/x-h2h' => 'WebHACC::Language::H2H', | 
| 83 |  | 'text/x-webidl' => 'WebHACC::Language::WebIDL', | 
| 84 |  |  | 
| 85 |  | 'text/xml' => 'WebHACC::Language::XML', | 
| 86 |  | 'application/atom+xml' => 'WebHACC::Language::XML', | 
| 87 |  | 'application/rss+xml' => 'WebHACC::Language::XML', | 
| 88 |  | 'image/svg+xml' => 'WebHACC::Language::XML', | 
| 89 |  | 'application/xhtml+xml' => 'WebHACC::Language::XML', | 
| 90 |  | 'application/xml' => 'WebHACC::Language::XML', | 
| 91 |  | ## TODO: Should we make all XML MIME Types fall | 
| 92 |  | ## into this category? | 
| 93 |  |  | 
| 94 |  | ## NOTE: This type has different model from normal XML types. | 
| 95 |  | 'application/rdf+xml' => 'WebHACC::Language::XML', | 
| 96 |  | }->{$input->{media_type}} || 'WebHACC::Language::Default'; | 
| 97 |  |  | 
| 98 |  | eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@"; | 
| 99 |  | my $checker = $checker_class->new; | 
| 100 |  | $checker->input ($input); | 
| 101 |  | $checker->output ($out); | 
| 102 |  | $checker->result ($result); | 
| 103 |  |  | 
| 104 |  | ## TODO: A cache manifest MUST be text/cache-manifest | 
| 105 |  | ## TODO: WebIDL media type "text/x-webidl" | 
| 106 |  |  | 
| 107 |  | $checker->generate_syntax_error_section; | 
| 108 |  | $checker->generate_source_string_section; | 
| 109 |  |  | 
| 110 |  | my @subdoc; | 
| 111 |  | $checker->onsubdoc (sub { | 
| 112 |  | push @subdoc, shift; | 
| 113 |  | }); | 
| 114 |  |  | 
| 115 |  | $checker->generate_structure_dump_section; | 
| 116 |  | $checker->generate_structure_error_section; | 
| 117 |  | $checker->generate_additional_sections; | 
| 118 |  |  | 
| 119 |  | my $id_prefix = 0; | 
| 120 |  | for my $_subinput (@subdoc) { | 
| 121 |  | my $subinput = WebHACC::Input::Subdocument->new (++$id_prefix); | 
| 122 |  | $subinput->{$_} = $_subinput->{$_} for keys %$_subinput; | 
| 123 |  | $subinput->{base_uri} = $subinput->{container_node}->base_uri | 
| 124 |  | unless defined $subinput->{base_uri}; | 
| 125 |  | $subinput->{parent_input} = $input; | 
| 126 |  |  | 
| 127 |  | my $subresult = WebHACC::Result->new; | 
| 128 |  | $subresult->output ($out); | 
| 129 |  | $subresult->parent_result ($result); | 
| 130 |  |  | 
| 131 |  | $subinput->start_section ($subresult); | 
| 132 |  | check_and_print ($subinput => $subresult => $out); | 
| 133 |  | $subinput->end_section ($subresult); | 
| 134 | } | } | 
|  | if ($$s =~ /\G([^\x0A]+)/gc) { |  | 
|  | print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |  | 
|  | } |  | 
|  | print STDOUT "</ol>"; |  | 
|  | } # print_input_string |  | 
| 135 |  |  | 
| 136 | sub print_document_tree ($) { | $result->generate_result_section; | 
|  | my $node = shift; |  | 
|  | my $r = '<ol class="xoxo">'; |  | 
|  |  |  | 
|  | my @node = ($node); |  | 
|  | while (@node) { |  | 
|  | my $child = shift @node; |  | 
|  | unless (ref $child) { |  | 
|  | $r .= $child; |  | 
|  | next; |  | 
|  | } |  | 
|  |  |  | 
|  | my $node_id = 'node-'.refaddr $child; |  | 
|  | my $nt = $child->node_type; |  | 
|  | if ($nt == $child->ELEMENT_NODE) { |  | 
|  | $r .= qq'<li id="$node_id"><code>' . htescape ($child->tag_name) . |  | 
|  | '</code>'; ## ISSUE: case |  | 
|  |  |  | 
|  | if ($child->has_attributes) { |  | 
|  | $r .= '<ul class="attributes">'; |  | 
|  | for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, 'node-'.refaddr $_] } |  | 
|  | @{$child->attributes}) { |  | 
|  | $r .= qq'<li id="$attr->[2]"><code>' . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case? |  | 
|  | $r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children |  | 
|  | } |  | 
|  | $r .= '</ul>'; |  | 
|  | } |  | 
|  |  |  | 
|  | if ($node->has_child_nodes) { |  | 
|  | $r .= '<ol class="children">'; |  | 
|  | unshift @node, @{$child->child_nodes}, '</ol>'; |  | 
|  | } |  | 
|  | } elsif ($nt == $child->TEXT_NODE) { |  | 
|  | $r .= qq'<li id="$node_id"><q>' . htescape ($child->data) . '</q></li>'; |  | 
|  | } elsif ($nt == $child->CDATA_SECTION_NODE) { |  | 
|  | $r .= qq'<li id="$node_id"><code><[CDATA[</code><q>' . htescape ($child->data) . '</q><code>]]></code></li>'; |  | 
|  | } elsif ($nt == $child->COMMENT_NODE) { |  | 
|  | $r .= qq'<li id="$node_id"><code><!--</code><q>' . htescape ($child->data) . '</q><code>--></code></li>'; |  | 
|  | } elsif ($nt == $child->DOCUMENT_NODE) { |  | 
|  | $r .= qq'<li id="$node_id">Document</li>'; |  | 
|  | if ($child->has_child_nodes) { |  | 
|  | $r .= '<ol>'; |  | 
|  | unshift @node, @{$child->child_nodes}, '</ol>'; |  | 
|  | } |  | 
|  | } elsif ($nt == $child->DOCUMENT_TYPE_NODE) { |  | 
|  | $r .= qq'<li id="$node_id"><code><!DOCTYPE></code><ul>'; |  | 
|  | $r .= '<li>Name = <q>@{[htescape ($child->name)]}</q></li>'; |  | 
|  | $r .= '<li>Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>'; |  | 
|  | $r .= '<li>System identifier = <q>@{[htescape ($child->system_id)]}</q></li>'; |  | 
|  | $r .= '</ul></li>'; |  | 
|  | } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) { |  | 
|  | $r .= qq'<li id="$node_id"><code><?@{[htescape ($child->target)]}?></code>'; |  | 
|  | $r .= '<ul><li>@{[htescape ($child->data)]}</li></ul></li>'; |  | 
|  | } else { |  | 
|  | $r .= qq'<li id="$node_id">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error |  | 
|  | } |  | 
|  | } |  | 
| 137 |  |  | 
| 138 | $r .= '</ol>'; | $out->input ($original_input); | 
| 139 | 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 |  | 
| 140 |  |  | 
| 141 | =head1 AUTHOR | =head1 AUTHOR | 
| 142 |  |  | 
| 144 |  |  | 
| 145 | =head1 LICENSE | =head1 LICENSE | 
| 146 |  |  | 
| 147 | Copyright 2007 Wakaba <w@suika.fam.cx> | Copyright 2007-2008 Wakaba <w@suika.fam.cx> | 
| 148 |  |  | 
| 149 | This library is free software; you can redistribute it | This library is free software; you can redistribute it | 
| 150 | and/or modify it under the same terms as Perl itself. | and/or modify it under the same terms as Perl itself. |