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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.21 - (show annotations) (download)
Thu Dec 11 03:22:57 2008 UTC (15 years, 11 months ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.20: +14 -10 lines
Error occurred while calculating annotation data.
++ ChangeLog	11 Dec 2008 03:20:10 -0000
2008-12-11  Wakaba  <wakaba@suika.fam.cx>

	* error-description-source.en.xml: Added descriptions for errors
	from Regexp::Parser and Regexp::Parser::JavaScript modules.

	* cc.cgi: Added support for JavaScript regular expressions.

++ html/WebHACC/Language/ChangeLog	11 Dec 2008 03:18:54 -0000
2008-12-11  Wakaba  <wakaba@suika.fam.cx>

	* RegExpJS.pm: New module.

++ html/WebHACC/ChangeLog	11 Dec 2008 03:22:42 -0000
2008-12-11  Wakaba  <wakaba@suika.fam.cx>

	* Output.pm (generate_input_section): Added support for JavaScript
	regular expressions.

2008-12-10  Wakaba  <wakaba@suika.fam.cx>

	* Result.pm: Added support for |valueref| parameter of an error.
	|pos_end| should point the (intercharacter) position where the
	highlighted substring ends, not the character before the position,
	otherwise empty substring cannot be represented well.

1 package WebHACC::Result;
2 use strict;
3
4 sub new ($) {
5 return bless {
6 global_status => 'conforming',
7 # or, 'should-error', 'non-conforming', 'uncertain'
8 subdoc_results => [],
9 }, shift;
10 } # new
11
12 sub output ($;$) {
13 if (@_ > 1) {
14 if (defined $_[1]) {
15 $_[0]->{output} = $_[1];
16 } else {
17 delete $_[0]->{output};
18 }
19 }
20
21 return $_[0]->{output};
22 } # output
23
24 sub parent_result ($;$) {
25 if (@_ > 1) {
26 if (defined $_[1]) {
27 $_[0]->{parent_result} = $_[1];
28 } else {
29 delete $_[0]->{parent_result};
30 }
31 }
32
33 return $_[0]->{parent_result};
34 } # parent_result
35
36 sub layer_applicable ($$) {
37 my $self = shift;
38 my $layer = shift;
39 $self->{layers}->{$layer}->{applicable} = 1;
40 } # layer_applicable
41
42 sub layer_uncertain ($$) {
43 my $self = shift;
44 my $layer = shift;
45 $self->{layers}->{$layer}->{uncertain} ||= 1;
46 $self->{layers}->{$layer}->{applicable} = 1;
47 $self->{global_status} = 'uncertain'
48 unless $self->{global_status} eq 'non-conforming';
49 } # layer_uncertain
50
51 sub add_error ($%) {
52 my ($self, %opt) = @_;
53
54 my $out = $self->output;
55 $out->has_error (1);
56
57 my $error_level = $opt{level};
58 if (not defined $error_level) {
59 $error_level = 'm'; ## NOTE: Unknown - an error of the implementation
60 } elsif ({
61 m => 1, s => 1, w => 1, i => 1, u => 1,
62 }->{$error_level}) {
63 #
64 } else {
65 $error_level = 'm'; ## NOTE: Unknown - an error of the implementation
66 }
67
68 my $error_layer = $opt{layer};
69 if (not defined $error_layer) {
70 $error_layer = 'syntax'; ## NOTE: Unknown - an error of the implementation
71 } elsif ({
72 transfer => 1,
73 encode => 1,
74 charset => 1,
75 syntax => 1,
76 structure => 1,
77 semantics => 1,
78 }->{$error_layer}) {
79 #
80 } else {
81 $error_layer = 'syntax'; ## NOTE: Unknown - an error of the implementation
82 }
83
84 my $class = qq[level-$error_level layer-$error_layer];
85
86 ## Line & column numbers (prepare values)
87
88 my $line;
89 my $column;
90
91 if (defined $opt{node}) {
92 $line = $opt{node}->get_user_data ('manakai_source_line');
93 if (defined $line) {
94 $column = $opt{node}->get_user_data ('manakai_source_column');
95 } elsif ($opt{node}->isa ('Message::IF::Node')) {
96 if ($opt{node}->node_type == $opt{node}->ATTRIBUTE_NODE) {
97 my $owner = $opt{node}->owner_element;
98 if ($owner) {
99 $line = $owner->get_user_data ('manakai_source_line');
100 $column = $owner->get_user_data ('manakai_source_column');
101 }
102 } else {
103 my $parent = $opt{node}->parent_node;
104 if ($parent) {
105 $line = $parent->get_user_data ('manakai_source_line');
106 $column = $parent->get_user_data ('manakai_source_column');
107 }
108 }
109 }
110 }
111 unless (defined $line) {
112 if (defined $opt{token} and defined $opt{token}->{line}) {
113 $line = $opt{token}->{line};
114 $column = $opt{token}->{column};
115 } elsif (defined $opt{line}) {
116 $line = $opt{line};
117 $column = $opt{column};
118 }
119 }
120 $line += $opt{line_diff} || 0 if defined $line;
121 $column += $opt{column_diff} || 0 if defined $column;
122 $line = $line - 1 || 1
123 if defined $line and not (defined $column and $column > 0);
124
125 $out->start_tag ('dt', class => $class,
126 'data-type' => $opt{type},
127 'data-level' => $error_level,
128 'data-layer' => $error_layer,
129 ($line ? ('data-line' => $line) : ()),
130 ($column ? ('data-column' => $column) : ()));
131 my $has_location;
132
133 ## URL
134
135 if (defined $opt{url}) {
136 $out->url ($opt{url});
137 $has_location = 1;
138 }
139
140 ## Line & column numbers (real output)
141
142 if (defined $line) {
143 if (defined $column and $column > 0) {
144 $out->xref ('Line #', text => $line, target => 'line-' . $line);
145 $out->text (' ');
146 $out->nl_text ('column #', text => $column);
147 } else {
148 $out->xref ('Line #', text => $line, target => 'line-' . $line);
149 }
150 $has_location = 1;
151 }
152
153 ## Node path
154
155 if (defined $opt{node}) {
156 $out->html (' ');
157 $out->node_link ($opt{node});
158 $has_location = 1;
159 }
160
161 if (defined $opt{index}) {
162 if ($opt{index_has_link}) {
163 $out->html (' ');
164 $out->xref ('Index #', text => (0+$opt{index}),
165 target => 'index-' . (0+$opt{index}));
166 } else {
167 $out->html (' ');
168 $out->nl_text ('Index #', text => (0+$opt{index}));
169 }
170 $has_location = 1;
171 }
172
173 if (not defined $opt{valueref} and defined $opt{value}) {
174 $opt{valueref} = \($opt{value});
175 }
176
177 if (defined $opt{valueref}) {
178 $out->html (' ');
179 if (defined $opt{pos_start}) {
180 $out->start_tag ('code');
181 $out->text (substr ${$opt{valueref}}, 0, $opt{pos_start});
182 $out->start_tag ('mark');
183 $out->text (substr ${$opt{valueref}}, $opt{pos_start},
184 $opt{pos_end} - $opt{pos_start});
185 $out->end_tag ('mark');
186 $out->text (substr ${$opt{valueref}}, $opt{pos_end})
187 if $opt{pos_end} < length ${$opt{valueref}};
188 $out->end_tag ('code');
189 } elsif ($opt{value_mark_end}) {
190 $out->start_tag ('code');
191 $out->text (${$opt{valueref}});
192 $out->start_tag ('mark');
193 $out->end_tag ('mark');
194 $out->end_tag ('code');
195 } elsif (defined $opt{value_mark}) {
196 $out->start_tag ('code');
197 for (split /($opt{value_mark})/, ${$opt{valueref}}) {
198 if (/$opt{value_mark}/) {
199 $out->start_tag ('mark');
200 $out->text ($_);
201 $out->end_tag ('mark');
202 } else {
203 $out->text ($_);
204 }
205 }
206 $out->end_tag ('code');
207 } else {
208 $out->code (${$opt{valueref}});
209 }
210 $has_location = 1;
211 }
212
213 unless ($has_location) {
214 if (defined $opt{input}) {
215 if (defined $opt{input}->{container_node}) {
216 my $original_input = $out->input;
217 $out->input ($opt{input}->{parent_input});
218 $out->node_link ($opt{input}->{container_node});
219 $out->input ($original_input);
220 $has_location = 1;
221 } elsif (defined $opt{input}->{request_uri}) {
222 $out->url ($opt{input}->{request_uri});
223 $has_location = 1;
224 } elsif (defined $opt{input}->url) {
225 $out->url ($opt{input}->url);
226 $has_location = 1;
227 }
228 }
229
230 unless ($has_location) {
231 $out->nl_text ('Unknown location');
232 }
233 }
234
235 $out->start_tag ('dd', class => $class);
236
237 ## Error level
238 $out->nl_text ('Error level ' . $error_level);
239 $out->text (': ');
240
241 ## Error message
242 my $error_type_text = $opt{type};
243 $out->nl_text ($error_type_text, node => $opt{node}, text => $opt{text},
244 value => ${$opt{valueref} or \''}, char => $opt{char},
245 octets => $opt{octets});
246
247 ## Link to a long description
248
249 my $fragment = $opt{type};
250 $fragment =~ tr/ /-/;
251 $fragment = $out->encode_url_component ($fragment);
252 $out->text (' [');
253 $out->start_tag ('a', href => '../error-description#' . $fragment,
254 rel => 'help');
255 $out->nl_text ('Description');
256 $out->end_tag ('a');
257 $out->text (']');
258
259 if ($error_level eq 'm') {
260 $self->{layers}->{$error_layer}->{must}++;
261 $self->{global_status} = 'non-conforming';
262 } elsif ($error_level eq 's') {
263 $self->{layers}->{$error_layer}->{should}++;
264 $self->{global_status} = 'should-error'
265 unless {'non-conforming' => 1,
266 uncertain => 1}->{$self->{global_status}};
267 } elsif ($error_level eq 'w') {
268 $self->{layers}->{$error_layer}->{warning}++;
269 } elsif ($error_level eq 'u') {
270 $self->{layers}->{$error_layer}->{uncertain}++;
271 $self->{global_status} = 'uncertain'
272 unless $self->{global_status} eq 'non-conforming';
273 } elsif ($error_level eq 'i') {
274 $self->{layers}->{$error_layer}->{info}++;
275 }
276 } # add_error
277
278 sub generate_result_section ($) {
279 my $self = shift;
280
281 my $result = $self;
282
283 my $out = $result->output;
284
285 $out->start_section (role => 'result');
286
287 my $para_class = {
288 'conforming' => 'result-para no-error',
289 'should-error' => 'result-para should-errors',
290 'non-conforming' => 'result-para must-errors',
291 'uncertain' => 'result-para uncertain',
292 }->{$self->{global_status}};
293 $out->start_tag ('p', class => $para_class);
294 $out->nl_text ('Conformance is ' . $self->{global_status});
295 $out->end_tag ('p');
296
297 $out->html (qq[<table>
298 <colgroup><col><col><colgroup><col><col><col><col><colgroup><col>
299 <thead>
300 <tr><th scope=col colspan=2>]);
301 for ('Error level m', 'Error level s', 'Error level w',
302 'Error level i', 'Score') {
303 $out->start_tag ('th');
304 $out->nl_text ($_);
305 }
306
307 my $maindoc_status = {must => 0, should => 0, warning => 0, info => 0,
308 uncertain => 0, applicable => 1};
309 my $subdocs_status = {must => 0, should => 0, warning => 0, info => 0,
310 uncertain => 0, applicable => 1};
311 my $global_status = {must => 0, should => 0, warning => 0, info => 0,
312 uncertain => 0, applicable => 1};
313
314 my $score_unit = 2;
315
316 my @row = (
317 sub {
318 $out->start_tag ('tbody');
319 $out->start_tag ('tr');
320 $out->start_tag ('th', colspan => 7, scope => 'col');
321 $out->nl_text ('Main document');
322 },
323 {label => 'Transfer L.', status => $self->{layers}->{transfer},
324 target => 'transfer-errors', score_base => 20,
325 parent_status => $maindoc_status},
326 {label => 'Encode L.', status => $self->{layers}->{encode},
327 target => 'parse-errors', score_base => 10,
328 parent_status => $maindoc_status},
329 {label => 'Char L.', status => $self->{layers}->{charset},
330 target => 'parse-errors', score_base => 10,
331 parent_status => $maindoc_status},
332 ## TODO: char semantics layer
333 {label => 'Syntax L.', status => $self->{layers}->{syntax},
334 target => 'parse-errors', score_base => 20,
335 parent_status => $maindoc_status},
336 {label => 'Structure L.', status => $self->{layers}->{structure},
337 target => 'document-errors', score_base => 20,
338 parent_status => $maindoc_status},
339 {label => 'Semantics L.', status => $self->{layers}->{semantics},
340 score_base => 20,
341 parent_status => $maindoc_status},
342 );
343
344 if (@{$self->{subdoc_results}}) {
345 push @row, {label => 'Subtotal', status => $maindoc_status,
346 score_base => 100,
347 parent_status => $global_status, is_total => 1};
348 push @row, sub {
349 $out->start_tag ('tbody');
350 $out->start_tag ('tr');
351 $out->start_tag ('th', colspan => 7, scope => 'col');
352 $out->nl_text ('Subdocuments');
353 };
354 for (@{$self->{subdoc_results}}) {
355 push @row, {label => '#' . $_->{input}->full_subdocument_index,
356 status => $_,
357 target => $_->{input}->id_prefix . 'result-summary',
358 score_base => 100, parent_status => $subdocs_status};
359 }
360 push @row, {label => 'Subtotal', status => $subdocs_status,
361 score_base => 100 * @{$self->{subdoc_results}},
362 parent_status => $global_status, is_total => 1};
363 } else {
364 $global_status = $maindoc_status;
365 }
366
367 push @row, sub {
368 $out->start_tag ('tfoot');
369 };
370 push @row, {label => 'Total', status => $global_status,
371 score_base => 100 * (@{$self->{subdoc_results}} + 1),
372 parent_status => {}, is_total => 1};
373
374 for my $x (@row) {
375 if (ref $x eq 'CODE') {
376 $x->();
377 next;
378 }
379
380 $x->{parent_status}->{$_} += $x->{status}->{$_}
381 for qw/must should warning info uncertain/;
382
383 my $row_class = $x->{status}->{uncertain} ? 'uncertain' : '';
384 $row_class .= ' total' if $x->{is_total};
385 $out->start_tag ('tr', class => $row_class);
386 my $uncertain = $x->{status}->{uncertain} ? '?' : '';
387
388 $out->start_tag ('td', class => 'subrow') unless $x->{is_total};
389
390 ## Layer name
391 $out->start_tag ('th', colspan => $x->{is_total} ? 2 : 1,
392 scope => 'row');
393 if (defined $x->{target} and
394 ($x->{status}->{must} or $x->{status}->{should} or
395 $x->{status}->{warning} or $x->{status}->{info} or
396 $x->{status}->{uncertain})) {
397 $out->xref ($x->{label}, target => $x->{target});
398 } else {
399 $out->nl_text ($x->{label});
400 }
401
402 ## MUST-level errors
403 $out->start_tag ('td', class => $x->{status}->{must} ? 'must-errors' : '');
404 if ($x->{status}->{applicable}) {
405 $out->text (($x->{status}->{must} or 0) . $uncertain);
406 } else {
407 $out->nl_text ('N/A');
408 }
409
410 ## SHOULD-level errors
411 $out->start_tag ('td',
412 class => $x->{status}->{should} ? 'should-errors' : '');
413 if ($x->{status}->{applicable}) {
414 $out->text (($x->{status}->{should} or 0) . $uncertain);
415 } else {
416 $out->nl_text ('N/A');
417 }
418
419 ## Warnings
420 $out->start_tag ('td', class => $x->{status}->{warning} ? 'warnings' : '');
421 if ($x->{status}->{applicable}) {
422 $out->text (($x->{status}->{warning} or 0) . $uncertain);
423 } else {
424 $out->nl_text ('N/A');
425 }
426
427 ## Informations
428 $out->start_tag ('td', class => $x->{status}->{info} ? 'infos' : '');
429 if ($x->{status}->{applicable}) {
430 $out->text (($x->{status}->{info} or 0) . $uncertain);
431 } else {
432 $out->nl_text ('N/A');
433 }
434
435 ## Score
436 $out->start_tag ('td',
437 class => $x->{status}->{must} ? 'score must-errors' :
438 $x->{status}->{should} ? 'score should-errors' :
439 'score');
440
441 my $max_score = $x->{score_base};
442 $max_score -= $x->{status}->{must} * $score_unit;
443 my $min_score = $max_score;
444 $min_score -= $x->{status}->{should} * $score_unit;
445
446 $out->start_tag ('strong');
447 if ($x->{status}->{uncertain}) {
448 $out->html ('&#x2212;&#x221E; '); # negative inifinity
449 $out->nl_text ('...');
450 $out->html ($max_score < 0 ?
451 ' &#x2212;' . substr ($max_score, 1) : ' ' . $max_score);
452 } elsif ($min_score != $max_score) {
453 $out->html ($min_score < 0 ?
454 '&#x2212;' . substr ($min_score, 1) . ' ': $min_score . ' ');
455 $out->nl_text ('...');
456 $out->html ($max_score < 0 ?
457 ' &#x2212;' . substr ($max_score, 1) : ' ' . $max_score);
458 } else {
459 $out->html ($max_score < 0 ?
460 '&#x2212;' . substr ($max_score, 1) : $max_score);
461 }
462 $out->end_tag ('strong');
463
464 $out->text (' / ' . $x->{score_base});
465 }
466
467 $out->end_tag ('table');
468
469 my $parent = $self->parent_result;
470 if ($parent) {
471 $global_status->{input} = $out->input;
472 push @{$parent->{subdoc_results}}, $global_status;
473 }
474
475 $out->nl_text ('This checker is work in progress.');
476 $out->end_section;
477 } # generate_result_section
478
479 1;

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24