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