Parent Directory
|
Revision Log
++ ChangeLog 5 Nov 2007 09:33:38 -0000 2007-11-05 Wakaba <wakaba@suika.fam.cx> * cc.cgi (get_error_level_label): New. * cc-style.css: New rules for error level descriptions. * error-description-source.xml: Description for error levels is added.
| 1 | wakaba | 1.1 | #!/usr/bin/perl |
| 2 | use strict; | ||
| 3 | wakaba | 1.23 | use utf8; |
| 4 | wakaba | 1.1 | |
| 5 | use lib qw[/home/httpd/html/www/markup/html/whatpm | ||
| 6 | wakaba | 1.16 | /home/wakaba/work/manakai2/lib]; |
| 7 | wakaba | 1.1 | use CGI::Carp qw[fatalsToBrowser]; |
| 8 | wakaba | 1.2 | use Scalar::Util qw[refaddr]; |
| 9 | wakaba | 1.16 | use Time::HiRes qw/time/; |
| 10 | wakaba | 1.1 | |
| 11 | wakaba | 1.2 | sub htescape ($) { |
| 12 | my $s = $_[0]; | ||
| 13 | $s =~ s/&/&/g; | ||
| 14 | $s =~ s/</</g; | ||
| 15 | $s =~ s/>/>/g; | ||
| 16 | $s =~ s/"/"/g; | ||
| 17 | wakaba | 1.12 | $s =~ s{([\x00-\x09\x0B-\x1F\x7F-\xA0\x{FEFF}\x{FFFC}-\x{FFFF}])}{ |
| 18 | sprintf '<var>U+%04X</var>', ord $1; | ||
| 19 | }ge; | ||
| 20 | wakaba | 1.2 | return $s; |
| 21 | } # htescape | ||
| 22 | |||
| 23 | wakaba | 1.16 | use Message::CGI::HTTP; |
| 24 | my $http = Message::CGI::HTTP->new; | ||
| 25 | wakaba | 1.1 | |
| 26 | wakaba | 1.16 | if ($http->get_meta_variable ('PATH_INFO') ne '/') { |
| 27 | wakaba | 1.8 | print STDOUT "Status: 404 Not Found\nContent-Type: text/plain; charset=us-ascii\n\n400"; |
| 28 | exit; | ||
| 29 | } | ||
| 30 | |||
| 31 | wakaba | 1.12 | binmode STDOUT, ':utf8'; |
| 32 | wakaba | 1.14 | $| = 1; |
| 33 | wakaba | 1.12 | |
| 34 | wakaba | 1.9 | require Message::DOM::DOMImplementation; |
| 35 | my $dom = Message::DOM::DOMImplementation->new; | ||
| 36 | |||
| 37 | wakaba | 1.7 | load_text_catalog ('en'); ## TODO: conneg |
| 38 | |||
| 39 | wakaba | 1.3 | my @nav; |
| 40 | wakaba | 1.2 | print STDOUT qq[Content-Type: text/html; charset=utf-8 |
| 41 | |||
| 42 | <!DOCTYPE html> | ||
| 43 | <html lang="en"> | ||
| 44 | <head> | ||
| 45 | <title>Web Document Conformance Checker (BETA)</title> | ||
| 46 | wakaba | 1.3 | <link rel="stylesheet" href="../cc-style.css" type="text/css"> |
| 47 | wakaba | 1.2 | </head> |
| 48 | <body> | ||
| 49 | wakaba | 1.13 | <h1><a href="../cc-interface">Web Document Conformance Checker</a> |
| 50 | (<em>beta</em>)</h1> | ||
| 51 | wakaba | 1.14 | ]; |
| 52 | wakaba | 1.2 | |
| 53 | wakaba | 1.14 | $| = 0; |
| 54 | my $input = get_input_document ($http, $dom); | ||
| 55 | wakaba | 1.16 | my $inner_html_element = $http->get_parameter ('e'); |
| 56 | my $char_length = 0; | ||
| 57 | my %time; | ||
| 58 | wakaba | 1.14 | |
| 59 | print qq[ | ||
| 60 | wakaba | 1.4 | <div id="document-info" class="section"> |
| 61 | wakaba | 1.2 | <dl> |
| 62 | wakaba | 1.9 | <dt>Request URI</dt> |
| 63 | <dd><code class="URI" lang=""><<a href="@{[htescape $input->{request_uri}]}">@{[htescape $input->{request_uri}]}</a>></code></dd> | ||
| 64 | wakaba | 1.2 | <dt>Document URI</dt> |
| 65 | wakaba | 1.9 | <dd><code class="URI" lang=""><<a href="@{[htescape $input->{uri}]}">@{[htescape $input->{uri}]}</a>></code></dd> |
| 66 | wakaba | 1.2 | ]; # no </dl> yet |
| 67 | wakaba | 1.3 | push @nav, ['#document-info' => 'Information']; |
| 68 | wakaba | 1.1 | |
| 69 | wakaba | 1.9 | if (defined $input->{s}) { |
| 70 | wakaba | 1.16 | $char_length = length $input->{s}; |
| 71 | wakaba | 1.9 | |
| 72 | print STDOUT qq[ | ||
| 73 | <dt>Base URI</dt> | ||
| 74 | <dd><code class="URI" lang=""><<a href="@{[htescape $input->{base_uri}]}">@{[htescape $input->{base_uri}]}</a>></code></dd> | ||
| 75 | <dt>Internet Media Type</dt> | ||
| 76 | <dd><code class="MIME" lang="en">@{[htescape $input->{media_type}]}</code> | ||
| 77 | @{[$input->{media_type_overridden} ? '<em>(overridden)</em>' : '']}</dd> | ||
| 78 | <dt>Character Encoding</dt> | ||
| 79 | <dd>@{[defined $input->{charset} ? '<code class="charset" lang="en">'.htescape ($input->{charset}).'</code>' : '(none)']} | ||
| 80 | @{[$input->{charset_overridden} ? '<em>(overridden)</em>' : '']}</dd> | ||
| 81 | wakaba | 1.16 | <dt>Length</dt> |
| 82 | <dd>$char_length byte@{[$char_length == 1 ? '' : 's']}</dd> | ||
| 83 | wakaba | 1.9 | </dl> |
| 84 | </div> | ||
| 85 | ]; | ||
| 86 | |||
| 87 | wakaba | 1.20 | my $result = {conforming_min => 1, conforming_max => 1}; |
| 88 | wakaba | 1.19 | print_http_header_section ($input, $result); |
| 89 | wakaba | 1.9 | |
| 90 | wakaba | 1.1 | my $doc; |
| 91 | my $el; | ||
| 92 | wakaba | 1.22 | my $manifest; |
| 93 | wakaba | 1.1 | |
| 94 | wakaba | 1.9 | if ($input->{media_type} eq 'text/html') { |
| 95 | wakaba | 1.19 | ($doc, $el) = print_syntax_error_html_section ($input, $result); |
| 96 | wakaba | 1.9 | print_source_string_section (\($input->{s}), $input->{charset}); |
| 97 | } elsif ({ | ||
| 98 | 'text/xml' => 1, | ||
| 99 | wakaba | 1.16 | 'application/atom+xml' => 1, |
| 100 | 'application/rss+xml' => 1, | ||
| 101 | 'application/svg+xml' => 1, | ||
| 102 | wakaba | 1.9 | 'application/xhtml+xml' => 1, |
| 103 | 'application/xml' => 1, | ||
| 104 | }->{$input->{media_type}}) { | ||
| 105 | wakaba | 1.19 | ($doc, $el) = print_syntax_error_xml_section ($input, $result); |
| 106 | wakaba | 1.9 | print_source_string_section (\($input->{s}), $doc->input_encoding); |
| 107 | wakaba | 1.22 | } elsif ($input->{media_type} eq 'text/cache-manifest') { |
| 108 | ## TODO: MUST be text/cache-manifest | ||
| 109 | $manifest = print_syntax_error_manifest_section ($input, $result); | ||
| 110 | print_source_string_section (\($input->{s}), 'utf-8'); | ||
| 111 | wakaba | 1.2 | } else { |
| 112 | wakaba | 1.9 | ## TODO: Change HTTP status code?? |
| 113 | wakaba | 1.18 | print_result_unknown_type_section ($input); |
| 114 | wakaba | 1.2 | } |
| 115 | |||
| 116 | if (defined $doc or defined $el) { | ||
| 117 | wakaba | 1.22 | print_structure_dump_dom_section ($doc, $el); |
| 118 | my $elements = print_structure_error_dom_section ($doc, $el, $result); | ||
| 119 | wakaba | 1.18 | print_table_section ($elements->{table}) if @{$elements->{table}}; |
| 120 | print_id_section ($elements->{id}) if keys %{$elements->{id}}; | ||
| 121 | print_term_section ($elements->{term}) if keys %{$elements->{term}}; | ||
| 122 | print_class_section ($elements->{class}) if keys %{$elements->{class}}; | ||
| 123 | wakaba | 1.22 | } elsif (defined $manifest) { |
| 124 | print_structure_dump_manifest_section ($manifest); | ||
| 125 | print_structure_error_manifest_section ($manifest, $result); | ||
| 126 | wakaba | 1.1 | } |
| 127 | |||
| 128 | wakaba | 1.19 | print_result_section ($result); |
| 129 | wakaba | 1.9 | } else { |
| 130 | wakaba | 1.18 | print STDOUT qq[</dl></div>]; |
| 131 | print_result_input_error_section ($input); | ||
| 132 | wakaba | 1.9 | } |
| 133 | wakaba | 1.3 | |
| 134 | wakaba | 1.2 | print STDOUT qq[ |
| 135 | wakaba | 1.3 | <ul class="navigation" id="nav-items"> |
| 136 | ]; | ||
| 137 | for (@nav) { | ||
| 138 | print STDOUT qq[<li><a href="$_->[0]">$_->[1]</a></li>]; | ||
| 139 | } | ||
| 140 | print STDOUT qq[ | ||
| 141 | </ul> | ||
| 142 | wakaba | 1.2 | </body> |
| 143 | </html> | ||
| 144 | ]; | ||
| 145 | wakaba | 1.1 | |
| 146 | wakaba | 1.22 | for (qw/decode parse parse_xml parse_manifest check check_manifest/) { |
| 147 | wakaba | 1.16 | next unless defined $time{$_}; |
| 148 | open my $file, '>>', ".cc-$_.txt" or die ".cc-$_.txt: $!"; | ||
| 149 | print $file $char_length, "\t", $time{$_}, "\n"; | ||
| 150 | } | ||
| 151 | |||
| 152 | wakaba | 1.1 | exit; |
| 153 | |||
| 154 | wakaba | 1.19 | sub add_error ($$$) { |
| 155 | my ($layer, $err, $result) = @_; | ||
| 156 | if (defined $err->{level}) { | ||
| 157 | if ($err->{level} eq 's') { | ||
| 158 | $result->{$layer}->{should}++; | ||
| 159 | $result->{$layer}->{score_min} -= 2; | ||
| 160 | $result->{conforming_min} = 0; | ||
| 161 | } elsif ($err->{level} eq 'w' or $err->{level} eq 'g') { | ||
| 162 | $result->{$layer}->{warning}++; | ||
| 163 | } elsif ($err->{level} eq 'unsupported') { | ||
| 164 | $result->{$layer}->{unsupported}++; | ||
| 165 | $result->{unsupported} = 1; | ||
| 166 | } else { | ||
| 167 | $result->{$layer}->{must}++; | ||
| 168 | $result->{$layer}->{score_max} -= 2; | ||
| 169 | $result->{$layer}->{score_min} -= 2; | ||
| 170 | $result->{conforming_min} = 0; | ||
| 171 | $result->{conforming_max} = 0; | ||
| 172 | } | ||
| 173 | } else { | ||
| 174 | $result->{$layer}->{must}++; | ||
| 175 | $result->{$layer}->{score_max} -= 2; | ||
| 176 | $result->{$layer}->{score_min} -= 2; | ||
| 177 | $result->{conforming_min} = 0; | ||
| 178 | $result->{conforming_max} = 0; | ||
| 179 | } | ||
| 180 | } # add_error | ||
| 181 | |||
| 182 | sub print_http_header_section ($$) { | ||
| 183 | my ($input, $result) = @_; | ||
| 184 | wakaba | 1.9 | return unless defined $input->{header_status_code} or |
| 185 | defined $input->{header_status_text} or | ||
| 186 | @{$input->{header_field}}; | ||
| 187 | |||
| 188 | push @nav, ['#source-header' => 'HTTP Header']; | ||
| 189 | print STDOUT qq[<div id="source-header" class="section"> | ||
| 190 | <h2>HTTP Header</h2> | ||
| 191 | |||
| 192 | <p><strong>Note</strong>: Due to the limitation of the | ||
| 193 | network library in use, the content of this section might | ||
| 194 | not be the real header.</p> | ||
| 195 | |||
| 196 | <table><tbody> | ||
| 197 | ]; | ||
| 198 | |||
| 199 | if (defined $input->{header_status_code}) { | ||
| 200 | print STDOUT qq[<tr><th scope="row">Status code</th>]; | ||
| 201 | print STDOUT qq[<td><code>@{[htescape ($input->{header_status_code})]}</code></td></tr>]; | ||
| 202 | } | ||
| 203 | if (defined $input->{header_status_text}) { | ||
| 204 | print STDOUT qq[<tr><th scope="row">Status text</th>]; | ||
| 205 | print STDOUT qq[<td><code>@{[htescape ($input->{header_status_text})]}</code></td></tr>]; | ||
| 206 | } | ||
| 207 | |||
| 208 | for (@{$input->{header_field}}) { | ||
| 209 | print STDOUT qq[<tr><th scope="row"><code>@{[htescape ($_->[0])]}</code></th>]; | ||
| 210 | print STDOUT qq[<td><code>@{[htescape ($_->[1])]}</code></td></tr>]; | ||
| 211 | } | ||
| 212 | |||
| 213 | print STDOUT qq[</tbody></table></div>]; | ||
| 214 | } # print_http_header_section | ||
| 215 | |||
| 216 | wakaba | 1.19 | sub print_syntax_error_html_section ($$) { |
| 217 | my ($input, $result) = @_; | ||
| 218 | wakaba | 1.18 | |
| 219 | require Encode; | ||
| 220 | require Whatpm::HTML; | ||
| 221 | |||
| 222 | $input->{charset} ||= 'ISO-8859-1'; ## TODO: for now. | ||
| 223 | |||
| 224 | my $time1 = time; | ||
| 225 | my $t = Encode::decode ($input->{charset}, $input->{s}); | ||
| 226 | $time{decode} = time - $time1; | ||
| 227 | |||
| 228 | print STDOUT qq[ | ||
| 229 | <div id="parse-errors" class="section"> | ||
| 230 | <h2>Parse Errors</h2> | ||
| 231 | |||
| 232 | <dl>]; | ||
| 233 | push @nav, ['#parse-errors' => 'Parse Error']; | ||
| 234 | |||
| 235 | my $onerror = sub { | ||
| 236 | my (%opt) = @_; | ||
| 237 | my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}); | ||
| 238 | if ($opt{column} > 0) { | ||
| 239 | print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a> column $opt{column}</dt>\n]; | ||
| 240 | } else { | ||
| 241 | $opt{line} = $opt{line} - 1 || 1; | ||
| 242 | print STDOUT qq[<dt class="$cls"><a href="#line-$opt{line}">Line $opt{line}</a></dt>\n]; | ||
| 243 | } | ||
| 244 | $type =~ tr/ /-/; | ||
| 245 | $type =~ s/\|/%7C/g; | ||
| 246 | $msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]]; | ||
| 247 | wakaba | 1.23 | print STDOUT qq[<dd class="$cls">], get_error_level_label (\%opt); |
| 248 | print STDOUT qq[$msg</dd>\n]; | ||
| 249 | wakaba | 1.19 | |
| 250 | add_error ('syntax', \%opt => $result); | ||
| 251 | wakaba | 1.18 | }; |
| 252 | |||
| 253 | my $doc = $dom->create_document; | ||
| 254 | my $el; | ||
| 255 | $time1 = time; | ||
| 256 | if (defined $inner_html_element and length $inner_html_element) { | ||
| 257 | $el = $doc->create_element_ns | ||
| 258 | ('http://www.w3.org/1999/xhtml', [undef, $inner_html_element]); | ||
| 259 | Whatpm::HTML->set_inner_html ($el, $t, $onerror); | ||
| 260 | } else { | ||
| 261 | Whatpm::HTML->parse_string ($t => $doc, $onerror); | ||
| 262 | } | ||
| 263 | $time{parse} = time - $time1; | ||
| 264 | |||
| 265 | print STDOUT qq[</dl></div>]; | ||
| 266 | |||
| 267 | return ($doc, $el); | ||
| 268 | } # print_syntax_error_html_section | ||
| 269 | |||
| 270 | wakaba | 1.19 | sub print_syntax_error_xml_section ($$) { |
| 271 | my ($input, $result) = @_; | ||
| 272 | wakaba | 1.18 | |
| 273 | require Message::DOM::XMLParserTemp; | ||
| 274 | |||
| 275 | print STDOUT qq[ | ||
| 276 | <div id="parse-errors" class="section"> | ||
| 277 | <h2>Parse Errors</h2> | ||
| 278 | |||
| 279 | <dl>]; | ||
| 280 | push @nav, ['#parse-errors' => 'Parse Error']; | ||
| 281 | |||
| 282 | my $onerror = sub { | ||
| 283 | my $err = shift; | ||
| 284 | my $line = $err->location->line_number; | ||
| 285 | print STDOUT qq[<dt><a href="#line-$line">Line $line</a> column ]; | ||
| 286 | print STDOUT $err->location->column_number, "</dt><dd>"; | ||
| 287 | print STDOUT htescape $err->text, "</dd>\n"; | ||
| 288 | wakaba | 1.19 | |
| 289 | add_error ('syntax', {type => $err->text, | ||
| 290 | level => [ | ||
| 291 | $err->SEVERITY_FATAL_ERROR => 'm', | ||
| 292 | $err->SEVERITY_ERROR => 'm', | ||
| 293 | $err->SEVERITY_WARNING => 's', | ||
| 294 | ]->[$err->severity]} => $result); | ||
| 295 | |||
| 296 | wakaba | 1.18 | return 1; |
| 297 | }; | ||
| 298 | |||
| 299 | my $time1 = time; | ||
| 300 | open my $fh, '<', \($input->{s}); | ||
| 301 | my $doc = Message::DOM::XMLParserTemp->parse_byte_stream | ||
| 302 | ($fh => $dom, $onerror, charset => $input->{charset}); | ||
| 303 | $time{parse_xml} = time - $time1; | ||
| 304 | |||
| 305 | print STDOUT qq[</dl></div>]; | ||
| 306 | |||
| 307 | return ($doc, undef); | ||
| 308 | } # print_syntax_error_xml_section | ||
| 309 | |||
| 310 | wakaba | 1.22 | sub print_syntax_error_manifest_section ($$) { |
| 311 | my ($input, $result) = @_; | ||
| 312 | |||
| 313 | require Whatpm::CacheManifest; | ||
| 314 | |||
| 315 | print STDOUT qq[ | ||
| 316 | <div id="parse-errors" class="section"> | ||
| 317 | <h2>Parse Errors</h2> | ||
| 318 | |||
| 319 | <dl>]; | ||
| 320 | push @nav, ['#parse-errors' => 'Parse Error']; | ||
| 321 | |||
| 322 | my $onerror = sub { | ||
| 323 | my (%opt) = @_; | ||
| 324 | my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}); | ||
| 325 | print STDOUT qq[<dt class="$cls">], get_error_label (\%opt), qq[</dt>]; | ||
| 326 | $type =~ tr/ /-/; | ||
| 327 | $type =~ s/\|/%7C/g; | ||
| 328 | $msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]]; | ||
| 329 | wakaba | 1.23 | print STDOUT qq[<dd class="$cls">], get_error_level_label (\%opt); |
| 330 | print STDOUT qq[$msg</dd>\n]; | ||
| 331 | wakaba | 1.22 | |
| 332 | add_error ('syntax', \%opt => $result); | ||
| 333 | }; | ||
| 334 | |||
| 335 | my $time1 = time; | ||
| 336 | my $manifest = Whatpm::CacheManifest->parse_byte_string | ||
| 337 | ($input->{s}, $input->{uri}, $input->{base_uri}, $onerror); | ||
| 338 | $time{parse_manifest} = time - $time1; | ||
| 339 | |||
| 340 | print STDOUT qq[</dl></div>]; | ||
| 341 | |||
| 342 | return $manifest; | ||
| 343 | } # print_syntax_error_manifest_section | ||
| 344 | |||
| 345 | wakaba | 1.9 | sub print_source_string_section ($$) { |
| 346 | require Encode; | ||
| 347 | my $enc = Encode::find_encoding ($_[1]); ## TODO: charset name -> Perl name | ||
| 348 | return unless $enc; | ||
| 349 | |||
| 350 | my $s = \($enc->decode (${$_[0]})); | ||
| 351 | my $i = 1; | ||
| 352 | push @nav, ['#source-string' => 'Source']; | ||
| 353 | print STDOUT qq[<div id="source-string" class="section"> | ||
| 354 | <h2>Document Source</h2> | ||
| 355 | <ol lang="">\n]; | ||
| 356 | wakaba | 1.7 | if (length $$s) { |
| 357 | while ($$s =~ /\G([^\x0A]*?)\x0D?\x0A/gc) { | ||
| 358 | print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; | ||
| 359 | $i++; | ||
| 360 | } | ||
| 361 | if ($$s =~ /\G([^\x0A]+)/gc) { | ||
| 362 | print STDOUT qq[<li id="line-$i">], htescape $1, "</li>\n"; | ||
| 363 | } | ||
| 364 | } else { | ||
| 365 | print STDOUT q[<li id="line-1"></li>]; | ||
| 366 | wakaba | 1.2 | } |
| 367 | wakaba | 1.9 | print STDOUT "</ol></div>"; |
| 368 | } # print_input_string_section | ||
| 369 | wakaba | 1.2 | |
| 370 | sub print_document_tree ($) { | ||
| 371 | wakaba | 1.1 | my $node = shift; |
| 372 | wakaba | 1.2 | my $r = '<ol class="xoxo">'; |
| 373 | wakaba | 1.1 | |
| 374 | wakaba | 1.2 | my @node = ($node); |
| 375 | wakaba | 1.1 | while (@node) { |
| 376 | my $child = shift @node; | ||
| 377 | wakaba | 1.2 | unless (ref $child) { |
| 378 | $r .= $child; | ||
| 379 | next; | ||
| 380 | } | ||
| 381 | |||
| 382 | my $node_id = 'node-'.refaddr $child; | ||
| 383 | my $nt = $child->node_type; | ||
| 384 | if ($nt == $child->ELEMENT_NODE) { | ||
| 385 | wakaba | 1.4 | my $child_nsuri = $child->namespace_uri; |
| 386 | $r .= qq[<li id="$node_id" class="tree-element"><code title="@{[defined $child_nsuri ? $child_nsuri : '']}">] . htescape ($child->tag_name) . | ||
| 387 | wakaba | 1.2 | '</code>'; ## ISSUE: case |
| 388 | |||
| 389 | if ($child->has_attributes) { | ||
| 390 | $r .= '<ul class="attributes">'; | ||
| 391 | wakaba | 1.4 | for my $attr (sort {$a->[0] cmp $b->[0]} map { [$_->name, $_->value, $_->namespace_uri, 'node-'.refaddr $_] } |
| 392 | wakaba | 1.2 | @{$child->attributes}) { |
| 393 | wakaba | 1.4 | $r .= qq[<li id="$attr->[3]" class="tree-attribute"><code title="@{[defined $_->[2] ? $_->[2] : '']}">] . htescape ($attr->[0]) . '</code> = '; ## ISSUE: case? |
| 394 | wakaba | 1.2 | $r .= '<q>' . htescape ($attr->[1]) . '</q></li>'; ## TODO: children |
| 395 | } | ||
| 396 | $r .= '</ul>'; | ||
| 397 | } | ||
| 398 | |||
| 399 | wakaba | 1.7 | if ($child->has_child_nodes) { |
| 400 | wakaba | 1.2 | $r .= '<ol class="children">'; |
| 401 | wakaba | 1.6 | unshift @node, @{$child->child_nodes}, '</ol></li>'; |
| 402 | } else { | ||
| 403 | $r .= '</li>'; | ||
| 404 | wakaba | 1.2 | } |
| 405 | } elsif ($nt == $child->TEXT_NODE) { | ||
| 406 | wakaba | 1.4 | $r .= qq'<li id="$node_id" class="tree-text"><q lang="">' . htescape ($child->data) . '</q></li>'; |
| 407 | wakaba | 1.2 | } elsif ($nt == $child->CDATA_SECTION_NODE) { |
| 408 | wakaba | 1.4 | $r .= qq'<li id="$node_id" class="tree-cdata"><code><[CDATA[</code><q lang="">' . htescape ($child->data) . '</q><code>]]></code></li>'; |
| 409 | wakaba | 1.2 | } elsif ($nt == $child->COMMENT_NODE) { |
| 410 | wakaba | 1.4 | $r .= qq'<li id="$node_id" class="tree-comment"><code><!--</code><q lang="">' . htescape ($child->data) . '</q><code>--></code></li>'; |
| 411 | wakaba | 1.2 | } elsif ($nt == $child->DOCUMENT_NODE) { |
| 412 | wakaba | 1.6 | $r .= qq'<li id="$node_id" class="tree-document">Document'; |
| 413 | wakaba | 1.7 | $r .= qq[<ul class="attributes">]; |
| 414 | $r .= qq[<li>@{[scalar get_text ('manakaiIsHTML:'.($child->manakai_is_html?1:0))]}</li>]; | ||
| 415 | $r .= qq[<li>@{[scalar get_text ('manakaiCompatMode:'.$child->manakai_compat_mode)]}</li>]; | ||
| 416 | wakaba | 1.9 | unless ($child->manakai_is_html) { |
| 417 | $r .= qq[<li>XML version = <code>@{[htescape ($child->xml_version)]}</code></li>]; | ||
| 418 | if (defined $child->xml_encoding) { | ||
| 419 | $r .= qq[<li>XML encoding = <code>@{[htescape ($child->xml_encoding)]}</code></li>]; | ||
| 420 | } else { | ||
| 421 | $r .= qq[<li>XML encoding = (null)</li>]; | ||
| 422 | } | ||
| 423 | $r .= qq[<li>XML standalone = @{[$child->xml_standalone ? 'true' : 'false']}</li>]; | ||
| 424 | } | ||
| 425 | wakaba | 1.7 | $r .= qq[</ul>]; |
| 426 | wakaba | 1.2 | if ($child->has_child_nodes) { |
| 427 | wakaba | 1.7 | $r .= '<ol class="children">'; |
| 428 | wakaba | 1.6 | unshift @node, @{$child->child_nodes}, '</ol></li>'; |
| 429 | wakaba | 1.1 | } |
| 430 | wakaba | 1.2 | } elsif ($nt == $child->DOCUMENT_TYPE_NODE) { |
| 431 | wakaba | 1.5 | $r .= qq'<li id="$node_id" class="tree-doctype"><code><!DOCTYPE></code><ul class="attributes">'; |
| 432 | $r .= qq[<li class="tree-doctype-name">Name = <q>@{[htescape ($child->name)]}</q></li>]; | ||
| 433 | $r .= qq[<li class="tree-doctype-publicid">Public identifier = <q>@{[htescape ($child->public_id)]}</q></li>]; | ||
| 434 | $r .= qq[<li class="tree-doctype-systemid">System identifier = <q>@{[htescape ($child->system_id)]}</q></li>]; | ||
| 435 | wakaba | 1.2 | $r .= '</ul></li>'; |
| 436 | } elsif ($nt == $child->PROCESSING_INSTRUCTION_NODE) { | ||
| 437 | 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>'; |
| 438 | wakaba | 1.1 | } else { |
| 439 | wakaba | 1.4 | $r .= qq'<li id="$node_id" class="tree-unknown">@{[$child->node_type]} @{[htescape ($child->node_name)]}</li>'; # error |
| 440 | wakaba | 1.1 | } |
| 441 | } | ||
| 442 | wakaba | 1.2 | |
| 443 | $r .= '</ol>'; | ||
| 444 | print STDOUT $r; | ||
| 445 | } # print_document_tree | ||
| 446 | wakaba | 1.1 | |
| 447 | wakaba | 1.22 | sub print_structure_dump_dom_section ($$) { |
| 448 | wakaba | 1.18 | my ($doc, $el) = @_; |
| 449 | |||
| 450 | print STDOUT qq[ | ||
| 451 | <div id="document-tree" class="section"> | ||
| 452 | <h2>Document Tree</h2> | ||
| 453 | ]; | ||
| 454 | push @nav, ['#document-tree' => 'Tree']; | ||
| 455 | |||
| 456 | print_document_tree ($el || $doc); | ||
| 457 | |||
| 458 | print STDOUT qq[</div>]; | ||
| 459 | wakaba | 1.22 | } # print_structure_dump_dom_section |
| 460 | |||
| 461 | sub print_structure_dump_manifest_section ($) { | ||
| 462 | my $manifest = shift; | ||
| 463 | |||
| 464 | print STDOUT qq[ | ||
| 465 | <div id="dump-manifest" class="section"> | ||
| 466 | <h2>Cache Manifest</h2> | ||
| 467 | ]; | ||
| 468 | push @nav, ['#dump-manifest' => 'Caceh Manifest']; | ||
| 469 | |||
| 470 | print STDOUT qq[<dl><dt>Explicit entries</dt>]; | ||
| 471 | for my $uri (@{$manifest->[0]}) { | ||
| 472 | my $euri = htescape ($uri); | ||
| 473 | print STDOUT qq[<dd><code class=uri><<a href="$euri">$euri</a>></code></dd>]; | ||
| 474 | } | ||
| 475 | |||
| 476 | print STDOUT qq[<dt>Fallback entries</dt><dd> | ||
| 477 | <table><thead><tr><th scope=row>Oppotunistic Caching Namespace</th> | ||
| 478 | <th scope=row>Fallback Entry</tr><tbody>]; | ||
| 479 | for my $uri (sort {$a cmp $b} keys %{$manifest->[1]}) { | ||
| 480 | my $euri = htescape ($uri); | ||
| 481 | my $euri2 = htescape ($manifest->[1]->{$uri}); | ||
| 482 | print STDOUT qq[<tr><td><code class=uri><<a href="$euri">$euri</a>></code></td> | ||
| 483 | <td><code class=uri><<a href="$euri2">$euri2</a>></code></td>]; | ||
| 484 | } | ||
| 485 | |||
| 486 | print STDOUT qq[</table><dt>Online whitelist</dt>]; | ||
| 487 | for my $uri (@{$manifest->[2]}) { | ||
| 488 | my $euri = htescape ($uri); | ||
| 489 | print STDOUT qq[<dd><code class=uri><<a href="$euri">$euri</a>></code></dd>]; | ||
| 490 | } | ||
| 491 | wakaba | 1.18 | |
| 492 | wakaba | 1.22 | print STDOUT qq[</dl></div>]; |
| 493 | } # print_structure_dump_manifest_section | ||
| 494 | |||
| 495 | sub print_structure_error_dom_section ($$$) { | ||
| 496 | wakaba | 1.19 | my ($doc, $el, $result) = @_; |
| 497 | wakaba | 1.18 | |
| 498 | print STDOUT qq[<div id="document-errors" class="section"> | ||
| 499 | <h2>Document Errors</h2> | ||
| 500 | |||
| 501 | <dl>]; | ||
| 502 | push @nav, ['#document-errors' => 'Document Error']; | ||
| 503 | |||
| 504 | require Whatpm::ContentChecker; | ||
| 505 | my $onerror = sub { | ||
| 506 | my %opt = @_; | ||
| 507 | my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}, $opt{node}); | ||
| 508 | $type =~ tr/ /-/; | ||
| 509 | $type =~ s/\|/%7C/g; | ||
| 510 | $msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]]; | ||
| 511 | wakaba | 1.22 | print STDOUT qq[<dt class="$cls">] . get_error_label (\%opt) . |
| 512 | wakaba | 1.23 | qq[</dt>\n<dd class="$cls">], get_error_level_label (\%opt); |
| 513 | print STDOUT $msg, "</dd>\n"; | ||
| 514 | wakaba | 1.19 | add_error ('structure', \%opt => $result); |
| 515 | wakaba | 1.18 | }; |
| 516 | |||
| 517 | my $elements; | ||
| 518 | my $time1 = time; | ||
| 519 | if ($el) { | ||
| 520 | $elements = Whatpm::ContentChecker->check_element ($el, $onerror); | ||
| 521 | } else { | ||
| 522 | $elements = Whatpm::ContentChecker->check_document ($doc, $onerror); | ||
| 523 | } | ||
| 524 | $time{check} = time - $time1; | ||
| 525 | |||
| 526 | print STDOUT qq[</dl></div>]; | ||
| 527 | |||
| 528 | return $elements; | ||
| 529 | wakaba | 1.22 | } # print_structure_error_dom_section |
| 530 | |||
| 531 | sub print_structure_error_manifest_section ($$$) { | ||
| 532 | my ($manifest, $result) = @_; | ||
| 533 | |||
| 534 | print STDOUT qq[<div id="document-errors" class="section"> | ||
| 535 | <h2>Document Errors</h2> | ||
| 536 | |||
| 537 | <dl>]; | ||
| 538 | push @nav, ['#document-errors' => 'Document Error']; | ||
| 539 | |||
| 540 | require Whatpm::CacheManifest; | ||
| 541 | Whatpm::CacheManifest->check_manifest ($manifest, sub { | ||
| 542 | my %opt = @_; | ||
| 543 | my ($type, $cls, $msg) = get_text ($opt{type}, $opt{level}, $opt{node}); | ||
| 544 | $type =~ tr/ /-/; | ||
| 545 | $type =~ s/\|/%7C/g; | ||
| 546 | $msg .= qq[ [<a href="../error-description#@{[htescape ($type)]}">Description</a>]]; | ||
| 547 | print STDOUT qq[<dt class="$cls">] . get_error_label (\%opt) . | ||
| 548 | qq[</dt>\n<dd class="$cls">], $msg, "</dd>\n"; | ||
| 549 | add_error ('structure', \%opt => $result); | ||
| 550 | }); | ||
| 551 | |||
| 552 | print STDOUT qq[</div>]; | ||
| 553 | } # print_structure_error_manifest_section | ||
| 554 | wakaba | 1.18 | |
| 555 | sub print_table_section ($) { | ||
| 556 | my $tables = shift; | ||
| 557 | |||
| 558 | push @nav, ['#tables' => 'Tables']; | ||
| 559 | print STDOUT qq[ | ||
| 560 | <div id="tables" class="section"> | ||
| 561 | <h2>Tables</h2> | ||
| 562 | |||
| 563 | <!--[if IE]><script type="text/javascript" src="../excanvas.js"></script><![endif]--> | ||
| 564 | <script src="../table-script.js" type="text/javascript"></script> | ||
| 565 | <noscript> | ||
| 566 | <p><em>Structure of tables are visualized here if scripting is enabled.</em></p> | ||
| 567 | </noscript> | ||
| 568 | ]; | ||
| 569 | |||
| 570 | require JSON; | ||
| 571 | |||
| 572 | my $i = 0; | ||
| 573 | for my $table_el (@$tables) { | ||
| 574 | $i++; | ||
| 575 | print STDOUT qq[<div class="section" id="table-$i"><h3>] . | ||
| 576 | get_node_link ($table_el) . q[</h3>]; | ||
| 577 | |||
| 578 | ## TODO: Make |ContentChecker| return |form_table| result | ||
| 579 | ## so that this script don't have to run the algorithm twice. | ||
| 580 | my $table = Whatpm::HTMLTable->form_table ($table_el); | ||
| 581 | |||
| 582 | for (@{$table->{column_group}}, @{$table->{column}}, $table->{caption}) { | ||
| 583 | next unless $_; | ||
| 584 | delete $_->{element}; | ||
| 585 | } | ||
| 586 | |||
| 587 | for (@{$table->{row_group}}) { | ||
| 588 | next unless $_; | ||
| 589 | next unless $_->{element}; | ||
| 590 | $_->{type} = $_->{element}->manakai_local_name; | ||
| 591 | delete $_->{element}; | ||
| 592 | } | ||
| 593 | |||
| 594 | for (@{$table->{cell}}) { | ||
| 595 | next unless $_; | ||
| 596 | for (@{$_}) { | ||
| 597 | next unless $_; | ||
| 598 | for (@$_) { | ||
| 599 | $_->{id} = refaddr $_->{element} if defined $_->{element}; |