/[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.83 - (hide annotations) (download)
Fri Aug 15 12:46:44 2008 UTC (16 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.82: +102 -71 lines
++ whatpm/Whatpm/ChangeLog	15 Aug 2008 12:45:57 -0000
	* ContentChecker.pm: All error reporting method calls are
	renewed.

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

++ whatpm/Whatpm/ContentChecker/ChangeLog	15 Aug 2008 12:46:27 -0000
2008-08-15  Wakaba  <wakaba@suika.fam.cx>

	* Atom.pm, HTML.pm: All error reporting method calls are
	revised.

1 wakaba 1.1 package Whatpm::ContentChecker;
2     use strict;
3 wakaba 1.83 our $VERSION=do{my @r=(q$Revision: 1.82 $=~/\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     $rdf->{ontriple} = sub {
308     my %opt = @_;
309     push @$triple,
310     [$opt{node}, $opt{subject}, $opt{predicate}, $opt{object}];
311 wakaba 1.74 if (defined $opt{id}) {
312     push @$triple,
313     [$opt{node},
314     $opt{id},
315     {uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#subject>},
316     $opt{subject}];
317     push @$triple,
318     [$opt{node},
319     $opt{id},
320     {uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate>},
321     $opt{predicate}];
322     push @$triple,
323     [$opt{node},
324     $opt{id},
325     {uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#object>},
326     $opt{object}];
327     push @$triple,
328     [$opt{node},
329     $opt{id},
330     {uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>},
331     {uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement>}];
332     }
333 wakaba 1.73 };
334     $rdf->convert_rdf_element ($item->{node});
335     },
336     };
337    
338 wakaba 1.83 my $default_error_level = {
339     must => 'm',
340     should => 's',
341     warn => 'w',
342     good => 'w',
343     info => 'i',
344     uncertain => 'u',
345    
346     fact => 'm',
347     xml_error => 'm', ## TODO: correct?
348     nc => 'm', ## XML Namespace Constraints ## TODO: correct?
349     };
350    
351 wakaba 1.56 sub check_document ($$$;$) {
352     my ($self, $doc, $onerror, $onsubdoc) = @_;
353 wakaba 1.42 $self = bless {}, $self unless ref $self;
354     $self->{onerror} = $onerror;
355 wakaba 1.56 $self->{onsubdoc} = $onsubdoc || sub {
356     warn "A subdocument is not conformance-checked";
357     };
358 wakaba 1.1
359 wakaba 1.83 $self->{level} ||= $default_error_level;
360 wakaba 1.48
361 wakaba 1.73 ## TODO: If application/rdf+xml, RDF/XML mode should be invoked.
362    
363 wakaba 1.42 my $docel = $doc->document_element;
364     unless (defined $docel) {
365     ## ISSUE: Should we check content of Document node?
366 wakaba 1.83 $onerror->(node => $doc, type => 'no document element',
367     level => $self->{level}->{must});
368 wakaba 1.42 ## ISSUE: Is this non-conforming (to what spec)? Or just a warning?
369     return {
370     class => {},
371     id => {}, table => [], term => {},
372     };
373 wakaba 1.1 }
374    
375 wakaba 1.42 ## ISSUE: Unexpanded entity references and HTML5 conformance
376 wakaba 1.1
377 wakaba 1.42 my $docel_nsuri = $docel->namespace_uri;
378     $docel_nsuri = '' unless defined $docel_nsuri;
379 wakaba 1.79 load_ns_module ($docel_nsuri);
380 wakaba 1.42 my $docel_def = $Element->{$docel_nsuri}->{$docel->manakai_local_name} ||
381     $Element->{$docel_nsuri}->{''} ||
382     $ElementDefault;
383     if ($docel_def->{is_root}) {
384     #
385 wakaba 1.50 } elsif ($docel_def->{is_xml_root}) {
386     unless ($doc->manakai_is_html) {
387     #
388     } else {
389 wakaba 1.83 $onerror->(node => $docel, type => 'element not allowed:root:xml',
390     level => $self->{level}->{must});
391 wakaba 1.50 }
392 wakaba 1.42 } else {
393 wakaba 1.83 $onerror->(node => $docel, type => 'element not allowed:root',
394     level => $self->{level}->{must});
395 wakaba 1.1 }
396    
397 wakaba 1.42 ## TODO: Check for other items other than document element
398     ## (second (errorous) element, text nodes, PI nodes, doctype nodes)
399 wakaba 1.2
400 wakaba 1.56 my $return = $self->check_element ($docel, $onerror, $onsubdoc);
401 wakaba 1.51
402 wakaba 1.52 ## TODO: Test for these checks are necessary.
403 wakaba 1.51 my $charset_name = $doc->input_encoding;
404     if (defined $charset_name) {
405     require Message::Charset::Info;
406     my $charset = $Message::Charset::Info::IANACharset->{$charset_name};
407    
408 wakaba 1.71 if ($doc->manakai_is_html) {
409     if (not $doc->manakai_has_bom and
410     not defined $doc->manakai_charset) {
411     unless ($charset->{is_html_ascii_superset}) {
412 wakaba 1.83 $onerror->(node => $doc, level => $self->{level}->{must},
413     type => 'non ascii superset',
414     text => $charset_name);
415 wakaba 1.71 }
416    
417     if (not $self->{has_charset} and ## TODO: This does not work now.
418     not $charset->{iana_names}->{'us-ascii'}) {
419 wakaba 1.83 $onerror->(node => $doc, level => $self->{level}->{must},
420     type => 'no character encoding declaration',
421     text => $charset_name);
422 wakaba 1.71 }
423 wakaba 1.51 }
424 wakaba 1.71
425     if ($charset->{iana_names}->{'utf-8'}) {
426     #
427     } elsif ($charset->{iana_names}->{'jis_x0212-1990'} or
428     $charset->{iana_names}->{'x-jis0208'} or
429     $charset->{iana_names}->{'utf-32'} or ## ISSUE: UTF-32BE? UTF-32LE?
430     $charset->{is_ebcdic_based}) {
431     $onerror->(node => $doc,
432 wakaba 1.83 type => 'bad character encoding',
433     text => $charset_name,
434     level => $self->{level}->{should},
435     layer => 'encode');
436 wakaba 1.71 } elsif ($charset->{iana_names}->{'cesu-8'} or
437     $charset->{iana_names}->{'utf-8'} or ## ISSUE: UNICODE-1-1-UTF-7?
438     $charset->{iana_names}->{'bocu-1'} or
439     $charset->{iana_names}->{'scsu'}) {
440     $onerror->(node => $doc,
441 wakaba 1.83 type => 'disallowed character encoding',
442     text => $charset_name,
443     level => $self->{level}->{must},
444     layer => 'encode');
445 wakaba 1.71 } else {
446     $onerror->(node => $doc,
447 wakaba 1.83 type => 'non-utf-8 character encoding',
448     text => $charset_name,
449     level => $self->{level}->{good},
450     layer => 'encode');
451 wakaba 1.51 }
452     }
453 wakaba 1.52 } elsif ($doc->manakai_is_html) {
454     ## NOTE: MUST and SHOULD requirements above cannot be tested,
455     ## since the document has no input charset encoding information.
456     $onerror->(node => $doc,
457 wakaba 1.83 type => 'character encoding unchecked',
458     level => $self->{level}->{info},
459     layer => 'encode');
460 wakaba 1.51 }
461    
462     return $return;
463 wakaba 1.42 } # check_document
464 wakaba 1.1
465 wakaba 1.81 ## Check an element. The element is checked as if it is an orphan node (i.e.
466     ## an element without a parent node).
467 wakaba 1.56 sub check_element ($$$;$) {
468     my ($self, $el, $onerror, $onsubdoc) = @_;
469 wakaba 1.42 $self = bless {}, $self unless ref $self;
470     $self->{onerror} = $onerror;
471 wakaba 1.56 $self->{onsubdoc} = $onsubdoc || sub {
472     warn "A subdocument is not conformance-checked";
473     };
474 wakaba 1.2
475 wakaba 1.83 $self->{level} ||= $default_error_level;
476 wakaba 1.48
477 wakaba 1.61 $self->{plus_elements} = {};
478     $self->{minus_elements} = {};
479 wakaba 1.42 $self->{id} = {};
480     $self->{term} = {};
481     $self->{usemap} = [];
482 wakaba 1.78 $self->{ref} = []; # datetemplate data references
483     $self->{template} = []; # datatemplate template references
484 wakaba 1.42 $self->{contextmenu} = [];
485     $self->{map} = {};
486     $self->{menu} = {};
487     $self->{has_link_type} = {};
488 wakaba 1.60 $self->{flag} = {};
489 wakaba 1.46 #$self->{has_uri_attr};
490     #$self->{has_hyperlink_element};
491 wakaba 1.51 #$self->{has_charset};
492 wakaba 1.57 #$self->{has_base};
493 wakaba 1.42 $self->{return} = {
494     class => {},
495 wakaba 1.80 id => $self->{id},
496     table => [], # table objects returned by Whatpm::HTMLTable
497     term => $self->{term},
498 wakaba 1.76 uri => {}, # URIs other than those in RDF triples
499     ## TODO: xmlns="", SYSTEM "", atom:* src="", xml:base=""
500 wakaba 1.73 rdf => [],
501 wakaba 1.42 };
502 wakaba 1.4
503 wakaba 1.60 my @item = ({type => 'element', node => $el, parent_state => {}});
504 wakaba 1.66 $item[-1]->{real_parent_state} = $item[-1]->{parent_state};
505 wakaba 1.60 while (@item) {
506     my $item = shift @item;
507     if (ref $item eq 'ARRAY') {
508     my $code = shift @$item;
509     next unless $code;## TODO: temp.
510     $code->(@$item);
511     } elsif ($item->{type} eq 'element') {
512     my $el_nsuri = $item->{node}->namespace_uri;
513     $el_nsuri = '' unless defined $el_nsuri;
514     my $el_ln = $item->{node}->manakai_local_name;
515 wakaba 1.79
516     load_ns_module ($el_nsuri);
517 wakaba 1.63
518     my $element_state = {};
519 wakaba 1.60 my $eldef = $Element->{$el_nsuri}->{$el_ln} ||
520     $Element->{$el_nsuri}->{''} ||
521 wakaba 1.42 $ElementDefault;
522 wakaba 1.61 my $content_def = $item->{transparent}
523     ? $item->{parent_def} || $eldef : $eldef;
524 wakaba 1.63 my $content_state = $item->{transparent}
525 wakaba 1.65 ? $item->{parent_def}
526     ? $item->{parent_state} || $element_state : $element_state
527     : $element_state;
528 wakaba 1.60
529 wakaba 1.67 unless ($eldef->{status} & FEATURE_STATUS_REC) {
530     my $status = $eldef->{status} & FEATURE_STATUS_CR ? 'cr' :
531     $eldef->{status} & FEATURE_STATUS_LC ? 'lc' :
532     $eldef->{status} & FEATURE_STATUS_WD ? 'wd' : 'non-standard';
533     $self->{onerror}->(node => $item->{node},
534     type => 'status:'.$status.':element',
535 wakaba 1.83 level => $self->{level}->{info});
536 wakaba 1.67 }
537 wakaba 1.70 if (not ($eldef->{status} & FEATURE_ALLOWED)) {
538     $self->{onerror}->(node => $item->{node},
539     type => 'element not defined',
540 wakaba 1.83 level => $self->{level}->{must});
541 wakaba 1.70 } elsif ($eldef->{status} & FEATURE_DEPRECATED_SHOULD) {
542     $self->{onerror}->(node => $item->{node},
543     type => 'deprecated:element',
544 wakaba 1.83 level => $self->{level}->{should});
545 wakaba 1.70 } elsif ($eldef->{status} & FEATURE_DEPRECATED_INFO) {
546     $self->{onerror}->(node => $item->{node},
547     type => 'deprecated:element',
548 wakaba 1.83 level => $self->{level}->{info});
549 wakaba 1.70 }
550 wakaba 1.67
551 wakaba 1.60 my @new_item;
552     push @new_item, [$eldef->{check_start}, $self, $item, $element_state];
553     push @new_item, [$eldef->{check_attrs}, $self, $item, $element_state];
554 wakaba 1.61
555 wakaba 1.60 my @child = @{$item->{node}->child_nodes};
556     while (@child) {
557     my $child = shift @child;
558     my $child_nt = $child->node_type;
559     if ($child_nt == 1) { # ELEMENT_NODE
560     my $child_nsuri = $child->namespace_uri;
561     $child_nsuri = '' unless defined $child_nsuri;
562     my $child_ln = $child->manakai_local_name;
563     if ($HTMLTransparentElements->{$child_nsuri}->{$child_ln} and
564     not (($self->{flag}->{in_head} or
565 wakaba 1.61 ($el_nsuri eq $HTML_NS and $el_ln eq 'head')) and
566     $child_nsuri eq $HTML_NS and $child_ln eq 'noscript')) {
567 wakaba 1.60 push @new_item, [$content_def->{check_child_element},
568     $self, $item, $child,
569 wakaba 1.66 $child_nsuri, $child_ln, 1,
570     $content_state, $element_state];
571 wakaba 1.60 push @new_item, {type => 'element', node => $child,
572 wakaba 1.65 parent_state => $content_state,
573 wakaba 1.61 parent_def => $content_def,
574 wakaba 1.66 real_parent_state => $element_state,
575 wakaba 1.60 transparent => 1};
576     } else {
577 wakaba 1.65 if ($item->{parent_def} and # has parent
578     $el_nsuri eq $HTML_NS) { ## $HTMLSemiTransparentElements
579 wakaba 1.61 if ($el_ln eq 'object') {
580     if ($self->{plus_elements}->{$child_nsuri}->{$child_ln}) {
581     #
582     } elsif ($child_nsuri eq $HTML_NS and $child_ln eq 'param') {
583     #
584     } else {
585 wakaba 1.62 $content_def = $item->{parent_def} || $content_def;
586 wakaba 1.63 $content_state = $item->{parent_state} || $content_state;
587 wakaba 1.62 }
588     } elsif ($el_ln eq 'video' or $el_ln eq 'audio') {
589     if ($self->{plus_elements}->{$child_nsuri}->{$child_ln}) {
590     #
591     } elsif ($child_nsuri eq $HTML_NS and $child_ln eq 'source') {
592     $element_state->{has_source} = 1;
593     } else {
594     $content_def = $item->{parent_def} || $content_def;
595 wakaba 1.63 $content_state = $item->{parent_state} || $content_state;
596 wakaba 1.61 }
597     }
598     }
599    
600 wakaba 1.60 push @new_item, [$content_def->{check_child_element},
601     $self, $item, $child,
602 wakaba 1.64 $child_nsuri, $child_ln,
603     $HTMLSemiTransparentElements
604     ->{$child_nsuri}->{$child_ln},
605 wakaba 1.66 $content_state, $element_state];
606 wakaba 1.60 push @new_item, {type => 'element', node => $child,
607 wakaba 1.65 parent_def => $content_def,
608 wakaba 1.66 real_parent_state => $element_state,
609 wakaba 1.65 parent_state => $content_state};
610 wakaba 1.60 }
611    
612     if ($HTMLEmbeddedContent->{$child_nsuri}->{$child_ln}) {
613     $element_state->{has_significant} = 1;
614     }
615     } elsif ($child_nt == 3 or # TEXT_NODE
616     $child_nt == 4) { # CDATA_SECTION_NODE
617     my $has_significant = ($child->data =~ /[^\x09-\x0D\x20]/);
618     push @new_item, [$content_def->{check_child_text},
619     $self, $item, $child, $has_significant,
620 wakaba 1.66 $content_state, $element_state];
621     $element_state->{has_significant} ||= $has_significant;
622 wakaba 1.61 if ($has_significant and
623     $HTMLSemiTransparentElements->{$el_nsuri}->{$el_ln}) {
624     $content_def = $item->{parent_def} || $content_def;
625     }
626 wakaba 1.60 } elsif ($child_nt == 5) { # ENTITY_REFERENCE_NODE
627     push @child, @{$child->child_nodes};
628 wakaba 1.1 }
629 wakaba 1.60 ## TODO: PI_NODE
630     ## TODO: Unknown node type
631 wakaba 1.1 }
632 wakaba 1.60
633     push @new_item, [$eldef->{check_end}, $self, $item, $element_state];
634    
635     unshift @item, @new_item;
636 wakaba 1.30 } else {
637 wakaba 1.60 die "$0: Internal error: Unsupported checking action type |$item->{type}|";
638 wakaba 1.4 }
639 wakaba 1.1 }
640 wakaba 1.17
641 wakaba 1.78 for (@{$self->{template}}) {
642     ## TODO: If the document is an XML document, ...
643     ## NOTE: If the document is an HTML document:
644     ## ISSUE: We need to percent-decode?
645     F: {
646     if ($self->{id}->{$_->[0]}) {
647     my $el = $self->{id}->{$_->[0]}->[0]->owner_element;
648     if ($el->node_type == 1 and # ELEMENT_NODE
649     $el->manakai_local_name eq 'datatemplate') {
650     my $nsuri = $el->namespace_uri;
651     if (defined $nsuri and $nsuri eq $HTML_NS) {
652     if ($el eq $_->[1]->owner_element) {
653     $self->{onerror}->(node => $_->[1],
654     type => 'fragment points itself',
655 wakaba 1.83 level => $self->{level}->{must});
656 wakaba 1.78 }
657    
658     last F;
659     }
660     }
661     }
662     ## TODO: Should we raise a "fragment points nothing" error instead
663     ## if the fragment identifier identifies no element?
664    
665     $self->{onerror}->(node => $_->[1], type => 'template:not template',
666 wakaba 1.83 level => $self->{level}->{must});
667 wakaba 1.78 } # F
668     }
669    
670     for (@{$self->{ref}}) {
671     ## TOOD: If XML
672     ## NOTE: If it is an HTML document:
673     if ($_->[0] eq '') {
674     ## NOTE: It points the top of the document.
675     } elsif ($self->{id}->{$_->[0]}) {
676     if ($self->{id}->{$_->[0]}->[0]->owner_element
677     eq $_->[1]->owner_element) {
678     $self->{onerror}->(node => $_->[1], type => 'fragment points itself',
679 wakaba 1.83 level => $self->{level}->{must});
680 wakaba 1.78 }
681     } else {
682     $self->{onerror}->(node => $_->[1], type => 'fragment points nothing',
683 wakaba 1.83 level => $self->{level}->{must});
684 wakaba 1.78 }
685     }
686    
687     ## TODO: Maybe we should have $document->manakai_get_by_fragment or something
688    
689 wakaba 1.17 for (@{$self->{usemap}}) {
690     unless ($self->{map}->{$_->[0]}) {
691 wakaba 1.83 $self->{onerror}->(node => $_->[1], type => 'no referenced map',
692     level => $self->{level}->{must});
693 wakaba 1.17 }
694     }
695    
696 wakaba 1.32 for (@{$self->{contextmenu}}) {
697     unless ($self->{menu}->{$_->[0]}) {
698 wakaba 1.83 $self->{onerror}->(node => $_->[1], type => 'no referenced menu',
699     level => $self->{level}->{must});
700 wakaba 1.32 }
701     }
702    
703 wakaba 1.61 delete $self->{plus_elements};
704     delete $self->{minus_elements};
705 wakaba 1.17 delete $self->{onerror};
706     delete $self->{id};
707     delete $self->{usemap};
708 wakaba 1.78 delete $self->{ref};
709     delete $self->{template};
710 wakaba 1.17 delete $self->{map};
711 wakaba 1.33 return $self->{return};
712 wakaba 1.1 } # check_element
713    
714 wakaba 1.60 sub _add_minus_elements ($$@) {
715     my $self = shift;
716     my $element_state = shift;
717     for my $elements (@_) {
718     for my $nsuri (keys %$elements) {
719     for my $ln (keys %{$elements->{$nsuri}}) {
720     unless ($self->{minus_elements}->{$nsuri}->{$ln}) {
721     $element_state->{minus_elements_original}->{$nsuri}->{$ln} = 0;
722     $self->{minus_elements}->{$nsuri}->{$ln} = 1;
723     }
724     }
725     }
726     }
727     } # _add_minus_elements
728    
729     sub _remove_minus_elements ($$) {
730     my $self = shift;
731     my $element_state = shift;
732     for my $nsuri (keys %{$element_state->{minus_elements_original}}) {
733     for my $ln (keys %{$element_state->{minus_elements_original}->{$nsuri}}) {
734     delete $self->{minus_elements}->{$nsuri}->{$ln};
735     }
736     }
737     } # _remove_minus_elements
738    
739     sub _add_plus_elements ($$@) {
740     my $self = shift;
741     my $element_state = shift;
742     for my $elements (@_) {
743     for my $nsuri (keys %$elements) {
744     for my $ln (keys %{$elements->{$nsuri}}) {
745     unless ($self->{plus_elements}->{$nsuri}->{$ln}) {
746     $element_state->{plus_elements_original}->{$nsuri}->{$ln} = 0;
747     $self->{plus_elements}->{$nsuri}->{$ln} = 1;
748     }
749     }
750     }
751     }
752     } # _add_plus_elements
753    
754     sub _remove_plus_elements ($$) {
755     my $self = shift;
756     my $element_state = shift;
757     for my $nsuri (keys %{$element_state->{plus_elements_original}}) {
758     for my $ln (keys %{$element_state->{plus_elements_original}->{$nsuri}}) {
759     delete $self->{plus_elements}->{$nsuri}->{$ln};
760     }
761     }
762     } # _remove_plus_elements
763    
764 wakaba 1.68 sub _attr_status_info ($$$) {
765     my ($self, $attr, $status_code) = @_;
766 wakaba 1.70
767     if (not ($status_code & FEATURE_ALLOWED)) {
768     $self->{onerror}->(node => $attr,
769     type => 'attribute not defined',
770 wakaba 1.83 level => $self->{level}->{must});
771 wakaba 1.70 } elsif ($status_code & FEATURE_DEPRECATED_SHOULD) {
772     $self->{onerror}->(node => $attr,
773     type => 'deprecated:attr',
774 wakaba 1.83 level => $self->{level}->{should});
775 wakaba 1.70 } elsif ($status_code & FEATURE_DEPRECATED_INFO) {
776     $self->{onerror}->(node => $attr,
777     type => 'deprecated:attr',
778 wakaba 1.83 level => $self->{level}->{info});
779 wakaba 1.70 }
780    
781 wakaba 1.68 my $status;
782     if ($status_code & FEATURE_STATUS_REC) {
783     return;
784     } elsif ($status_code & FEATURE_STATUS_CR) {
785     $status = 'cr';
786     } elsif ($status_code & FEATURE_STATUS_LC) {
787     $status = 'lc';
788     } elsif ($status_code & FEATURE_STATUS_WD) {
789     $status = 'wd';
790     } else {
791     $status = 'non-standard';
792     }
793     $self->{onerror}->(node => $attr,
794     type => 'status:'.$status.':attr',
795 wakaba 1.83 level => $self->{level}->{info});
796 wakaba 1.68 } # _attr_status_info
797    
798 wakaba 1.2 sub _add_minuses ($@) {
799     my $self = shift;
800     my $r = {};
801     for my $list (@_) {
802     for my $ns (keys %$list) {
803     for my $ln (keys %{$list->{$ns}}) {
804     unless ($self->{minuses}->{$ns}->{$ln}) {
805     $self->{minuses}->{$ns}->{$ln} = 1;
806     $r->{$ns}->{$ln} = 1;
807     }
808     }
809     }
810     }
811 wakaba 1.4 return {type => 'plus', list => $r};
812 wakaba 1.2 } # _add_minuses
813    
814 wakaba 1.50 sub _add_pluses ($@) {
815     my $self = shift;
816     my $r = {};
817     for my $list (@_) {
818     for my $ns (keys %$list) {
819     for my $ln (keys %{$list->{$ns}}) {
820     unless ($self->{pluses}->{$ns}->{$ln}) {
821     $self->{pluses}->{$ns}->{$ln} = 1;
822     $r->{$ns}->{$ln} = 1;
823     }
824     }
825     }
826     }
827     return {type => 'minus', list => $r};
828     } # _add_pluses
829    
830 wakaba 1.2 sub _remove_minuses ($$) {
831 wakaba 1.4 my ($self, $todo) = @_;
832 wakaba 1.50 if ($todo->{type} eq 'minus') {
833     for my $ns (keys %{$todo->{list}}) {
834     for my $ln (keys %{$todo->{list}->{$ns}}) {
835     delete $self->{pluses}->{$ns}->{$ln} if $todo->{list}->{$ns}->{$ln};
836     }
837 wakaba 1.2 }
838 wakaba 1.50 } elsif ($todo->{type} eq 'plus') {
839     for my $ns (keys %{$todo->{list}}) {
840     for my $ln (keys %{$todo->{list}->{$ns}}) {
841     delete $self->{minuses}->{$ns}->{$ln} if $todo->{list}->{$ns}->{$ln};
842     }
843     }
844     } else {
845     die "$0: Unknown +- type: $todo->{type}";
846 wakaba 1.2 }
847     1;
848     } # _remove_minuses
849    
850 wakaba 1.50 ## NOTE: Priority for "minuses" and "pluses" are currently left
851     ## undefined and implemented inconsistently; it is not a problem for
852     ## now, since no element belongs to both lists.
853    
854 wakaba 1.30 sub _check_get_children ($$$) {
855     my ($self, $node, $parent_todo) = @_;
856 wakaba 1.4 my $new_todos = [];
857 wakaba 1.2 my $sib = [];
858     TP: {
859     my $node_ns = $node->namespace_uri;
860     $node_ns = '' unless defined $node_ns;
861     my $node_ln = $node->manakai_local_name;
862 wakaba 1.45 if ($HTMLTransparentElements->{$node_ns}->{$node_ln}) {
863     if ($node_ns eq $HTML_NS and $node_ln eq 'noscript') {
864     if ($parent_todo->{flag}->{in_head}) {
865     #
866     } else {
867     my $end = $self->_add_minuses ({$HTML_NS, {noscript => 1}});
868     push @$sib, $end;
869    
870     unshift @$sib, @{$node->child_nodes};
871     push @$new_todos, {type => 'element-attributes', node => $node};
872     last TP;
873     }
874 wakaba 1.58 } elsif ($node_ns eq $HTML_NS and $node_ln eq 'del') {
875     my $sig_flag = $parent_todo->{flag}->{has_descendant}->{significant};
876     unshift @$sib, @{$node->child_nodes};
877     push @$new_todos, {type => 'element-attributes', node => $node};
878     push @$new_todos,
879     {type => 'code',
880     code => sub {
881     $parent_todo->{flag}->{has_descendant}->{significant} = 0
882     if not $sig_flag;
883     }};
884     last TP;
885 wakaba 1.45 } else {
886     unshift @$sib, @{$node->child_nodes};
887     push @$new_todos, {type => 'element-attributes', node => $node};
888     last TP;
889 wakaba 1.2 }
890     }
891 wakaba 1.8 if ($node_ns eq $HTML_NS and ($node_ln eq 'video' or $node_ln eq 'audio')) {
892 wakaba 1.2 if ($node->has_attribute_ns (undef, 'src')) {
893     unshift @$sib, @{$node->child_nodes};
894 wakaba 1.9 push @$new_todos, {type => 'element-attributes', node => $node};
895 wakaba 1.2 last TP;
896     } else {
897     my @cn = @{$node->child_nodes};
898     CN: while (@cn) {
899     my $cn = shift @cn;
900     my $cnt = $cn->node_type;
901     if ($cnt == 1) {
902 wakaba 1.8 my $cn_nsuri = $cn->namespace_uri;
903     $cn_nsuri = '' unless defined $cn_nsuri;
904     if ($cn_nsuri eq $HTML_NS and $cn->manakai_local_name eq 'source') {
905 wakaba 1.2 #
906     } else {
907     last CN;
908     }
909     } elsif ($cnt == 3 or $cnt == 4) {
910     if ($cn->data =~ /[^\x09-\x0D\x20]/) {
911     last CN;
912     }
913     }
914     } # CN
915     unshift @$sib, @cn;
916     }
917 wakaba 1.57 } elsif ($node_ns eq $HTML_NS and $node_ln eq 'object') {
918     my @cn = @{$node->child_nodes};
919     CN: while (@cn) {
920     my $cn = shift @cn;
921     my $cnt = $cn->node_type;
922     if ($cnt == 1) {
923     my $cn_nsuri = $cn->namespace_uri;
924     $cn_nsuri = '' unless defined $cn_nsuri;
925     if ($cn_nsuri eq $HTML_NS and $cn->manakai_local_name eq 'param') {
926     #
927     } else {
928     last CN;
929     }
930     } elsif ($cnt == 3 or $cnt == 4) {
931     if ($cn->data =~ /[^\x09-\x0D\x20]/) {
932     last CN;
933     }
934     }
935     } # CN
936     unshift @$sib, @cn;
937 wakaba 1.2 }
938 wakaba 1.4 push @$new_todos, {type => 'element', node => $node};
939 wakaba 1.2 } # TP
940 wakaba 1.30
941     for my $new_todo (@$new_todos) {
942     $new_todo->{flag} = {%{$parent_todo->{flag} or {}}};
943     }
944    
945 wakaba 1.4 return ($sib, $new_todos);
946 wakaba 1.2 } # _check_get_children
947    
948 wakaba 1.44 =head1 LICENSE
949    
950 wakaba 1.56 Copyright 2007-2008 Wakaba <w@suika.fam.cx>
951 wakaba 1.44
952     This library is free software; you can redistribute it
953     and/or modify it under the same terms as Perl itself.
954    
955     =cut
956    
957 wakaba 1.1 1;
958 wakaba 1.83 # $Date: 2008/06/08 12:22:54 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24