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'; |
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}] |
push @{$self->{nav}}, |
142 |
|
[$id => $opt{short_title} || $opt{title} => $opt{text}] |
143 |
if $self->{section_rank} == 2; |
if $self->{section_rank} == 2; |
144 |
} |
} |
145 |
my $section_rank = $self->{section_rank}; |
my $section_rank = $self->{section_rank}; |
146 |
$section_rank = 6 if $section_rank > 6; |
$section_rank = 6 if $section_rank > 6; |
147 |
$self->html ('><h' . $section_rank . '>' . |
$self->html ('><h' . $section_rank . '>'); |
148 |
$htescape->($opt{title}) . |
$self->nl_text ($opt{title}, text => $opt{text}); |
149 |
'</h' . $section_rank . '>'); |
$self->html ('</h' . $section_rank . '>'); |
150 |
} # start_section |
} # start_section |
151 |
|
|
152 |
sub end_section ($) { |
sub end_section ($) { |
199 |
my $self = shift; |
my $self = shift; |
200 |
|
|
201 |
$self->script (q[addSourceToParseErrorList ('] . $self->input->id_prefix . |
$self->script (q[addSourceToParseErrorList ('] . $self->input->id_prefix . |
202 |
q[', '] . shift . q[')]); |
q[', '] . shift () . q[')]); |
203 |
} # add_source_to_parse_error_list |
} # add_source_to_parse_error_list |
204 |
|
|
205 |
sub start_code_block ($) { |
sub start_code_block ($) { |
227 |
sub dt ($$;%) { |
sub dt ($$;%) { |
228 |
my ($self, $content, %opt) = @_; |
my ($self, $content, %opt) = @_; |
229 |
$self->start_tag ('dt', %opt); |
$self->start_tag ('dt', %opt); |
230 |
$self->text ($content); |
$self->nl_text ($content, text => $opt{text}); |
231 |
} # dt |
} # dt |
232 |
|
|
233 |
|
sub select ($$%) { |
234 |
|
my ($self, $options, %opt) = @_; |
235 |
|
|
236 |
|
my $selected = $opt{selected}; |
237 |
|
delete $opt{selected}; |
238 |
|
|
239 |
|
$self->start_tag ('select', %opt); |
240 |
|
|
241 |
|
my @options = @$options; |
242 |
|
while (@options) { |
243 |
|
my $opt = shift @options; |
244 |
|
if ($opt->{options}) { |
245 |
|
$self->html ('<optgroup label="'); |
246 |
|
$self->nl_text ($opt->{label}); |
247 |
|
$self->html ('">'); |
248 |
|
unshift @options, @{$opt->{options}}, {end_options => 1}; |
249 |
|
} elsif ($opt->{end_options}) { |
250 |
|
$self->end_tag ('optgroup'); |
251 |
|
} else { |
252 |
|
$self->start_tag ('option', value => $opt->{value}, |
253 |
|
((defined $selected and $opt->{value} eq $selected) |
254 |
|
? (selected => '') : ())); |
255 |
|
$self->nl_text (defined $opt->{label} ? $opt->{label} : $opt->{value}); |
256 |
|
} |
257 |
|
} |
258 |
|
|
259 |
|
$self->end_tag ('select'); |
260 |
|
} # select |
261 |
|
|
262 |
sub link ($$%) { |
sub link ($$%) { |
263 |
my ($self, $content, %opt) = @_; |
my ($self, $content, %opt) = @_; |
264 |
$self->start_tag ('a', %opt, href => $opt{url}); |
$self->start_tag ('a', %opt, href => $opt{url}); |
269 |
sub xref ($$%) { |
sub xref ($$%) { |
270 |
my ($self, $content, %opt) = @_; |
my ($self, $content, %opt) = @_; |
271 |
$self->html ('<a href="#' . $htescape->($self->input->id_prefix . $opt{target}) . '">'); |
$self->html ('<a href="#' . $htescape->($self->input->id_prefix . $opt{target}) . '">'); |
272 |
$self->text ($content); |
$self->nl_text ($content, text => $opt{text}); |
273 |
$self->html ('</a>'); |
$self->html ('</a>'); |
274 |
} # xref |
} # xref |
275 |
|
|
279 |
$self->link ($content, %opt); |
$self->link ($content, %opt); |
280 |
} # link_to_webhacc |
} # link_to_webhacc |
281 |
|
|
|
|
|
282 |
my $get_node_path = sub ($) { |
my $get_node_path = sub ($) { |
283 |
my $node = shift; |
my $node = shift; |
284 |
my @r; |
my @r; |
306 |
return join '/', @r; |
return join '/', @r; |
307 |
}; # $get_node_path |
}; # $get_node_path |
308 |
|
|
309 |
|
my $get_object_path = sub ($) { |
310 |
|
my $node = shift; |
311 |
|
my @r; |
312 |
|
while (defined $node) { |
313 |
|
my $ref = ref $node; |
314 |
|
$ref =~ /([^:]+)$/; |
315 |
|
my $rs = $1; |
316 |
|
my $node_name = $node->node_name; |
317 |
|
if (defined $node_name) { |
318 |
|
$rs .= ' <code>' . $htescape->($node_name) . '</code>'; |
319 |
|
} |
320 |
|
$node = undef; |
321 |
|
unshift @r, $rs; |
322 |
|
} |
323 |
|
return join '/', @r; |
324 |
|
}; # $get_object_path |
325 |
|
|
326 |
sub node_link ($$) { |
sub node_link ($$) { |
327 |
my ($self, $node) = @_; |
my ($self, $node) = @_; |
328 |
$self->xref ($get_node_path->($node), target => 'node-' . refaddr $node); |
if ($node->isa ('Message::IF::Node')) { |
329 |
|
$self->xref ($get_node_path->($node), target => 'node-' . refaddr $node); |
330 |
|
} else { |
331 |
|
$self->html ($get_object_path->($node)); |
332 |
|
} |
333 |
} # node_link |
} # node_link |
334 |
|
|
335 |
|
{ |
336 |
|
my $Msg = {}; |
337 |
|
|
338 |
|
sub load_text_catalog ($$) { |
339 |
|
my $self = shift; |
340 |
|
|
341 |
|
my $lang = shift; # MUST be a canonical lang name |
342 |
|
my $file_name = qq[cc-msg.$lang.txt]; |
343 |
|
$lang = 'en' unless -f $file_name; |
344 |
|
$self->{primary_language} = $lang; |
345 |
|
|
346 |
|
open my $file, '<:utf8', $file_name or die "$0: $file_name: $!"; |
347 |
|
while (<$file>) { |
348 |
|
if (s/^([^;]+);([^;]*);//) { |
349 |
|
my ($type, $cls, $msg) = ($1, $2, $_); |
350 |
|
$msg =~ tr/\x0D\x0A//d; |
351 |
|
$Msg->{$type} = [$cls, $msg]; |
352 |
|
} |
353 |
|
} |
354 |
|
} # load_text_catalog |
355 |
|
|
356 |
|
sub nl_text ($$;%) { |
357 |
|
my ($self, $type, %opt) = @_; |
358 |
|
my $node = $opt{node}; |
359 |
|
|
360 |
|
my @arg; |
361 |
|
{ |
362 |
|
if (defined $Msg->{$type}) { |
363 |
|
my $msg = $Msg->{$type}->[1]; |
364 |
|
if ($msg =~ /<var>/) { |
365 |
|
$msg =~ s{<var>\$([0-9]+)</var>}{ |
366 |
|
defined $arg[$1] ? $htescape->($arg[$1]) : '(undef)'; |
367 |
|
}ge; |
368 |
|
$msg =~ s{<var>{\@([A-Za-z0-9:_.-]+)}</var>}{ |
369 |
|
UNIVERSAL::can ($node, 'get_attribute_ns') |
370 |
|
? $htescape->($node->get_attribute_ns (undef, $1)) : '' |
371 |
|
}ge; |
372 |
|
$msg =~ s{<var>{\@}</var>}{ |
373 |
|
UNIVERSAL::can ($node, 'value') ? $htescape->($node->value) : '' |
374 |
|
}ge; |
375 |
|
$msg =~ s{<var>{text}</var>}{ |
376 |
|
defined $opt{text} ? $htescape->($opt{text}) : '' |
377 |
|
}ge; |
378 |
|
$msg =~ s{<var>{local-name}</var>}{ |
379 |
|
UNIVERSAL::can ($node, 'manakai_local_name') |
380 |
|
? $htescape->($node->manakai_local_name) : '' |
381 |
|
}ge; |
382 |
|
$msg =~ s{<var>{element-local-name}</var>}{ |
383 |
|
(UNIVERSAL::can ($node, 'owner_element') and |
384 |
|
$node->owner_element) |
385 |
|
? $htescape->($node->owner_element->manakai_local_name) : '' |
386 |
|
}ge; |
387 |
|
} |
388 |
|
$self->html ($msg); |
389 |
|
return; |
390 |
|
} elsif ($type =~ s/:([^:]*)$//) { |
391 |
|
unshift @arg, $1; |
392 |
|
redo; |
393 |
|
} |
394 |
|
} |
395 |
|
$self->text ($type); |
396 |
|
} # nl_text |
397 |
|
|
398 |
|
} |
399 |
|
|
400 |
sub nav_list ($) { |
sub nav_list ($) { |
401 |
my $self = shift; |
my $self = shift; |
402 |
$self->html (q[<ul class="navigation" id="nav-items">]); |
$self->html (q[<ul class="navigation" id="nav-items">]); |
403 |
for (@{$self->{nav}}) { |
for (@{$self->{nav}}) { |
404 |
$self->html (qq[<li><a href="#@{[$htescape->($_->[0])]}">@{[$htescape->($_->[1])]}</a>]); |
$self->html (qq[<li><a href="#@{[$htescape->($_->[0])]}">]); |
405 |
|
$self->nl_text ($_->[1], text => $_->[2]); |
406 |
|
$self->html ('</a>'); |
407 |
} |
} |
408 |
$self->html ('</ul>'); |
$self->html ('</ul>'); |
409 |
} # nav_list |
} # nav_list |
423 |
|
|
424 |
sub html_header ($) { |
sub html_header ($) { |
425 |
my $self = shift; |
my $self = shift; |
426 |
$self->html (q[<!DOCTYPE html> |
$self->html (q[<!DOCTYPE html>]); |
427 |
<html lang="en"> |
$self->start_tag ('html', lang => $self->{primary_language}); |
428 |
<head> |
$self->html (q[<head><title>]); |
429 |
<title>WebHACC (BETA) Result</title> |
$self->nl_text (q[WebHACC:Title]); |
430 |
|
$self->html (q[</title> |
431 |
<link rel="stylesheet" href="../cc-style.css" type="text/css"> |
<link rel="stylesheet" href="../cc-style.css" type="text/css"> |
432 |
|
<script src="../cc-script.js"></script> |
433 |
</head> |
</head> |
434 |
<body> |
<body> |
435 |
<h1><a href="../cc-interface"><abbr title="Web Hypertext Application Conformance Checker (BETA)"><img src="../icons/title" alt="WebHACC"></abbr></a></h1> |
<h1>]); |
436 |
]); |
$self->nl_text (q[WebHACC:Heading]); |
437 |
|
$self->html ('</h1>'); |
438 |
} # html_header |
} # html_header |
439 |
|
|
440 |
|
sub generate_input_section ($$) { |
441 |
|
my ($out, $cgi) = @_; |
442 |
|
|
443 |
|
my $options = sub ($) { |
444 |
|
my $context = shift; |
445 |
|
|
446 |
|
$out->html (q[<div class=details><p class=legend onclick="nextSibling.style.display = nextSibling.style.display == 'none' ? 'block' : 'none'">]); |
447 |
|
$out->nl_text (q[Options]); |
448 |
|
$out->start_tag ('div'); |
449 |
|
|
450 |
|
if ($context eq 'url') { |
451 |
|
$out->start_tag ('p'); |
452 |
|
$out->start_tag ('label'); |
453 |
|
$out->start_tag ('input', type => 'checkbox', name => 'error-page', |
454 |
|
value => 1, |
455 |
|
($cgi->get_parameter ('error-page') |
456 |
|
? (checked => '') : ())); |
457 |
|
$out->nl_text ('Check error page'); |
458 |
|
$out->end_tag ('label'); |
459 |
|
} |
460 |
|
|
461 |
|
$out->start_tag ('p'); |
462 |
|
$out->start_tag ('label'); |
463 |
|
$out->nl_text (q[Content type]); |
464 |
|
$out->text (': '); |
465 |
|
$out->select ([ |
466 |
|
{value => '', label => 'As specified'}, |
467 |
|
{value => 'application/atom+xml'}, |
468 |
|
{value => 'application/xhtml+xml'}, |
469 |
|
{value => 'application/xml'}, |
470 |
|
{value => 'text/html'}, |
471 |
|
{value => 'text/xml'}, |
472 |
|
{value => 'text/css'}, |
473 |
|
{value => 'text/cache-manifest'}, |
474 |
|
{value => 'text/x-webidl'}, |
475 |
|
], name => 'i', selected => scalar $cgi->get_parameter ('i')); |
476 |
|
$out->end_tag ('label'); |
477 |
|
|
478 |
|
if ($context ne 'text') { |
479 |
|
$out->start_tag ('p'); |
480 |
|
$out->start_tag ('label'); |
481 |
|
$out->nl_text (q[Charset]); |
482 |
|
$out->text (q[: ]); |
483 |
|
$out->select ([ |
484 |
|
{value => '', label => 'As specified'}, |
485 |
|
{label => 'Japanese charsets', options => [ |
486 |
|
{value => 'Windows-31J'}, |
487 |
|
{value => 'Shift_JIS'}, |
488 |
|
{value => 'EUC-JP'}, |
489 |
|
{value => 'ISO-2022-JP'}, |
490 |
|
]}, |
491 |
|
{label => 'European charsets', options => [ |
492 |
|
{value => 'Windows-1252'}, |
493 |
|
{value => 'ISO-8859-1'}, |
494 |
|
{value => 'US-ASCII'}, |
495 |
|
]}, |
496 |
|
{label => 'Asian charsets', options => [ |
497 |
|
{value => 'Windows-874'}, |
498 |
|
{value => 'ISO-8859-11'}, |
499 |
|
{value => 'TIS-620'}, |
500 |
|
]}, |
501 |
|
{label => 'Unicode charsets', options => [ |
502 |
|
{value => 'UTF-8'}, |
503 |
|
{value => 'UTF-8n'}, |
504 |
|
]}, |
505 |
|
], name => 'charset', |
506 |
|
selected => scalar $cgi->get_parameter ('charset')); |
507 |
|
$out->end_tag ('label'); |
508 |
|
} |
509 |
|
|
510 |
|
if ($context eq 'text') { |
511 |
|
$out->start_tag ('p'); |
512 |
|
$out->start_tag ('label'); |
513 |
|
$out->nl_text ('Setting innerHTML'); |
514 |
|
$out->text (': '); |
515 |
|
$out->start_tag ('input', name => 'e', |
516 |
|
value => scalar $cgi->get_parameter ('e')); |
517 |
|
$out->end_tag ('label'); |
518 |
|
} |
519 |
|
|
520 |
|
$out->html (q[</div></div>]); |
521 |
|
}; # $options |
522 |
|
|
523 |
|
$out->start_section (id => 'input', title => 'Input'); |
524 |
|
|
525 |
|
$out->start_section (id => 'input-url', title => 'By URL'); |
526 |
|
$out->start_tag ('form', action => './#result-summary', |
527 |
|
'accept-charset' => 'utf-8', |
528 |
|
method => 'get'); |
529 |
|
$out->start_tag ('input', type => 'hidden', name => '_charset_'); |
530 |
|
|
531 |
|
$out->start_tag ('p'); |
532 |
|
$out->start_tag ('label'); |
533 |
|
$out->nl_text ('URL'); |
534 |
|
$out->text (': '); |
535 |
|
$out->start_tag ('input', |
536 |
|
name => 'uri', |
537 |
|
type => 'url', |
538 |
|
value => $cgi->get_parameter ('uri')); |
539 |
|
$out->end_tag ('label'); |
540 |
|
|
541 |
|
$options->('url'); |
542 |
|
|
543 |
|
$out->start_tag ('p'); |
544 |
|
$out->start_tag ('button', type => 'submit'); |
545 |
|
$out->nl_text ('Check'); |
546 |
|
|
547 |
|
$out->end_tag ('form'); |
548 |
|
$out->end_section; |
549 |
|
|
550 |
|
$out->end_tag ('fieldset'); |
551 |
|
|
552 |
|
## TODO: File upload |
553 |
|
|
554 |
|
$out->start_section (id => 'input-text', title => 'By direct input'); |
555 |
|
$out->start_tag ('form', action => './#result-summary', |
556 |
|
'accept-charset' => 'utf-8', |
557 |
|
method => 'post'); |
558 |
|
$out->start_tag ('input', type => 'hidden', name => '_charset_'); |
559 |
|
|
560 |
|
$out->start_tag ('p'); |
561 |
|
$out->start_tag ('label'); |
562 |
|
$out->nl_text ('Document source to check'); |
563 |
|
$out->text (': '); |
564 |
|
$out->start_tag ('br'); |
565 |
|
$out->start_tag ('textarea', |
566 |
|
name => 's'); |
567 |
|
my $s = $cgi->get_parameter ('s'); |
568 |
|
$out->html ($htescape_value->($s)) if defined $s; |
569 |
|
$out->end_tag ('textarea'); |
570 |
|
$out->end_tag ('label'); |
571 |
|
|
572 |
|
$options->('text'); |
573 |
|
|
574 |
|
$out->start_tag ('p'); |
575 |
|
$out->start_tag ('button', type => 'submit', |
576 |
|
onclick => 'form.method = form.s.value.length > 512 ? "post" : "get"'); |
577 |
|
$out->nl_text ('Check'); |
578 |
|
$out->end_tag ('button'); |
579 |
|
|
580 |
|
$out->end_tag ('form'); |
581 |
|
$out->end_section; |
582 |
|
|
583 |
|
$out->end_section; |
584 |
|
} # generate_input_section |
585 |
|
|
586 |
sub encode_url_component ($$) { |
sub encode_url_component ($$) { |
587 |
shift; |
shift; |
588 |
require Encode; |
require Encode; |