/[suikacvs]/test/html-webhacc/WebHACC/Output.pm
Suika

Contents of /test/html-webhacc/WebHACC/Output.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (show annotations) (download)
Fri Aug 15 12:11:56 2008 UTC (16 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.17: +3 -0 lines
++ ChangeLog	15 Aug 2008 12:10:51 -0000
	* error-description-source.xml: Missing entries for
	Whatpm::ContentChecker and Whatpm::ContentChecker::HTML
	error types are added.  Error entries used by these
	modules are marked as such.

2008-08-15  Wakaba  <wakaba@suika.fam.cx>

++ html/WebHACC/Language/ChangeLog	15 Aug 2008 12:11:51 -0000
	* DOM.pm: Use error's own "layer" parameter, if any.

2008-08-15  Wakaba  <wakaba@suika.fam.cx>

++ html/WebHACC/ChangeLog	15 Aug 2008 12:11:25 -0000
	* Output.pm (nl_text): New rule, <var>{value}</var>,
	is implemented.

2008-08-15  Wakaba  <wakaba@suika.fam.cx>

1 package WebHACC::Output;
2 use strict;
3
4 require IO::Handle;
5 use Scalar::Util qw/refaddr/;
6
7 my $htescape = sub ($) {
8 my $s = $_[0];
9 $s =~ s/&/&amp;/g;
10 $s =~ s/</&lt;/g;
11 $s =~ s/>/&gt;/g;
12 $s =~ s/"/&quot;/g;
13 $s =~ s{([\x00-\x09\x0B-\x1F\x7F-\xA0\x{FEFF}\x{FFFC}-\x{FFFF}])}{
14 sprintf '<var>U+%04X</var>', ord $1;
15 }ge;
16 return $s;
17 };
18
19 my $htescape_value = sub ($) {
20 my $s = $_[0];
21 $s =~ s/&/&amp;/g;
22 $s =~ s/</&lt;/g;
23 $s =~ s/>/&gt;/g;
24 $s =~ s/"/&quot;/g;
25 return $s;
26 };
27
28 sub new ($) {
29 require WebHACC::Input;
30 return bless {nav => [], section_rank => 1,
31 input => WebHACC::Input->new}, shift;
32 } # new
33
34 sub input ($;$) {
35 if (@_ > 1) {
36 if (defined $_[1]) {
37 $_[0]->{input} = $_[1];
38 } else {
39 $_[0]->{input} = WebHACC::Input->new;
40 }
41 }
42
43 return $_[0]->{input};
44 } # input
45
46 sub handle ($;$) {
47 if (@_ > 1) {
48 if (defined $_[1]) {
49 $_[0]->{handle} = $_[1];
50 } else {
51 delete $_[0]->{handle};
52 }
53 }
54
55 return $_[0]->{handle};
56 } # handle
57
58 sub has_error ($;$) {
59 if (@_ > 1) {
60 if (defined $_[1]) {
61 $_[0]->{has_error} = 1;
62 } else {
63 delete $_[0]->{has_error};
64 }
65 }
66
67 return $_[0]->{has_error};
68 } # has_error
69
70 sub set_utf8 ($) {
71 binmode shift->{handle}, ':utf8';
72 } # set_utf8
73
74 sub set_flush ($) {
75 shift->{handle}->autoflush (1);
76 } # set_flush
77
78 sub unset_flush ($) {
79 shift->{handle}->autoflush (0);
80 } # unset_flush
81
82 sub html ($$) {
83 shift->{handle}->print (shift);
84 } # html
85
86 sub text ($$) {
87 shift->html ($htescape->(shift));
88 } # text
89
90 sub url ($$%) {
91 my ($self, $url, %opt) = @_;
92 $self->html (q[<code class=uri>&lt;]);
93 $self->link ($url, %opt, url => $url);
94 $self->html (q[></code>]);
95 } # url
96
97 sub start_tag ($$%) {
98 my ($self, $tag_name, %opt) = @_;
99 $self->html ('<' . $htescape_value->($tag_name)); # escape for safety
100 if (exists $opt{id}) {
101 my $id = $self->input->id_prefix . $opt{id};
102 $self->html (' id="' . $htescape_value->($id) . '"');
103 delete $opt{id};
104 }
105 for (keys %opt) { # for safety
106 $self->html (' ' . $htescape_value->($_) . '="' .
107 $htescape_value->($opt{$_}) . '"');
108 }
109 $self->html ('>');
110 } # start_tag
111
112 sub end_tag ($$) {
113 shift->html ('</' . $htescape_value->(shift) . '>');
114 } # end_tag
115
116 sub start_section ($%) {
117 my ($self, %opt) = @_;
118
119 my $class = 'section';
120 if (defined $opt{role}) {
121 if ($opt{role} eq 'parse-errors') {
122 $opt{id} ||= 'parse-errors';
123 $opt{title} ||= 'Parse Errors Section';
124 $opt{short_title} ||= 'Parse Errors';
125 $class .= ' errors';
126 delete $opt{role};
127 } elsif ($opt{role} eq 'structure-errors') {
128 $opt{id} ||= 'document-errors';
129 $opt{title} ||= 'Structural Errors';
130 $opt{short_title} ||= 'Struct. Errors';
131 $class .= ' errors';
132 delete $opt{role};
133 } elsif ($opt{role} eq 'transfer-errors') {
134 $opt{id} ||= 'transfer-errors';
135 $opt{title} ||= 'Transfer Errors';
136 $opt{short_title} ||= 'Trans. Errors';
137 $class .= ' errors';
138 delete $opt{role};
139 } elsif ($opt{role} eq 'reformatted') {
140 $opt{id} ||= 'document-tree';
141 $opt{title} ||= 'Reformatted Document Source';
142 $opt{short_title} ||= 'Reformatted';
143 $class .= ' dump';
144 delete $opt{role}
145 } elsif ($opt{role} eq 'tree') {
146 $opt{id} ||= 'document-tree';
147 $opt{title} ||= 'Document Tree';
148 $opt{short_title} ||= 'Tree';
149 $class .= ' dump';
150 delete $opt{role};
151 } elsif ($opt{role} eq 'structure') {
152 $opt{id} ||= 'document-structure';
153 $opt{title} ||= 'Document Structure';
154 $opt{short_title} ||= 'Structure';
155 $class .= ' dump';
156 delete $opt{role};
157 } elsif ($opt{role} eq 'subdoc') {
158 $class .= ' subdoc';
159 delete $opt{role};
160 } elsif ($opt{role} eq 'source') {
161 $opt{id} ||= 'source-string';
162 $opt{title} ||= 'Document Source';
163 $opt{short_title} ||= 'Source';
164 $class .= ' source';
165 delete $opt{role};
166 } elsif ($opt{role} eq 'result') {
167 $opt{id} ||= 'result-summary';
168 $opt{title} ||= 'Result';
169 $class .= ' result';
170 delete $opt{role};
171 }
172 }
173
174 $self->{section_rank}++;
175 $self->html (qq[<div class="$class"]);
176 if (defined $opt{id}) {
177 my $prefix = $self->input->id_prefix;
178 $opt{parent_id} ||= $prefix;
179 my $id = $prefix . $opt{id};
180 $self->html (' id="' . $htescape->($id) . '">');
181 if ($self->{section_rank} == 2 or length $opt{parent_id}) {
182 my $st = $opt{short_title} || $opt{title};
183 push @{$self->{nav}},
184 [$id => $st => $opt{text}];
185
186 $self->start_tag ('script');
187 $self->html (qq[ addSectionLink ('$id', ']);
188 $self->nl_text ($st, text => $opt{text});
189 if (defined $opt{parent_id}) {
190 $self->html (q[', '] . $opt{parent_id});
191 }
192 $self->html (q[') ]);
193 $self->end_tag ('script');
194 }
195 } else {
196 $self->html ('>');
197 }
198 my $section_rank = $self->{section_rank};
199 $section_rank = 6 if $section_rank > 6;
200 $self->html ('<h' . $section_rank . '>');
201 $self->nl_text ($opt{title}, text => $opt{text});
202 $self->html ('</h' . $section_rank . '>');
203 } # start_section
204
205 sub end_section ($) {
206 my $self = shift;
207 $self->html ('</div>');
208 $self->{handle}->flush;
209 $self->{section_rank}--;
210 } # end_section
211
212 sub start_error_list ($%) {
213 my ($self, %opt) = @_;
214
215 if (defined $opt{role}) {
216 if ($opt{role} eq 'parse-errors') {
217 $opt{id} ||= 'parse-errors-list';
218 delete $opt{role};
219 } elsif ($opt{role} eq 'structure-errors') {
220 $opt{id} ||= 'document-errors-list';
221 delete $opt{role};
222 } elsif ($opt{role} eq 'transfer-errors') {
223 $opt{id} ||= 'transfer-errors-list';
224 delete $opt{role};
225 }
226 }
227
228 $self->start_tag ('dl', %opt);
229
230 delete $self->{has_error}; # reset
231 } # start_error_list
232
233 sub end_error_list ($%) {
234 my ($self, %opt) = @_;
235
236 my $no_error_message = 'No error found.';
237
238 if (defined $opt{role}) {
239 if ($opt{role} eq 'parse-errors') {
240 $self->end_tag ('dl');
241 ## NOTE: For parse error list, the |add_source_to_parse_error_list|
242 ## method is invoked at the end of |generate_source_string_section|,
243 ## since that generation method is invoked after the error list
244 ## is generated.
245 $no_error_message = 'No parse error found.';
246 } elsif ($opt{role} eq 'structure-errors') {
247 $self->end_tag ('dl');
248 $self->add_source_to_parse_error_list ('document-errors-list');
249 $no_error_message = 'No structural error found.';
250 } elsif ($opt{role} eq 'transfer-errors') {
251 $self->end_tag ('dl');
252 $no_error_message = 'No transfer error found.';
253 } else {
254 $self->end_tag ('dl');
255 }
256 } else {
257 $self->end_tag ('dl');
258 }
259
260 unless ($self->{has_error}) {
261 $self->start_tag ('p', class => 'no-errors');
262 $self->nl_text ($no_error_message);
263 }
264 } # end_error_list
265
266 sub add_source_to_parse_error_list ($$) {
267 my $self = shift;
268
269 $self->script (q[addSourceToParseErrorList ('] . $self->input->id_prefix .
270 q[', '] . shift () . q[')]);
271 } # add_source_to_parse_error_list
272
273 sub start_code_block ($) {
274 shift->html ('<pre><code>');
275 } # start_code_block
276
277 sub end_code_block ($) {
278 shift->html ('</code></pre>');
279 } # end_code_block
280
281 sub code ($$;%) {
282 my ($self, $content, %opt) = @_;
283 $self->start_tag ('code', %opt);
284 $self->text ($content);
285 $self->html ('</code>');
286 } # code
287
288 sub script ($$;%) {
289 my ($self, $content, %opt) = @_;
290 $self->start_tag ('script', %opt);
291 $self->html ($content);
292 $self->html ('</script>');
293 } # script
294
295 sub dt ($$;%) {
296 my ($self, $content, %opt) = @_;
297 $self->start_tag ('dt', %opt);
298 $self->nl_text ($content, text => $opt{text});
299 } # dt
300
301 sub select ($$%) {
302 my ($self, $options, %opt) = @_;
303
304 my $selected = $opt{selected};
305 delete $opt{selected};
306
307 $self->start_tag ('select', %opt);
308
309 my @options = @$options;
310 while (@options) {
311 my $opt = shift @options;
312 if ($opt->{options}) {
313 $self->html ('<optgroup label="');
314 $self->nl_text ($opt->{label});
315 $self->html ('">');
316 unshift @options, @{$opt->{options}}, {end_options => 1};
317 } elsif ($opt->{end_options}) {
318 $self->end_tag ('optgroup');
319 } else {
320 $self->start_tag ('option', value => $opt->{value},
321 ((defined $selected and $opt->{value} eq $selected)
322 ? (selected => '') : ()));
323 $self->nl_text (defined $opt->{label} ? $opt->{label} : $opt->{value});
324 }
325 }
326
327 $self->end_tag ('select');
328 } # select
329
330 sub link ($$%) {
331 my ($self, $content, %opt) = @_;
332 $self->start_tag ('a', %opt, href => $opt{url});
333 $self->text ($content);
334 $self->html ('</a>');
335 } # link
336
337 sub xref ($$%) {
338 my ($self, $content, %opt) = @_;
339 $self->html ('<a href="#' . $htescape->($self->input->id_prefix . $opt{target}) . '">');
340 $self->nl_text ($content, text => $opt{text});
341 $self->html ('</a>');
342 } # xref
343
344 sub link_to_webhacc ($$%) {
345 my ($self, $content, %opt) = @_;
346 $opt{url} = './?uri=' . $self->encode_url_component ($opt{url});
347 $self->link ($content, %opt);
348 } # link_to_webhacc
349
350 my $get_node_path = sub ($) {
351 my $node = shift;
352 my @r;
353 while (defined $node) {
354 my $rs;
355 if ($node->node_type == 1) {
356 $rs = $node->node_name;
357 $node = $node->parent_node;
358 } elsif ($node->node_type == 2) {
359 $rs = '@' . $node->node_name;
360 $node = $node->owner_element;
361 } elsif ($node->node_type == 3) {
362 $rs = '"' . $node->data . '"';
363 $node = $node->parent_node;
364 } elsif ($node->node_type == 9) {
365 @r = ('') unless @r;
366 $rs = '';
367 $node = $node->parent_node;
368 } else {
369 $rs = '#' . $node->node_type;
370 $node = $node->parent_node;
371 }
372 unshift @r, $rs;
373 }
374 return join '/', @r;
375 }; # $get_node_path
376
377 my $get_object_path = sub ($) {
378 my $node = shift;
379 my @r;
380 while (defined $node) {
381 my $ref = ref $node;
382 $ref =~ /([^:]+)$/;
383 my $rs = $1;
384 my $node_name = $node->node_name;
385 if (defined $node_name) {
386 $rs .= ' <code>' . $htescape->($node_name) . '</code>';
387 }
388 $node = undef;
389 unshift @r, $rs;
390 }
391 return join '/', @r;
392 }; # $get_object_path
393
394 sub node_link ($$) {
395 my ($self, $node) = @_;
396 if ($node->isa ('Message::IF::Node')) {
397 $self->xref ($get_node_path->($node), target => 'node-' . refaddr $node);
398 } else {
399 $self->html ($get_object_path->($node));
400 }
401 } # node_link
402
403 {
404 my $Msg = {};
405
406 sub load_text_catalog ($$) {
407 my $self = shift;
408
409 my $lang = shift; # MUST be a canonical lang name
410 my $file_name = qq[cc-msg.$lang.txt];
411 $lang = 'en' unless -f $file_name;
412 $self->{primary_language} = $lang;
413
414 open my $file, '<:utf8', $file_name or die "$0: $file_name: $!";
415 while (<$file>) {
416 if (s/^([^;]+);([^;]*);//) {
417 my ($type, $cls, $msg) = ($1, $2, $_);
418 $msg =~ tr/\x0D\x0A//d;
419 $Msg->{$type} = [$cls, $msg];
420 }
421 }
422 } # load_text_catalog
423
424 sub nl_text ($$;%) {
425 my ($self, $type, %opt) = @_;
426 my $node = $opt{node};
427
428 my @arg;
429 {
430 if (defined $Msg->{$type}) {
431 my $msg = $Msg->{$type}->[1];
432 if ($msg =~ /<var>/) {
433 $msg =~ s{<var>\$([0-9]+)</var>}{
434 defined $arg[$1] ? $htescape->($arg[$1]) : '(undef)';
435 }ge;
436 $msg =~ s{<var>{\@([A-Za-z0-9:_.-]+)}</var>}{
437 UNIVERSAL::can ($node, 'get_attribute_ns')
438 ? $htescape->($node->get_attribute_ns (undef, $1)) : ''
439 }ge;
440 $msg =~ s{<var>{\@}</var>}{
441 UNIVERSAL::can ($node, 'value') ? $htescape->($node->value) : ''
442 }ge;
443 $msg =~ s{<var>{text}</var>}{
444 defined $opt{text} ? $htescape->($opt{text}) : ''
445 }ge;
446 $msg =~ s{<var>{value}</var>}{
447 defined $opt{value} ? $htescape->($opt{value}) : ''
448 }ge;
449 $msg =~ s{<var>{local-name}</var>}{
450 UNIVERSAL::can ($node, 'manakai_local_name')
451 ? $htescape->($node->manakai_local_name) : ''
452 }ge;
453 $msg =~ s{<var>{element-local-name}</var>}{
454 (UNIVERSAL::can ($node, 'owner_element') and
455 $node->owner_element)
456 ? $htescape->($node->owner_element->manakai_local_name) : ''
457 }ge;
458 }
459 $self->html ($msg);
460 return;
461 } elsif ($type =~ s/:([^:]*)$//) {
462 unshift @arg, $1;
463 redo;
464 }
465 }
466 $self->text ($type);
467 } # nl_text
468
469 }
470
471 sub nav_list ($) {
472 my $self = shift;
473 $self->html (q[<ul class="navigation" id="nav-items">]);
474 for (@{$self->{nav}}) {
475 $self->html (qq[<li><a href="#@{[$htescape->($_->[0])]}">]);
476 $self->nl_text ($_->[1], text => $_->[2]);
477 $self->html ('</a>');
478 }
479 $self->html ('</ul>');
480 } # nav_list
481
482 sub http_header ($) {
483 shift->html (qq[Content-Type: text/html; charset=utf-8\n\n]);
484 } # http_header
485
486 sub http_error ($$) {
487 my $self = shift;
488 my $code = 0+shift;
489 my $text = {
490 404 => 'Not Found',
491 }->{$code};
492 $self->html (qq[Status: $code $text\nContent-Type: text/html ; charset=us-ascii\n\n$code $text]);
493 } # http_error
494
495 sub html_header ($) {
496 my $self = shift;
497 $self->html (q[<!DOCTYPE html>]);
498 $self->start_tag ('html', lang => $self->{primary_language});
499 $self->html (q[<head><title>]);
500 $self->nl_text (q[WebHACC:Title]);
501 $self->html (q[</title>
502 <link rel="stylesheet" href="../cc-style.css" type="text/css">
503 <script src="../cc-script.js"></script>
504 </head>
505 <body onclick=" return onbodyclick (event) " onload=" onbodyload () ">
506 <h1>]);
507 $self->nl_text (q[WebHACC:Heading]);
508 $self->html (q[</h1><script> insertNavSections () </script>]);
509 } # html_header
510
511 sub generate_input_section ($$) {
512 my ($out, $cgi) = @_;
513
514 require Encode;
515 my $decode = sub ($) {
516 if (defined $_[0]) {
517 return Encode::decode ('utf-8', $_[0]);
518 } else {
519 return undef;
520 }
521 }; # $decode
522
523 my $options = sub ($) {
524 my $context = shift;
525
526 $out->html (q[<div class="details default"><p class=legend onclick="nextSibling.style.display = nextSibling.style.display == 'block' ? 'none' : 'block'; parentNode.className = nextSibling.style.display == 'none' ? 'details' : 'details open'">]);
527 $out->nl_text (q[Options]);
528 $out->start_tag ('div');
529
530 if ($context eq 'url') {
531 $out->start_tag ('p');
532 $out->start_tag ('label');
533 $out->start_tag ('input', type => 'checkbox', name => 'error-page',
534 value => 1,
535 ($cgi->get_parameter ('error-page')
536 ? (checked => '') : ()));
537 $out->nl_text ('Check error page');
538 $out->end_tag ('label');
539 }
540
541 $out->start_tag ('p');
542 $out->start_tag ('label');
543 $out->nl_text (q[Content type]);
544 $out->text (': ');
545 $out->select ([
546 {value => '', label => 'As specified'},
547 {value => 'application/atom+xml'},
548 {value => 'application/xhtml+xml'},
549 {value => 'application/xml'},
550 {value => 'text/html'},
551 {value => 'text/xml'},
552 {value => 'text/css'},
553 {value => 'text/cache-manifest'},
554 {value => 'text/x-webidl'},
555 ], name => 'i', selected => scalar $cgi->get_parameter ('i'));
556 $out->end_tag ('label');
557
558 if ($context ne 'text') {
559 $out->start_tag ('p');
560 $out->start_tag ('label');
561 $out->nl_text (q[Charset]);
562 $out->text (q[: ]);
563 $out->select ([
564 {value => '', label => 'As specified'},
565 {label => 'Japanese charsets', options => [
566 {value => 'Windows-31J'},
567 {value => 'Shift_JIS'},
568 {value => 'EUC-JP'},
569 {value => 'ISO-2022-JP'},
570 ]},
571 {label => 'European charsets', options => [
572 {value => 'Windows-1252'},
573 {value => 'ISO-8859-1'},
574 {value => 'US-ASCII'},
575 ]},
576 {label => 'Asian charsets', options => [
577 {value => 'Windows-874'},
578 {value => 'ISO-8859-11'},
579 {value => 'TIS-620'},
580 ]},
581 {label => 'Unicode charsets', options => [
582 {value => 'UTF-8'},
583 {value => 'UTF-8n'},
584 ]},
585 ], name => 'charset',
586 selected => scalar $cgi->get_parameter ('charset'));
587 $out->end_tag ('label');
588 }
589
590 if ($context eq 'text') {
591 $out->start_tag ('p');
592 $out->start_tag ('label');
593 $out->nl_text ('Setting innerHTML');
594 $out->text (': ');
595 $out->start_tag ('input', name => 'e',
596 value => $decode->(scalar $cgi->get_parameter ('e')));
597 $out->end_tag ('label');
598 }
599
600 $out->html (q[</div></div>]);
601 }; # $options
602
603 $out->start_section (id => 'input', title => 'Input');
604 $out->html (q[<script> insertNavSections ('input') </script>]);
605
606 $out->start_section (id => 'input-url', title => 'By URL',
607 parent_id => 'input');
608 $out->start_tag ('form', action => './#result-summary',
609 'accept-charset' => 'utf-8',
610 method => 'get');
611 $out->start_tag ('input', type => 'hidden', name => '_charset_');
612
613 $out->start_tag ('p');
614 $out->start_tag ('label');
615 $out->nl_text ('URL');
616 $out->text (': ');
617 $out->start_tag ('input',
618 name => 'uri',
619 type => 'url',
620 value => $decode->(scalar $cgi->get_parameter ('uri')));
621 $out->end_tag ('label');
622
623 $out->start_tag ('p');
624 $out->start_tag ('button', type => 'submit');
625 $out->nl_text ('Check');
626 $out->end_tag ('button');
627
628 $options->('url');
629
630 $out->end_tag ('form');
631 $out->end_section;
632
633 ## TODO: File upload
634
635 $out->start_section (id => 'input-text', title => 'By direct input',
636 parent_id => 'input');
637 $out->start_tag ('form', action => './#result-summary',
638 'accept-charset' => 'utf-8',
639 method => 'post');
640 $out->start_tag ('input', type => 'hidden', name => '_charset_');
641
642 $out->start_tag ('p');
643 $out->start_tag ('label');
644 $out->nl_text ('Document source to check');
645 $out->text (': ');
646 $out->start_tag ('br');
647 $out->start_tag ('textarea',
648 name => 's');
649 my $s = $decode->($cgi->get_parameter ('s'));
650 $out->html ($htescape_value->($s)) if defined $s;
651 $out->end_tag ('textarea');
652 $out->end_tag ('label');
653
654 $out->start_tag ('p');
655 $out->start_tag ('button', type => 'submit',
656 onclick => 'form.method = form.s.value.length > 512 ? "post" : "get"');
657 $out->nl_text ('Check');
658 $out->end_tag ('button');
659
660 $options->('text');
661
662 $out->end_tag ('form');
663 $out->end_section;
664
665 $out->script (q[
666 if (!document.webhaccNavigated &&
667 document.getElementsByTagName ('textarea')[0].value.length > 0) {
668 showTab ('input-text');
669 document.webhaccNavigated = false;
670 }
671 ]);
672
673 $out->end_section;
674 } # generate_input_section
675
676 sub encode_url_component ($$) {
677 shift;
678 require Encode;
679 my $s = Encode::encode ('utf8', shift);
680 $s =~ s/([^0-9A-Za-z_.~-])/sprintf '%%%02X', ord $1/ge;
681 return $s;
682 } # encode_url_component
683
684 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24