1 |
#!/usr/bin/perl |
2 |
use strict; |
3 |
|
4 |
use lib qw[/home/httpd/html/www/markup/html/whatpm |
5 |
/home/wakaba/work/manakai/lib |
6 |
/home/wakaba/public_html/-temp/wiki/lib]; |
7 |
use CGI::Carp qw[fatalsToBrowser]; |
8 |
use Scalar::Util qw[refaddr]; |
9 |
|
10 |
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; |
23 |
|
24 |
## TODO: _charset_ |
25 |
|
26 |
my $input_format = $http->parameter ('i') || 'text/html'; |
27 |
my $inner_html_element = $http->parameter ('e'); |
28 |
my $input_uri = 'thismessage:/'; |
29 |
|
30 |
my $s = $http->parameter ('s'); |
31 |
if (length $s > 1000_000) { |
32 |
print STDOUT "Status: 400 Document Too Long\nContent-Type: text/plain; charset=us-ascii\n\nToo long"; |
33 |
exit; |
34 |
} |
35 |
|
36 |
my @nav; |
37 |
print STDOUT qq[Content-Type: text/html; charset=utf-8 |
38 |
|
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" class="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; |
58 |
my $dom = Message::DOM::DOMImplementation->____new; |
59 |
my $doc; |
60 |
my $el; |
61 |
|
62 |
if ($input_format eq 'text/html') { |
63 |
require Encode; |
64 |
require Whatpm::HTML; |
65 |
|
66 |
$s = Encode::decode ('utf-8', $s); |
67 |
|
68 |
print STDOUT qq[ |
69 |
<dt>Character Encoding</dt> |
70 |
<dd>(none)</dd> |
71 |
</dl> |
72 |
</div> |
73 |
|
74 |
<div id="source-string" class="section"> |
75 |
<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 |
<dl> |
86 |
]; |
87 |
push @nav, ['#parse-errors' => 'Parse Error']; |
88 |
|
89 |
my $onerror = sub { |
90 |
my (%opt) = @_; |
91 |
if ($opt{column} > 0) { |
92 |
print STDOUT qq[<dt><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}</dt>\n]; |
93 |
} else { |
94 |
$opt{line}--; |
95 |
print STDOUT qq[<dt><a href="#line-$opt{line}">Line $opt{line}</a></dt>\n]; |
96 |
} |
97 |
print STDOUT qq[<dd>@{[htescape $opt{type}]}</dd>\n]; |
98 |
}; |
99 |
|
100 |
$doc = $dom->create_document; |
101 |
if (defined $inner_html_element and length $inner_html_element) { |
102 |
$el = $doc->create_element_ns |
103 |
('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); |
104 |
Whatpm::HTML->set_inner_html ($el, $s, $onerror); |
105 |
} else { |
106 |
Whatpm::HTML->parse_string ($s => $doc, $onerror); |
107 |
} |
108 |
|
109 |
print STDOUT qq[ |
110 |
</dl> |
111 |
</div> |
112 |
]; |
113 |
} elsif ($input_format eq 'application/xhtml+xml') { |
114 |
require Message::DOM::XMLParserTemp; |
115 |
require Encode; |
116 |
|
117 |
my $t = Encode::decode ('utf-8', $s); |
118 |
|
119 |
print STDOUT qq[ |
120 |
<dt>Character Encoding</dt> |
121 |
<dd>(none)</dd> |
122 |
</dl> |
123 |
</div> |
124 |
|
125 |
<div id="source-string" class="section"> |
126 |
<h2>Document Source</h2> |
127 |
]; |
128 |
push @nav, ['#source-string' => 'Source']; |
129 |
print_source_string (\$t); |
130 |
print STDOUT qq[ |
131 |
</div> |
132 |
|
133 |
<div id="parse-errors" class="section"> |
134 |
<h2>Parse Errors</h2> |
135 |
|
136 |
<dl> |
137 |
]; |
138 |
push @nav, ['#parse-errors' => 'Parse Error']; |
139 |
|
140 |
my $onerror = sub { |
141 |
my $err = shift; |
142 |
my $line = $err->location->line_number; |
143 |
print STDOUT qq[<dt><a href="#line-$line">Line $line</a> column ]; |
144 |
print STDOUT $err->location->column_number, "</dt><dd>"; |
145 |
print STDOUT htescape $err->text, "</dd>\n"; |
146 |
return 1; |
147 |
}; |
148 |
|
149 |
open my $fh, '<', \$s; |
150 |
$doc = Message::DOM::XMLParserTemp->parse_byte_stream |
151 |
($fh => $dom, $onerror, charset => 'utf-8'); |
152 |
|
153 |
print STDOUT qq[ |
154 |
</dl> |
155 |
</div> |
156 |
]; |
157 |
} else { |
158 |
print STDOUT qq[ |
159 |
</dl> |
160 |
</div> |
161 |
|
162 |
<div id="result-summary" class="section"> |
163 |
<p><em>Media type <code class="MIME" lang="en">@{[htescape $input_format]}</code> is not supported!</em></p> |
164 |
</div> |
165 |
]; |
166 |
push @nav, ['#result-summary' => 'Result']; |
167 |
} |
168 |
|
169 |
|
170 |
if (defined $doc or defined $el) { |
171 |
print STDOUT qq[ |
172 |
<div id="document-tree" class="section"> |
173 |
<h2>Document Tree</h2> |
174 |
]; |
175 |
push @nav, ['#document-tree' => 'Tree']; |
176 |
|
177 |
print_document_tree ($el || $doc); |
178 |
|
179 |
print STDOUT qq[ |
180 |
</div> |
181 |
|
182 |
<div id="document-errors" class="section"> |
183 |
<h2>Document Errors</h2> |
184 |
|
185 |
<dl> |
186 |
]; |
187 |
push @nav, ['#document-errors' => 'Document Error']; |
188 |
|
189 |
require Whatpm::ContentChecker; |
190 |
my $onerror = sub { |
191 |
my %opt = @_; |
192 |
print STDOUT qq[<dt>] . get_node_link ($opt{node}) . |
193 |
"</dt>\n<dd>", htescape $opt{type}, "</dd>\n"; |
194 |
}; |
195 |
|
196 |
my $elements; |
197 |
if ($el) { |
198 |
$elements = Whatpm::ContentChecker->check_element ($el, $onerror); |
199 |
} else { |
200 |
$elements = Whatpm::ContentChecker->check_document ($doc, $onerror); |
201 |
} |
202 |
|
203 |
print STDOUT qq[ |
204 |
</dl> |
205 |
</div> |
206 |
]; |
207 |
|
208 |
if (@{$elements->{table}}) { |
209 |
require JSON; |
210 |
|
211 |
print STDOUT qq[ |
212 |
<div id="tables" class="section"> |
213 |
<h2>Tables</h2> |
214 |
|
215 |
<!--[if IE]><script type="text/javascript" src="../excanvas.js"></script><![endif]--> |
216 |
<script src="../table-script.js" type="text/javascript"></script> |
217 |
<noscript> |
218 |
<p><em>Structure of tables are visualized here if scripting is enabled.</em></p> |
219 |
</noscript> |
220 |
]; |
221 |
|
222 |
my $i = 0; |
223 |
for my $table_el (@{$elements->{table}}) { |
224 |
$i++; |
225 |
print STDOUT qq[<div class="section" id="table-$i"><h3>] . |
226 |
get_node_link ($table_el) . q[</h3>]; |
227 |
|
228 |
my $table = Whatpm::HTMLTable->form_table ($table_el); |
229 |
|
230 |
for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption}) { |
231 |
next unless $_; |
232 |
delete $_->{element}; |
233 |
} |
234 |
|
235 |
for (@{$table->{row_group}}) { |
236 |
next unless $_; |
237 |
next unless $_->{element}; |
238 |
$_->{type} = $_->{element}->manakai_local_name; |
239 |
delete $_->{element}; |
240 |
} |
241 |
|
242 |
for (@{$table->{cell}}) { |
243 |
next unless $_; |
244 |
for (@{$_}) { |
245 |
next unless $_; |
246 |
for (@$_) { |
247 |
$_->{id} = refaddr $_->{element} if defined $_->{element}; |
248 |
delete $_->{element}; |
249 |
} |
250 |
} |
251 |
} |
252 |
|
253 |
print STDOUT '</div><script type="text/javascript">tableToCanvas ('; |
254 |
print STDOUT JSON::objToJson ($table); |
255 |
print STDOUT qq[, document.getElementById ('table-$i'));</script>]; |
256 |
} |
257 |
|
258 |
print STDOUT qq[</div>]; |
259 |
} |
260 |
|
261 |
if (keys %{$elements->{term}}) { |
262 |
print STDOUT qq[ |
263 |
<div id="terms" class="section"> |
264 |
<h2>Terms</h2> |
265 |
|
266 |
<dl> |
267 |
]; |
268 |
for my $term (sort {$a cmp $b} keys %{$elements->{term}}) { |
269 |
print STDOUT qq[<dt>@{[htescape $term]}</dt>]; |
270 |
for (@{$elements->{term}->{$term}}) { |
271 |
print STDOUT qq[<dd>].get_node_link ($_).qq[</dd>]; |
272 |
} |
273 |
} |
274 |
print STDOUT qq[</dl></div>]; |
275 |
} |
276 |
} |
277 |
|
278 |
## TODO: Show result |
279 |
|
280 |
print STDOUT qq[ |
281 |
<ul class="navigation" id="nav-items"> |
282 |
]; |
283 |
for (@nav) { |
284 |
print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>]; |
285 |
} |
286 |
print STDOUT qq[ |
287 |
</ul> |
288 |
</body> |
289 |
</html> |
290 |
]; |
291 |
|
292 |
exit; |
293 |
|
294 |
sub print_source_string ($) { |
295 |
my $s = $_[0]; |
296 |
my $i = 1; |
297 |
print STDOUT qq[<ol lang="">\n]; |
298 |
while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) { |
299 |
print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |
300 |
$i++; |
301 |
} |
302 |
if ($$s =~ /\G([^\x0A]+)/gc) { |
303 |
print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |
304 |
} |
305 |
print STDOUT "</ol>"; |
306 |
} # print_input_string |
307 |
|
308 |
sub print_document_tree ($) { |
309 |
my $node = shift; |
310 |
my $r = '<ol class="xoxo">'; |
311 |
|
312 |
my @node = ($node); |
313 |
while (@node) { |
314 |
my $child = shift @node; |
315 |
unless (ref $child) { |
316 |
$r .= $child; |
317 |
next; |
318 |
} |
319 |
|
320 |
my $node_id = 'node-'.refaddr $child; |
321 |
my $nt = $child->node_type; |
322 |
if ($nt == $child->ELEMENT_NODE) { |
323 |
my $child_nsuri = $child->namespace_uri; |
324 |
$r .= qq[<li id="$node_id" class="tree-element"><code title="@{[defined $child_nsuri ? $child_nsuri : '']}">] . htescape ($child->tag_name) . |
325 |
'</code>'; ## ISSUE: case |
326 |
|
327 |
if ($child->has_attributes) { |
328 |
$r .= '<ul class="attributes">'; |
329 |
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] } |
330 |
@{$child->attributes}) { |
331 |
$r .= qq[<li id="$attr->[3]" class="tree-attribute"><code title="@{[defined $_->[2] ? $_->[2] : '']}">] . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case? |
332 |
$r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children |
333 |
} |
334 |
$r .= '</ul>'; |
335 |
} |
336 |
|
337 |
if ($node->has_child_nodes) { |
338 |
$r .= '<ol class="children">'; |
339 |
unshift @node, @{$child->child_nodes}, '</ol></li>'; |
340 |
} else { |
341 |
$r .= '</li>'; |
342 |
} |
343 |
} elsif ($nt == $child->TEXT_NODE) { |
344 |
$r .= qq'<li id="$node_id" class="tree-text"><q lang="">' . htescape ($child->data) . '</q></li>'; |
345 |
} elsif ($nt == $child->CDATA_SECTION_NODE) { |
346 |
$r .= qq'<li id="$node_id" class="tree-cdata"><code><[CDATA[</code><q lang="">' . htescape ($child->data) . '</q><code>]]></code></li>'; |
347 |
} elsif ($nt == $child->COMMENT_NODE) { |
348 |
$r .= qq'<li id="$node_id" class="tree-comment"><code><!--</code><q lang="">' . htescape ($child->data) . '</q><code>--></code></li>'; |
349 |
} elsif ($nt == $child->DOCUMENT_NODE) { |
350 |
$r .= qq'<li id="$node_id" class="tree-document">Document'; |
351 |
if ($child->has_child_nodes) { |
352 |
$r .= '<ol>'; |
353 |
unshift @node, @{$child->child_nodes}, '</ol></li>'; |
354 |
} |
355 |
} elsif ($nt == $child->DOCUMENT_TYPE_NODE) { |
356 |
$r .= qq'<li id="$node_id" class="tree-doctype"><code><!DOCTYPE></code><ul class="attributes">'; |
357 |
$r .= qq[<li class="tree-doctype-name">Name = <q>@{[htescape ($child->name)]}</q></li>]; |
358 |
$r .= qq[<li class="tree-doctype-publicid">Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>]; |
359 |
$r .= qq[<li class="tree-doctype-systemid">System identifier = <q>@{[htescape ($child->system_id)]}</q></li>]; |
360 |
$r .= '</ul></li>'; |
361 |
} elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) { |
362 |
$r .= qq'<li id="$node_id" class="tree-id"><code><?@{[htescape ($child->target)]}</code> <q>@{[htescape ($child->data)]}</q><code>?></code></li>'; |
363 |
} else { |
364 |
$r .= qq'<li id="$node_id" class="tree-unknown">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error |
365 |
} |
366 |
} |
367 |
|
368 |
$r .= '</ol>'; |
369 |
print STDOUT $r; |
370 |
} # print_document_tree |
371 |
|
372 |
sub get_node_path ($) { |
373 |
my $node = shift; |
374 |
my @r; |
375 |
while (defined $node) { |
376 |
my $rs; |
377 |
if ($node->node_type == 1) { |
378 |
$rs = $node->manakai_local_name; |
379 |
$node = $node->parent_node; |
380 |
} elsif ($node->node_type == 2) { |
381 |
$rs = '@' . $node->manakai_local_name; |
382 |
$node = $node->owner_element; |
383 |
} elsif ($node->node_type == 3) { |
384 |
$rs = '"' . $node->data . '"'; |
385 |
$node = $node->parent_node; |
386 |
} elsif ($node->node_type == 9) { |
387 |
$rs = ''; |
388 |
$node = $node->parent_node; |
389 |
} else { |
390 |
$rs = '#' . $node->node_type; |
391 |
$node = $node->parent_node; |
392 |
} |
393 |
unshift @r, $rs; |
394 |
} |
395 |
return join '/', @r; |
396 |
} # get_node_path |
397 |
|
398 |
sub get_node_link ($) { |
399 |
return qq[<a href="#node-@{[refaddr $_[0]]}">] . |
400 |
htescape (get_node_path ($_[0])) . qq[</a>]; |
401 |
} # get_node_link |
402 |
|
403 |
=head1 AUTHOR |
404 |
|
405 |
Wakaba <w@suika.fam.cx>. |
406 |
|
407 |
=head1 LICENSE |
408 |
|
409 |
Copyright 2007 Wakaba <w@suika.fam.cx> |
410 |
|
411 |
This library is free software; you can redistribute it |
412 |
and/or modify it under the same terms as Perl itself. |
413 |
|
414 |
=cut |
415 |
|
416 |
## $Date: 2007/06/30 08:26:08 $ |