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