1 |
wakaba |
1.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 |
wakaba |
1.2 |
use Scalar::Util qw[refaddr]; |
9 |
wakaba |
1.1 |
|
10 |
|
|
use SuikaWiki::Input::HTTP; ## TODO: Use some better CGI module |
11 |
|
|
|
12 |
wakaba |
1.2 |
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 |
wakaba |
1.1 |
my $http = SuikaWiki::Input::HTTP->new; |
23 |
|
|
|
24 |
|
|
## TODO: _charset_ |
25 |
|
|
|
26 |
wakaba |
1.2 |
my $input_format = $http->parameter ('i') || 'text/html'; |
27 |
|
|
my $inner_html_element = $http->parameter ('e'); |
28 |
|
|
my $input_uri = 'thismessage:/'; |
29 |
wakaba |
1.1 |
|
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 |
wakaba |
1.2 |
|
36 |
wakaba |
1.3 |
my @nav; |
37 |
wakaba |
1.2 |
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 |
wakaba |
1.3 |
<link rel="stylesheet" href="../cc-style.css" type="text/css"> |
44 |
wakaba |
1.2 |
</head> |
45 |
|
|
<body> |
46 |
|
|
<h1>Web Document Conformance Checker (<em>beta</em>)</h1> |
47 |
|
|
|
48 |
wakaba |
1.4 |
<div id="document-info" class="section"> |
49 |
wakaba |
1.2 |
<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 |
wakaba |
1.3 |
push @nav, ['#document-info' => 'Information']; |
56 |
wakaba |
1.1 |
|
57 |
|
|
require Message::DOM::DOMImplementation; |
58 |
|
|
my $dom = Message::DOM::DOMImplementation->____new; |
59 |
|
|
my $doc; |
60 |
|
|
my $el; |
61 |
|
|
|
62 |
wakaba |
1.2 |
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 |
wakaba |
1.3 |
</div> |
73 |
wakaba |
1.2 |
|
74 |
|
|
<div id="source-string" class="section"> |
75 |
wakaba |
1.3 |
<h2>Document Source</h2> |
76 |
wakaba |
1.2 |
]; |
77 |
wakaba |
1.3 |
push @nav, ['#source-string' => 'Source']; |
78 |
wakaba |
1.2 |
print_source_string (\$s); |
79 |
|
|
print STDOUT qq[ |
80 |
|
|
</div> |
81 |
wakaba |
1.1 |
|
82 |
wakaba |
1.2 |
<div id="parse-errors" class="section"> |
83 |
|
|
<h2>Parse Errors</h2> |
84 |
|
|
|
85 |
wakaba |
1.4 |
<dl> |
86 |
wakaba |
1.2 |
]; |
87 |
wakaba |
1.3 |
push @nav, ['#parse-errors' => 'Parse Error']; |
88 |
wakaba |
1.1 |
|
89 |
|
|
my $onerror = sub { |
90 |
|
|
my (%opt) = @_; |
91 |
wakaba |
1.2 |
if ($opt{column} > 0) { |
92 |
wakaba |
1.4 |
print STDOUT qq[<dt><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}</dt>\n]; |
93 |
wakaba |
1.2 |
} else { |
94 |
|
|
$opt{line}--; |
95 |
wakaba |
1.4 |
print STDOUT qq[<dt><a href="#line-$opt{line}">Line $opt{line}</a></dt>\n]; |
96 |
wakaba |
1.2 |
} |
97 |
wakaba |
1.4 |
print STDOUT qq[<dd>@{[htescape $opt{type}]}</dd>\n]; |
98 |
wakaba |
1.1 |
}; |
99 |
|
|
|
100 |
|
|
$doc = $dom->create_document; |
101 |
wakaba |
1.2 |
if (defined $inner_html_element and length $inner_html_element) { |
102 |
wakaba |
1.1 |
$el = $doc->create_element_ns |
103 |
wakaba |
1.2 |
('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); |
104 |
wakaba |
1.1 |
Whatpm::HTML->set_inner_html ($el, $s, $onerror); |
105 |
|
|
} else { |
106 |
|
|
Whatpm::HTML->parse_string ($s => $doc, $onerror); |
107 |
|
|
} |
108 |
|
|
|
109 |
wakaba |
1.2 |
print STDOUT qq[ |
110 |
wakaba |
1.4 |
</dl> |
111 |
wakaba |
1.2 |
</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 |
wakaba |
1.3 |
</div> |
124 |
wakaba |
1.2 |
|
125 |
|
|
<div id="source-string" class="section"> |
126 |
wakaba |
1.3 |
<h2>Document Source</h2> |
127 |
wakaba |
1.2 |
]; |
128 |
wakaba |
1.3 |
push @nav, ['#source-string' => 'Source']; |
129 |
wakaba |
1.2 |
print_source_string (\$t); |
130 |
|
|
print STDOUT qq[ |
131 |
|
|
</div> |
132 |
wakaba |
1.1 |
|
133 |
wakaba |
1.2 |
<div id="parse-errors" class="section"> |
134 |
|
|
<h2>Parse Errors</h2> |
135 |
wakaba |
1.1 |
|
136 |
wakaba |
1.4 |
<dl> |
137 |
wakaba |
1.2 |
]; |
138 |
wakaba |
1.3 |
push @nav, ['#parse-errors' => 'Parse Error']; |
139 |
wakaba |
1.1 |
|
140 |
|
|
my $onerror = sub { |
141 |
|
|
my $err = shift; |
142 |
wakaba |
1.2 |
my $line = $err->location->line_number; |
143 |
wakaba |
1.4 |
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 |
wakaba |
1.1 |
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 |
wakaba |
1.2 |
print STDOUT qq[ |
154 |
wakaba |
1.4 |
</dl> |
155 |
wakaba |
1.2 |
</div> |
156 |
|
|
]; |
157 |
|
|
} else { |
158 |
|
|
print STDOUT qq[ |
159 |
|
|
</dl> |
160 |
wakaba |
1.4 |
</div> |
161 |
wakaba |
1.2 |
|
162 |
wakaba |
1.3 |
<div id="result-summary" class="section"> |
163 |
wakaba |
1.2 |
<p><em>Media type <code class="MIME" lang="en">@{[htescape $input_format]}</code> is not supported!</em></p> |
164 |
wakaba |
1.3 |
</div> |
165 |
wakaba |
1.2 |
]; |
166 |
wakaba |
1.3 |
push @nav, ['#result-summary' => 'Result']; |
167 |
wakaba |
1.2 |
} |
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 |
wakaba |
1.3 |
push @nav, ['#document-tree' => 'Tree']; |
176 |
wakaba |
1.2 |
|
177 |
|
|
print_document_tree ($el || $doc); |
178 |
|
|
|
179 |
|
|
print STDOUT qq[ |
180 |
|
|
</div> |
181 |
wakaba |
1.1 |
|
182 |
wakaba |
1.2 |
<div id="document-errors" class="section"> |
183 |
|
|
<h2>Document Errors</h2> |
184 |
|
|
|
185 |
wakaba |
1.4 |
<dl> |
186 |
wakaba |
1.2 |
]; |
187 |
wakaba |
1.3 |
push @nav, ['#document-errors' => 'Document Error']; |
188 |
wakaba |
1.1 |
|
189 |
|
|
require Whatpm::ContentChecker; |
190 |
|
|
my $onerror = sub { |
191 |
|
|
my %opt = @_; |
192 |
wakaba |
1.4 |
print STDOUT qq[<dt><a href="#node-@{[refaddr $opt{node}]}">], |
193 |
wakaba |
1.2 |
htescape get_node_path ($opt{node}), |
194 |
wakaba |
1.4 |
"</a></dt>\n<dd>", htescape $opt{type}, "</dd>\n"; |
195 |
wakaba |
1.1 |
}; |
196 |
wakaba |
1.2 |
|
197 |
wakaba |
1.5 |
my $elements; |
198 |
wakaba |
1.1 |
if ($el) { |
199 |
wakaba |
1.5 |
$elements = Whatpm::ContentChecker->check_element ($el, $onerror); |
200 |
wakaba |
1.1 |
} else { |
201 |
wakaba |
1.5 |
$elements = Whatpm::ContentChecker->check_document ($doc, $onerror); |
202 |
wakaba |
1.1 |
} |
203 |
wakaba |
1.2 |
|
204 |
|
|
print STDOUT qq[ |
205 |
wakaba |
1.4 |
</dl> |
206 |
wakaba |
1.2 |
</div> |
207 |
|
|
]; |
208 |
wakaba |
1.5 |
|
209 |
|
|
if (@{$elements->{table}}) { |
210 |
|
|
require JSON; |
211 |
|
|
|
212 |
|
|
print STDOUT qq[ |
213 |
|
|
<div id="tables" class="section"> |
214 |
|
|
<h2>Tables</h2> |
215 |
|
|
|
216 |
|
|
<!--[if IE]><script type="text/javascript" src="../excanvas.js"></script><![endif]--> |
217 |
|
|
<script src="../table-script.js" type="text/javascript"></script> |
218 |
|
|
<noscript> |
219 |
|
|
<p><em>Structure of tables are visualized here if scripting is enabled.</em></p> |
220 |
|
|
</noscript> |
221 |
|
|
]; |
222 |
|
|
|
223 |
|
|
my $i = 0; |
224 |
|
|
for my $table_el (@{$elements->{table}}) { |
225 |
|
|
$i++; |
226 |
|
|
print STDOUT qq[<div class="section" id="table-$i"><h3>]; |
227 |
|
|
print STDOUT qq[<a href="#node-@{[refaddr $table_el]}">], |
228 |
|
|
htescape get_node_path ($table_el); |
229 |
|
|
print STDOUT qq[</a></h3>\n]; |
230 |
|
|
|
231 |
|
|
my $table = Whatpm::HTMLTable->form_table ($table_el); |
232 |
|
|
|
233 |
|
|
for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption}) { |
234 |
|
|
next unless $_; |
235 |
|
|
delete $_->{element}; |
236 |
|
|
} |
237 |
|
|
|
238 |
|
|
for (@{$table->{row_group}}) { |
239 |
|
|
next unless $_; |
240 |
|
|
next unless $_->{element}; |
241 |
|
|
$_->{type} = $_->{element}->manakai_local_name; |
242 |
|
|
delete $_->{element}; |
243 |
|
|
} |
244 |
|
|
|
245 |
|
|
for (@{$table->{cell}}) { |
246 |
|
|
next unless $_; |
247 |
|
|
for (@{$_}) { |
248 |
|
|
next unless $_; |
249 |
|
|
for (@$_) { |
250 |
|
|
$_->{id} = refaddr $_->{element} if defined $_->{element}; |
251 |
|
|
delete $_->{element}; |
252 |
|
|
} |
253 |
|
|
} |
254 |
|
|
} |
255 |
|
|
|
256 |
|
|
print STDOUT '</div><script type="text/javascript">tableToCanvas ('; |
257 |
|
|
print STDOUT JSON::objToJson ($table); |
258 |
|
|
print STDOUT qq[, document.getElementById ('table-$i'));</script>]; |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
print STDOUT qq[</div>]; |
262 |
|
|
} |
263 |
wakaba |
1.1 |
} |
264 |
|
|
|
265 |
wakaba |
1.2 |
## TODO: Show result |
266 |
wakaba |
1.3 |
|
267 |
wakaba |
1.2 |
print STDOUT qq[ |
268 |
wakaba |
1.3 |
<ul class="navigation" id="nav-items"> |
269 |
|
|
]; |
270 |
|
|
for (@nav) { |
271 |
|
|
print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>]; |
272 |
|
|
} |
273 |
|
|
print STDOUT qq[ |
274 |
|
|
</ul> |
275 |
wakaba |
1.2 |
</body> |
276 |
|
|
</html> |
277 |
|
|
]; |
278 |
wakaba |
1.1 |
|
279 |
|
|
exit; |
280 |
|
|
|
281 |
wakaba |
1.2 |
sub print_source_string ($) { |
282 |
|
|
my $s = $_[0]; |
283 |
|
|
my $i = 1; |
284 |
|
|
print STDOUT qq[<ol lang="">\n]; |
285 |
|
|
while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) { |
286 |
|
|
print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |
287 |
|
|
$i++; |
288 |
|
|
} |
289 |
|
|
if ($$s =~ /\G([^\x0A]+)/gc) { |
290 |
|
|
print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; |
291 |
|
|
} |
292 |
|
|
print STDOUT "</ol>"; |
293 |
|
|
} # print_input_string |
294 |
|
|
|
295 |
|
|
sub print_document_tree ($) { |
296 |
wakaba |
1.1 |
my $node = shift; |
297 |
wakaba |
1.2 |
my $r = '<ol class="xoxo">'; |
298 |
wakaba |
1.1 |
|
299 |
wakaba |
1.2 |
my @node = ($node); |
300 |
wakaba |
1.1 |
while (@node) { |
301 |
|
|
my $child = shift @node; |
302 |
wakaba |
1.2 |
unless (ref $child) { |
303 |
|
|
$r .= $child; |
304 |
|
|
next; |
305 |
|
|
} |
306 |
|
|
|
307 |
|
|
my $node_id = 'node-'.refaddr $child; |
308 |
|
|
my $nt = $child->node_type; |
309 |
|
|
if ($nt == $child->ELEMENT_NODE) { |
310 |
wakaba |
1.4 |
my $child_nsuri = $child->namespace_uri; |
311 |
|
|
$r .= qq[<li id="$node_id" class="tree-element"><code title="@{[defined $child_nsuri ? $child_nsuri : '']}">] . htescape ($child->tag_name) . |
312 |
wakaba |
1.2 |
'</code>'; ## ISSUE: case |
313 |
|
|
|
314 |
|
|
if ($child->has_attributes) { |
315 |
|
|
$r .= '<ul class="attributes">'; |
316 |
wakaba |
1.4 |
for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] } |
317 |
wakaba |
1.2 |
@{$child->attributes}) { |
318 |
wakaba |
1.4 |
$r .= qq[<li id="$attr->[3]" class="tree-attribute"><code title="@{[defined $_->[2] ? $_->[2] : '']}">] . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case? |
319 |
wakaba |
1.2 |
$r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children |
320 |
|
|
} |
321 |
|
|
$r .= '</ul>'; |
322 |
|
|
} |
323 |
|
|
|
324 |
|
|
if ($node->has_child_nodes) { |
325 |
|
|
$r .= '<ol class="children">'; |
326 |
|
|
unshift @node, @{$child->child_nodes}, '</ol>'; |
327 |
|
|
} |
328 |
|
|
} elsif ($nt == $child->TEXT_NODE) { |
329 |
wakaba |
1.4 |
$r .= qq'<li id="$node_id" class="tree-text"><q lang="">' . htescape ($child->data) . '</q></li>'; |
330 |
wakaba |
1.2 |
} elsif ($nt == $child->CDATA_SECTION_NODE) { |
331 |
wakaba |
1.4 |
$r .= qq'<li id="$node_id" class="tree-cdata"><code><[CDATA[</code><q lang="">' . htescape ($child->data) . '</q><code>]]></code></li>'; |
332 |
wakaba |
1.2 |
} elsif ($nt == $child->COMMENT_NODE) { |
333 |
wakaba |
1.4 |
$r .= qq'<li id="$node_id" class="tree-comment"><code><!--</code><q lang="">' . htescape ($child->data) . '</q><code>--></code></li>'; |
334 |
wakaba |
1.2 |
} elsif ($nt == $child->DOCUMENT_NODE) { |
335 |
wakaba |
1.4 |
$r .= qq'<li id="$node_id" class="tree-document">Document</li>'; |
336 |
wakaba |
1.2 |
if ($child->has_child_nodes) { |
337 |
|
|
$r .= '<ol>'; |
338 |
|
|
unshift @node, @{$child->child_nodes}, '</ol>'; |
339 |
wakaba |
1.1 |
} |
340 |
wakaba |
1.2 |
} elsif ($nt == $child->DOCUMENT_TYPE_NODE) { |
341 |
wakaba |
1.5 |
$r .= qq'<li id="$node_id" class="tree-doctype"><code><!DOCTYPE></code><ul class="attributes">'; |
342 |
|
|
$r .= qq[<li class="tree-doctype-name">Name = <q>@{[htescape ($child->name)]}</q></li>]; |
343 |
|
|
$r .= qq[<li class="tree-doctype-publicid">Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>]; |
344 |
|
|
$r .= qq[<li class="tree-doctype-systemid">System identifier = <q>@{[htescape ($child->system_id)]}</q></li>]; |
345 |
wakaba |
1.2 |
$r .= '</ul></li>'; |
346 |
|
|
} elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) { |
347 |
wakaba |
1.4 |
$r .= qq'<li id="$node_id" class="tree-id"><code><?@{[htescape ($child->target)]}</code> <q>@{[htescape ($child->data)]}</q><code>?></code></li>'; |
348 |
wakaba |
1.1 |
} else { |
349 |
wakaba |
1.4 |
$r .= qq'<li id="$node_id" class="tree-unknown">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error |
350 |
wakaba |
1.1 |
} |
351 |
|
|
} |
352 |
wakaba |
1.2 |
|
353 |
|
|
$r .= '</ol>'; |
354 |
|
|
print STDOUT $r; |
355 |
|
|
} # print_document_tree |
356 |
wakaba |
1.1 |
|
357 |
|
|
sub get_node_path ($) { |
358 |
|
|
my $node = shift; |
359 |
|
|
my @r; |
360 |
|
|
while (defined $node) { |
361 |
|
|
my $rs; |
362 |
|
|
if ($node->node_type == 1) { |
363 |
|
|
$rs = $node->manakai_local_name; |
364 |
|
|
$node = $node->parent_node; |
365 |
|
|
} elsif ($node->node_type == 2) { |
366 |
|
|
$rs = '@' . $node->manakai_local_name; |
367 |
|
|
$node = $node->owner_element; |
368 |
|
|
} elsif ($node->node_type == 3) { |
369 |
|
|
$rs = '"' . $node->data . '"'; |
370 |
|
|
$node = $node->parent_node; |
371 |
|
|
} elsif ($node->node_type == 9) { |
372 |
|
|
$rs = ''; |
373 |
|
|
$node = $node->parent_node; |
374 |
|
|
} else { |
375 |
|
|
$rs = '#' . $node->node_type; |
376 |
|
|
$node = $node->parent_node; |
377 |
|
|
} |
378 |
|
|
unshift @r, $rs; |
379 |
|
|
} |
380 |
|
|
return join '/', @r; |
381 |
|
|
} # get_node_path |
382 |
|
|
|
383 |
|
|
=head1 AUTHOR |
384 |
|
|
|
385 |
|
|
Wakaba <w@suika.fam.cx>. |
386 |
|
|
|
387 |
|
|
=head1 LICENSE |
388 |
|
|
|
389 |
|
|
Copyright 2007 Wakaba <w@suika.fam.cx> |
390 |
|
|
|
391 |
|
|
This library is free software; you can redistribute it |
392 |
|
|
and/or modify it under the same terms as Perl itself. |
393 |
|
|
|
394 |
|
|
=cut |
395 |
|
|
|
396 |
wakaba |
1.5 |
## $Date: 2007/06/27 14:36:45 $ |