/[suikacvs]/markup/html/whatpm/Whatpm/ContentChecker.pm
Suika

Contents of /markup/html/whatpm/Whatpm/ContentChecker.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.93 - (hide annotations) (download)
Tue Sep 16 14:41:37 2008 UTC (16 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.92: +3 -2 lines
++ whatpm/Whatpm/ChangeLog	16 Sep 2008 14:41:24 -0000
	* Makefile: WebIDL.html added.

	* WebIDL.pod: New documentation.

2008-09-16  Wakaba  <wakaba@suika.fam.cx>

1 wakaba 1.1 package Whatpm::ContentChecker;
2     use strict;
3 wakaba 1.93 our $VERSION=do{my @r=(q$Revision: 1.92 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4 wakaba 1.1
5 wakaba 1.18 require Whatpm::URIChecker;
6    
7 wakaba 1.13 ## ISSUE: How XML and XML Namespaces conformance can (or cannot)
8     ## be applied to an in-memory representation (i.e. DOM)?
9    
10 wakaba 1.50 ## TODO: Conformance of an HTML document with non-html root element.
11    
12 wakaba 1.70 ## Stability
13 wakaba 1.67 sub FEATURE_STATUS_REC () { 0b1 } ## Interoperable standard
14     sub FEATURE_STATUS_CR () { 0b10 } ## Call for implementation
15     sub FEATURE_STATUS_LC () { 0b100 } ## Last call for comments
16     sub FEATURE_STATUS_WD () { 0b1000 } ## Working or editor's draft
17    
18 wakaba 1.70 ## Deprecated
19     sub FEATURE_DEPRECATED_SHOULD () { 0b100000 } ## SHOULD-level
20     sub FEATURE_DEPRECATED_INFO () { 0b1000000 } ## Does not affect conformance
21    
22     ## Conformance
23     sub FEATURE_ALLOWED () { 0b10000 }
24    
25 wakaba 1.42 my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
26 wakaba 1.9 my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
27     my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
28    
29 wakaba 1.42 my $Namespace = {
30 wakaba 1.79 '' => {loaded => 1},
31 wakaba 1.43 q<http://www.w3.org/2005/Atom> => {module => 'Whatpm::ContentChecker::Atom'},
32 wakaba 1.72 q<http://purl.org/syndication/history/1.0>
33     => {module => 'Whatpm::ContentChecker::Atom'},
34     q<http://purl.org/syndication/threading/1.0>
35     => {module => 'Whatpm::ContentChecker::Atom'},
36 wakaba 1.42 $HTML_NS => {module => 'Whatpm::ContentChecker::HTML'},
37     $XML_NS => {loaded => 1},
38     $XMLNS_NS => {loaded => 1},
39 wakaba 1.73 q<http://www.w3.org/1999/02/22-rdf-syntax-ns#> => {loaded => 1},
40 wakaba 1.42 };
41    
42 wakaba 1.79 sub load_ns_module ($) {
43     my $nsuri = shift; # namespace URI or ''
44     unless ($Namespace->{$nsuri}->{loaded}) {
45     if ($Namespace->{$nsuri}->{module}) {
46     eval qq{ require $Namespace->{$nsuri}->{module} } or die $@;
47     } else {
48     $Namespace->{$nsuri}->{loaded} = 1;
49     }
50     }
51     } # load_ns_module
52    
53 wakaba 1.42 our $AttrChecker = {
54 wakaba 1.9 $XML_NS => {
55 wakaba 1.13 space => sub {
56     my ($self, $attr) = @_;
57     my $value = $attr->value;
58     if ($value eq 'default' or $value eq 'preserve') {
59     #
60     } else {
61     ## NOTE: An XML "error"
62 wakaba 1.83 $self->{onerror}->(node => $attr, level => $self->{level}->{xml_error},
63 wakaba 1.33 type => 'invalid attribute value');
64 wakaba 1.13 }
65     },
66     lang => sub {
67 wakaba 1.35 my ($self, $attr) = @_;
68 wakaba 1.47 my $value = $attr->value;
69     if ($value eq '') {
70     #
71     } else {
72     require Whatpm::LangTag;
73     Whatpm::LangTag->check_rfc3066_language_tag ($value, sub {
74 wakaba 1.83 $self->{onerror}->(@_, node => $attr);
75 wakaba 1.85 }, $self->{level});
76 wakaba 1.47 }
77    
78 wakaba 1.13 ## NOTE: "The values of the attribute are language identifiers
79     ## as defined by [IETF RFC 3066], Tags for the Identification
80     ## of Languages, or its successor; in addition, the empty string
81     ## may be specified." ("may" in lower case)
82 wakaba 1.47 ## NOTE: Is an RFC 3066-valid (but RFC 4647-invalid) language tag
83     ## allowed today?
84    
85     ## TODO: test data
86    
87 wakaba 1.89 my $nsuri = $attr->owner_element->namespace_uri;
88     if (defined $nsuri and $nsuri eq $HTML_NS) {
89     my $lang_attr = $attr->owner_element->get_attribute_node_ns
90     (undef, 'lang');
91     if ($lang_attr) {
92     my $lang_attr_value = $lang_attr->value;
93     $lang_attr_value =~ tr/A-Z/a-z/; ## ASCII case-insensitive
94     my $value = $value;
95     $value =~ tr/A-Z/a-z/; ## ASCII case-insensitive
96     if ($lang_attr_value ne $value) {
97     ## NOTE: HTML5 Section "The |lang| and |xml:lang| attributes"
98     $self->{onerror}->(node => $attr,
99     type => 'xml:lang ne lang',
100     level => $self->{level}->{must});
101     }
102     }
103     }
104    
105 wakaba 1.35 if ($attr->owner_document->manakai_is_html) { # MUST NOT
106 wakaba 1.83 $self->{onerror}->(node => $attr, type => 'in HTML:xml:lang',
107     level => $self->{level}->{must});
108 wakaba 1.35 ## TODO: Test data...
109     }
110 wakaba 1.13 },
111     base => sub {
112     my ($self, $attr) = @_;
113     my $value = $attr->value;
114     if ($value =~ /[^\x{0000}-\x{10FFFF}]/) { ## ISSUE: Should we disallow noncharacters?
115     $self->{onerror}->(node => $attr,
116 wakaba 1.83 type => 'invalid attribute value',
117     level => $self->{level}->{fact}, ## TODO: correct?
118     );
119 wakaba 1.13 }
120 wakaba 1.18 ## NOTE: Conformance to URI standard is not checked since there is
121     ## no author requirement on conformance in the XML Base specification.
122 wakaba 1.13 },
123     id => sub {
124     my ($self, $attr) = @_;
125     my $value = $attr->value;
126     $value =~ s/[\x09\x0A\x0D\x20]+/ /g;
127     $value =~ s/^\x20//;
128     $value =~ s/\x20$//;
129     ## TODO: NCName in XML 1.0 or 1.1
130     ## TODO: declared type is ID?
131 wakaba 1.83 if ($self->{id}->{$value}) {
132     $self->{onerror}->(node => $attr,
133     type => 'duplicate ID',
134     level => $self->{level}->{xml_id_error});
135 wakaba 1.37 push @{$self->{id}->{$value}}, $attr;
136 wakaba 1.13 } else {
137 wakaba 1.37 $self->{id}->{$value} = [$attr];
138 wakaba 1.13 }
139     },
140 wakaba 1.9 },
141     $XMLNS_NS => {
142 wakaba 1.13 '' => sub {
143     my ($self, $attr) = @_;
144     my $ln = $attr->manakai_local_name;
145     my $value = $attr->value;
146     if ($value eq $XML_NS and $ln ne 'xml') {
147     $self->{onerror}
148 wakaba 1.83 ->(node => $attr,
149     type => 'Reserved Prefixes and Namespace Names:Name',
150     text => $value,
151     level => $self->{level}->{nc});
152 wakaba 1.13 } elsif ($value eq $XMLNS_NS) {
153     $self->{onerror}
154 wakaba 1.83 ->(node => $attr,
155     type => 'Reserved Prefixes and Namespace Names:Name',
156     text => $value,
157     level => $self->{level}->{nc});
158 wakaba 1.13 }
159     if ($ln eq 'xml' and $value ne $XML_NS) {
160     $self->{onerror}
161 wakaba 1.83 ->(node => $attr,
162     type => 'Reserved Prefixes and Namespace Names:Prefix',
163     text => $ln,
164     level => $self->{level}->{nc});
165 wakaba 1.13 } elsif ($ln eq 'xmlns') {
166     $self->{onerror}
167 wakaba 1.83 ->(node => $attr,
168     type => 'Reserved Prefixes and Namespace Names:Prefix',
169     text => $ln,
170     level => $self->{level}->{nc});
171 wakaba 1.13 }
172     ## TODO: If XML 1.0 and empty
173     },
174     xmlns => sub {
175     my ($self, $attr) = @_;
176     ## TODO: In XML 1.0, URI reference [RFC 3986] or an empty string
177     ## TODO: In XML 1.1, IRI reference [RFC 3987] or an empty string
178 wakaba 1.18 ## TODO: relative references are deprecated
179 wakaba 1.13 my $value = $attr->value;
180     if ($value eq $XML_NS) {
181     $self->{onerror}
182 wakaba 1.83 ->(node => $attr,
183     type => 'Reserved Prefixes and Namespace Names:Name',
184     text => $value,
185     level => $self->{level}->{nc});
186 wakaba 1.13 } elsif ($value eq $XMLNS_NS) {
187     $self->{onerror}
188 wakaba 1.83 ->(node => $attr,
189     type => 'Reserved Prefixes and Namespace Names:Name',
190     text => $value,
191     level => $self->{level}->{nc});
192 wakaba 1.13 }
193     },
194 wakaba 1.9 },
195     };
196    
197 wakaba 1.14 ## ISSUE: Should we really allow these attributes?
198 wakaba 1.13 $AttrChecker->{''}->{'xml:space'} = $AttrChecker->{$XML_NS}->{space};
199     $AttrChecker->{''}->{'xml:lang'} = $AttrChecker->{$XML_NS}->{lang};
200 wakaba 1.89 ## NOTE: Checker for (null, "xml:lang") attribute is shadowed for
201     ## HTML elements in Whatpm::ContentChecker::HTML.
202 wakaba 1.13 $AttrChecker->{''}->{'xml:base'} = $AttrChecker->{$XML_NS}->{base};
203     $AttrChecker->{''}->{'xml:id'} = $AttrChecker->{$XML_NS}->{id};
204    
205 wakaba 1.79 our $AttrStatus;
206    
207     for (qw/space lang base id/) {
208     $AttrStatus->{$XML_NS}->{$_} = FEATURE_STATUS_REC | FEATURE_ALLOWED;
209     $AttrStatus->{''}->{"xml:$_"} = FEATURE_STATUS_REC | FEATURE_ALLOWED;
210     ## XML 1.0: FEATURE_STATUS_CR
211     ## XML 1.1: FEATURE_STATUS_REC
212     ## XML Namespaces 1.0: FEATURE_STATUS_CR
213     ## XML Namespaces 1.1: FEATURE_STATUS_REC
214     ## XML Base: FEATURE_STATUS_REC
215     ## xml:id: FEATURE_STATUS_REC
216     }
217    
218     $AttrStatus->{$XMLNS_NS}->{''} = FEATURE_STATUS_REC | FEATURE_ALLOWED;
219    
220     ## TODO: xsi:schemaLocation for XHTML2 support (very, very low priority)
221    
222 wakaba 1.60 our %AnyChecker = (
223     check_start => sub { },
224     check_attrs => sub {
225     my ($self, $item, $element_state) = @_;
226     for my $attr (@{$item->{node}->attributes}) {
227 wakaba 1.9 my $attr_ns = $attr->namespace_uri;
228 wakaba 1.92 if (defined $attr_ns) {
229     load_ns_module ($attr_ns);
230     } else {
231     $attr_ns = '';
232     }
233 wakaba 1.9 my $attr_ln = $attr->manakai_local_name;
234 wakaba 1.79
235 wakaba 1.9 my $checker = $AttrChecker->{$attr_ns}->{$attr_ln}
236 wakaba 1.60 || $AttrChecker->{$attr_ns}->{''};
237 wakaba 1.79 my $status = $AttrStatus->{$attr_ns}->{$attr_ln}
238     || $AttrStatus->{$attr_ns}->{''};
239     if (not defined $status) {
240     $status = FEATURE_ALLOWED;
241     ## NOTE: FEATURE_ALLOWED for all attributes, since the element
242     ## is not supported and therefore "attribute not defined" error
243     ## should not raised (too verbose) and global attributes should be
244     ## allowed anyway (if a global attribute has its specified creteria
245     ## for where it may be specified, then it should be checked in it's
246     ## checker function).
247     }
248 wakaba 1.9 if ($checker) {
249     $checker->($self, $attr);
250 wakaba 1.17 } else {
251 wakaba 1.83 $self->{onerror}->(node => $attr,
252     type => 'unknown attribute',
253     level => $self->{level}->{uncertain});
254 wakaba 1.9 }
255 wakaba 1.79 $self->_attr_status_info ($attr, $status);
256 wakaba 1.9 }
257     },
258 wakaba 1.60 check_child_element => sub {
259     my ($self, $item, $child_el, $child_nsuri, $child_ln,
260     $child_is_transparent, $element_state) = @_;
261     if ($self->{minus_elements}->{$child_nsuri}->{$child_ln}) {
262     $self->{onerror}->(node => $child_el,
263     type => 'element not allowed:minus',
264 wakaba 1.83 level => $self->{level}->{must});
265 wakaba 1.60 } elsif ($self->{plus_elements}->{$child_nsuri}->{$child_ln}) {
266     #
267     } else {
268     #
269     }
270     },
271     check_child_text => sub { },
272     check_end => sub {
273     my ($self, $item, $element_state) = @_;
274 wakaba 1.82 ## NOTE: There is a modified copy of the code below for |html:ruby|.
275 wakaba 1.60 if ($element_state->{has_significant}) {
276 wakaba 1.66 $item->{real_parent_state}->{has_significant} = 1;
277 wakaba 1.60 }
278     },
279     );
280    
281     our $ElementDefault = {
282     %AnyChecker,
283 wakaba 1.70 status => FEATURE_ALLOWED,
284     ## NOTE: No "element not defined" error - it is not supported anyway.
285 wakaba 1.60 check_start => sub {
286     my ($self, $item, $element_state) = @_;
287 wakaba 1.83 $self->{onerror}->(node => $item->{node},
288     type => 'unknown element',
289     level => $self->{level}->{uncertain});
290 wakaba 1.60 },
291 wakaba 1.1 };
292    
293 wakaba 1.60 our $HTMLEmbeddedContent = {
294     ## NOTE: All embedded content is also phrasing content.
295     $HTML_NS => {
296     img => 1, iframe => 1, embed => 1, object => 1, video => 1, audio => 1,
297     canvas => 1,
298     },
299     q<http://www.w3.org/1998/Math/MathML> => {math => 1},
300     q<http://www.w3.org/2000/svg> => {svg => 1},
301     ## NOTE: Foreign elements with content (but no metadata) are
302     ## embedded content.
303     };
304    
305 wakaba 1.7 my $HTMLTransparentElements = {
306 wakaba 1.90 $HTML_NS => {qw/ins 1 del 1 font 1 noscript 1 canvas 1 a 1/},
307 wakaba 1.29 ## NOTE: |html:noscript| is transparent if scripting is disabled
308     ## and not in |head|.
309 wakaba 1.7 };
310    
311 wakaba 1.61 my $HTMLSemiTransparentElements = {
312     $HTML_NS => {object => 1, video => 1, audio => 1},
313     };
314 wakaba 1.57
315 wakaba 1.42 our $Element = {};
316 wakaba 1.7
317 wakaba 1.73 $Element->{q<http://www.w3.org/1999/02/22-rdf-syntax-ns#>}->{RDF} = {
318     %AnyChecker,
319     status => FEATURE_STATUS_REC | FEATURE_ALLOWED,
320     is_root => 1, ## ISSUE: Not explicitly allowed for non application/rdf+xml
321     check_start => sub {
322     my ($self, $item, $element_state) = @_;
323     my $triple = [];
324     push @{$self->{return}->{rdf}}, [$item->{node}, $triple];
325     require Whatpm::RDFXML;
326     my $rdf = Whatpm::RDFXML->new;
327 wakaba 1.75 ## TODO: Should we make bnodeid unique in a document?
328 wakaba 1.73 $rdf->{onerror} = $self->{onerror};
329 wakaba 1.84 $rdf->{level} = $self->{level};
330 wakaba 1.73 $rdf->{ontriple} = sub {
331     my %opt = @_;
332     push @$triple,
333     [$opt{node}, $opt{subject}, $opt{predicate}, $opt{object}];
334 wakaba 1.74 if (defined $opt{id}) {
335     push @$triple,
336     [$opt{node},
337     $opt{id},
338     {uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#subject>},
339     $opt{subject}];
340     push @$triple,
341     [$opt{node},
342     $opt{id},
343     {uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate>},
344     $opt{predicate}];
345     push @$triple,
346     [$opt{node},
347     $opt{id},
348     {uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#object>},
349     $opt{object}];
350     push @$triple,
351     [$opt{node},
352     $opt{id},
353     {uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>},
354     {uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement>}];
355     }
356 wakaba 1.73 };
357     $rdf->convert_rdf_element ($item->{node});
358     },
359     };
360    
361 wakaba 1.83 my $default_error_level = {
362     must => 'm',
363     should => 's',
364     warn => 'w',
365     good => 'w',
366 wakaba 1.93 undefined => 'w',
367 wakaba 1.83 info => 'i',
368 wakaba 1.85
369 wakaba 1.83 uncertain => 'u',
370    
371 wakaba 1.84 html4_fact => 'm',
372 wakaba 1.89 html5_no_may => 'm',
373    
374 wakaba 1.83 xml_error => 'm', ## TODO: correct?
375 wakaba 1.88 xml_id_error => 'm', ## TODO: ?
376 wakaba 1.83 nc => 'm', ## XML Namespace Constraints ## TODO: correct?
377 wakaba 1.84
378 wakaba 1.91 ## |Whatpm::URIChecker|
379 wakaba 1.85 uri_syntax => 'm',
380     uri_fact => 'm',
381     uri_lc_must => 'm',
382     uri_lc_should => 'w',
383    
384 wakaba 1.91 ## |Whatpm::IMTChecker|
385 wakaba 1.87 mime_must => 'm', # lowercase "must"
386     mime_fact => 'm',
387     mime_strongly_discouraged => 'w',
388     mime_discouraged => 'w',
389    
390 wakaba 1.91 ## |Whatpm::LangTag|
391 wakaba 1.85 langtag_fact => 'm',
392    
393 wakaba 1.91 ## |Whatpm::RDFXML|
394 wakaba 1.84 rdf_fact => 'm',
395     rdf_grammer => 'm',
396     rdf_lc_must => 'm',
397 wakaba 1.91
398     ## |Message::Charset::Info| and |Whatpm::Charset::DecodeHandle|
399     charset_variant => 'm',
400     ## An error caused by use of a variant charset that is not conforming
401     ## to the original charset (e.g. use of 0x80 in an ISO-8859-1 document
402     ## which is interpreted as a Windows-1252 document instead).
403     charset_fact => 'm',
404     iso_shall => 'm',
405 wakaba 1.83 };
406    
407 wakaba 1.56 sub check_document ($$$;$) {
408     my ($self, $doc, $onerror, $onsubdoc) = @_;
409 wakaba 1.42 $self = bless {}, $self unless ref $self;
410     $self->{onerror} = $onerror;
411 wakaba 1.56 $self->{onsubdoc} = $onsubdoc || sub {
412     warn "A subdocument is not conformance-checked";
413     };
414 wakaba 1.1
415 wakaba 1.83 $self->{level} ||= $default_error_level;
416 wakaba 1.48
417 wakaba 1.73 ## TODO: If application/rdf+xml, RDF/XML mode should be invoked.
418    
419 wakaba 1.42 my $docel = $doc->document_element;
420     unless (defined $docel) {
421     ## ISSUE: Should we check content of Document node?
422 wakaba 1.83 $onerror->(node => $doc, type => 'no document element',
423     level => $self->{level}->{must});
424 wakaba 1.42 ## ISSUE: Is this non-conforming (to what spec)? Or just a warning?
425     return {
426     class => {},
427     id => {}, table => [], term => {},
428     };
429 wakaba 1.1 }
430    
431 wakaba 1.42 ## ISSUE: Unexpanded entity references and HTML5 conformance
432 wakaba 1.1
433 wakaba 1.42 my $docel_nsuri = $docel->namespace_uri;
434 wakaba 1.92 if (defined $docel_nsuri) {
435     load_ns_module ($docel_nsuri);
436     } else {
437     $docel_nsuri = '';
438     }
439 wakaba 1.42 my $docel_def = $Element->{$docel_nsuri}->{$docel->manakai_local_name} ||
440     $Element->{$docel_nsuri}->{''} ||
441     $ElementDefault;
442     if ($docel_def->{is_root}) {
443     #
444 wakaba 1.50 } elsif ($docel_def->{is_xml_root}) {
445     unless ($doc->manakai_is_html) {
446     #
447     } else {
448 wakaba 1.83 $onerror->(node => $docel, type => 'element not allowed:root:xml',
449     level => $self->{level}->{must});
450 wakaba 1.50 }
451 wakaba 1.42 } else {
452 wakaba 1.83 $onerror->(node => $docel, type => 'element not allowed:root',
453     level => $self->{level}->{must});
454 wakaba 1.1 }
455    
456 wakaba 1.42 ## TODO: Check for other items other than document element
457     ## (second (errorous) element, text nodes, PI nodes, doctype nodes)
458 wakaba 1.2
459 wakaba 1.56 my $return = $self->check_element ($docel, $onerror, $onsubdoc);
460 wakaba 1.51
461 wakaba 1.52 ## TODO: Test for these checks are necessary.
462 wakaba 1.51 my $charset_name = $doc->input_encoding;
463     if (defined $charset_name) {
464     require Message::Charset::Info;
465     my $charset = $Message::Charset::Info::IANACharset->{$charset_name};
466    
467 wakaba 1.71 if ($doc->manakai_is_html) {
468     if (not $doc->manakai_has_bom and
469     not defined $doc->manakai_charset) {
470     unless ($charset->{is_html_ascii_superset}) {
471 wakaba 1.86 $onerror->(node => $doc,
472     level => $self->{level}->{must},
473 wakaba 1.83 type => 'non ascii superset',
474     text => $charset_name);
475 wakaba 1.71 }
476    
477     if (not $self->{has_charset} and ## TODO: This does not work now.
478     not $charset->{iana_names}->{'us-ascii'}) {
479 wakaba 1.86 $onerror->(node => $doc,
480     level => $self->{level}->{must},
481 wakaba 1.83 type => 'no character encoding declaration',
482     text => $charset_name);
483 wakaba 1.71 }
484 wakaba 1.51 }
485 wakaba 1.71
486     if ($charset->{iana_names}->{'utf-8'}) {
487     #
488     } elsif ($charset->{iana_names}->{'jis_x0212-1990'} or
489     $charset->{iana_names}->{'x-jis0208'} or
490     $charset->{iana_names}->{'utf-32'} or ## ISSUE: UTF-32BE? UTF-32LE?
491 wakaba 1.91 ($charset->{category} & Message::Charset::Info::CHARSET_CATEGORY_EBCDIC ())) {
492 wakaba 1.71 $onerror->(node => $doc,
493 wakaba 1.83 type => 'bad character encoding',
494     text => $charset_name,
495     level => $self->{level}->{should},
496     layer => 'encode');
497 wakaba 1.71 } elsif ($charset->{iana_names}->{'cesu-8'} or
498     $charset->{iana_names}->{'utf-8'} or ## ISSUE: UNICODE-1-1-UTF-7?
499     $charset->{iana_names}->{'bocu-1'} or
500     $charset->{iana_names}->{'scsu'}) {
501     $onerror->(node => $doc,
502 wakaba 1.83 type => 'disallowed character encoding',
503     text => $charset_name,
504     level => $self->{level}->{must},
505     layer => 'encode');
506 wakaba 1.71 } else {
507     $onerror->(node => $doc,
508 wakaba 1.83 type => 'non-utf-8 character encoding',
509     text => $charset_name,
510     level => $self->{level}->{good},
511     layer => 'encode');
512 wakaba 1.51 }
513     }
514 wakaba 1.52 } elsif ($doc->manakai_is_html) {
515     ## NOTE: MUST and SHOULD requirements above cannot be tested,
516     ## since the document has no input charset encoding information.
517     $onerror->(node => $doc,
518 wakaba 1.83 type => 'character encoding unchecked',
519     level => $self->{level}->{info},
520     layer => 'encode');
521 wakaba 1.51 }
522    
523     return $return;
524 wakaba 1.42 } # check_document
525 wakaba 1.1
526 wakaba 1.81 ## Check an element. The element is checked as if it is an orphan node (i.e.
527     ## an element without a parent node).
528 wakaba 1.56 sub check_element ($$$;$) {
529     my ($self, $el, $onerror, $onsubdoc) = @_;
530 wakaba 1.42 $self = bless {}, $self unless ref $self;
531     $self->{onerror} = $onerror;
532 wakaba 1.56 $self->{onsubdoc} = $onsubdoc || sub {
533     warn "A subdocument is not conformance-checked";
534     };
535 wakaba 1.2
536 wakaba 1.83 $self->{level} ||= $default_error_level;
537 wakaba 1.48
538 wakaba 1.61 $self->{plus_elements} = {};
539     $self->{minus_elements} = {};
540 wakaba 1.42 $self->{id} = {};
541     $self->{term} = {};
542     $self->{usemap} = [];
543 wakaba 1.78 $self->{ref} = []; # datetemplate data references
544     $self->{template} = []; # datatemplate template references
545 wakaba 1.42 $self->{contextmenu} = [];
546     $self->{map} = {};
547     $self->{menu} = {};
548     $self->{has_link_type} = {};
549 wakaba 1.60 $self->{flag} = {};
550 wakaba 1.46 #$self->{has_uri_attr};
551     #$self->{has_hyperlink_element};
552 wakaba 1.51 #$self->{has_charset};
553 wakaba 1.57 #$self->{has_base};
554 wakaba 1.42 $self->{return} = {
555     class => {},
556 wakaba 1.80 id => $self->{id},
557     table => [], # table objects returned by Whatpm::HTMLTable
558     term => $self->{term},
559 wakaba 1.76 uri => {}, # URIs other than those in RDF triples
560     ## TODO: xmlns="", SYSTEM "", atom:* src="", xml:base=""
561 wakaba 1.73 rdf => [],
562 wakaba 1.42 };
563 wakaba 1.4
564 wakaba 1.60 my @item = ({type => 'element', node => $el, parent_state => {}});
565 wakaba 1.66 $item[-1]->{real_parent_state} = $item[-1]->{parent_state};
566 wakaba 1.60 while (@item) {
567     my $item = shift @item;
568     if (ref $item eq 'ARRAY') {
569     my $code = shift @$item;
570     next unless $code;## TODO: temp.
571     $code->(@$item);
572     } elsif ($item->{type} eq 'element') {
573     my $el_nsuri = $item->{node}->namespace_uri;
574 wakaba 1.92 if (defined $el_nsuri) {
575     load_ns_module ($el_nsuri);
576     } else {
577     $el_nsuri = '';
578     }
579 wakaba 1.60 my $el_ln = $item->{node}->manakai_local_name;
580 wakaba 1.92
581 wakaba 1.63 my $element_state = {};
582 wakaba 1.60 my $eldef = $Element->{$el_nsuri}->{$el_ln} ||
583     $Element->{$el_nsuri}->{''} ||
584 wakaba 1.42 $ElementDefault;
585 wakaba 1.61 my $content_def = $item->{transparent}
586     ? $item->{parent_def} || $eldef : $eldef;
587 wakaba 1.63 my $content_state = $item->{transparent}
588 wakaba 1.65 ? $item->{parent_def}
589     ? $item->{parent_state} || $element_state : $element_state
590     : $element_state;
591 wakaba 1.60
592 wakaba 1.67 unless ($eldef->{status} & FEATURE_STATUS_REC) {
593     my $status = $eldef->{status} & FEATURE_STATUS_CR ? 'cr' :
594     $eldef->{status} & FEATURE_STATUS_LC ? 'lc' :
595     $eldef->{status} & FEATURE_STATUS_WD ? 'wd' : 'non-standard';
596     $self->{onerror}->(node => $item->{node},
597     type => 'status:'.$status.':element',
598 wakaba 1.83 level => $self->{level}->{info});
599 wakaba 1.67 }
600 wakaba 1.70 if (not ($eldef->{status} & FEATURE_ALLOWED)) {
601     $self->{onerror}->(node => $item->{node},
602     type => 'element not defined',
603 wakaba 1.83 level => $self->{level}->{must});
604 wakaba 1.70 } elsif ($eldef->{status} & FEATURE_DEPRECATED_SHOULD) {
605     $self->{onerror}->(node => $item->{node},
606     type => 'deprecated:element',
607 wakaba 1.83 level => $self->{level}->{should});
608 wakaba 1.70 } elsif ($eldef->{status} & FEATURE_DEPRECATED_INFO) {
609     $self->{onerror}->(node => $item->{node},
610     type => 'deprecated:element',
611 wakaba 1.83 level => $self->{level}->{info});
612 wakaba 1.70 }
613 wakaba 1.67
614 wakaba 1.60 my @new_item;
615     push @new_item, [$eldef->{check_start}, $self, $item, $element_state];
616     push @new_item, [$eldef->{check_attrs}, $self, $item, $element_state];
617 wakaba 1.61
618 wakaba 1.60 my @child = @{$item->{node}->child_nodes};
619     while (@child) {
620     my $child = shift @child;
621     my $child_nt = $child->node_type;
622     if ($child_nt == 1) { # ELEMENT_NODE
623     my $child_nsuri = $child->namespace_uri;
624     $child_nsuri = '' unless defined $child_nsuri;
625     my $child_ln = $child->manakai_local_name;
626     if ($HTMLTransparentElements->{$child_nsuri}->{$child_ln} and
627     not (($self->{flag}->{in_head} or
628 wakaba 1.61 ($el_nsuri eq $HTML_NS and $el_ln eq 'head')) and
629     $child_nsuri eq $HTML_NS and $child_ln eq 'noscript')) {
630 wakaba 1.60 push @new_item, [$content_def->{check_child_element},
631     $self, $item, $child,
632 wakaba 1.66 $child_nsuri, $child_ln, 1,
633     $content_state, $element_state];
634 wakaba 1.60 push @new_item, {type => 'element', node => $child,
635 wakaba 1.65 parent_state => $content_state,
636 wakaba 1.61 parent_def => $content_def,
637 wakaba 1.66 real_parent_state => $element_state,
638 wakaba 1.60 transparent => 1};
639     } else {
640 wakaba 1.65 if ($item->{parent_def} and # has parent
641     $el_nsuri eq $HTML_NS) { ## $HTMLSemiTransparentElements
642 wakaba 1.61 if ($el_ln eq 'object') {
643     if ($self->{plus_elements}->{$child_nsuri}->{$child_ln}) {
644     #
645     } elsif ($child_nsuri eq $HTML_NS and $child_ln eq 'param') {
646     #
647     } else {
648 wakaba 1.62 $content_def = $item->{parent_def} || $content_def;
649 wakaba 1.63 $content_state = $item->{parent_state} || $content_state;
650 wakaba 1.62 }
651     } elsif ($el_ln eq 'video' or $el_ln eq 'audio') {
652     if ($self->{plus_elements}->{$child_nsuri}->{$child_ln}) {
653     #
654     } elsif ($child_nsuri eq $HTML_NS and $child_ln eq 'source') {
655     $element_state->{has_source} = 1;
656     } else {
657     $content_def = $item->{parent_def} || $content_def;
658 wakaba 1.63 $content_state = $item->{parent_state} || $content_state;
659 wakaba 1.61 }
660     }
661     }
662    
663 wakaba 1.60 push @new_item, [$content_def->{check_child_element},
664     $self, $item, $child,
665 wakaba 1.64 $child_nsuri, $child_ln,
666     $HTMLSemiTransparentElements
667     ->{$child_nsuri}->{$child_ln},
668 wakaba 1.66 $content_state, $element_state];
669 wakaba 1.60 push @new_item, {type => 'element', node => $child,
670 wakaba 1.65 parent_def => $content_def,
671 wakaba 1.66 real_parent_state => $element_state,
672 wakaba 1.65 parent_state => $content_state};
673 wakaba 1.60 }
674    
675     if ($HTMLEmbeddedContent->{$child_nsuri}->{$child_ln}) {
676     $element_state->{has_significant} = 1;
677     }
678     } elsif ($child_nt == 3 or # TEXT_NODE
679     $child_nt == 4) { # CDATA_SECTION_NODE
680     my $has_significant = ($child->data =~ /[^\x09-\x0D\x20]/);
681     push @new_item, [$content_def->{check_child_text},
682     $self, $item, $child, $has_significant,
683 wakaba 1.66 $content_state, $element_state];
684     $element_state->{has_significant} ||= $has_significant;
685 wakaba 1.61 if ($has_significant and
686     $HTMLSemiTransparentElements->{$el_nsuri}->{$el_ln}) {
687     $content_def = $item->{parent_def} || $content_def;
688     }
689 wakaba 1.60 } elsif ($child_nt == 5) { # ENTITY_REFERENCE_NODE
690     push @child, @{$child->child_nodes};
691 wakaba 1.1 }
692 wakaba 1.60 ## TODO: PI_NODE
693     ## TODO: Unknown node type
694 wakaba 1.1 }
695 wakaba 1.60
696     push @new_item, [$eldef->{check_end}, $self, $item, $element_state];
697    
698     unshift @item, @new_item;
699 wakaba 1.30 } else {
700 wakaba 1.60 die "$0: Internal error: Unsupported checking action type |$item->{type}|";
701 wakaba 1.4 }
702 wakaba 1.1 }
703 wakaba 1.17
704 wakaba 1.78 for (@{$self->{template}}) {
705     ## TODO: If the document is an XML document, ...
706     ## NOTE: If the document is an HTML document:
707     ## ISSUE: We need to percent-decode?
708     F: {
709     if ($self->{id}->{$_->[0]}) {
710     my $el = $self->{id}->{$_->[0]}->[0]->owner_element;
711     if ($el->node_type == 1 and # ELEMENT_NODE
712     $el->manakai_local_name eq 'datatemplate') {
713     my $nsuri = $el->namespace_uri;
714     if (defined $nsuri and $nsuri eq $HTML_NS) {
715     if ($el eq $_->[1]->owner_element) {
716     $self->{onerror}->(node => $_->[1],
717     type => 'fragment points itself',
718 wakaba 1.83 level => $self->{level}->{must});
719 wakaba 1.78 }
720    
721     last F;
722     }
723     }
724     }
725     ## TODO: Should we raise a "fragment points nothing" error instead
726     ## if the fragment identifier identifies no element?
727    
728     $self->{onerror}->(node => $_->[1], type => 'template:not template',
729 wakaba 1.83 level => $self->{level}->{must});
730 wakaba 1.78 } # F
731     }
732    
733     for (@{$self->{ref}}) {
734     ## TOOD: If XML
735     ## NOTE: If it is an HTML document:
736     if ($_->[0] eq '') {
737     ## NOTE: It points the top of the document.
738     } elsif ($self->{id}->{$_->[0]}) {
739     if ($self->{id}->{$_->[0]}->[0]->owner_element
740     eq $_->[1]->owner_element) {
741     $self->{onerror}->(node => $_->[1], type => 'fragment points itself',
742 wakaba 1.83 level => $self->{level}->{must});
743 wakaba 1.78 }
744     } else {
745     $self->{onerror}->(node => $_->[1], type => 'fragment points nothing',
746 wakaba 1.83 level => $self->{level}->{must});
747 wakaba 1.78 }
748     }
749    
750     ## TODO: Maybe we should have $document->manakai_get_by_fragment or something
751    
752 wakaba 1.17 for (@{$self->{usemap}}) {
753     unless ($self->{map}->{$_->[0]}) {
754 wakaba 1.83 $self->{onerror}->(node => $_->[1], type => 'no referenced map',
755     level => $self->{level}->{must});
756 wakaba 1.17 }
757     }
758    
759 wakaba 1.32 for (@{$self->{contextmenu}}) {
760     unless ($self->{menu}->{$_->[0]}) {
761 wakaba 1.83 $self->{onerror}->(node => $_->[1], type => 'no referenced menu',
762     level => $self->{level}->{must});
763 wakaba 1.32 }
764     }
765    
766 wakaba 1.61 delete $self->{plus_elements};
767     delete $self->{minus_elements};
768 wakaba 1.17 delete $self->{onerror};
769     delete $self->{id};
770     delete $self->{usemap};
771 wakaba 1.78 delete $self->{ref};
772     delete $self->{template};
773 wakaba 1.17 delete $self->{map};
774 wakaba 1.33 return $self->{return};
775 wakaba 1.1 } # check_element
776    
777 wakaba 1.60 sub _add_minus_elements ($$@) {
778     my $self = shift;
779     my $element_state = shift;
780     for my $elements (@_) {
781     for my $nsuri (keys %$elements) {
782     for my $ln (keys %{$elements->{$nsuri}}) {
783     unless ($self->{minus_elements}->{$nsuri}->{$ln}) {
784     $element_state->{minus_elements_original}->{$nsuri}->{$ln} = 0;
785     $self->{minus_elements}->{$nsuri}->{$ln} = 1;
786     }
787     }
788     }
789     }
790     } # _add_minus_elements
791    
792     sub _remove_minus_elements ($$) {
793     my $self = shift;
794     my $element_state = shift;
795     for my $nsuri (keys %{$element_state->{minus_elements_original}}) {
796     for my $ln (keys %{$element_state->{minus_elements_original}->{$nsuri}}) {
797     delete $self->{minus_elements}->{$nsuri}->{$ln};
798     }
799     }
800     } # _remove_minus_elements
801    
802     sub _add_plus_elements ($$@) {
803     my $self = shift;
804     my $element_state = shift;
805     for my $elements (@_) {
806     for my $nsuri (keys %$elements) {
807     for my $ln (keys %{$elements->{$nsuri}}) {
808     unless ($self->{plus_elements}->{$nsuri}->{$ln}) {
809     $element_state->{plus_elements_original}->{$nsuri}->{$ln} = 0;
810     $self->{plus_elements}->{$nsuri}->{$ln} = 1;
811     }
812     }
813     }
814     }
815     } # _add_plus_elements
816    
817     sub _remove_plus_elements ($$) {
818     my $self = shift;
819     my $element_state = shift;
820     for my $nsuri (keys %{$element_state->{plus_elements_original}}) {
821     for my $ln (keys %{$element_state->{plus_elements_original}->{$nsuri}}) {
822     delete $self->{plus_elements}->{$nsuri}->{$ln};
823     }
824     }
825     } # _remove_plus_elements
826    
827 wakaba 1.68 sub _attr_status_info ($$$) {
828     my ($self, $attr, $status_code) = @_;
829 wakaba 1.70
830     if (not ($status_code & FEATURE_ALLOWED)) {
831     $self->{onerror}->(node => $attr,
832     type => 'attribute not defined',
833 wakaba 1.83 level => $self->{level}->{must});
834 wakaba 1.70 } elsif ($status_code & FEATURE_DEPRECATED_SHOULD) {
835     $self->{onerror}->(node => $attr,
836     type => 'deprecated:attr',
837 wakaba 1.83 level => $self->{level}->{should});
838 wakaba 1.70 } elsif ($status_code & FEATURE_DEPRECATED_INFO) {
839     $self->{onerror}->(node => $attr,
840     type => 'deprecated:attr',
841 wakaba 1.83 level => $self->{level}->{info});
842 wakaba 1.70 }
843    
844 wakaba 1.68 my $status;
845     if ($status_code & FEATURE_STATUS_REC) {
846     return;
847     } elsif ($status_code & FEATURE_STATUS_CR) {
848     $status = 'cr';
849     } elsif ($status_code & FEATURE_STATUS_LC) {
850     $status = 'lc';
851     } elsif ($status_code & FEATURE_STATUS_WD) {
852     $status = 'wd';
853     } else {
854     $status = 'non-standard';
855     }
856     $self->{onerror}->(node => $attr,
857     type => 'status:'.$status.':attr',
858 wakaba 1.83 level => $self->{level}->{info});
859 wakaba 1.68 } # _attr_status_info
860    
861 wakaba 1.2 sub _add_minuses ($@) {
862     my $self = shift;
863     my $r = {};
864     for my $list (@_) {
865     for my $ns (keys %$list) {
866     for my $ln (keys %{$list->{$ns}}) {
867     unless ($self->{minuses}->{$ns}->{$ln}) {
868     $self->{minuses}->{$ns}->{$ln} = 1;
869     $r->{$ns}->{$ln} = 1;
870     }
871     }
872     }
873     }
874 wakaba 1.4 return {type => 'plus', list => $r};
875 wakaba 1.2 } # _add_minuses
876    
877 wakaba 1.50 sub _add_pluses ($@) {
878     my $self = shift;
879     my $r = {};
880     for my $list (@_) {
881     for my $ns (keys %$list) {
882     for my $ln (keys %{$list->{$ns}}) {
883     unless ($self->{pluses}->{$ns}->{$ln}) {
884     $self->{pluses}->{$ns}->{$ln} = 1;
885     $r->{$ns}->{$ln} = 1;
886     }
887     }
888     }
889     }
890     return {type => 'minus', list => $r};
891     } # _add_pluses
892    
893 wakaba 1.2 sub _remove_minuses ($$) {
894 wakaba 1.4 my ($self, $todo) = @_;
895 wakaba 1.50 if ($todo->{type} eq 'minus') {
896     for my $ns (keys %{$todo->{list}}) {
897     for my $ln (keys %{$todo->{list}->{$ns}}) {
898     delete $self->{pluses}->{$ns}->{$ln} if $todo->{list}->{$ns}->{$ln};
899     }
900 wakaba 1.2 }
901 wakaba 1.50 } elsif ($todo->{type} eq 'plus') {
902     for my $ns (keys %{$todo->{list}}) {
903     for my $ln (keys %{$todo->{list}->{$ns}}) {
904     delete $self->{minuses}->{$ns}->{$ln} if $todo->{list}->{$ns}->{$ln};
905     }
906     }
907     } else {
908     die "$0: Unknown +- type: $todo->{type}";
909 wakaba 1.2 }
910     1;
911     } # _remove_minuses
912    
913 wakaba 1.50 ## NOTE: Priority for "minuses" and "pluses" are currently left
914     ## undefined and implemented inconsistently; it is not a problem for
915     ## now, since no element belongs to both lists.
916    
917 wakaba 1.30 sub _check_get_children ($$$) {
918     my ($self, $node, $parent_todo) = @_;
919 wakaba 1.4 my $new_todos = [];
920 wakaba 1.2 my $sib = [];
921     TP: {
922     my $node_ns = $node->namespace_uri;
923     $node_ns = '' unless defined $node_ns;
924     my $node_ln = $node->manakai_local_name;
925 wakaba 1.45 if ($HTMLTransparentElements->{$node_ns}->{$node_ln}) {
926     if ($node_ns eq $HTML_NS and $node_ln eq 'noscript') {
927     if ($parent_todo->{flag}->{in_head}) {
928     #
929     } else {
930     my $end = $self->_add_minuses ({$HTML_NS, {noscript => 1}});
931     push @$sib, $end;
932    
933     unshift @$sib, @{$node->child_nodes};
934     push @$new_todos, {type => 'element-attributes', node => $node};
935     last TP;
936     }
937 wakaba 1.58 } elsif ($node_ns eq $HTML_NS and $node_ln eq 'del') {
938     my $sig_flag = $parent_todo->{flag}->{has_descendant}->{significant};
939     unshift @$sib, @{$node->child_nodes};
940     push @$new_todos, {type => 'element-attributes', node => $node};
941     push @$new_todos,
942     {type => 'code',
943     code => sub {
944     $parent_todo->{flag}->{has_descendant}->{significant} = 0
945     if not $sig_flag;
946     }};
947     last TP;
948 wakaba 1.45 } else {
949     unshift @$sib, @{$node->child_nodes};
950     push @$new_todos, {type => 'element-attributes', node => $node};
951     last TP;
952 wakaba 1.2 }
953     }
954 wakaba 1.8 if ($node_ns eq $HTML_NS and ($node_ln eq 'video' or $node_ln eq 'audio')) {
955 wakaba 1.2 if ($node->has_attribute_ns (undef, 'src')) {
956     unshift @$sib, @{$node->child_nodes};
957 wakaba 1.9 push @$new_todos, {type => 'element-attributes', node => $node};
958 wakaba 1.2 last TP;
959     } else {
960     my @cn = @{$node->child_nodes};
961     CN: while (@cn) {
962     my $cn = shift @cn;
963     my $cnt = $cn->node_type;
964     if ($cnt == 1) {
965 wakaba 1.8 my $cn_nsuri = $cn->namespace_uri;
966     $cn_nsuri = '' unless defined $cn_nsuri;
967     if ($cn_nsuri eq $HTML_NS and $cn->manakai_local_name eq 'source') {
968 wakaba 1.2 #
969     } else {
970     last CN;
971     }
972     } elsif ($cnt == 3 or $cnt == 4) {
973     if ($cn->data =~ /[^\x09-\x0D\x20]/) {
974     last CN;
975     }
976     }
977     } # CN
978     unshift @$sib, @cn;
979     }
980 wakaba 1.57 } elsif ($node_ns eq $HTML_NS and $node_ln eq 'object') {
981     my @cn = @{$node->child_nodes};
982     CN: while (@cn) {
983     my $cn = shift @cn;
984     my $cnt = $cn->node_type;
985     if ($cnt == 1) {
986     my $cn_nsuri = $cn->namespace_uri;
987     $cn_nsuri = '' unless defined $cn_nsuri;
988     if ($cn_nsuri eq $HTML_NS and $cn->manakai_local_name eq 'param') {
989     #
990     } else {
991     last CN;
992     }
993     } elsif ($cnt == 3 or $cnt == 4) {
994     if ($cn->data =~ /[^\x09-\x0D\x20]/) {
995     last CN;
996     }
997     }
998     } # CN
999     unshift @$sib, @cn;
1000 wakaba 1.2 }
1001 wakaba 1.4 push @$new_todos, {type => 'element', node => $node};
1002 wakaba 1.2 } # TP
1003 wakaba 1.30
1004     for my $new_todo (@$new_todos) {
1005     $new_todo->{flag} = {%{$parent_todo->{flag} or {}}};
1006     }
1007    
1008 wakaba 1.4 return ($sib, $new_todos);
1009 wakaba 1.2 } # _check_get_children
1010    
1011 wakaba 1.44 =head1 LICENSE
1012    
1013 wakaba 1.56 Copyright 2007-2008 Wakaba <w@suika.fam.cx>
1014 wakaba 1.44
1015     This library is free software; you can redistribute it
1016     and/or modify it under the same terms as Perl itself.
1017    
1018     =cut
1019    
1020 wakaba 1.1 1;
1021 wakaba 1.93 # $Date: 2008/09/15 02:54:12 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24