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