1 |
#!/usr/bin/perl |
#!/usr/bin/perl -d:DProf |
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/</</g; |
|
14 |
$s =~ s/>/>/g; |
require WebHACC::Output; |
15 |
$s =~ s/"/"/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; |
|
|
} |
|
|
|
|
|
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="/www/style/html/xhtml"> |
|
|
<style> |
|
|
q { |
|
|
white-space: pre; |
|
|
white-space: -moz-pre-wrap; |
|
|
white-space: pre-wrap; |
|
|
} |
|
|
</style> |
|
|
</head> |
|
|
<body> |
|
|
<h1>Web Document Conformance Checker (<em>beta</em>)</h1> |
|
|
|
|
|
<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 |
|
|
|
|
|
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 id="source-string" class="section"> |
|
|
]; |
|
|
print_source_string (\$s); |
|
|
print STDOUT qq[ |
|
|
</div> |
|
|
|
|
|
<div id="parse-errors" class="section"> |
|
|
<h2>Parse Errors</h2> |
|
|
|
|
|
<ul> |
|
|
]; |
|
|
|
|
|
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 id="source-string" class="section"> |
|
|
]; |
|
|
print_source_string (\$t); |
|
|
print STDOUT qq[ |
|
|
</div> |
|
|
|
|
|
<div id="parse-errors" class="section"> |
|
|
<h2>Parse Errors</h2> |
|
|
|
|
|
<ul> |
|
|
]; |
|
|
|
|
|
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> |
|
18 |
|
|
19 |
<p><em>Media type <code class="MIME" lang="en">@{[htescape $input_format]}</code> is not supported!</em></p> |
if ($http->get_meta_variable ('PATH_INFO') ne '/') { |
20 |
]; |
$out->http_error (404); |
21 |
|
exit; |
22 |
} |
} |
23 |
|
|
24 |
|
## TODO: We need real conneg support... |
25 |
if (defined $doc or defined $el) { |
my $primary_language = 'en'; |
26 |
print STDOUT qq[ |
if ($ENV{HTTP_ACCEPT_LANGUAGE} =~ /ja/) { |
27 |
<div id="document-tree" class="section"> |
$primary_language = 'ja'; |
28 |
<h2>Document Tree</h2> |
} |
29 |
]; |
$out->load_text_catalog ($primary_language); |
30 |
|
|
31 |
print_document_tree ($el || $doc); |
$out->set_flush; |
32 |
|
$out->http_header; |
33 |
print STDOUT qq[ |
$out->html_header; |
34 |
</div> |
$out->unset_flush; |
35 |
|
|
36 |
<div id="document-errors" class="section"> |
$out->generate_input_section ($http); |
37 |
<h2>Document Errors</h2> |
|
38 |
|
my $u = $http->get_parameter ('uri'); |
39 |
<ul> |
my $s = $http->get_parameter ('s'); |
40 |
]; |
if ((not defined $u or not length $u) and |
41 |
|
(not defined $s or not length $s)) { |
42 |
require Whatpm::ContentChecker; |
exit; |
|
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> |
|
|
]; |
|
43 |
} |
} |
44 |
|
|
45 |
## TODO: Show result |
require WebHACC::Result; |
46 |
print STDOUT qq[ |
my $result = WebHACC::Result->new; |
47 |
</body> |
$result->output ($out); |
48 |
</html> |
|
49 |
]; |
require WebHACC::Input; |
50 |
|
my $input = WebHACC::Input->get_document ($http => $result => $out); |
51 |
exit; |
|
52 |
|
check_and_print ($input => $result => $out); |
53 |
sub print_source_string ($) { |
|
54 |
my $s = $_[0]; |
$out->nav_list; |
55 |
my $i = 1; |
|
56 |
print STDOUT qq[<ol lang="">\n]; |
exit; |
57 |
while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) { |
} |
58 |
print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |
|
59 |
$i++; |
sub check_and_print ($$$) { |
60 |
|
my ($input, $result, $out) = @_; |
61 |
|
my $original_input = $out->input; |
62 |
|
$out->input ($input); |
63 |
|
|
64 |
|
$input->generate_info_section ($result); |
65 |
|
|
66 |
|
$input->generate_transfer_sections ($result); |
67 |
|
|
68 |
|
unless (defined $input->{s}) { |
69 |
|
## NOTE: This is an error of the implementation. |
70 |
|
$result->layer_uncertain ('transfer'); |
71 |
|
$result->generate_result_section; |
72 |
|
|
73 |
|
$out->input ($original_input); |
74 |
|
return; |
75 |
|
} |
76 |
|
|
77 |
|
my $checker_class = { |
78 |
|
'text/cache-manifest' => 'WebHACC::Language::CacheManifest', |
79 |
|
'text/css' => 'WebHACC::Language::CSS', |
80 |
|
'text/html' => 'WebHACC::Language::HTML', |
81 |
|
'text/x-h2h' => 'WebHACC::Language::H2H', |
82 |
|
'text/x-webidl' => 'WebHACC::Language::WebIDL', |
83 |
|
|
84 |
|
'text/xml' => 'WebHACC::Language::XML', |
85 |
|
'application/atom+xml' => 'WebHACC::Language::XML', |
86 |
|
'application/rss+xml' => 'WebHACC::Language::XML', |
87 |
|
'image/svg+xml' => 'WebHACC::Language::XML', |
88 |
|
'application/xhtml+xml' => 'WebHACC::Language::XML', |
89 |
|
'application/xml' => 'WebHACC::Language::XML', |
90 |
|
## TODO: Should we make all XML MIME Types fall |
91 |
|
## into this category? |
92 |
|
|
93 |
|
## NOTE: This type has different model from normal XML types. |
94 |
|
'application/rdf+xml' => 'WebHACC::Language::XML', |
95 |
|
}->{$input->{media_type}} || 'WebHACC::Language::Default'; |
96 |
|
|
97 |
|
eval qq{ require $checker_class } or die "$0: Loading $checker_class: $@"; |
98 |
|
my $checker = $checker_class->new; |
99 |
|
$checker->input ($input); |
100 |
|
$checker->output ($out); |
101 |
|
$checker->result ($result); |
102 |
|
|
103 |
|
## TODO: A cache manifest MUST be text/cache-manifest |
104 |
|
## TODO: WebIDL media type "text/x-webidl" |
105 |
|
|
106 |
|
$checker->generate_syntax_error_section; |
107 |
|
$checker->generate_source_string_section; |
108 |
|
|
109 |
|
my @subdoc; |
110 |
|
$checker->onsubdoc (sub { |
111 |
|
push @subdoc, shift; |
112 |
|
}); |
113 |
|
|
114 |
|
$checker->generate_structure_dump_section; |
115 |
|
$checker->generate_structure_error_section; |
116 |
|
$checker->generate_additional_sections; |
117 |
|
|
118 |
|
my $id_prefix = 0; |
119 |
|
for my $_subinput (@subdoc) { |
120 |
|
my $subinput = WebHACC::Input::Subdocument->new (++$id_prefix); |
121 |
|
$subinput->{$_} = $_subinput->{$_} for keys %$_subinput; |
122 |
|
$subinput->{base_uri} = $subinput->{container_node}->base_uri |
123 |
|
unless defined $subinput->{base_uri}; |
124 |
|
$subinput->{parent_input} = $input; |
125 |
|
|
126 |
|
my $subresult = WebHACC::Result->new; |
127 |
|
$subresult->output ($out); |
128 |
|
$subresult->parent_result ($result); |
129 |
|
|
130 |
|
$subinput->start_section ($subresult); |
131 |
|
check_and_print ($subinput => $subresult => $out); |
132 |
|
$subinput->end_section ($subresult); |
133 |
} |
} |
|
if ($$s =~ /\G([^\x0A]+)/gc) { |
|
|
print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |
|
|
} |
|
|
print STDOUT "</ol>"; |
|
|
} # print_input_string |
|
134 |
|
|
135 |
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 |
|
|
} |
|
|
} |
|
136 |
|
|
137 |
$r .= '</ol>'; |
$out->input ($original_input); |
138 |
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 |
|
139 |
|
|
140 |
=head1 AUTHOR |
=head1 AUTHOR |
141 |
|
|
143 |
|
|
144 |
=head1 LICENSE |
=head1 LICENSE |
145 |
|
|
146 |
Copyright 2007 Wakaba <w@suika.fam.cx> |
Copyright 2007-2008 Wakaba <w@suika.fam.cx> |
147 |
|
|
148 |
This library is free software; you can redistribute it |
This library is free software; you can redistribute it |
149 |
and/or modify it under the same terms as Perl itself. |
and/or modify it under the same terms as Perl itself. |