--- test/html-webhacc/cc.cgi 2007/06/27 13:30:15 1.3
+++ test/html-webhacc/cc.cgi 2008/07/21 09:15:55 1.57
@@ -1,40 +1,36 @@
#!/usr/bin/perl
use strict;
+use utf8;
use lib qw[/home/httpd/html/www/markup/html/whatpm
- /home/wakaba/work/manakai/lib
- /home/wakaba/public_html/-temp/wiki/lib];
+ /home/wakaba/work/manakai2/lib];
use CGI::Carp qw[fatalsToBrowser];
use Scalar::Util qw[refaddr];
-use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module
+ require WebHACC::Input;
+ require WebHACC::Result;
+ require WebHACC::Output;
-sub htescape ($) {
- my $s = $_[0];
- $s =~ s/&/&/g;
- $s =~ s/</g;
- $s =~ s/>/>/g;
- $s =~ s/"/"/g;
- $s =~ s!([\x00-\x09\x0B-\x1F\x7F-\x80])!sprintf 'U+%04X', 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";
+my $out;
+
+ require Message::DOM::DOMImplementation;
+ my $dom = Message::DOM::DOMImplementation->new;
+{
+ use Message::CGI::HTTP;
+ my $http = Message::CGI::HTTP->new;
+
+ if ($http->get_meta_variable ('PATH_INFO') ne '/') {
+ print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400";
exit;
}
+
+ load_text_catalog ('en'); ## TODO: conneg
- my @nav;
- print STDOUT qq[Content-Type: text/html; charset=utf-8
+ $out = WebHACC::Output->new;
+ $out->handle (*STDOUT);
+ $out->set_utf8;
+ $out->set_flush;
+ $out->html (qq[Content-Type: text/html; charset=utf-8
@@ -43,285 +39,341 @@
-Web Document Conformance Checker (beta)
+
+]);
-
-
-- Document URI
- <@{[htescape $input_uri]}>
-- Internet Media Type
- @{[htescape $input_format]}
-]; # no
yet
- push @nav, ['#document-info' => 'Information'];
+ my $input = get_input_document ($http, $dom);
- 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[
-
Character Encoding
-
(none)
-
-
-
-
-
Document Source
-];
- push @nav, ['#source-string' => 'Source'];
- print_source_string (\$s);
- print STDOUT qq[
-
-
-
-
Parse Errors
-
-
-];
- push @nav, ['#parse-errors' => 'Parse Error'];
-
- my $onerror = sub {
- my (%opt) = @_;
- if ($opt{column} > 0) {
- print STDOUT qq[- Line $opt{line} column $opt{column}: ];
- } else {
- $opt{line}--;
- print STDOUT qq[
- Line $opt{line}: ];
- }
- print STDOUT qq[@{[htescape $opt{type}]}
\n];
- };
+ $out->input ($input);
+ $out->unset_flush;
- $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);
- }
+ my $result = WebHACC::Result->new;
+ $result->output ($out);
+ $result->{conforming_min} = 1;
+ $result->{conforming_max} = 1;
- print STDOUT qq[
-
-
-];
- } elsif ($input_format eq 'application/xhtml+xml') {
- require Message::DOM::XMLParserTemp;
- require Encode;
-
- my $t = Encode::decode ('utf-8', $s);
-
- print STDOUT qq[
-Character Encoding
- (none)
-
-
-
-
-
Document Source
-];
- push @nav, ['#source-string' => 'Source'];
- print_source_string (\$t);
- print STDOUT qq[
-
-
-
-
Parse Errors
-
-
-];
- push @nav, ['#parse-errors' => 'Parse Error'];
-
- my $onerror = sub {
- my $err = shift;
- my $line = $err->location->line_number;
- print STDOUT qq[- Line $line column ];
- print STDOUT $err->location->column_number, ": ";
- print STDOUT htescape $err->text, "
\n";
- return 1;
- };
-
- open my $fh, '<', \$s;
- $doc = Message::DOM::XMLParserTemp->parse_byte_stream
- ($fh => $dom, $onerror, charset => 'utf-8');
-
- print STDOUT qq[
-
-
-];
- } else {
- print STDOUT qq[
-
+ $out->html ('');
+
+ check_and_print ($input => $result => $out);
+
+ $result->generate_result_section;
+
+ $out->nav_list;
-
-
Media type @{[htescape $input_format]}
is not supported!
-
-];
- push @nav, ['#result-summary' => 'Result'];
+ exit;
+}
+
+sub check_and_print ($$$) {
+ my ($input, $result, $out) = @_;
+ my $original_input = $out->input;
+ $out->input ($input);
+
+ $input->generate_info_section ($result);
+
+ $input->generate_transfer_sections ($result);
+
+ unless (defined $input->{s}) {
+ $result->{conforming_min} = 0;
+ return;
}
+ my $checker_class = {
+ 'text/cache-manifest' => 'WebHACC::Language::CacheManifest',
+ 'text/css' => 'WebHACC::Language::CSS',
+ 'text/html' => 'WebHACC::Language::HTML',
+ 'text/x-webidl' => 'WebHACC::Language::WebIDL',
+
+ 'text/xml' => 'WebHACC::Language::XML',
+ 'application/atom+xml' => 'WebHACC::Language::XML',
+ 'application/rss+xml' => 'WebHACC::Language::XML',
+ 'image/svg+xml' => 'WebHACC::Language::XML',
+ 'application/xhtml+xml' => 'WebHACC::Language::XML',
+ 'application/xml' => 'WebHACC::Language::XML',
+ ## TODO: Should we make all XML MIME Types fall
+ ## into this category?
+
+ ## NOTE: This type has different model from normal XML types.
+ 'application/rdf+xml' => 'WebHACC::Language::XML',
+ }->{$input->{media_type}} || 'WebHACC::Language::Default';
+
+ eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@";
+ my $checker = $checker_class->new;
+ $checker->input ($input);
+ $checker->output ($out);
+ $checker->result ($result);
+
+ ## TODO: A cache manifest MUST be text/cache-manifest
+ ## TODO: WebIDL media type "text/x-webidl"
+
+ $checker->generate_syntax_error_section;
+ $checker->generate_source_string_section;
+
+ my @subdoc;
+ $checker->onsubdoc (sub {
+ push @subdoc, shift;
+ });
+
+ $checker->generate_structure_dump_section;
+ $checker->generate_structure_error_section;
+ $checker->generate_additional_sections;
+
+ my $id_prefix = 0;
+ for my $_subinput (@subdoc) {
+ my $subinput = WebHACC::Input::Subdocument->new (++$id_prefix);
+ $subinput->{$_} = $_subinput->{$_} for keys %$_subinput;
+ $subinput->{base_uri} = $subinput->{container_node}->base_uri
+ unless defined $subinput->{base_uri};
+ $subinput->{parent_input} = $input;
+
+ $subinput->start_section ($result);
+ check_and_print ($subinput => $result => $out);
+ $subinput->end_section ($result);
+ }
- if (defined $doc or defined $el) {
- print STDOUT qq[
-
-
Document Tree
-];
- push @nav, ['#document-tree' => 'Tree'];
-
- print_document_tree ($el || $doc);
-
- print STDOUT qq[
-
-
-
-
Document Errors
-
-
-];
- push @nav, ['#document-errors' => 'Document Error'];
-
- require Whatpm::ContentChecker;
- my $onerror = sub {
- my %opt = @_;
- print STDOUT qq[- ],
- htescape get_node_path ($opt{node}),
- ": ", htescape $opt{type}, "
\n";
- };
+ $out->input ($original_input);
+} # check_and_print
- if ($el) {
- Whatpm::ContentChecker->check_element ($el, $onerror);
- } else {
- Whatpm::ContentChecker->check_document ($doc, $onerror);
+
+{
+ my $Msg = {};
+
+sub load_text_catalog ($) {
+# my $self = shift;
+ my $lang = shift; # MUST be a canonical lang name
+ open my $file, '<:utf8', "cc-msg.$lang.txt"
+ or die "$0: cc-msg.$lang.txt: $!";
+ while (<$file>) {
+ if (s/^([^;]+);([^;]*);//) {
+ my ($type, $cls, $msg) = ($1, $2, $_);
+ $msg =~ tr/\x0D\x0A//d;
+ $Msg->{$type} = [$cls, $msg];
+ }
+ }
+} # load_text_catalog
+
+sub get_text ($;$$) {
+# my $self = shift;
+ my ($type, $level, $node) = @_;
+ $type = $level . ':' . $type if defined $level;
+ $level = 'm' unless defined $level;
+ my @arg;
+ {
+ if (defined $Msg->{$type}) {
+ my $msg = $Msg->{$type}->[1];
+ $msg =~ s{\$([0-9]+)}{
+ defined $arg[$1] ? ($arg[$1]) : '(undef)';
+ }ge; ##BUG: ^ must be escaped
+ $msg =~ s{{\@([A-Za-z0-9:_.-]+)}}{
+ UNIVERSAL::can ($node, 'get_attribute_ns')
+ ? ($node->get_attribute_ns (undef, $1)) : ''
+ }ge; ## BUG: ^ must be escaped
+ $msg =~ s{{\@}}{ ## BUG: v must be escaped
+ UNIVERSAL::can ($node, 'value') ? ($node->value) : ''
+ }ge;
+ $msg =~ s{{local-name}}{
+ UNIVERSAL::can ($node, 'manakai_local_name')
+ ? ($node->manakai_local_name) : ''
+ }ge; ## BUG: ^ must be escaped
+ $msg =~ s{{element-local-name}}{
+ (UNIVERSAL::can ($node, 'owner_element') and
+ $node->owner_element)
+ ? ($node->owner_element->manakai_local_name)
+ : '' ## BUG: ^ must be escaped
+ }ge;
+ return ($type, 'level-' . $level . ' ' . $Msg->{$type}->[0], $msg);
+ } elsif ($type =~ s/:([^:]*)$//) {
+ unshift @arg, $1;
+ redo;
+ }
+ }
+ return ($type, 'level-'.$level, ($_[0]));
+ ## BUG: ^ must be escaped
+} # get_text
+
+}
+
+sub get_input_document ($$) {
+ my ($http, $dom) = @_;
+
+ require Encode;
+ my $request_uri = Encode::decode ('utf-8', $http->get_parameter ('uri'));
+ my $r = WebHACC::Input->new;
+ if (defined $request_uri and length $request_uri) {
+ my $uri = $dom->create_uri_reference ($request_uri);
+ unless ({
+ http => 1,
+ }->{lc $uri->uri_scheme}) {
+ $r = WebHACC::Input::Error->new;
+ $r->{uri} = $request_uri;
+ $r->{request_uri} = $request_uri;
+ $r->{error_status_text} = 'URL scheme not allowed';
}
- print STDOUT qq[
-
-
-];
- }
-
- ## TODO: Show result
-
- print STDOUT qq[
-
-];
- for (@nav) {
- print STDOUT qq[- $_->[1]
];
- }
- print STDOUT qq[
-
-
-