16 |
return $s; |
return $s; |
17 |
}; |
}; |
18 |
|
|
19 |
|
my $htescape_value = sub ($) { |
20 |
|
my $s = $_[0]; |
21 |
|
$s =~ s/&/&/g; |
22 |
|
$s =~ s/</</g; |
23 |
|
$s =~ s/>/>/g; |
24 |
|
$s =~ s/"/"/g; |
25 |
|
return $s; |
26 |
|
}; |
27 |
|
|
28 |
sub new ($) { |
sub new ($) { |
29 |
return bless {nav => [], section_rank => 1}, shift; |
require WebHACC::Input; |
30 |
|
return bless {nav => [], section_rank => 1, |
31 |
|
input => WebHACC::Input->new}, shift; |
32 |
} # new |
} # new |
33 |
|
|
34 |
sub input ($;$) { |
sub input ($;$) { |
36 |
if (defined $_[1]) { |
if (defined $_[1]) { |
37 |
$_[0]->{input} = $_[1]; |
$_[0]->{input} = $_[1]; |
38 |
} else { |
} else { |
39 |
delete $_[0]->{input}; |
$_[0]->{input} = WebHACC::Input->new; |
40 |
} |
} |
41 |
} |
} |
42 |
|
|
84 |
|
|
85 |
sub start_tag ($$%) { |
sub start_tag ($$%) { |
86 |
my ($self, $tag_name, %opt) = @_; |
my ($self, $tag_name, %opt) = @_; |
87 |
$self->html ('<' . $htescape->($tag_name)); # escape for safety |
$self->html ('<' . $htescape_value->($tag_name)); # escape for safety |
88 |
if (exists $opt{id}) { |
if (exists $opt{id}) { |
89 |
my $id = $self->input->id_prefix . $opt{id}; |
my $id = $self->input->id_prefix . $opt{id}; |
90 |
$self->html (' id="' . $htescape->($id) . '"'); |
$self->html (' id="' . $htescape_value->($id) . '"'); |
91 |
delete $opt{id}; |
delete $opt{id}; |
92 |
} |
} |
93 |
for (keys %opt) { # for safety |
for (keys %opt) { # for safety |
94 |
$self->html (' ' . $htescape->($_) . '="' . $htescape->($opt{$_}) . '"'); |
$self->html (' ' . $htescape_value->($_) . '="' . |
95 |
|
$htescape_value->($opt{$_}) . '"'); |
96 |
} |
} |
97 |
$self->html ('>'); |
$self->html ('>'); |
98 |
} # start_tag |
} # start_tag |
99 |
|
|
100 |
sub end_tag ($$) { |
sub end_tag ($$) { |
101 |
shift->html ('</' . $htescape->(shift) . '>'); |
shift->html ('</' . $htescape_value->(shift) . '>'); |
102 |
} # end_tag |
} # end_tag |
103 |
|
|
104 |
sub start_section ($%) { |
sub start_section ($%) { |
105 |
my ($self, %opt) = @_; |
my ($self, %opt) = @_; |
106 |
|
|
107 |
|
my $class = 'section'; |
108 |
if (defined $opt{role}) { |
if (defined $opt{role}) { |
109 |
if ($opt{role} eq 'parse-errors') { |
if ($opt{role} eq 'parse-errors') { |
110 |
$opt{id} ||= 'parse-errors'; |
$opt{id} ||= 'parse-errors'; |
111 |
$opt{title} ||= 'Parse Errors'; |
$opt{title} ||= 'Parse Errors Section'; |
112 |
|
$opt{short_title} ||= 'Parse Errors'; |
113 |
delete $opt{role}; |
delete $opt{role}; |
114 |
} elsif ($opt{role} eq 'structure-errors') { |
} elsif ($opt{role} eq 'structure-errors') { |
115 |
$opt{id} ||= 'document-errors'; |
$opt{id} ||= 'document-errors'; |
131 |
$opt{title} ||= 'Document Structure'; |
$opt{title} ||= 'Document Structure'; |
132 |
$opt{short_title} ||= 'Structure'; |
$opt{short_title} ||= 'Structure'; |
133 |
delete $opt{role}; |
delete $opt{role}; |
134 |
|
} elsif ($opt{role} eq 'subdoc') { |
135 |
|
$class .= ' subdoc'; |
136 |
|
delete $opt{role}; |
137 |
} |
} |
138 |
} |
} |
139 |
|
|
140 |
$self->{section_rank}++; |
$self->{section_rank}++; |
141 |
$self->html ('<div class=section'); |
$self->html (qq[<div class="$class"]); |
142 |
if (defined $opt{id}) { |
if (defined $opt{id}) { |
143 |
my $id = $self->input->id_prefix . $opt{id}; |
my $prefix = $self->input->id_prefix; |
144 |
$self->html (' id="' . $htescape->($id) . '"'); |
$opt{parent_id} ||= $prefix; |
145 |
push @{$self->{nav}}, [$id => $opt{short_title} || $opt{title}] |
my $id = $prefix . $opt{id}; |
146 |
if $self->{section_rank} == 2; |
$self->html (' id="' . $htescape->($id) . '">'); |
147 |
|
if ($self->{section_rank} == 2 or length $opt{parent_id}) { |
148 |
|
my $st = $opt{short_title} || $opt{title}; |
149 |
|
push @{$self->{nav}}, |
150 |
|
[$id => $st => $opt{text}]; |
151 |
|
|
152 |
|
$self->start_tag ('script'); |
153 |
|
$self->html (qq[ addSectionLink ('$id', ']); |
154 |
|
$self->nl_text ($st, text => $opt{text}); |
155 |
|
if (defined $opt{parent_id}) { |
156 |
|
$self->html (q[', '] . $opt{parent_id}); |
157 |
|
} |
158 |
|
$self->html (q[') ]); |
159 |
|
$self->end_tag ('script'); |
160 |
|
} |
161 |
|
} else { |
162 |
|
$self->html ('>'); |
163 |
} |
} |
164 |
my $section_rank = $self->{section_rank}; |
my $section_rank = $self->{section_rank}; |
165 |
$section_rank = 6 if $section_rank > 6; |
$section_rank = 6 if $section_rank > 6; |
166 |
$self->html ('><h' . $section_rank . '>' . |
$self->html ('<h' . $section_rank . '>'); |
167 |
$htescape->($opt{title}) . |
$self->nl_text ($opt{title}, text => $opt{text}); |
168 |
'</h' . $section_rank . '>'); |
$self->html ('</h' . $section_rank . '>'); |
169 |
} # start_section |
} # start_section |
170 |
|
|
171 |
sub end_section ($) { |
sub end_section ($) { |
218 |
my $self = shift; |
my $self = shift; |
219 |
|
|
220 |
$self->script (q[addSourceToParseErrorList ('] . $self->input->id_prefix . |
$self->script (q[addSourceToParseErrorList ('] . $self->input->id_prefix . |
221 |
q[', '] . shift . q[')]); |
q[', '] . shift () . q[')]); |
222 |
} # add_source_to_parse_error_list |
} # add_source_to_parse_error_list |
223 |
|
|
224 |
sub start_code_block ($) { |
sub start_code_block ($) { |
246 |
sub dt ($$;%) { |
sub dt ($$;%) { |
247 |
my ($self, $content, %opt) = @_; |
my ($self, $content, %opt) = @_; |
248 |
$self->start_tag ('dt', %opt); |
$self->start_tag ('dt', %opt); |
249 |
$self->text ($content); |
$self->nl_text ($content, text => $opt{text}); |
250 |
} # dt |
} # dt |
251 |
|
|
252 |
|
sub select ($$%) { |
253 |
|
my ($self, $options, %opt) = @_; |
254 |
|
|
255 |
|
my $selected = $opt{selected}; |
256 |
|
delete $opt{selected}; |
257 |
|
|
258 |
|
$self->start_tag ('select', %opt); |
259 |
|
|
260 |
|
my @options = @$options; |
261 |
|
while (@options) { |
262 |
|
my $opt = shift @options; |
263 |
|
if ($opt->{options}) { |
264 |
|
$self->html ('<optgroup label="'); |
265 |
|
$self->nl_text ($opt->{label}); |
266 |
|
$self->html ('">'); |
267 |
|
unshift @options, @{$opt->{options}}, {end_options => 1}; |
268 |
|
} elsif ($opt->{end_options}) { |
269 |
|
$self->end_tag ('optgroup'); |
270 |
|
} else { |
271 |
|
$self->start_tag ('option', value => $opt->{value}, |
272 |
|
((defined $selected and $opt->{value} eq $selected) |
273 |
|
? (selected => '') : ())); |
274 |
|
$self->nl_text (defined $opt->{label} ? $opt->{label} : $opt->{value}); |
275 |
|
} |
276 |
|
} |
277 |
|
|
278 |
|
$self->end_tag ('select'); |
279 |
|
} # select |
280 |
|
|
281 |
sub link ($$%) { |
sub link ($$%) { |
282 |
my ($self, $content, %opt) = @_; |
my ($self, $content, %opt) = @_; |
283 |
$self->start_tag ('a', %opt, href => $opt{url}); |
$self->start_tag ('a', %opt, href => $opt{url}); |
288 |
sub xref ($$%) { |
sub xref ($$%) { |
289 |
my ($self, $content, %opt) = @_; |
my ($self, $content, %opt) = @_; |
290 |
$self->html ('<a href="#' . $htescape->($self->input->id_prefix . $opt{target}) . '">'); |
$self->html ('<a href="#' . $htescape->($self->input->id_prefix . $opt{target}) . '">'); |
291 |
$self->text ($content); |
$self->nl_text ($content, text => $opt{text}); |
292 |
$self->html ('</a>'); |
$self->html ('</a>'); |
293 |
} # xref |
} # xref |
294 |
|
|
298 |
$self->link ($content, %opt); |
$self->link ($content, %opt); |
299 |
} # link_to_webhacc |
} # link_to_webhacc |
300 |
|
|
|
|
|
301 |
my $get_node_path = sub ($) { |
my $get_node_path = sub ($) { |
302 |
my $node = shift; |
my $node = shift; |
303 |
my @r; |
my @r; |
325 |
return join '/', @r; |
return join '/', @r; |
326 |
}; # $get_node_path |
}; # $get_node_path |
327 |
|
|
328 |
|
my $get_object_path = sub ($) { |
329 |
|
my $node = shift; |
330 |
|
my @r; |
331 |
|
while (defined $node) { |
332 |
|
my $ref = ref $node; |
333 |
|
$ref =~ /([^:]+)$/; |
334 |
|
my $rs = $1; |
335 |
|
my $node_name = $node->node_name; |
336 |
|
if (defined $node_name) { |
337 |
|
$rs .= ' <code>' . $htescape->($node_name) . '</code>'; |
338 |
|
} |
339 |
|
$node = undef; |
340 |
|
unshift @r, $rs; |
341 |
|
} |
342 |
|
return join '/', @r; |
343 |
|
}; # $get_object_path |
344 |
|
|
345 |
sub node_link ($$) { |
sub node_link ($$) { |
346 |
my ($self, $node) = @_; |
my ($self, $node) = @_; |
347 |
$self->xref ($get_node_path->($node), target => 'node-' . refaddr $node); |
if ($node->isa ('Message::IF::Node')) { |
348 |
|
$self->xref ($get_node_path->($node), target => 'node-' . refaddr $node); |
349 |
|
} else { |
350 |
|
$self->html ($get_object_path->($node)); |
351 |
|
} |
352 |
} # node_link |
} # node_link |
353 |
|
|
354 |
|
{ |
355 |
|
my $Msg = {}; |
356 |
|
|
357 |
|
sub load_text_catalog ($$) { |
358 |
|
my $self = shift; |
359 |
|
|
360 |
|
my $lang = shift; # MUST be a canonical lang name |
361 |
|
my $file_name = qq[cc-msg.$lang.txt]; |
362 |
|
$lang = 'en' unless -f $file_name; |
363 |
|
$self->{primary_language} = $lang; |
364 |
|
|
365 |
|
open my $file, '<:utf8', $file_name or die "$0: $file_name: $!"; |
366 |
|
while (<$file>) { |
367 |
|
if (s/^([^;]+);([^;]*);//) { |
368 |
|
my ($type, $cls, $msg) = ($1, $2, $_); |
369 |
|
$msg =~ tr/\x0D\x0A//d; |
370 |
|
$Msg->{$type} = [$cls, $msg]; |
371 |
|
} |
372 |
|
} |
373 |
|
} # load_text_catalog |
374 |
|
|
375 |
|
sub nl_text ($$;%) { |
376 |
|
my ($self, $type, %opt) = @_; |
377 |
|
my $node = $opt{node}; |
378 |
|
|
379 |
|
my @arg; |
380 |
|
{ |
381 |
|
if (defined $Msg->{$type}) { |
382 |
|
my $msg = $Msg->{$type}->[1]; |
383 |
|
if ($msg =~ /<var>/) { |
384 |
|
$msg =~ s{<var>\$([0-9]+)</var>}{ |
385 |
|
defined $arg[$1] ? $htescape->($arg[$1]) : '(undef)'; |
386 |
|
}ge; |
387 |
|
$msg =~ s{<var>{\@([A-Za-z0-9:_.-]+)}</var>}{ |
388 |
|
UNIVERSAL::can ($node, 'get_attribute_ns') |
389 |
|
? $htescape->($node->get_attribute_ns (undef, $1)) : '' |
390 |
|
}ge; |
391 |
|
$msg =~ s{<var>{\@}</var>}{ |
392 |
|
UNIVERSAL::can ($node, 'value') ? $htescape->($node->value) : '' |
393 |
|
}ge; |
394 |
|
$msg =~ s{<var>{text}</var>}{ |
395 |
|
defined $opt{text} ? $htescape->($opt{text}) : '' |
396 |
|
}ge; |
397 |
|
$msg =~ s{<var>{local-name}</var>}{ |
398 |
|
UNIVERSAL::can ($node, 'manakai_local_name') |
399 |
|
? $htescape->($node->manakai_local_name) : '' |
400 |
|
}ge; |
401 |
|
$msg =~ s{<var>{element-local-name}</var>}{ |
402 |
|
(UNIVERSAL::can ($node, 'owner_element') and |
403 |
|
$node->owner_element) |
404 |
|
? $htescape->($node->owner_element->manakai_local_name) : '' |
405 |
|
}ge; |
406 |
|
} |
407 |
|
$self->html ($msg); |
408 |
|
return; |
409 |
|
} elsif ($type =~ s/:([^:]*)$//) { |
410 |
|
unshift @arg, $1; |
411 |
|
redo; |
412 |
|
} |
413 |
|
} |
414 |
|
$self->text ($type); |
415 |
|
} # nl_text |
416 |
|
|
417 |
|
} |
418 |
|
|
419 |
sub nav_list ($) { |
sub nav_list ($) { |
420 |
my $self = shift; |
my $self = shift; |
421 |
$self->html (q[<ul class="navigation" id="nav-items">]); |
$self->html (q[<ul class="navigation" id="nav-items">]); |
422 |
for (@{$self->{nav}}) { |
for (@{$self->{nav}}) { |
423 |
$self->html (qq[<li><a href="#@{[$htescape->($_->[0])]}">@{[$htescape->($_->[1])]}</a>]); |
$self->html (qq[<li><a href="#@{[$htescape->($_->[0])]}">]); |
424 |
|
$self->nl_text ($_->[1], text => $_->[2]); |
425 |
|
$self->html ('</a>'); |
426 |
} |
} |
427 |
$self->html ('</ul>'); |
$self->html ('</ul>'); |
428 |
} # nav_list |
} # nav_list |
442 |
|
|
443 |
sub html_header ($) { |
sub html_header ($) { |
444 |
my $self = shift; |
my $self = shift; |
445 |
$self->html (q[<!DOCTYPE html> |
$self->html (q[<!DOCTYPE html>]); |
446 |
<html lang="en"> |
$self->start_tag ('html', lang => $self->{primary_language}); |
447 |
<head> |
$self->html (q[<head><title>]); |
448 |
<title>Web Document Conformance Checker (BETA)</title> |
$self->nl_text (q[WebHACC:Title]); |
449 |
|
$self->html (q[</title> |
450 |
<link rel="stylesheet" href="../cc-style.css" type="text/css"> |
<link rel="stylesheet" href="../cc-style.css" type="text/css"> |
451 |
|
<script src="../cc-script.js"></script> |
452 |
</head> |
</head> |
453 |
<body> |
<body onclick=" return onbodyclick (event) " onload=" onbodyload () "> |
454 |
<h1><a href="../cc-interface">Web Document Conformance Checker</a> |
<h1>]); |
455 |
(<em>beta</em>)</h1> |
$self->nl_text (q[WebHACC:Heading]); |
456 |
]); |
$self->html (q[</h1><script> insertNavSections () </script>]); |
457 |
} # html_header |
} # html_header |
458 |
|
|
459 |
|
sub generate_input_section ($$) { |
460 |
|
my ($out, $cgi) = @_; |
461 |
|
|
462 |
|
my $options = sub ($) { |
463 |
|
my $context = shift; |
464 |
|
|
465 |
|
$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'">]); |
466 |
|
$out->nl_text (q[Options]); |
467 |
|
$out->start_tag ('div'); |
468 |
|
|
469 |
|
if ($context eq 'url') { |
470 |
|
$out->start_tag ('p'); |
471 |
|
$out->start_tag ('label'); |
472 |
|
$out->start_tag ('input', type => 'checkbox', name => 'error-page', |
473 |
|
value => 1, |
474 |
|
($cgi->get_parameter ('error-page') |
475 |
|
? (checked => '') : ())); |
476 |
|
$out->nl_text ('Check error page'); |
477 |
|
$out->end_tag ('label'); |
478 |
|
} |
479 |
|
|
480 |
|
$out->start_tag ('p'); |
481 |
|
$out->start_tag ('label'); |
482 |
|
$out->nl_text (q[Content type]); |
483 |
|
$out->text (': '); |
484 |
|
$out->select ([ |
485 |
|
{value => '', label => 'As specified'}, |
486 |
|
{value => 'application/atom+xml'}, |
487 |
|
{value => 'application/xhtml+xml'}, |
488 |
|
{value => 'application/xml'}, |
489 |
|
{value => 'text/html'}, |
490 |
|
{value => 'text/xml'}, |
491 |
|
{value => 'text/css'}, |
492 |
|
{value => 'text/cache-manifest'}, |
493 |
|
{value => 'text/x-webidl'}, |
494 |
|
], name => 'i', selected => scalar $cgi->get_parameter ('i')); |
495 |
|
$out->end_tag ('label'); |
496 |
|
|
497 |
|
if ($context ne 'text') { |
498 |
|
$out->start_tag ('p'); |
499 |
|
$out->start_tag ('label'); |
500 |
|
$out->nl_text (q[Charset]); |
501 |
|
$out->text (q[: ]); |
502 |
|
$out->select ([ |
503 |
|
{value => '', label => 'As specified'}, |
504 |
|
{label => 'Japanese charsets', options => [ |
505 |
|
{value => 'Windows-31J'}, |
506 |
|
{value => 'Shift_JIS'}, |
507 |
|
{value => 'EUC-JP'}, |
508 |
|
{value => 'ISO-2022-JP'}, |
509 |
|
]}, |
510 |
|
{label => 'European charsets', options => [ |
511 |
|
{value => 'Windows-1252'}, |
512 |
|
{value => 'ISO-8859-1'}, |
513 |
|
{value => 'US-ASCII'}, |
514 |
|
]}, |
515 |
|
{label => 'Asian charsets', options => [ |
516 |
|
{value => 'Windows-874'}, |
517 |
|
{value => 'ISO-8859-11'}, |
518 |
|
{value => 'TIS-620'}, |
519 |
|
]}, |
520 |
|
{label => 'Unicode charsets', options => [ |
521 |
|
{value => 'UTF-8'}, |
522 |
|
{value => 'UTF-8n'}, |
523 |
|
]}, |
524 |
|
], name => 'charset', |
525 |
|
selected => scalar $cgi->get_parameter ('charset')); |
526 |
|
$out->end_tag ('label'); |
527 |
|
} |
528 |
|
|
529 |
|
if ($context eq 'text') { |
530 |
|
$out->start_tag ('p'); |
531 |
|
$out->start_tag ('label'); |
532 |
|
$out->nl_text ('Setting innerHTML'); |
533 |
|
$out->text (': '); |
534 |
|
$out->start_tag ('input', name => 'e', |
535 |
|
value => scalar $cgi->get_parameter ('e')); |
536 |
|
$out->end_tag ('label'); |
537 |
|
} |
538 |
|
|
539 |
|
$out->html (q[</div></div>]); |
540 |
|
}; # $options |
541 |
|
|
542 |
|
$out->start_section (id => 'input', title => 'Input'); |
543 |
|
$out->html (q[<script> insertNavSections ('input') </script>]); |
544 |
|
|
545 |
|
$out->start_section (id => 'input-url', title => 'By URL', |
546 |
|
parent_id => 'input'); |
547 |
|
$out->start_tag ('form', action => './#result-summary', |
548 |
|
'accept-charset' => 'utf-8', |
549 |
|
method => 'get'); |
550 |
|
$out->start_tag ('input', type => 'hidden', name => '_charset_'); |
551 |
|
|
552 |
|
$out->start_tag ('p'); |
553 |
|
$out->start_tag ('label'); |
554 |
|
$out->nl_text ('URL'); |
555 |
|
$out->text (': '); |
556 |
|
$out->start_tag ('input', |
557 |
|
name => 'uri', |
558 |
|
type => 'url', |
559 |
|
value => $cgi->get_parameter ('uri')); |
560 |
|
$out->end_tag ('label'); |
561 |
|
|
562 |
|
$out->start_tag ('p'); |
563 |
|
$out->start_tag ('button', type => 'submit'); |
564 |
|
$out->nl_text ('Check'); |
565 |
|
$out->end_tag ('button'); |
566 |
|
|
567 |
|
$options->('url'); |
568 |
|
|
569 |
|
$out->end_tag ('form'); |
570 |
|
$out->end_section; |
571 |
|
|
572 |
|
$out->end_tag ('fieldset'); |
573 |
|
|
574 |
|
## TODO: File upload |
575 |
|
|
576 |
|
$out->start_section (id => 'input-text', title => 'By direct input', |
577 |
|
parent_id => 'input'); |
578 |
|
$out->start_tag ('form', action => './#result-summary', |
579 |
|
'accept-charset' => 'utf-8', |
580 |
|
method => 'post'); |
581 |
|
$out->start_tag ('input', type => 'hidden', name => '_charset_'); |
582 |
|
|
583 |
|
$out->start_tag ('p'); |
584 |
|
$out->start_tag ('label'); |
585 |
|
$out->nl_text ('Document source to check'); |
586 |
|
$out->text (': '); |
587 |
|
$out->start_tag ('br'); |
588 |
|
$out->start_tag ('textarea', |
589 |
|
name => 's'); |
590 |
|
my $s = $cgi->get_parameter ('s'); |
591 |
|
$out->html ($htescape_value->($s)) if defined $s; |
592 |
|
$out->end_tag ('textarea'); |
593 |
|
$out->end_tag ('label'); |
594 |
|
|
595 |
|
$out->start_tag ('p'); |
596 |
|
$out->start_tag ('button', type => 'submit', |
597 |
|
onclick => 'form.method = form.s.value.length > 512 ? "post" : "get"'); |
598 |
|
$out->nl_text ('Check'); |
599 |
|
$out->end_tag ('button'); |
600 |
|
|
601 |
|
$options->('text'); |
602 |
|
|
603 |
|
$out->end_tag ('form'); |
604 |
|
$out->end_section; |
605 |
|
|
606 |
|
$out->script (q[ |
607 |
|
if (!document.webhaccNavigated && |
608 |
|
document.getElementsByTagName ('textarea')[0].value.length > 0) { |
609 |
|
showTab ('input-text'); |
610 |
|
document.webhaccNavigated = false; |
611 |
|
} |
612 |
|
]); |
613 |
|
|
614 |
|
$out->end_section; |
615 |
|
} # generate_input_section |
616 |
|
|
617 |
sub encode_url_component ($$) { |
sub encode_url_component ($$) { |
618 |
shift; |
shift; |
619 |
require Encode; |
require Encode; |