/[suikacvs]/test/html-webhacc/cc.cgi
Suika

Contents of /test/html-webhacc/cc.cgi

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations) (download)
Wed Jun 27 13:30:15 2007 UTC (17 years, 5 months ago) by wakaba
Branch: MAIN
Changes since 1.2: +26 -9 lines
Style sheet

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/</&lt;/g;
16     $s =~ s/>/&gt;/g;
17     $s =~ s/"/&quot;/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.3 <div id="document-info" section="section">
49 wakaba 1.2 <dl>
50     <dt>Document URI</dt>
51     <dd><code class="URI" lang="">&lt;<a href="@{[htescape $input_uri]}">@{[htescape $input_uri]}</a>&gt;</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     <ul>
86     ];
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     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 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     </ul>
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 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.2 <ul>
137     ];
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     print STDOUT qq[<li><a href="#line-$line">Line $line</a> column ];
144     print STDOUT $err->location->column_number, ": ";
145     print STDOUT htescape $err->text, "</li>\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     </ul>
155     </div>
156     ];
157     } else {
158     print STDOUT qq[
159     </dl>
160    
161 wakaba 1.3 <div id="result-summary" class="section">
162 wakaba 1.2 <p><em>Media type <code class="MIME" lang="en">@{[htescape $input_format]}</code> is not supported!</em></p>
163 wakaba 1.3 </div>
164 wakaba 1.2 ];
165 wakaba 1.3 push @nav, ['#result-summary' => 'Result'];
166 wakaba 1.2 }
167    
168    
169     if (defined $doc or defined $el) {
170     print STDOUT qq[
171     <div id="document-tree" class="section">
172     <h2>Document Tree</h2>
173     ];
174 wakaba 1.3 push @nav, ['#document-tree' => 'Tree'];
175 wakaba 1.2
176     print_document_tree ($el || $doc);
177    
178     print STDOUT qq[
179     </div>
180 wakaba 1.1
181 wakaba 1.2 <div id="document-errors" class="section">
182     <h2>Document Errors</h2>
183    
184     <ul>
185     ];
186 wakaba 1.3 push @nav, ['#document-errors' => 'Document Error'];
187 wakaba 1.1
188     require Whatpm::ContentChecker;
189     my $onerror = sub {
190     my %opt = @_;
191 wakaba 1.2 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 wakaba 1.1 };
195 wakaba 1.2
196 wakaba 1.1 if ($el) {
197     Whatpm::ContentChecker->check_element ($el, $onerror);
198     } else {
199     Whatpm::ContentChecker->check_document ($doc, $onerror);
200     }
201 wakaba 1.2
202     print STDOUT qq[
203     </ul>
204     </div>
205     ];
206 wakaba 1.1 }
207    
208 wakaba 1.2 ## TODO: Show result
209 wakaba 1.3
210 wakaba 1.2 print STDOUT qq[
211 wakaba 1.3 <ul class="navigation" id="nav-items">
212     ];
213     for (@nav) {
214     print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>];
215     }
216     print STDOUT qq[
217     </ul>
218 wakaba 1.2 </body>
219     </html>
220     ];
221 wakaba 1.1
222     exit;
223    
224 wakaba 1.2 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 wakaba 1.1 my $node = shift;
240 wakaba 1.2 my $r = '<ol class="xoxo">';
241 wakaba 1.1
242 wakaba 1.2 my @node = ($node);
243 wakaba 1.1 while (@node) {
244     my $child = shift @node;
245 wakaba 1.2 unless (ref $child) {
246     $r .= $child;
247     next;
248     }
249    
250     my $node_id = 'node-'.refaddr $child;
251     my $nt = $child->node_type;
252     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>&lt;[CDATA[</code><q>' . htescape ($child->data) . '</q><code>]]&gt;</code></li>';
274     } elsif ($nt == $child->COMMENT_NODE) {
275     $r .= qq'<li id="$node_id"><code>&lt;!--</code><q>' . htescape ($child->data) . '</q><code>--&gt;</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 wakaba 1.1 }
282 wakaba 1.2 } elsif ($nt == $child->DOCUMENT_TYPE_NODE) {
283     $r .= qq'<li id="$node_id"><code>&lt;!DOCTYPE&gt;</code><ul>';
284     $r .= '<li>Name = <q>@{[htescape ($child->name)]}</q></li>';
285     $r .= '<li>Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>';
286     $r .= '<li>System identifier = <q>@{[htescape ($child->system_id)]}</q></li>';
287     $r .= '</ul></li>';
288     } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) {
289     $r .= qq'<li id="$node_id"><code>&lt;?@{[htescape ($child->target)]}?&gt;</code>';
290     $r .= '<ul><li>@{[htescape ($child->data)]}</li></ul></li>';
291 wakaba 1.1 } else {
292 wakaba 1.2 $r .= qq'<li id="$node_id">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error
293 wakaba 1.1 }
294     }
295 wakaba 1.2
296     $r .= '</ol>';
297     print STDOUT $r;
298     } # print_document_tree
299 wakaba 1.1
300     sub get_node_path ($) {
301     my $node = shift;
302     my @r;
303     while (defined $node) {
304     my $rs;
305     if ($node->node_type == 1) {
306     $rs = $node->manakai_local_name;
307     $node = $node->parent_node;
308     } elsif ($node->node_type == 2) {
309     $rs = '@' . $node->manakai_local_name;
310     $node = $node->owner_element;
311     } elsif ($node->node_type == 3) {
312     $rs = '"' . $node->data . '"';
313     $node = $node->parent_node;
314     } elsif ($node->node_type == 9) {
315     $rs = '';
316     $node = $node->parent_node;
317     } else {
318     $rs = '#' . $node->node_type;
319     $node = $node->parent_node;
320     }
321     unshift @r, $rs;
322     }
323     return join '/', @r;
324     } # get_node_path
325    
326     =head1 AUTHOR
327    
328     Wakaba <w@suika.fam.cx>.
329    
330     =head1 LICENSE
331    
332     Copyright 2007 Wakaba <w@suika.fam.cx>
333    
334     This library is free software; you can redistribute it
335     and/or modify it under the same terms as Perl itself.
336    
337     =cut
338    
339 wakaba 1.3 ## $Date: 2007/06/27 12:35:24 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24