/[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.84 - (hide annotations) (download)
Fri Aug 15 14:13:42 2008 UTC (16 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.83: +8 -3 lines
++ whatpm/Whatpm/ChangeLog	15 Aug 2008 14:12:36 -0000
	* ContentChecker.pm: Support for RDF/XML error levels.

	* HTMLTable.pm, RDFXML.pm: Support for new style of error level
	specifying.  Error types are revised.

2008-08-15  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ContentChecker/ChangeLog	15 Aug 2008 14:13:40 -0000
	* HTML.pm: Mark HTML4's "fact"-level errors as such.  Typo fixed.

2008-08-15  Wakaba  <wakaba@suika.fam.cx>

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24