1 |
package Whatpm::ContentChecker; |
2 |
use strict; |
3 |
our $VERSION=do{my @r=(q$Revision: 1.72 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r}; |
4 |
|
5 |
require Whatpm::URIChecker; |
6 |
|
7 |
## ISSUE: How XML and XML Namespaces conformance can (or cannot) |
8 |
## be applied to an in-memory representation (i.e. DOM)? |
9 |
|
10 |
## TODO: Conformance of an HTML document with non-html root element. |
11 |
|
12 |
## Stability |
13 |
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 |
## 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 |
my $HTML_NS = q<http://www.w3.org/1999/xhtml>; |
26 |
my $XML_NS = q<http://www.w3.org/XML/1998/namespace>; |
27 |
my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>; |
28 |
|
29 |
my $Namespace = { |
30 |
q<http://www.w3.org/2005/Atom> => {module => 'Whatpm::ContentChecker::Atom'}, |
31 |
q<http://purl.org/syndication/history/1.0> |
32 |
=> {module => 'Whatpm::ContentChecker::Atom'}, |
33 |
q<http://purl.org/syndication/threading/1.0> |
34 |
=> {module => 'Whatpm::ContentChecker::Atom'}, |
35 |
$HTML_NS => {module => 'Whatpm::ContentChecker::HTML'}, |
36 |
$XML_NS => {loaded => 1}, |
37 |
$XMLNS_NS => {loaded => 1}, |
38 |
q<http://www.w3.org/1999/02/22-rdf-syntax-ns#> => {loaded => 1}, |
39 |
}; |
40 |
|
41 |
our $AttrChecker = { |
42 |
$XML_NS => { |
43 |
space => sub { |
44 |
my ($self, $attr) = @_; |
45 |
my $value = $attr->value; |
46 |
if ($value eq 'default' or $value eq 'preserve') { |
47 |
# |
48 |
} else { |
49 |
## NOTE: An XML "error" |
50 |
$self->{onerror}->(node => $attr, level => 'error', |
51 |
type => 'invalid attribute value'); |
52 |
} |
53 |
}, |
54 |
lang => sub { |
55 |
my ($self, $attr) = @_; |
56 |
my $value = $attr->value; |
57 |
if ($value eq '') { |
58 |
# |
59 |
} else { |
60 |
require Whatpm::LangTag; |
61 |
Whatpm::LangTag->check_rfc3066_language_tag ($value, sub { |
62 |
my %opt = @_; |
63 |
my $type = 'LangTag:'.$opt{type}; |
64 |
$type .= ':' . $opt{subtag} if defined $opt{subtag}; |
65 |
$self->{onerror}->(node => $attr, type => $type, |
66 |
value => $opt{value}, level => $opt{level}); |
67 |
}); |
68 |
} |
69 |
|
70 |
## NOTE: "The values of the attribute are language identifiers |
71 |
## as defined by [IETF RFC 3066], Tags for the Identification |
72 |
## of Languages, or its successor; in addition, the empty string |
73 |
## may be specified." ("may" in lower case) |
74 |
## NOTE: Is an RFC 3066-valid (but RFC 4647-invalid) language tag |
75 |
## allowed today? |
76 |
|
77 |
## TODO: test data |
78 |
|
79 |
if ($attr->owner_document->manakai_is_html) { # MUST NOT |
80 |
$self->{onerror}->(node => $attr, type => 'in HTML:xml:lang'); |
81 |
## TODO: Test data... |
82 |
} |
83 |
}, |
84 |
base => sub { |
85 |
my ($self, $attr) = @_; |
86 |
my $value = $attr->value; |
87 |
if ($value =~ /[^\x{0000}-\x{10FFFF}]/) { ## ISSUE: Should we disallow noncharacters? |
88 |
$self->{onerror}->(node => $attr, |
89 |
type => 'invalid attribute value'); |
90 |
} |
91 |
## NOTE: Conformance to URI standard is not checked since there is |
92 |
## no author requirement on conformance in the XML Base specification. |
93 |
}, |
94 |
id => sub { |
95 |
my ($self, $attr) = @_; |
96 |
my $value = $attr->value; |
97 |
$value =~ s/[\x09\x0A\x0D\x20]+/ /g; |
98 |
$value =~ s/^\x20//; |
99 |
$value =~ s/\x20$//; |
100 |
## TODO: NCName in XML 1.0 or 1.1 |
101 |
## TODO: declared type is ID? |
102 |
if ($self->{id}->{$value}) { ## NOTE: An xml:id error |
103 |
$self->{onerror}->(node => $attr, level => 'error', |
104 |
type => 'duplicate ID'); |
105 |
push @{$self->{id}->{$value}}, $attr; |
106 |
} else { |
107 |
$self->{id}->{$value} = [$attr]; |
108 |
} |
109 |
}, |
110 |
}, |
111 |
$XMLNS_NS => { |
112 |
'' => sub { |
113 |
my ($self, $attr) = @_; |
114 |
my $ln = $attr->manakai_local_name; |
115 |
my $value = $attr->value; |
116 |
if ($value eq $XML_NS and $ln ne 'xml') { |
117 |
$self->{onerror} |
118 |
->(node => $attr, level => 'NC', |
119 |
type => 'Reserved Prefixes and Namespace Names:=xml'); |
120 |
} elsif ($value eq $XMLNS_NS) { |
121 |
$self->{onerror} |
122 |
->(node => $attr, level => 'NC', |
123 |
type => 'Reserved Prefixes and Namespace Names:=xmlns'); |
124 |
} |
125 |
if ($ln eq 'xml' and $value ne $XML_NS) { |
126 |
$self->{onerror} |
127 |
->(node => $attr, level => 'NC', |
128 |
type => 'Reserved Prefixes and Namespace Names:xmlns:xml='); |
129 |
} elsif ($ln eq 'xmlns') { |
130 |
$self->{onerror} |
131 |
->(node => $attr, level => 'NC', |
132 |
type => 'Reserved Prefixes and Namespace Names:xmlns:xmlns='); |
133 |
} |
134 |
## TODO: If XML 1.0 and empty |
135 |
}, |
136 |
xmlns => sub { |
137 |
my ($self, $attr) = @_; |
138 |
## TODO: In XML 1.0, URI reference [RFC 3986] or an empty string |
139 |
## TODO: In XML 1.1, IRI reference [RFC 3987] or an empty string |
140 |
## TODO: relative references are deprecated |
141 |
my $value = $attr->value; |
142 |
if ($value eq $XML_NS) { |
143 |
$self->{onerror} |
144 |
->(node => $attr, level => 'NC', |
145 |
type => 'Reserved Prefixes and Namespace Names:=xml'); |
146 |
} elsif ($value eq $XMLNS_NS) { |
147 |
$self->{onerror} |
148 |
->(node => $attr, level => 'NC', |
149 |
type => 'Reserved Prefixes and Namespace Names:=xmlns'); |
150 |
} |
151 |
}, |
152 |
}, |
153 |
}; |
154 |
|
155 |
## ISSUE: Should we really allow these attributes? |
156 |
$AttrChecker->{''}->{'xml:space'} = $AttrChecker->{$XML_NS}->{space}; |
157 |
$AttrChecker->{''}->{'xml:lang'} = $AttrChecker->{$XML_NS}->{lang}; |
158 |
$AttrChecker->{''}->{'xml:base'} = $AttrChecker->{$XML_NS}->{base}; |
159 |
$AttrChecker->{''}->{'xml:id'} = $AttrChecker->{$XML_NS}->{id}; |
160 |
|
161 |
our %AnyChecker = ( |
162 |
check_start => sub { }, |
163 |
check_attrs => sub { |
164 |
my ($self, $item, $element_state) = @_; |
165 |
for my $attr (@{$item->{node}->attributes}) { |
166 |
my $attr_ns = $attr->namespace_uri; |
167 |
$attr_ns = '' unless defined $attr_ns; |
168 |
my $attr_ln = $attr->manakai_local_name; |
169 |
my $checker = $AttrChecker->{$attr_ns}->{$attr_ln} |
170 |
|| $AttrChecker->{$attr_ns}->{''}; |
171 |
if ($checker) { |
172 |
$checker->($self, $attr); |
173 |
} else { |
174 |
$self->{onerror}->(node => $attr, level => 'unsupported', |
175 |
type => 'attribute'); |
176 |
} |
177 |
} |
178 |
}, |
179 |
check_child_element => sub { |
180 |
my ($self, $item, $child_el, $child_nsuri, $child_ln, |
181 |
$child_is_transparent, $element_state) = @_; |
182 |
if ($self->{minus_elements}->{$child_nsuri}->{$child_ln}) { |
183 |
$self->{onerror}->(node => $child_el, |
184 |
type => 'element not allowed:minus', |
185 |
level => $self->{must_level}); |
186 |
} elsif ($self->{plus_elements}->{$child_nsuri}->{$child_ln}) { |
187 |
# |
188 |
} else { |
189 |
# |
190 |
} |
191 |
}, |
192 |
check_child_text => sub { }, |
193 |
check_end => sub { |
194 |
my ($self, $item, $element_state) = @_; |
195 |
if ($element_state->{has_significant}) { |
196 |
$item->{real_parent_state}->{has_significant} = 1; |
197 |
} |
198 |
}, |
199 |
); |
200 |
|
201 |
our $ElementDefault = { |
202 |
%AnyChecker, |
203 |
status => FEATURE_ALLOWED, |
204 |
## NOTE: No "element not defined" error - it is not supported anyway. |
205 |
check_start => sub { |
206 |
my ($self, $item, $element_state) = @_; |
207 |
$self->{onerror}->(node => $item->{node}, level => 'unsupported', |
208 |
type => 'element'); |
209 |
}, |
210 |
}; |
211 |
|
212 |
our $HTMLEmbeddedContent = { |
213 |
## NOTE: All embedded content is also phrasing content. |
214 |
$HTML_NS => { |
215 |
img => 1, iframe => 1, embed => 1, object => 1, video => 1, audio => 1, |
216 |
canvas => 1, |
217 |
}, |
218 |
## NOTE: MathML is mentioned in the HTML5 spec. |
219 |
q<http://www.w3.org/1998/Math/MathML> => {math => 1}, |
220 |
## NOTE: SVG is mentioned in the HTML5 spec. |
221 |
q<http://www.w3.org/2000/svg> => {svg => 1}, |
222 |
## NOTE: Foreign elements with content (but no metadata) are |
223 |
## embedded content. |
224 |
}; |
225 |
|
226 |
my $HTMLTransparentElements = { |
227 |
$HTML_NS => {qw/ins 1 del 1 font 1 noscript 1 canvas 1/}, |
228 |
## NOTE: |html:noscript| is transparent if scripting is disabled |
229 |
## and not in |head|. |
230 |
}; |
231 |
|
232 |
my $HTMLSemiTransparentElements = { |
233 |
$HTML_NS => {object => 1, video => 1, audio => 1}, |
234 |
}; |
235 |
|
236 |
our $Element = {}; |
237 |
|
238 |
$Element->{q<http://www.w3.org/1999/02/22-rdf-syntax-ns#>}->{RDF} = { |
239 |
%AnyChecker, |
240 |
status => FEATURE_STATUS_REC | FEATURE_ALLOWED, |
241 |
is_root => 1, ## ISSUE: Not explicitly allowed for non application/rdf+xml |
242 |
check_start => sub { |
243 |
my ($self, $item, $element_state) = @_; |
244 |
my $triple = []; |
245 |
push @{$self->{return}->{rdf}}, [$item->{node}, $triple]; |
246 |
require Whatpm::RDFXML; |
247 |
my $rdf = Whatpm::RDFXML->new; |
248 |
$rdf->{onerror} = $self->{onerror}; |
249 |
$rdf->{ontriple} = sub { |
250 |
my %opt = @_; |
251 |
push @$triple, |
252 |
[$opt{node}, $opt{subject}, $opt{predicate}, $opt{object}]; |
253 |
}; |
254 |
$rdf->convert_rdf_element ($item->{node}); |
255 |
}, |
256 |
}; |
257 |
|
258 |
sub check_document ($$$;$) { |
259 |
my ($self, $doc, $onerror, $onsubdoc) = @_; |
260 |
$self = bless {}, $self unless ref $self; |
261 |
$self->{onerror} = $onerror; |
262 |
$self->{onsubdoc} = $onsubdoc || sub { |
263 |
warn "A subdocument is not conformance-checked"; |
264 |
}; |
265 |
|
266 |
$self->{must_level} = 'm'; |
267 |
$self->{fact_level} = 'f'; |
268 |
$self->{should_level} = 's'; |
269 |
$self->{good_level} = 'w'; |
270 |
$self->{info_level} = 'i'; |
271 |
$self->{unsupported_level} = 'u'; |
272 |
|
273 |
## TODO: If application/rdf+xml, RDF/XML mode should be invoked. |
274 |
|
275 |
my $docel = $doc->document_element; |
276 |
unless (defined $docel) { |
277 |
## ISSUE: Should we check content of Document node? |
278 |
$onerror->(node => $doc, type => 'no document element'); |
279 |
## ISSUE: Is this non-conforming (to what spec)? Or just a warning? |
280 |
return { |
281 |
class => {}, |
282 |
id => {}, table => [], term => {}, |
283 |
}; |
284 |
} |
285 |
|
286 |
## ISSUE: Unexpanded entity references and HTML5 conformance |
287 |
|
288 |
my $docel_nsuri = $docel->namespace_uri; |
289 |
$docel_nsuri = '' unless defined $docel_nsuri; |
290 |
unless ($Namespace->{$docel_nsuri}->{loaded}) { |
291 |
if ($Namespace->{$docel_nsuri}->{module}) { |
292 |
eval qq{ require $Namespace->{$docel_nsuri}->{module} } or die $@; |
293 |
} else { |
294 |
$Namespace->{$docel_nsuri}->{loaded} = 1; |
295 |
} |
296 |
} |
297 |
my $docel_def = $Element->{$docel_nsuri}->{$docel->manakai_local_name} || |
298 |
$Element->{$docel_nsuri}->{''} || |
299 |
$ElementDefault; |
300 |
if ($docel_def->{is_root}) { |
301 |
# |
302 |
} elsif ($docel_def->{is_xml_root}) { |
303 |
unless ($doc->manakai_is_html) { |
304 |
# |
305 |
} else { |
306 |
$onerror->(node => $docel, type => 'element not allowed:root:xml'); |
307 |
} |
308 |
} else { |
309 |
$onerror->(node => $docel, type => 'element not allowed:root'); |
310 |
} |
311 |
|
312 |
## TODO: Check for other items other than document element |
313 |
## (second (errorous) element, text nodes, PI nodes, doctype nodes) |
314 |
|
315 |
my $return = $self->check_element ($docel, $onerror, $onsubdoc); |
316 |
|
317 |
## TODO: Test for these checks are necessary. |
318 |
my $charset_name = $doc->input_encoding; |
319 |
if (defined $charset_name) { |
320 |
require Message::Charset::Info; |
321 |
my $charset = $Message::Charset::Info::IANACharset->{$charset_name}; |
322 |
|
323 |
if ($doc->manakai_is_html) { |
324 |
if (not $doc->manakai_has_bom and |
325 |
not defined $doc->manakai_charset) { |
326 |
unless ($charset->{is_html_ascii_superset}) { |
327 |
$onerror->(node => $doc, level => $self->{must_level}, |
328 |
type => 'non ascii superset:'.$charset_name); |
329 |
} |
330 |
|
331 |
if (not $self->{has_charset} and ## TODO: This does not work now. |
332 |
not $charset->{iana_names}->{'us-ascii'}) { |
333 |
$onerror->(node => $doc, level => $self->{must_level}, |
334 |
type => 'no character encoding declaration:'.$charset_name); |
335 |
} |
336 |
} |
337 |
|
338 |
if ($charset->{iana_names}->{'utf-8'}) { |
339 |
# |
340 |
} elsif ($charset->{iana_names}->{'jis_x0212-1990'} or |
341 |
$charset->{iana_names}->{'x-jis0208'} or |
342 |
$charset->{iana_names}->{'utf-32'} or ## ISSUE: UTF-32BE? UTF-32LE? |
343 |
$charset->{is_ebcdic_based}) { |
344 |
$onerror->(node => $doc, |
345 |
type => 'character encoding:'.$charset_name, |
346 |
level => $self->{should_level}); |
347 |
} elsif ($charset->{iana_names}->{'cesu-8'} or |
348 |
$charset->{iana_names}->{'utf-8'} or ## ISSUE: UNICODE-1-1-UTF-7? |
349 |
$charset->{iana_names}->{'bocu-1'} or |
350 |
$charset->{iana_names}->{'scsu'}) { |
351 |
$onerror->(node => $doc, |
352 |
type => 'character encoding:'.$charset_name, |
353 |
level => $self->{must_level}); |
354 |
} else { |
355 |
$onerror->(node => $doc, |
356 |
type => 'character encoding:'.$charset_name, |
357 |
level => $self->{good_level}); |
358 |
} |
359 |
} |
360 |
} elsif ($doc->manakai_is_html) { |
361 |
## NOTE: MUST and SHOULD requirements above cannot be tested, |
362 |
## since the document has no input charset encoding information. |
363 |
$onerror->(node => $doc, |
364 |
type => 'character encoding:', |
365 |
level => 'unsupported'); |
366 |
} |
367 |
|
368 |
return $return; |
369 |
} # check_document |
370 |
|
371 |
sub check_element ($$$;$) { |
372 |
my ($self, $el, $onerror, $onsubdoc) = @_; |
373 |
$self = bless {}, $self unless ref $self; |
374 |
$self->{onerror} = $onerror; |
375 |
$self->{onsubdoc} = $onsubdoc || sub { |
376 |
warn "A subdocument is not conformance-checked"; |
377 |
}; |
378 |
|
379 |
$self->{must_level} = 'm'; |
380 |
$self->{fact_level} = 'f'; |
381 |
$self->{should_level} = 's'; |
382 |
$self->{good_level} = 'w'; |
383 |
$self->{info_level} = 'i'; |
384 |
$self->{unsupported_level} = 'u'; |
385 |
|
386 |
$self->{plus_elements} = {}; |
387 |
$self->{minus_elements} = {}; |
388 |
$self->{id} = {}; |
389 |
$self->{term} = {}; |
390 |
$self->{usemap} = []; |
391 |
$self->{contextmenu} = []; |
392 |
$self->{map} = {}; |
393 |
$self->{menu} = {}; |
394 |
$self->{has_link_type} = {}; |
395 |
$self->{flag} = {}; |
396 |
#$self->{has_uri_attr}; |
397 |
#$self->{has_hyperlink_element}; |
398 |
#$self->{has_charset}; |
399 |
#$self->{has_base}; |
400 |
$self->{return} = { |
401 |
class => {}, |
402 |
id => $self->{id}, table => [], term => $self->{term}, |
403 |
rdf => [], |
404 |
}; |
405 |
|
406 |
my @item = ({type => 'element', node => $el, parent_state => {}}); |
407 |
$item[-1]->{real_parent_state} = $item[-1]->{parent_state}; |
408 |
while (@item) { |
409 |
my $item = shift @item; |
410 |
if (ref $item eq 'ARRAY') { |
411 |
my $code = shift @$item; |
412 |
next unless $code;## TODO: temp. |
413 |
$code->(@$item); |
414 |
} elsif ($item->{type} eq 'element') { |
415 |
my $el_nsuri = $item->{node}->namespace_uri; |
416 |
$el_nsuri = '' unless defined $el_nsuri; |
417 |
my $el_ln = $item->{node}->manakai_local_name; |
418 |
|
419 |
unless ($Namespace->{$el_nsuri}->{loaded}) { |
420 |
if ($Namespace->{$el_nsuri}->{module}) { |
421 |
eval qq{ require $Namespace->{$el_nsuri}->{module} } or die $@; |
422 |
} else { |
423 |
$Namespace->{$el_nsuri}->{loaded} = 1; |
424 |
} |
425 |
} |
426 |
|
427 |
my $element_state = {}; |
428 |
my $eldef = $Element->{$el_nsuri}->{$el_ln} || |
429 |
$Element->{$el_nsuri}->{''} || |
430 |
$ElementDefault; |
431 |
my $content_def = $item->{transparent} |
432 |
? $item->{parent_def} || $eldef : $eldef; |
433 |
my $content_state = $item->{transparent} |
434 |
? $item->{parent_def} |
435 |
? $item->{parent_state} || $element_state : $element_state |
436 |
: $element_state; |
437 |
|
438 |
unless ($eldef->{status} & FEATURE_STATUS_REC) { |
439 |
my $status = $eldef->{status} & FEATURE_STATUS_CR ? 'cr' : |
440 |
$eldef->{status} & FEATURE_STATUS_LC ? 'lc' : |
441 |
$eldef->{status} & FEATURE_STATUS_WD ? 'wd' : 'non-standard'; |
442 |
$self->{onerror}->(node => $item->{node}, |
443 |
type => 'status:'.$status.':element', |
444 |
level => $self->{info_level}); |
445 |
} |
446 |
if (not ($eldef->{status} & FEATURE_ALLOWED)) { |
447 |
$self->{onerror}->(node => $item->{node}, |
448 |
type => 'element not defined', |
449 |
level => $self->{must_level}); |
450 |
} elsif ($eldef->{status} & FEATURE_DEPRECATED_SHOULD) { |
451 |
$self->{onerror}->(node => $item->{node}, |
452 |
type => 'deprecated:element', |
453 |
level => $self->{should_level}); |
454 |
} elsif ($eldef->{status} & FEATURE_DEPRECATED_INFO) { |
455 |
$self->{onerror}->(node => $item->{node}, |
456 |
type => 'deprecated:element', |
457 |
level => $self->{info_level}); |
458 |
} |
459 |
|
460 |
my @new_item; |
461 |
push @new_item, [$eldef->{check_start}, $self, $item, $element_state]; |
462 |
push @new_item, [$eldef->{check_attrs}, $self, $item, $element_state]; |
463 |
|
464 |
my @child = @{$item->{node}->child_nodes}; |
465 |
while (@child) { |
466 |
my $child = shift @child; |
467 |
my $child_nt = $child->node_type; |
468 |
if ($child_nt == 1) { # ELEMENT_NODE |
469 |
my $child_nsuri = $child->namespace_uri; |
470 |
$child_nsuri = '' unless defined $child_nsuri; |
471 |
my $child_ln = $child->manakai_local_name; |
472 |
if ($HTMLTransparentElements->{$child_nsuri}->{$child_ln} and |
473 |
not (($self->{flag}->{in_head} or |
474 |
($el_nsuri eq $HTML_NS and $el_ln eq 'head')) and |
475 |
$child_nsuri eq $HTML_NS and $child_ln eq 'noscript')) { |
476 |
push @new_item, [$content_def->{check_child_element}, |
477 |
$self, $item, $child, |
478 |
$child_nsuri, $child_ln, 1, |
479 |
$content_state, $element_state]; |
480 |
push @new_item, {type => 'element', node => $child, |
481 |
parent_state => $content_state, |
482 |
parent_def => $content_def, |
483 |
real_parent_state => $element_state, |
484 |
transparent => 1}; |
485 |
} else { |
486 |
if ($item->{parent_def} and # has parent |
487 |
$el_nsuri eq $HTML_NS) { ## $HTMLSemiTransparentElements |
488 |
if ($el_ln eq 'object') { |
489 |
if ($self->{plus_elements}->{$child_nsuri}->{$child_ln}) { |
490 |
# |
491 |
} elsif ($child_nsuri eq $HTML_NS and $child_ln eq 'param') { |
492 |
# |
493 |
} else { |
494 |
$content_def = $item->{parent_def} || $content_def; |
495 |
$content_state = $item->{parent_state} || $content_state; |
496 |
} |
497 |
} elsif ($el_ln eq 'video' or $el_ln eq 'audio') { |
498 |
if ($self->{plus_elements}->{$child_nsuri}->{$child_ln}) { |
499 |
# |
500 |
} elsif ($child_nsuri eq $HTML_NS and $child_ln eq 'source') { |
501 |
$element_state->{has_source} = 1; |
502 |
} else { |
503 |
$content_def = $item->{parent_def} || $content_def; |
504 |
$content_state = $item->{parent_state} || $content_state; |
505 |
} |
506 |
} |
507 |
} |
508 |
|
509 |
push @new_item, [$content_def->{check_child_element}, |
510 |
$self, $item, $child, |
511 |
$child_nsuri, $child_ln, |
512 |
$HTMLSemiTransparentElements |
513 |
->{$child_nsuri}->{$child_ln}, |
514 |
$content_state, $element_state]; |
515 |
push @new_item, {type => 'element', node => $child, |
516 |
parent_def => $content_def, |
517 |
real_parent_state => $element_state, |
518 |
parent_state => $content_state}; |
519 |
} |
520 |
|
521 |
if ($HTMLEmbeddedContent->{$child_nsuri}->{$child_ln}) { |
522 |
$element_state->{has_significant} = 1; |
523 |
} |
524 |
} elsif ($child_nt == 3 or # TEXT_NODE |
525 |
$child_nt == 4) { # CDATA_SECTION_NODE |
526 |
my $has_significant = ($child->data =~ /[^\x09-\x0D\x20]/); |
527 |
push @new_item, [$content_def->{check_child_text}, |
528 |
$self, $item, $child, $has_significant, |
529 |
$content_state, $element_state]; |
530 |
$element_state->{has_significant} ||= $has_significant; |
531 |
if ($has_significant and |
532 |
$HTMLSemiTransparentElements->{$el_nsuri}->{$el_ln}) { |
533 |
$content_def = $item->{parent_def} || $content_def; |
534 |
} |
535 |
} elsif ($child_nt == 5) { # ENTITY_REFERENCE_NODE |
536 |
push @child, @{$child->child_nodes}; |
537 |
} |
538 |
## TODO: PI_NODE |
539 |
## TODO: Unknown node type |
540 |
} |
541 |
|
542 |
push @new_item, [$eldef->{check_end}, $self, $item, $element_state]; |
543 |
|
544 |
unshift @item, @new_item; |
545 |
} else { |
546 |
die "$0: Internal error: Unsupported checking action type |$item->{type}|"; |
547 |
} |
548 |
} |
549 |
|
550 |
for (@{$self->{usemap}}) { |
551 |
unless ($self->{map}->{$_->[0]}) { |
552 |
$self->{onerror}->(node => $_->[1], type => 'no referenced map'); |
553 |
} |
554 |
} |
555 |
|
556 |
for (@{$self->{contextmenu}}) { |
557 |
unless ($self->{menu}->{$_->[0]}) { |
558 |
$self->{onerror}->(node => $_->[1], type => 'no referenced menu'); |
559 |
} |
560 |
} |
561 |
|
562 |
delete $self->{plus_elements}; |
563 |
delete $self->{minus_elements}; |
564 |
delete $self->{onerror}; |
565 |
delete $self->{id}; |
566 |
delete $self->{usemap}; |
567 |
delete $self->{map}; |
568 |
return $self->{return}; |
569 |
} # check_element |
570 |
|
571 |
sub _add_minus_elements ($$@) { |
572 |
my $self = shift; |
573 |
my $element_state = shift; |
574 |
for my $elements (@_) { |
575 |
for my $nsuri (keys %$elements) { |
576 |
for my $ln (keys %{$elements->{$nsuri}}) { |
577 |
unless ($self->{minus_elements}->{$nsuri}->{$ln}) { |
578 |
$element_state->{minus_elements_original}->{$nsuri}->{$ln} = 0; |
579 |
$self->{minus_elements}->{$nsuri}->{$ln} = 1; |
580 |
} |
581 |
} |
582 |
} |
583 |
} |
584 |
} # _add_minus_elements |
585 |
|
586 |
sub _remove_minus_elements ($$) { |
587 |
my $self = shift; |
588 |
my $element_state = shift; |
589 |
for my $nsuri (keys %{$element_state->{minus_elements_original}}) { |
590 |
for my $ln (keys %{$element_state->{minus_elements_original}->{$nsuri}}) { |
591 |
delete $self->{minus_elements}->{$nsuri}->{$ln}; |
592 |
} |
593 |
} |
594 |
} # _remove_minus_elements |
595 |
|
596 |
sub _add_plus_elements ($$@) { |
597 |
my $self = shift; |
598 |
my $element_state = shift; |
599 |
for my $elements (@_) { |
600 |
for my $nsuri (keys %$elements) { |
601 |
for my $ln (keys %{$elements->{$nsuri}}) { |
602 |
unless ($self->{plus_elements}->{$nsuri}->{$ln}) { |
603 |
$element_state->{plus_elements_original}->{$nsuri}->{$ln} = 0; |
604 |
$self->{plus_elements}->{$nsuri}->{$ln} = 1; |
605 |
} |
606 |
} |
607 |
} |
608 |
} |
609 |
} # _add_plus_elements |
610 |
|
611 |
sub _remove_plus_elements ($$) { |
612 |
my $self = shift; |
613 |
my $element_state = shift; |
614 |
for my $nsuri (keys %{$element_state->{plus_elements_original}}) { |
615 |
for my $ln (keys %{$element_state->{plus_elements_original}->{$nsuri}}) { |
616 |
delete $self->{plus_elements}->{$nsuri}->{$ln}; |
617 |
} |
618 |
} |
619 |
} # _remove_plus_elements |
620 |
|
621 |
sub _attr_status_info ($$$) { |
622 |
my ($self, $attr, $status_code) = @_; |
623 |
|
624 |
if (not ($status_code & FEATURE_ALLOWED)) { |
625 |
$self->{onerror}->(node => $attr, |
626 |
type => 'attribute not defined', |
627 |
level => $self->{must_level}); |
628 |
} elsif ($status_code & FEATURE_DEPRECATED_SHOULD) { |
629 |
$self->{onerror}->(node => $attr, |
630 |
type => 'deprecated:attr', |
631 |
level => $self->{should_level}); |
632 |
} elsif ($status_code & FEATURE_DEPRECATED_INFO) { |
633 |
$self->{onerror}->(node => $attr, |
634 |
type => 'deprecated:attr', |
635 |
level => $self->{info_level}); |
636 |
} |
637 |
|
638 |
my $status; |
639 |
if ($status_code & FEATURE_STATUS_REC) { |
640 |
return; |
641 |
} elsif ($status_code & FEATURE_STATUS_CR) { |
642 |
$status = 'cr'; |
643 |
} elsif ($status_code & FEATURE_STATUS_LC) { |
644 |
$status = 'lc'; |
645 |
} elsif ($status_code & FEATURE_STATUS_WD) { |
646 |
$status = 'wd'; |
647 |
} else { |
648 |
$status = 'non-standard'; |
649 |
} |
650 |
$self->{onerror}->(node => $attr, |
651 |
type => 'status:'.$status.':attr', |
652 |
level => $self->{info_level}); |
653 |
} # _attr_status_info |
654 |
|
655 |
sub _add_minuses ($@) { |
656 |
my $self = shift; |
657 |
my $r = {}; |
658 |
for my $list (@_) { |
659 |
for my $ns (keys %$list) { |
660 |
for my $ln (keys %{$list->{$ns}}) { |
661 |
unless ($self->{minuses}->{$ns}->{$ln}) { |
662 |
$self->{minuses}->{$ns}->{$ln} = 1; |
663 |
$r->{$ns}->{$ln} = 1; |
664 |
} |
665 |
} |
666 |
} |
667 |
} |
668 |
return {type => 'plus', list => $r}; |
669 |
} # _add_minuses |
670 |
|
671 |
sub _add_pluses ($@) { |
672 |
my $self = shift; |
673 |
my $r = {}; |
674 |
for my $list (@_) { |
675 |
for my $ns (keys %$list) { |
676 |
for my $ln (keys %{$list->{$ns}}) { |
677 |
unless ($self->{pluses}->{$ns}->{$ln}) { |
678 |
$self->{pluses}->{$ns}->{$ln} = 1; |
679 |
$r->{$ns}->{$ln} = 1; |
680 |
} |
681 |
} |
682 |
} |
683 |
} |
684 |
return {type => 'minus', list => $r}; |
685 |
} # _add_pluses |
686 |
|
687 |
sub _remove_minuses ($$) { |
688 |
my ($self, $todo) = @_; |
689 |
if ($todo->{type} eq 'minus') { |
690 |
for my $ns (keys %{$todo->{list}}) { |
691 |
for my $ln (keys %{$todo->{list}->{$ns}}) { |
692 |
delete $self->{pluses}->{$ns}->{$ln} if $todo->{list}->{$ns}->{$ln}; |
693 |
} |
694 |
} |
695 |
} elsif ($todo->{type} eq 'plus') { |
696 |
for my $ns (keys %{$todo->{list}}) { |
697 |
for my $ln (keys %{$todo->{list}->{$ns}}) { |
698 |
delete $self->{minuses}->{$ns}->{$ln} if $todo->{list}->{$ns}->{$ln}; |
699 |
} |
700 |
} |
701 |
} else { |
702 |
die "$0: Unknown +- type: $todo->{type}"; |
703 |
} |
704 |
1; |
705 |
} # _remove_minuses |
706 |
|
707 |
## NOTE: Priority for "minuses" and "pluses" are currently left |
708 |
## undefined and implemented inconsistently; it is not a problem for |
709 |
## now, since no element belongs to both lists. |
710 |
|
711 |
sub _check_get_children ($$$) { |
712 |
my ($self, $node, $parent_todo) = @_; |
713 |
my $new_todos = []; |
714 |
my $sib = []; |
715 |
TP: { |
716 |
my $node_ns = $node->namespace_uri; |
717 |
$node_ns = '' unless defined $node_ns; |
718 |
my $node_ln = $node->manakai_local_name; |
719 |
if ($HTMLTransparentElements->{$node_ns}->{$node_ln}) { |
720 |
if ($node_ns eq $HTML_NS and $node_ln eq 'noscript') { |
721 |
if ($parent_todo->{flag}->{in_head}) { |
722 |
# |
723 |
} else { |
724 |
my $end = $self->_add_minuses ({$HTML_NS, {noscript => 1}}); |
725 |
push @$sib, $end; |
726 |
|
727 |
unshift @$sib, @{$node->child_nodes}; |
728 |
push @$new_todos, {type => 'element-attributes', node => $node}; |
729 |
last TP; |
730 |
} |
731 |
} elsif ($node_ns eq $HTML_NS and $node_ln eq 'del') { |
732 |
my $sig_flag = $parent_todo->{flag}->{has_descendant}->{significant}; |
733 |
unshift @$sib, @{$node->child_nodes}; |
734 |
push @$new_todos, {type => 'element-attributes', node => $node}; |
735 |
push @$new_todos, |
736 |
{type => 'code', |
737 |
code => sub { |
738 |
$parent_todo->{flag}->{has_descendant}->{significant} = 0 |
739 |
if not $sig_flag; |
740 |
}}; |
741 |
last TP; |
742 |
} else { |
743 |
unshift @$sib, @{$node->child_nodes}; |
744 |
push @$new_todos, {type => 'element-attributes', node => $node}; |
745 |
last TP; |
746 |
} |
747 |
} |
748 |
if ($node_ns eq $HTML_NS and ($node_ln eq 'video' or $node_ln eq 'audio')) { |
749 |
if ($node->has_attribute_ns (undef, 'src')) { |
750 |
unshift @$sib, @{$node->child_nodes}; |
751 |
push @$new_todos, {type => 'element-attributes', node => $node}; |
752 |
last TP; |
753 |
} else { |
754 |
my @cn = @{$node->child_nodes}; |
755 |
CN: while (@cn) { |
756 |
my $cn = shift @cn; |
757 |
my $cnt = $cn->node_type; |
758 |
if ($cnt == 1) { |
759 |
my $cn_nsuri = $cn->namespace_uri; |
760 |
$cn_nsuri = '' unless defined $cn_nsuri; |
761 |
if ($cn_nsuri eq $HTML_NS and $cn->manakai_local_name eq 'source') { |
762 |
# |
763 |
} else { |
764 |
last CN; |
765 |
} |
766 |
} elsif ($cnt == 3 or $cnt == 4) { |
767 |
if ($cn->data =~ /[^\x09-\x0D\x20]/) { |
768 |
last CN; |
769 |
} |
770 |
} |
771 |
} # CN |
772 |
unshift @$sib, @cn; |
773 |
} |
774 |
} elsif ($node_ns eq $HTML_NS and $node_ln eq 'object') { |
775 |
my @cn = @{$node->child_nodes}; |
776 |
CN: while (@cn) { |
777 |
my $cn = shift @cn; |
778 |
my $cnt = $cn->node_type; |
779 |
if ($cnt == 1) { |
780 |
my $cn_nsuri = $cn->namespace_uri; |
781 |
$cn_nsuri = '' unless defined $cn_nsuri; |
782 |
if ($cn_nsuri eq $HTML_NS and $cn->manakai_local_name eq 'param') { |
783 |
# |
784 |
} else { |
785 |
last CN; |
786 |
} |
787 |
} elsif ($cnt == 3 or $cnt == 4) { |
788 |
if ($cn->data =~ /[^\x09-\x0D\x20]/) { |
789 |
last CN; |
790 |
} |
791 |
} |
792 |
} # CN |
793 |
unshift @$sib, @cn; |
794 |
} |
795 |
push @$new_todos, {type => 'element', node => $node}; |
796 |
} # TP |
797 |
|
798 |
for my $new_todo (@$new_todos) { |
799 |
$new_todo->{flag} = {%{$parent_todo->{flag} or {}}}; |
800 |
} |
801 |
|
802 |
return ($sib, $new_todos); |
803 |
} # _check_get_children |
804 |
|
805 |
=head1 LICENSE |
806 |
|
807 |
Copyright 2007-2008 Wakaba <w@suika.fam.cx> |
808 |
|
809 |
This library is free software; you can redistribute it |
810 |
and/or modify it under the same terms as Perl itself. |
811 |
|
812 |
=cut |
813 |
|
814 |
1; |
815 |
# $Date: 2008/03/20 10:30:20 $ |