5 |
/home/wakaba/work/manakai/lib |
/home/wakaba/work/manakai/lib |
6 |
/home/wakaba/public_html/-temp/wiki/lib]; |
/home/wakaba/public_html/-temp/wiki/lib]; |
7 |
use CGI::Carp qw[fatalsToBrowser]; |
use CGI::Carp qw[fatalsToBrowser]; |
8 |
use Time::HiRes qw/time/; |
use Scalar::Util qw[refaddr]; |
9 |
|
|
10 |
use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module |
use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module |
11 |
|
|
12 |
|
sub htescape ($) { |
13 |
|
my $s = $_[0]; |
14 |
|
$s =~ s/&/&/g; |
15 |
|
$s =~ s/</</g; |
16 |
|
$s =~ s/>/>/g; |
17 |
|
$s =~ s/"/"/g; |
18 |
|
$s =~ s!([\x00-\x09\x0B-\x1F\x7F-\x80])!sprintf '<var>U+%04X</var>', ord $1!ge; |
19 |
|
return $s; |
20 |
|
} # htescape |
21 |
|
|
22 |
my $http = SuikaWiki::Input::HTTP->new; |
my $http = SuikaWiki::Input::HTTP->new; |
23 |
|
|
24 |
## TODO: _charset_ |
## TODO: _charset_ |
25 |
|
|
26 |
my @mode = split m#/#, scalar $http->meta_variable ('PATH_INFO'), -1; |
my $input_format = $http->parameter ('i') || 'text/html'; |
27 |
shift @mode if @mode and $mode[0] == ''; |
my $inner_html_element = $http->parameter ('e'); |
28 |
## TODO: decode unreserved characters |
my $input_uri = 'thismessage:/'; |
29 |
|
|
30 |
my $s = $http->parameter ('s'); |
my $s = $http->parameter ('s'); |
31 |
if (length $s > 1000_000) { |
if (length $s > 1000_000) { |
32 |
print STDOUT "Status: 400 Document Too Long\nContent-Type: text/plain; charset=us-ascii\n\nToo long"; |
print STDOUT "Status: 400 Document Too Long\nContent-Type: text/plain; charset=us-ascii\n\nToo long"; |
33 |
exit; |
exit; |
34 |
} |
} |
35 |
my $char_length = length $s; |
|
36 |
my %time; |
my @nav; |
37 |
my $time1; |
print STDOUT qq[Content-Type: text/html; charset=utf-8 |
38 |
my $time2; |
|
39 |
|
<!DOCTYPE html> |
40 |
|
<html lang="en"> |
41 |
|
<head> |
42 |
|
<title>Web Document Conformance Checker (BETA)</title> |
43 |
|
<link rel="stylesheet" href="../cc-style.css" type="text/css"> |
44 |
|
</head> |
45 |
|
<body> |
46 |
|
<h1>Web Document Conformance Checker (<em>beta</em>)</h1> |
47 |
|
|
48 |
|
<div id="document-info" section="section"> |
49 |
|
<dl> |
50 |
|
<dt>Document URI</dt> |
51 |
|
<dd><code class="URI" lang=""><<a href="@{[htescape $input_uri]}">@{[htescape $input_uri]}</a>></code></dd> |
52 |
|
<dt>Internet Media Type</dt> |
53 |
|
<dd><code class="MIME" lang="en">@{[htescape $input_format]}</code></dd> |
54 |
|
]; # no </dl> yet |
55 |
|
push @nav, ['#document-info' => 'Information']; |
56 |
|
|
57 |
require Message::DOM::DOMImplementation; |
require Message::DOM::DOMImplementation; |
58 |
my $dom = Message::DOM::DOMImplementation->____new; |
my $dom = Message::DOM::DOMImplementation->____new; |
|
# $| = 1; |
|
59 |
my $doc; |
my $doc; |
60 |
my $el; |
my $el; |
61 |
|
|
62 |
if (@mode == 3 and $mode[0] eq 'html' and |
if ($input_format eq 'text/html') { |
63 |
($mode[2] eq 'html' or $mode[2] eq 'test')) { |
require Encode; |
64 |
print STDOUT "Content-Type: text/plain; charset=utf-8\n\n"; |
require Whatpm::HTML; |
65 |
|
|
66 |
require Encode; |
$s = Encode::decode ('utf-8', $s); |
67 |
require Whatpm::HTML; |
|
68 |
|
print STDOUT qq[ |
69 |
$time1 = time; |
<dt>Character Encoding</dt> |
70 |
$s = Encode::decode ('utf-8', $s); |
<dd>(none)</dd> |
71 |
$time2 = time; |
</dl> |
72 |
$time{decode} = $time2 - $time1; |
</div> |
73 |
|
|
74 |
|
<div id="source-string" class="section"> |
75 |
print STDOUT "#errors\n"; |
<h2>Document Source</h2> |
76 |
|
]; |
77 |
|
push @nav, ['#source-string' => 'Source']; |
78 |
|
print_source_string (\$s); |
79 |
|
print STDOUT qq[ |
80 |
|
</div> |
81 |
|
|
82 |
|
<div id="parse-errors" class="section"> |
83 |
|
<h2>Parse Errors</h2> |
84 |
|
|
85 |
|
<ul> |
86 |
|
]; |
87 |
|
push @nav, ['#parse-errors' => 'Parse Error']; |
88 |
|
|
89 |
my $onerror = sub { |
my $onerror = sub { |
90 |
my (%opt) = @_; |
my (%opt) = @_; |
91 |
print STDOUT "$opt{line},$opt{column},$opt{type}\n"; |
if ($opt{column} > 0) { |
92 |
|
print STDOUT qq[<li><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}: ]; |
93 |
|
} else { |
94 |
|
$opt{line}--; |
95 |
|
print STDOUT qq[<li><a href="#line-$opt{line}">Line $opt{line}</a>: ]; |
96 |
|
} |
97 |
|
print STDOUT qq[@{[htescape $opt{type}]}</li>\n]; |
98 |
}; |
}; |
99 |
|
|
100 |
$doc = $dom->create_document; |
$doc = $dom->create_document; |
101 |
$time1 = time; |
if (defined $inner_html_element and length $inner_html_element) { |
|
if (length $mode[1]) { |
|
102 |
$el = $doc->create_element_ns |
$el = $doc->create_element_ns |
103 |
('http://www.w3.org/1999/xhtml', [undef, $mode[1]]); |
('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); |
104 |
Whatpm::HTML->set_inner_html ($el, $s, $onerror); |
Whatpm::HTML->set_inner_html ($el, $s, $onerror); |
105 |
} else { |
} else { |
106 |
Whatpm::HTML->parse_string ($s => $doc, $onerror); |
Whatpm::HTML->parse_string ($s => $doc, $onerror); |
107 |
} |
} |
|
$time2 = time; |
|
|
$time{parse} = $time2 - $time1; |
|
108 |
|
|
109 |
print "#document\n"; |
print STDOUT qq[ |
110 |
|
</ul> |
111 |
my $out; |
</div> |
112 |
if ($mode[2] eq 'html') { |
]; |
113 |
$time1 = time; |
} elsif ($input_format eq 'application/xhtml+xml') { |
114 |
$out = Whatpm::HTML->get_inner_html ($el || $doc); |
require Message::DOM::XMLParserTemp; |
115 |
$time2 = time; |
require Encode; |
116 |
$time{serialize_html} = $time2 - $time1; |
|
117 |
} else { # test |
my $t = Encode::decode ('utf-8', $s); |
118 |
$time1 = time; |
|
119 |
$out = test_serialize ($el || $doc); |
print STDOUT qq[ |
120 |
$time2 = time; |
<dt>Character Encoding</dt> |
121 |
$time{serialize_test} = $time2 - $time1; |
<dd>(none)</dd> |
122 |
} |
</dl> |
123 |
print STDOUT Encode::encode ('utf-8', $$out); |
</div> |
124 |
print STDOUT "\n"; |
|
125 |
} elsif (@mode == 3 and $mode[0] eq 'xhtml' and |
<div id="source-string" class="section"> |
126 |
($mode[2] eq 'html' or $mode[2] eq 'test')) { |
<h2>Document Source</h2> |
127 |
print STDOUT "Content-Type: text/plain; charset=utf-8\n\n"; |
]; |
128 |
|
push @nav, ['#source-string' => 'Source']; |
129 |
require Message::DOM::XMLParserTemp; |
print_source_string (\$t); |
130 |
print STDOUT "#errors\n"; |
print STDOUT qq[ |
131 |
|
</div> |
132 |
|
|
133 |
|
<div id="parse-errors" class="section"> |
134 |
|
<h2>Parse Errors</h2> |
135 |
|
|
136 |
|
<ul> |
137 |
|
]; |
138 |
|
push @nav, ['#parse-errors' => 'Parse Error']; |
139 |
|
|
140 |
my $onerror = sub { |
my $onerror = sub { |
141 |
my $err = shift; |
my $err = shift; |
142 |
print STDOUT $err->location->line_number, ","; |
my $line = $err->location->line_number; |
143 |
print STDOUT $err->location->column_number, ","; |
print STDOUT qq[<li><a href="#line-$line">Line $line</a> column ]; |
144 |
print STDOUT $err->text, "\n"; |
print STDOUT $err->location->column_number, ": "; |
145 |
|
print STDOUT htescape $err->text, "</li>\n"; |
146 |
return 1; |
return 1; |
147 |
}; |
}; |
148 |
|
|
149 |
open my $fh, '<', \$s; |
open my $fh, '<', \$s; |
|
my $time1 = time; |
|
150 |
$doc = Message::DOM::XMLParserTemp->parse_byte_stream |
$doc = Message::DOM::XMLParserTemp->parse_byte_stream |
151 |
($fh => $dom, $onerror, charset => 'utf-8'); |
($fh => $dom, $onerror, charset => 'utf-8'); |
|
my $time2 = time; |
|
|
$time{parse_xml} = $time2 - $time1; |
|
152 |
|
|
153 |
print "#document\n"; |
print STDOUT qq[ |
154 |
|
</ul> |
155 |
|
</div> |
156 |
|
]; |
157 |
|
} else { |
158 |
|
print STDOUT qq[ |
159 |
|
</dl> |
160 |
|
|
161 |
|
<div id="result-summary" class="section"> |
162 |
|
<p><em>Media type <code class="MIME" lang="en">@{[htescape $input_format]}</code> is not supported!</em></p> |
163 |
|
</div> |
164 |
|
]; |
165 |
|
push @nav, ['#result-summary' => 'Result']; |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
my $out; |
if (defined $doc or defined $el) { |
170 |
if ($mode[2] eq 'html') { |
print STDOUT qq[ |
171 |
## TODO: Use XHTML serializer |
<div id="document-tree" class="section"> |
172 |
#$out = Whatpm::HTML->get_inner_html ($doc); |
<h2>Document Tree</h2> |
173 |
} else { # test |
]; |
174 |
$time1 = time; |
push @nav, ['#document-tree' => 'Tree']; |
175 |
$out = test_serialize ($doc); |
|
176 |
$time2 = time; |
print_document_tree ($el || $doc); |
177 |
$time{serialize_test} = $time2 - $time1; |
|
178 |
} |
print STDOUT qq[ |
179 |
print STDOUT Encode::encode ('utf-8', $$out); |
</div> |
180 |
print STDOUT "\n"; |
|
181 |
} else { |
<div id="document-errors" class="section"> |
182 |
print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n404"; |
<h2>Document Errors</h2> |
183 |
exit; |
|
184 |
} |
<ul> |
185 |
|
]; |
186 |
|
push @nav, ['#document-errors' => 'Document Error']; |
187 |
|
|
|
if ($http->parameter ('dom5')) { |
|
188 |
require Whatpm::ContentChecker; |
require Whatpm::ContentChecker; |
189 |
my $onerror = sub { |
my $onerror = sub { |
190 |
my %opt = @_; |
my %opt = @_; |
191 |
print STDOUT get_node_path ($opt{node}) . ';' . $opt{type} . "\n"; |
print STDOUT qq[<li><a href="#node-@{[refaddr $opt{node}]}">], |
192 |
|
htescape get_node_path ($opt{node}), |
193 |
|
"</a>: ", htescape $opt{type}, "</li>\n"; |
194 |
}; |
}; |
195 |
print STDOUT "#domerrors\n"; |
|
|
$time1 = time; |
|
196 |
if ($el) { |
if ($el) { |
197 |
Whatpm::ContentChecker->check_element ($el, $onerror); |
Whatpm::ContentChecker->check_element ($el, $onerror); |
198 |
} else { |
} else { |
199 |
Whatpm::ContentChecker->check_document ($doc, $onerror); |
Whatpm::ContentChecker->check_document ($doc, $onerror); |
200 |
} |
} |
201 |
$time2 = time; |
|
202 |
$time{check} = $time2 - $time1; |
print STDOUT qq[ |
203 |
|
</ul> |
204 |
|
</div> |
205 |
|
]; |
206 |
} |
} |
207 |
|
|
208 |
print STDOUT "#log\n"; |
## TODO: Show result |
209 |
for (qw/decode parse parse_xml serialize_html serialize_xml serialize_test |
|
210 |
check/) { |
print STDOUT qq[ |
211 |
next unless defined $time{$_}; |
<ul class="navigation" id="nav-items"> |
212 |
print STDOUT { |
]; |
213 |
decode => 'bytes->chars', |
for (@nav) { |
214 |
parse => 'html5(chars)->dom5', |
print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>]; |
|
parse_xml => 'xml1(chars)->dom5', |
|
|
serialize_html => 'dom5->html5(char)', |
|
|
serialize_xml => 'dom5->xml1(char)', |
|
|
serialize_test => 'dom5->test(char)', |
|
|
check => 'dom5 check', |
|
|
}->{$_}; |
|
|
print STDOUT "\t", $time{$_}, "s\n"; |
|
|
open my $file, '>>', ".manakai-$_.txt" or die ".manakai-$_.txt: $!"; |
|
|
print $file $char_length, "\t", $time{$_}, "\n"; |
|
215 |
} |
} |
216 |
|
print STDOUT qq[ |
217 |
|
</ul> |
218 |
|
</body> |
219 |
|
</html> |
220 |
|
]; |
221 |
|
|
222 |
exit; |
exit; |
223 |
|
|
224 |
sub test_serialize ($) { |
sub print_source_string ($) { |
225 |
|
my $s = $_[0]; |
226 |
|
my $i = 1; |
227 |
|
print STDOUT qq[<ol lang="">\n]; |
228 |
|
while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) { |
229 |
|
print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |
230 |
|
$i++; |
231 |
|
} |
232 |
|
if ($$s =~ /\G([^\x0A]+)/gc) { |
233 |
|
print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |
234 |
|
} |
235 |
|
print STDOUT "</ol>"; |
236 |
|
} # print_input_string |
237 |
|
|
238 |
|
sub print_document_tree ($) { |
239 |
my $node = shift; |
my $node = shift; |
240 |
my $r = ''; |
my $r = '<ol class="xoxo">'; |
241 |
|
|
242 |
my @node = map { [$_, ''] } @{$node->child_nodes}; |
my @node = ($node); |
243 |
while (@node) { |
while (@node) { |
244 |
my $child = shift @node; |
my $child = shift @node; |
245 |
my $nt = $child->[0]->node_type; |
unless (ref $child) { |
246 |
if ($nt == $child->[0]->ELEMENT_NODE) { |
$r .= $child; |
247 |
$r .= '| ' . $child->[1] . '<' . $child->[0]->tag_name . ">\x0A"; ## ISSUE: case? |
next; |
248 |
|
} |
249 |
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value] } |
|
250 |
@{$child->[0]->attributes}) { |
my $node_id = 'node-'.refaddr $child; |
251 |
$r .= '| ' . $child->[1] . ' ' . $attr->[0] . '="'; ## ISSUE: case? |
my $nt = $child->node_type; |
252 |
$r .= $attr->[1] . '"' . "\x0A"; |
if ($nt == $child->ELEMENT_NODE) { |
253 |
|
$r .= qq'<li id="$node_id"><code>' . htescape ($child->tag_name) . |
254 |
|
'</code>'; ## ISSUE: case |
255 |
|
|
256 |
|
if ($child->has_attributes) { |
257 |
|
$r .= '<ul class="attributes">'; |
258 |
|
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, 'node-'.refaddr $_] } |
259 |
|
@{$child->attributes}) { |
260 |
|
$r .= qq'<li id="$attr->[2]"><code>' . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case? |
261 |
|
$r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children |
262 |
|
} |
263 |
|
$r .= '</ul>'; |
264 |
|
} |
265 |
|
|
266 |
|
if ($node->has_child_nodes) { |
267 |
|
$r .= '<ol class="children">'; |
268 |
|
unshift @node, @{$child->child_nodes}, '</ol>'; |
269 |
|
} |
270 |
|
} elsif ($nt == $child->TEXT_NODE) { |
271 |
|
$r .= qq'<li id="$node_id"><q>' . htescape ($child->data) . '</q></li>'; |
272 |
|
} elsif ($nt == $child->CDATA_SECTION_NODE) { |
273 |
|
$r .= qq'<li id="$node_id"><code><[CDATA[</code><q>' . htescape ($child->data) . '</q><code>]]></code></li>'; |
274 |
|
} elsif ($nt == $child->COMMENT_NODE) { |
275 |
|
$r .= qq'<li id="$node_id"><code><!--</code><q>' . htescape ($child->data) . '</q><code>--></code></li>'; |
276 |
|
} elsif ($nt == $child->DOCUMENT_NODE) { |
277 |
|
$r .= qq'<li id="$node_id">Document</li>'; |
278 |
|
if ($child->has_child_nodes) { |
279 |
|
$r .= '<ol>'; |
280 |
|
unshift @node, @{$child->child_nodes}, '</ol>'; |
281 |
} |
} |
282 |
|
} elsif ($nt == $child->DOCUMENT_TYPE_NODE) { |
283 |
unshift @node, |
$r .= qq'<li id="$node_id"><code><!DOCTYPE></code><ul>'; |
284 |
map { [$_, $child->[1] . ' '] } @{$child->[0]->child_nodes}; |
$r .= '<li>Name = <q>@{[htescape ($child->name)]}</q></li>'; |
285 |
} elsif ($nt == $child->[0]->TEXT_NODE) { |
$r .= '<li>Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>'; |
286 |
$r .= '| ' . $child->[1] . '"' . $child->[0]->data . '"' . "\x0A"; |
$r .= '<li>System identifier = <q>@{[htescape ($child->system_id)]}</q></li>'; |
287 |
} elsif ($nt == $child->[0]->CDATA_SECTION_NODE) { |
$r .= '</ul></li>'; |
288 |
$r .= '| ' . $child->[1] . '<![CDATA[' . $child->[0]->data . "]]>\x0A"; |
} elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) { |
289 |
} elsif ($nt == $child->[0]->COMMENT_NODE) { |
$r .= qq'<li id="$node_id"><code><?@{[htescape ($child->target)]}?></code>'; |
290 |
$r .= '| ' . $child->[1] . '<!-- ' . $child->[0]->data . " -->\x0A"; |
$r .= '<ul><li>@{[htescape ($child->data)]}</li></ul></li>'; |
|
} elsif ($nt == $child->[0]->DOCUMENT_TYPE_NODE) { |
|
|
$r .= '| ' . $child->[1] . '<!DOCTYPE ' . $child->[0]->name . ">\x0A"; |
|
|
} elsif ($nt == $child->[0]->PROCESSING_INSTRUCTION_NODE) { |
|
|
$r .= '| ' . $child->[1] . '<?' . $child->[0]->target . ' ' . |
|
|
$child->[0]->data . "?>\x0A"; |
|
291 |
} else { |
} else { |
292 |
$r .= '| ' . $child->[1] . $child->[0]->node_type . "\x0A"; # error |
$r .= qq'<li id="$node_id">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error |
293 |
} |
} |
294 |
} |
} |
295 |
|
|
296 |
return \$r; |
$r .= '</ol>'; |
297 |
} # test_serialize |
print STDOUT $r; |
298 |
|
} # print_document_tree |
299 |
|
|
300 |
sub get_node_path ($) { |
sub get_node_path ($) { |
301 |
my $node = shift; |
my $node = shift; |