1 |
wakaba |
1.1 |
package Whatpm::ContentChecker; |
2 |
|
|
use strict; |
3 |
wakaba |
1.98 |
our $VERSION=do{my @r=(q$Revision: 1.97 $=~/\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 |
|
|
check_start => sub { }, |
225 |
|
|
check_attrs => sub { |
226 |
|
|
my ($self, $item, $element_state) = @_; |
227 |
|
|
for my $attr (@{$item->{node}->attributes}) { |
228 |
wakaba |
1.9 |
my $attr_ns = $attr->namespace_uri; |
229 |
wakaba |
1.92 |
if (defined $attr_ns) { |
230 |
|
|
load_ns_module ($attr_ns); |
231 |
|
|
} else { |
232 |
|
|
$attr_ns = ''; |
233 |
|
|
} |
234 |
wakaba |
1.9 |
my $attr_ln = $attr->manakai_local_name; |
235 |
wakaba |
1.79 |
|
236 |
wakaba |
1.9 |
my $checker = $AttrChecker->{$attr_ns}->{$attr_ln} |
237 |
wakaba |
1.60 |
|| $AttrChecker->{$attr_ns}->{''}; |
238 |
wakaba |
1.79 |
my $status = $AttrStatus->{$attr_ns}->{$attr_ln} |
239 |
|
|
|| $AttrStatus->{$attr_ns}->{''}; |
240 |
|
|
if (not defined $status) { |
241 |
|
|
$status = FEATURE_ALLOWED; |
242 |
|
|
## NOTE: FEATURE_ALLOWED for all attributes, since the element |
243 |
|
|
## is not supported and therefore "attribute not defined" error |
244 |
|
|
## should not raised (too verbose) and global attributes should be |
245 |
|
|
## allowed anyway (if a global attribute has its specified creteria |
246 |
|
|
## for where it may be specified, then it should be checked in it's |
247 |
|
|
## checker function). |
248 |
|
|
} |
249 |
wakaba |
1.9 |
if ($checker) { |
250 |
|
|
$checker->($self, $attr); |
251 |
wakaba |
1.17 |
} else { |
252 |
wakaba |
1.83 |
$self->{onerror}->(node => $attr, |
253 |
|
|
type => 'unknown attribute', |
254 |
|
|
level => $self->{level}->{uncertain}); |
255 |
wakaba |
1.9 |
} |
256 |
wakaba |
1.79 |
$self->_attr_status_info ($attr, $status); |
257 |
wakaba |
1.9 |
} |
258 |
|
|
}, |
259 |
wakaba |
1.60 |
check_child_element => sub { |
260 |
|
|
my ($self, $item, $child_el, $child_nsuri, $child_ln, |
261 |
|
|
$child_is_transparent, $element_state) = @_; |
262 |
|
|
if ($self->{minus_elements}->{$child_nsuri}->{$child_ln}) { |
263 |
|
|
$self->{onerror}->(node => $child_el, |
264 |
|
|
type => 'element not allowed:minus', |
265 |
wakaba |
1.83 |
level => $self->{level}->{must}); |
266 |
wakaba |
1.60 |
} elsif ($self->{plus_elements}->{$child_nsuri}->{$child_ln}) { |
267 |
|
|
# |
268 |
|
|
} else { |
269 |
|
|
# |
270 |
|
|
} |
271 |
|
|
}, |
272 |
|
|
check_child_text => sub { }, |
273 |
|
|
check_end => sub { |
274 |
|
|
my ($self, $item, $element_state) = @_; |
275 |
wakaba |
1.82 |
## NOTE: There is a modified copy of the code below for |html:ruby|. |
276 |
wakaba |
1.60 |
if ($element_state->{has_significant}) { |
277 |
wakaba |
1.66 |
$item->{real_parent_state}->{has_significant} = 1; |
278 |
wakaba |
1.60 |
} |
279 |
|
|
}, |
280 |
|
|
); |
281 |
|
|
|
282 |
|
|
our $ElementDefault = { |
283 |
|
|
%AnyChecker, |
284 |
wakaba |
1.70 |
status => FEATURE_ALLOWED, |
285 |
|
|
## NOTE: No "element not defined" error - it is not supported anyway. |
286 |
wakaba |
1.60 |
check_start => sub { |
287 |
|
|
my ($self, $item, $element_state) = @_; |
288 |
wakaba |
1.83 |
$self->{onerror}->(node => $item->{node}, |
289 |
|
|
type => 'unknown element', |
290 |
|
|
level => $self->{level}->{uncertain}); |
291 |
wakaba |
1.60 |
}, |
292 |
wakaba |
1.1 |
}; |
293 |
|
|
|
294 |
wakaba |
1.60 |
our $HTMLEmbeddedContent = { |
295 |
|
|
## NOTE: All embedded content is also phrasing content. |
296 |
|
|
$HTML_NS => { |
297 |
|
|
img => 1, iframe => 1, embed => 1, object => 1, video => 1, audio => 1, |
298 |
|
|
canvas => 1, |
299 |
|
|
}, |
300 |
|
|
q<http://www.w3.org/1998/Math/MathML> => {math => 1}, |
301 |
|
|
q<http://www.w3.org/2000/svg> => {svg => 1}, |
302 |
|
|
## NOTE: Foreign elements with content (but no metadata) are |
303 |
|
|
## embedded content. |
304 |
|
|
}; |
305 |
|
|
|
306 |
wakaba |
1.95 |
our $IsInHTMLInteractiveContent = sub { |
307 |
|
|
my ($el, $nsuri, $ln) = @_; |
308 |
|
|
|
309 |
|
|
## NOTE: This CODE returns whether an element that is conditionally |
310 |
|
|
## categorizzed as an interactive content is currently in that |
311 |
|
|
## condition or not. See $HTMLInteractiveContent list defined in |
312 |
|
|
## Whatpm::ContentChecler::HTML for the list of all (conditionally |
313 |
|
|
## or permanently) interactive content. |
314 |
|
|
|
315 |
|
|
if ($nsuri eq $HTML_NS and ($ln eq 'video' or $ln eq 'audio')) { |
316 |
|
|
return $el->has_attribute ('controls'); |
317 |
|
|
} elsif ($nsuri eq $HTML_NS and $ln eq 'menu') { |
318 |
|
|
my $value = $el->get_attribute ('type'); |
319 |
|
|
$value =~ tr/A-Z/a-z/; # ASCII case-insensitive |
320 |
|
|
return ($value eq 'toolbar'); |
321 |
|
|
} else { |
322 |
|
|
return 1; |
323 |
|
|
} |
324 |
|
|
}; # $IsInHTMLInteractiveContent |
325 |
|
|
|
326 |
wakaba |
1.7 |
my $HTMLTransparentElements = { |
327 |
wakaba |
1.90 |
$HTML_NS => {qw/ins 1 del 1 font 1 noscript 1 canvas 1 a 1/}, |
328 |
wakaba |
1.29 |
## NOTE: |html:noscript| is transparent if scripting is disabled |
329 |
|
|
## and not in |head|. |
330 |
wakaba |
1.7 |
}; |
331 |
|
|
|
332 |
wakaba |
1.61 |
my $HTMLSemiTransparentElements = { |
333 |
|
|
$HTML_NS => {object => 1, video => 1, audio => 1}, |
334 |
|
|
}; |
335 |
wakaba |
1.57 |
|
336 |
wakaba |
1.42 |
our $Element = {}; |
337 |
wakaba |
1.7 |
|
338 |
wakaba |
1.73 |
$Element->{q<http://www.w3.org/1999/02/22-rdf-syntax-ns#>}->{RDF} = { |
339 |
|
|
%AnyChecker, |
340 |
|
|
status => FEATURE_STATUS_REC | FEATURE_ALLOWED, |
341 |
|
|
is_root => 1, ## ISSUE: Not explicitly allowed for non application/rdf+xml |
342 |
|
|
check_start => sub { |
343 |
|
|
my ($self, $item, $element_state) = @_; |
344 |
|
|
my $triple = []; |
345 |
|
|
push @{$self->{return}->{rdf}}, [$item->{node}, $triple]; |
346 |
|
|
require Whatpm::RDFXML; |
347 |
|
|
my $rdf = Whatpm::RDFXML->new; |
348 |
wakaba |
1.75 |
## TODO: Should we make bnodeid unique in a document? |
349 |
wakaba |
1.73 |
$rdf->{onerror} = $self->{onerror}; |
350 |
wakaba |
1.84 |
$rdf->{level} = $self->{level}; |
351 |
wakaba |
1.73 |
$rdf->{ontriple} = sub { |
352 |
|
|
my %opt = @_; |
353 |
|
|
push @$triple, |
354 |
|
|
[$opt{node}, $opt{subject}, $opt{predicate}, $opt{object}]; |
355 |
wakaba |
1.74 |
if (defined $opt{id}) { |
356 |
|
|
push @$triple, |
357 |
|
|
[$opt{node}, |
358 |
|
|
$opt{id}, |
359 |
|
|
{uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#subject>}, |
360 |
|
|
$opt{subject}]; |
361 |
|
|
push @$triple, |
362 |
|
|
[$opt{node}, |
363 |
|
|
$opt{id}, |
364 |
|
|
{uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate>}, |
365 |
|
|
$opt{predicate}]; |
366 |
|
|
push @$triple, |
367 |
|
|
[$opt{node}, |
368 |
|
|
$opt{id}, |
369 |
|
|
{uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#object>}, |
370 |
|
|
$opt{object}]; |
371 |
|
|
push @$triple, |
372 |
|
|
[$opt{node}, |
373 |
|
|
$opt{id}, |
374 |
|
|
{uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>}, |
375 |
|
|
{uri => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement>}]; |
376 |
|
|
} |
377 |
wakaba |
1.73 |
}; |
378 |
|
|
$rdf->convert_rdf_element ($item->{node}); |
379 |
|
|
}, |
380 |
|
|
}; |
381 |
|
|
|
382 |
wakaba |
1.83 |
my $default_error_level = { |
383 |
|
|
must => 'm', |
384 |
|
|
should => 's', |
385 |
|
|
warn => 'w', |
386 |
|
|
good => 'w', |
387 |
wakaba |
1.93 |
undefined => 'w', |
388 |
wakaba |
1.83 |
info => 'i', |
389 |
wakaba |
1.85 |
|
390 |
wakaba |
1.83 |
uncertain => 'u', |
391 |
|
|
|
392 |
wakaba |
1.84 |
html4_fact => 'm', |
393 |
wakaba |
1.89 |
html5_no_may => 'm', |
394 |
|
|
|
395 |
wakaba |
1.83 |
xml_error => 'm', ## TODO: correct? |
396 |
wakaba |
1.88 |
xml_id_error => 'm', ## TODO: ? |
397 |
wakaba |
1.83 |
nc => 'm', ## XML Namespace Constraints ## TODO: correct? |
398 |
wakaba |
1.84 |
|
399 |
wakaba |
1.91 |
## |Whatpm::URIChecker| |
400 |
wakaba |
1.85 |
uri_syntax => 'm', |
401 |
|
|
uri_fact => 'm', |
402 |
|
|
uri_lc_must => 'm', |
403 |
|
|
uri_lc_should => 'w', |
404 |
|
|
|
405 |
wakaba |
1.91 |
## |Whatpm::IMTChecker| |
406 |
wakaba |
1.87 |
mime_must => 'm', # lowercase "must" |
407 |
|
|
mime_fact => 'm', |
408 |
|
|
mime_strongly_discouraged => 'w', |
409 |
|
|
mime_discouraged => 'w', |
410 |
|
|
|
411 |
wakaba |
1.91 |
## |Whatpm::LangTag| |
412 |
wakaba |
1.85 |
langtag_fact => 'm', |
413 |
|
|
|
414 |
wakaba |
1.91 |
## |Whatpm::RDFXML| |
415 |
wakaba |
1.84 |
rdf_fact => 'm', |
416 |
|
|
rdf_grammer => 'm', |
417 |
|
|
rdf_lc_must => 'm', |
418 |
wakaba |
1.91 |
|
419 |
|
|
## |Message::Charset::Info| and |Whatpm::Charset::DecodeHandle| |
420 |
|
|
charset_variant => 'm', |
421 |
|
|
## An error caused by use of a variant charset that is not conforming |
422 |
|
|
## to the original charset (e.g. use of 0x80 in an ISO-8859-1 document |
423 |
|
|
## which is interpreted as a Windows-1252 document instead). |
424 |
|
|
charset_fact => 'm', |
425 |
|
|
iso_shall => 'm', |
426 |
wakaba |
1.83 |
}; |
427 |
|
|
|
428 |
wakaba |
1.56 |
sub check_document ($$$;$) { |
429 |
|
|
my ($self, $doc, $onerror, $onsubdoc) = @_; |
430 |
wakaba |
1.42 |
$self = bless {}, $self unless ref $self; |
431 |
|
|
$self->{onerror} = $onerror; |
432 |
wakaba |
1.56 |
$self->{onsubdoc} = $onsubdoc || sub { |
433 |
|
|
warn "A subdocument is not conformance-checked"; |
434 |
|
|
}; |
435 |
wakaba |
1.1 |
|
436 |
wakaba |
1.83 |
$self->{level} ||= $default_error_level; |
437 |
wakaba |
1.48 |
|
438 |
wakaba |
1.73 |
## TODO: If application/rdf+xml, RDF/XML mode should be invoked. |
439 |
|
|
|
440 |
wakaba |
1.42 |
my $docel = $doc->document_element; |
441 |
|
|
unless (defined $docel) { |
442 |
|
|
## ISSUE: Should we check content of Document node? |
443 |
wakaba |
1.83 |
$onerror->(node => $doc, type => 'no document element', |
444 |
|
|
level => $self->{level}->{must}); |
445 |
wakaba |
1.42 |
## ISSUE: Is this non-conforming (to what spec)? Or just a warning? |
446 |
|
|
return { |
447 |
|
|
class => {}, |
448 |
|
|
id => {}, table => [], term => {}, |
449 |
|
|
}; |
450 |
wakaba |
1.1 |
} |
451 |
|
|
|
452 |
wakaba |
1.42 |
## ISSUE: Unexpanded entity references and HTML5 conformance |
453 |
wakaba |
1.1 |
|
454 |
wakaba |
1.42 |
my $docel_nsuri = $docel->namespace_uri; |
455 |
wakaba |
1.92 |
if (defined $docel_nsuri) { |
456 |
|
|
load_ns_module ($docel_nsuri); |
457 |
|
|
} else { |
458 |
|
|
$docel_nsuri = ''; |
459 |
|
|
} |
460 |
wakaba |
1.42 |
my $docel_def = $Element->{$docel_nsuri}->{$docel->manakai_local_name} || |
461 |
|
|
$Element->{$docel_nsuri}->{''} || |
462 |
|
|
$ElementDefault; |
463 |
|
|
if ($docel_def->{is_root}) { |
464 |
|
|
# |
465 |
wakaba |
1.50 |
} elsif ($docel_def->{is_xml_root}) { |
466 |
|
|
unless ($doc->manakai_is_html) { |
467 |
|
|
# |
468 |
|
|
} else { |
469 |
wakaba |
1.83 |
$onerror->(node => $docel, type => 'element not allowed:root:xml', |
470 |
|
|
level => $self->{level}->{must}); |
471 |
wakaba |
1.50 |
} |
472 |
wakaba |
1.42 |
} else { |
473 |
wakaba |
1.83 |
$onerror->(node => $docel, type => 'element not allowed:root', |
474 |
|
|
level => $self->{level}->{must}); |
475 |
wakaba |
1.1 |
} |
476 |
|
|
|
477 |
wakaba |
1.42 |
## TODO: Check for other items other than document element |
478 |
|
|
## (second (errorous) element, text nodes, PI nodes, doctype nodes) |
479 |
wakaba |
1.2 |
|
480 |
wakaba |
1.56 |
my $return = $self->check_element ($docel, $onerror, $onsubdoc); |
481 |
wakaba |
1.51 |
|
482 |
wakaba |
1.52 |
## TODO: Test for these checks are necessary. |
483 |
wakaba |
1.51 |
my $charset_name = $doc->input_encoding; |
484 |
|
|
if (defined $charset_name) { |
485 |
|
|
require Message::Charset::Info; |
486 |
|
|
my $charset = $Message::Charset::Info::IANACharset->{$charset_name}; |
487 |
|
|
|
488 |
wakaba |
1.71 |
if ($doc->manakai_is_html) { |
489 |
|
|
if (not $doc->manakai_has_bom and |
490 |
|
|
not defined $doc->manakai_charset) { |
491 |
|
|
unless ($charset->{is_html_ascii_superset}) { |
492 |
wakaba |
1.86 |
$onerror->(node => $doc, |
493 |
|
|
level => $self->{level}->{must}, |
494 |
wakaba |
1.83 |
type => 'non ascii superset', |
495 |
|
|
text => $charset_name); |
496 |
wakaba |
1.71 |
} |
497 |
|
|
|
498 |
|
|
if (not $self->{has_charset} and ## TODO: This does not work now. |
499 |
|
|
not $charset->{iana_names}->{'us-ascii'}) { |
500 |
wakaba |
1.86 |
$onerror->(node => $doc, |
501 |
|
|
level => $self->{level}->{must}, |
502 |
wakaba |
1.83 |
type => 'no character encoding declaration', |
503 |
|
|
text => $charset_name); |
504 |
wakaba |
1.71 |
} |
505 |
wakaba |
1.51 |
} |
506 |
wakaba |
1.71 |
|
507 |
|
|
if ($charset->{iana_names}->{'utf-8'}) { |
508 |
|
|
# |
509 |
|
|
} elsif ($charset->{iana_names}->{'jis_x0212-1990'} or |
510 |
|
|
$charset->{iana_names}->{'x-jis0208'} or |
511 |
|
|
$charset->{iana_names}->{'utf-32'} or ## ISSUE: UTF-32BE? UTF-32LE? |
512 |
wakaba |
1.91 |
($charset->{category} & Message::Charset::Info::CHARSET_CATEGORY_EBCDIC ())) { |
513 |
wakaba |
1.71 |
$onerror->(node => $doc, |
514 |
wakaba |
1.83 |
type => 'bad character encoding', |
515 |
|
|
text => $charset_name, |
516 |
|
|
level => $self->{level}->{should}, |
517 |
|
|
layer => 'encode'); |
518 |
wakaba |
1.71 |
} elsif ($charset->{iana_names}->{'cesu-8'} or |
519 |
|
|
$charset->{iana_names}->{'utf-8'} or ## ISSUE: UNICODE-1-1-UTF-7? |
520 |
|
|
$charset->{iana_names}->{'bocu-1'} or |
521 |
|
|
$charset->{iana_names}->{'scsu'}) { |
522 |
|
|
$onerror->(node => $doc, |
523 |
wakaba |
1.83 |
type => 'disallowed character encoding', |
524 |
|
|
text => $charset_name, |
525 |
|
|
level => $self->{level}->{must}, |
526 |
|
|
layer => 'encode'); |
527 |
wakaba |
1.71 |
} else { |
528 |
|
|
$onerror->(node => $doc, |
529 |
wakaba |
1.83 |
type => 'non-utf-8 character encoding', |
530 |
|
|
text => $charset_name, |
531 |
|
|
level => $self->{level}->{good}, |
532 |
|
|
layer => 'encode'); |
533 |
wakaba |
1.51 |
} |
534 |
|
|
} |
535 |
wakaba |
1.52 |
} elsif ($doc->manakai_is_html) { |
536 |
|
|
## NOTE: MUST and SHOULD requirements above cannot be tested, |
537 |
|
|
## since the document has no input charset encoding information. |
538 |
|
|
$onerror->(node => $doc, |
539 |
wakaba |
1.83 |
type => 'character encoding unchecked', |
540 |
|
|
level => $self->{level}->{info}, |
541 |
|
|
layer => 'encode'); |
542 |
wakaba |
1.51 |
} |
543 |
|
|
|
544 |
|
|
return $return; |
545 |
wakaba |
1.42 |
} # check_document |
546 |
wakaba |
1.1 |
|
547 |
wakaba |
1.81 |
## Check an element. The element is checked as if it is an orphan node (i.e. |
548 |
|
|
## an element without a parent node). |
549 |
wakaba |
1.56 |
sub check_element ($$$;$) { |
550 |
|
|
my ($self, $el, $onerror, $onsubdoc) = @_; |
551 |
wakaba |
1.42 |
$self = bless {}, $self unless ref $self; |
552 |
|
|
$self->{onerror} = $onerror; |
553 |
wakaba |
1.56 |
$self->{onsubdoc} = $onsubdoc || sub { |
554 |
|
|
warn "A subdocument is not conformance-checked"; |
555 |
|
|
}; |
556 |
wakaba |
1.2 |
|
557 |
wakaba |
1.83 |
$self->{level} ||= $default_error_level; |
558 |
wakaba |
1.48 |
|
559 |
wakaba |
1.61 |
$self->{plus_elements} = {}; |
560 |
|
|
$self->{minus_elements} = {}; |
561 |
wakaba |
1.42 |
$self->{id} = {}; |
562 |
wakaba |
1.98 |
$self->{id_type} = {}; # 'menu' / 'labelable' |
563 |
wakaba |
1.97 |
$self->{form} = {}; |
564 |
wakaba |
1.42 |
$self->{term} = {}; |
565 |
|
|
$self->{usemap} = []; |
566 |
wakaba |
1.78 |
$self->{ref} = []; # datetemplate data references |
567 |
|
|
$self->{template} = []; # datatemplate template references |
568 |
wakaba |
1.42 |
$self->{contextmenu} = []; |
569 |
|
|
$self->{map} = {}; |
570 |
|
|
$self->{has_link_type} = {}; |
571 |
wakaba |
1.60 |
$self->{flag} = {}; |
572 |
wakaba |
1.46 |
#$self->{has_uri_attr}; |
573 |
|
|
#$self->{has_hyperlink_element}; |
574 |
wakaba |
1.51 |
#$self->{has_charset}; |
575 |
wakaba |
1.57 |
#$self->{has_base}; |
576 |
wakaba |
1.42 |
$self->{return} = { |
577 |
|
|
class => {}, |
578 |
wakaba |
1.80 |
id => $self->{id}, |
579 |
|
|
table => [], # table objects returned by Whatpm::HTMLTable |
580 |
|
|
term => $self->{term}, |
581 |
wakaba |
1.76 |
uri => {}, # URIs other than those in RDF triples |
582 |
|
|
## TODO: xmlns="", SYSTEM "", atom:* src="", xml:base="" |
583 |
wakaba |
1.73 |
rdf => [], |
584 |
wakaba |
1.42 |
}; |
585 |
wakaba |
1.4 |
|
586 |
wakaba |
1.60 |
my @item = ({type => 'element', node => $el, parent_state => {}}); |
587 |
wakaba |
1.66 |
$item[-1]->{real_parent_state} = $item[-1]->{parent_state}; |
588 |
wakaba |
1.60 |
while (@item) { |
589 |
|
|
my $item = shift @item; |
590 |
|
|
if (ref $item eq 'ARRAY') { |
591 |
|
|
my $code = shift @$item; |
592 |
|
|
next unless $code;## TODO: temp. |
593 |
|
|
$code->(@$item); |
594 |
|
|
} elsif ($item->{type} eq 'element') { |
595 |
|
|
my $el_nsuri = $item->{node}->namespace_uri; |
596 |
wakaba |
1.92 |
if (defined $el_nsuri) { |
597 |
|
|
load_ns_module ($el_nsuri); |
598 |
|
|
} else { |
599 |
|
|
$el_nsuri = ''; |
600 |
|
|
} |
601 |
wakaba |
1.60 |
my $el_ln = $item->{node}->manakai_local_name; |
602 |
wakaba |
1.92 |
|
603 |
wakaba |
1.63 |
my $element_state = {}; |
604 |
wakaba |
1.60 |
my $eldef = $Element->{$el_nsuri}->{$el_ln} || |
605 |
|
|
$Element->{$el_nsuri}->{''} || |
606 |
wakaba |
1.42 |
$ElementDefault; |
607 |
wakaba |
1.61 |
my $content_def = $item->{transparent} |
608 |
|
|
? $item->{parent_def} || $eldef : $eldef; |
609 |
wakaba |
1.63 |
my $content_state = $item->{transparent} |
610 |
wakaba |
1.65 |
? $item->{parent_def} |
611 |
|
|
? $item->{parent_state} || $element_state : $element_state |
612 |
|
|
: $element_state; |
613 |
wakaba |
1.60 |
|
614 |
wakaba |
1.67 |
unless ($eldef->{status} & FEATURE_STATUS_REC) { |
615 |
|
|
my $status = $eldef->{status} & FEATURE_STATUS_CR ? 'cr' : |
616 |
|
|
$eldef->{status} & FEATURE_STATUS_LC ? 'lc' : |
617 |
|
|
$eldef->{status} & FEATURE_STATUS_WD ? 'wd' : 'non-standard'; |
618 |
|
|
$self->{onerror}->(node => $item->{node}, |
619 |
|
|
type => 'status:'.$status.':element', |
620 |
wakaba |
1.83 |
level => $self->{level}->{info}); |
621 |
wakaba |
1.67 |
} |
622 |
wakaba |
1.70 |
if (not ($eldef->{status} & FEATURE_ALLOWED)) { |
623 |
|
|
$self->{onerror}->(node => $item->{node}, |
624 |
|
|
type => 'element not defined', |
625 |
wakaba |
1.83 |
level => $self->{level}->{must}); |
626 |
wakaba |
1.70 |
} elsif ($eldef->{status} & FEATURE_DEPRECATED_SHOULD) { |
627 |
|
|
$self->{onerror}->(node => $item->{node}, |
628 |
|
|
type => 'deprecated:element', |
629 |
wakaba |
1.83 |
level => $self->{level}->{should}); |
630 |
wakaba |
1.70 |
} elsif ($eldef->{status} & FEATURE_DEPRECATED_INFO) { |
631 |
|
|
$self->{onerror}->(node => $item->{node}, |
632 |
|
|
type => 'deprecated:element', |
633 |
wakaba |
1.83 |
level => $self->{level}->{info}); |
634 |
wakaba |
1.70 |
} |
635 |
wakaba |
1.67 |
|
636 |
wakaba |
1.60 |
my @new_item; |
637 |
|
|
push @new_item, [$eldef->{check_start}, $self, $item, $element_state]; |
638 |
|
|
push @new_item, [$eldef->{check_attrs}, $self, $item, $element_state]; |
639 |
wakaba |
1.61 |
|
640 |
wakaba |
1.60 |
my @child = @{$item->{node}->child_nodes}; |
641 |
|
|
while (@child) { |
642 |
|
|
my $child = shift @child; |
643 |
|
|
my $child_nt = $child->node_type; |
644 |
|
|
if ($child_nt == 1) { # ELEMENT_NODE |
645 |
|
|
my $child_nsuri = $child->namespace_uri; |
646 |
|
|
$child_nsuri = '' unless defined $child_nsuri; |
647 |
|
|
my $child_ln = $child->manakai_local_name; |
648 |
|
|
if ($HTMLTransparentElements->{$child_nsuri}->{$child_ln} and |
649 |
|
|
not (($self->{flag}->{in_head} or |
650 |
wakaba |
1.61 |
($el_nsuri eq $HTML_NS and $el_ln eq 'head')) and |
651 |
|
|
$child_nsuri eq $HTML_NS and $child_ln eq 'noscript')) { |
652 |
wakaba |
1.60 |
push @new_item, [$content_def->{check_child_element}, |
653 |
|
|
$self, $item, $child, |
654 |
wakaba |
1.66 |
$child_nsuri, $child_ln, 1, |
655 |
|
|
$content_state, $element_state]; |
656 |
wakaba |
1.60 |
push @new_item, {type => 'element', node => $child, |
657 |
wakaba |
1.65 |
parent_state => $content_state, |
658 |
wakaba |
1.61 |
parent_def => $content_def, |
659 |
wakaba |
1.66 |
real_parent_state => $element_state, |
660 |
wakaba |
1.60 |
transparent => 1}; |
661 |
|
|
} else { |
662 |
wakaba |
1.65 |
if ($item->{parent_def} and # has parent |
663 |
|
|
$el_nsuri eq $HTML_NS) { ## $HTMLSemiTransparentElements |
664 |
wakaba |
1.61 |
if ($el_ln eq 'object') { |
665 |
|
|
if ($self->{plus_elements}->{$child_nsuri}->{$child_ln}) { |
666 |
|
|
# |
667 |
|
|
} elsif ($child_nsuri eq $HTML_NS and $child_ln eq 'param') { |
668 |
|
|
# |
669 |
|
|
} else { |
670 |
wakaba |
1.62 |
$content_def = $item->{parent_def} || $content_def; |
671 |
wakaba |
1.63 |
$content_state = $item->{parent_state} || $content_state; |
672 |
wakaba |
1.62 |
} |
673 |
|
|
} elsif ($el_ln eq 'video' or $el_ln eq 'audio') { |
674 |
|
|
if ($self->{plus_elements}->{$child_nsuri}->{$child_ln}) { |
675 |
|
|
# |
676 |
|
|
} elsif ($child_nsuri eq $HTML_NS and $child_ln eq 'source') { |
677 |
|
|
$element_state->{has_source} = 1; |
678 |
|
|
} else { |
679 |
|
|
$content_def = $item->{parent_def} || $content_def; |
680 |
wakaba |
1.63 |
$content_state = $item->{parent_state} || $content_state; |
681 |
wakaba |
1.61 |
} |
682 |
|
|
} |
683 |
|
|
} |
684 |
|
|
|
685 |
wakaba |
1.60 |
push @new_item, [$content_def->{check_child_element}, |
686 |
|
|
$self, $item, $child, |
687 |
wakaba |
1.64 |
$child_nsuri, $child_ln, |
688 |
|
|
$HTMLSemiTransparentElements |
689 |
|
|
->{$child_nsuri}->{$child_ln}, |
690 |
wakaba |
1.66 |
$content_state, $element_state]; |
691 |
wakaba |
1.60 |
push @new_item, {type => 'element', node => $child, |
692 |
wakaba |
1.65 |
parent_def => $content_def, |
693 |
wakaba |
1.66 |
real_parent_state => $element_state, |
694 |
wakaba |
1.65 |
parent_state => $content_state}; |
695 |
wakaba |
1.60 |
} |
696 |
|
|
|
697 |
|
|
if ($HTMLEmbeddedContent->{$child_nsuri}->{$child_ln}) { |
698 |
|
|
$element_state->{has_significant} = 1; |
699 |
|
|
} |
700 |
|
|
} elsif ($child_nt == 3 or # TEXT_NODE |
701 |
|
|
$child_nt == 4) { # CDATA_SECTION_NODE |
702 |
wakaba |
1.96 |
my $has_significant = ($child->data =~ /[^\x09\x0A\x0C\x0D\x20]/); |
703 |
wakaba |
1.60 |
push @new_item, [$content_def->{check_child_text}, |
704 |
|
|
$self, $item, $child, $has_significant, |
705 |
wakaba |
1.66 |
$content_state, $element_state]; |
706 |
|
|
$element_state->{has_significant} ||= $has_significant; |
707 |
wakaba |
1.61 |
if ($has_significant and |
708 |
|
|
$HTMLSemiTransparentElements->{$el_nsuri}->{$el_ln}) { |
709 |
|
|
$content_def = $item->{parent_def} || $content_def; |
710 |
|
|
} |
711 |
wakaba |
1.60 |
} elsif ($child_nt == 5) { # ENTITY_REFERENCE_NODE |
712 |
|
|
push @child, @{$child->child_nodes}; |
713 |
wakaba |
1.1 |
} |
714 |
wakaba |
1.60 |
## TODO: PI_NODE |
715 |
|
|
## TODO: Unknown node type |
716 |
wakaba |
1.1 |
} |
717 |
wakaba |
1.60 |
|
718 |
|
|
push @new_item, [$eldef->{check_end}, $self, $item, $element_state]; |
719 |
|
|
|
720 |
|
|
unshift @item, @new_item; |
721 |
wakaba |
1.30 |
} else { |
722 |
wakaba |
1.60 |
die "$0: Internal error: Unsupported checking action type |$item->{type}|"; |
723 |
wakaba |
1.4 |
} |
724 |
wakaba |
1.1 |
} |
725 |
wakaba |
1.17 |
|
726 |
wakaba |
1.78 |
for (@{$self->{template}}) { |
727 |
|
|
## TODO: If the document is an XML document, ... |
728 |
|
|
## NOTE: If the document is an HTML document: |
729 |
|
|
## ISSUE: We need to percent-decode? |
730 |
|
|
F: { |
731 |
|
|
if ($self->{id}->{$_->[0]}) { |
732 |
|
|
my $el = $self->{id}->{$_->[0]}->[0]->owner_element; |
733 |
|
|
if ($el->node_type == 1 and # ELEMENT_NODE |
734 |
|
|
$el->manakai_local_name eq 'datatemplate') { |
735 |
|
|
my $nsuri = $el->namespace_uri; |
736 |
|
|
if (defined $nsuri and $nsuri eq $HTML_NS) { |
737 |
|
|
if ($el eq $_->[1]->owner_element) { |
738 |
|
|
$self->{onerror}->(node => $_->[1], |
739 |
|
|
type => 'fragment points itself', |
740 |
wakaba |
1.83 |
level => $self->{level}->{must}); |
741 |
wakaba |
1.78 |
} |
742 |
|
|
|
743 |
|
|
last F; |
744 |
|
|
} |
745 |
|
|
} |
746 |
|
|
} |
747 |
|
|
## TODO: Should we raise a "fragment points nothing" error instead |
748 |
|
|
## if the fragment identifier identifies no element? |
749 |
|
|
|
750 |
|
|
$self->{onerror}->(node => $_->[1], type => 'template:not template', |
751 |
wakaba |
1.83 |
level => $self->{level}->{must}); |
752 |
wakaba |
1.78 |
} # F |
753 |
|
|
} |
754 |
|
|
|
755 |
|
|
for (@{$self->{ref}}) { |
756 |
|
|
## TOOD: If XML |
757 |
|
|
## NOTE: If it is an HTML document: |
758 |
|
|
if ($_->[0] eq '') { |
759 |
|
|
## NOTE: It points the top of the document. |
760 |
|
|
} elsif ($self->{id}->{$_->[0]}) { |
761 |
|
|
if ($self->{id}->{$_->[0]}->[0]->owner_element |
762 |
|
|
eq $_->[1]->owner_element) { |
763 |
|
|
$self->{onerror}->(node => $_->[1], type => 'fragment points itself', |
764 |
wakaba |
1.83 |
level => $self->{level}->{must}); |
765 |
wakaba |
1.78 |
} |
766 |
|
|
} else { |
767 |
|
|
$self->{onerror}->(node => $_->[1], type => 'fragment points nothing', |
768 |
wakaba |
1.83 |
level => $self->{level}->{must}); |
769 |
wakaba |
1.78 |
} |
770 |
|
|
} |
771 |
|
|
|
772 |
|
|
## TODO: Maybe we should have $document->manakai_get_by_fragment or something |
773 |
|
|
|
774 |
wakaba |
1.17 |
for (@{$self->{usemap}}) { |
775 |
|
|
unless ($self->{map}->{$_->[0]}) { |
776 |
wakaba |
1.83 |
$self->{onerror}->(node => $_->[1], type => 'no referenced map', |
777 |
|
|
level => $self->{level}->{must}); |
778 |
wakaba |
1.17 |
} |
779 |
|
|
} |
780 |
|
|
|
781 |
wakaba |
1.32 |
for (@{$self->{contextmenu}}) { |
782 |
wakaba |
1.98 |
if ($self->{id}->{$_->[0]} and |
783 |
|
|
$self->{id_type}->{$_->[0]} eq 'menu') { |
784 |
|
|
# |
785 |
|
|
} else { |
786 |
wakaba |
1.83 |
$self->{onerror}->(node => $_->[1], type => 'no referenced menu', |
787 |
|
|
level => $self->{level}->{must}); |
788 |
wakaba |
1.32 |
} |
789 |
|
|
} |
790 |
|
|
|
791 |
wakaba |
1.61 |
delete $self->{plus_elements}; |
792 |
|
|
delete $self->{minus_elements}; |
793 |
wakaba |
1.17 |
delete $self->{onerror}; |
794 |
|
|
delete $self->{id}; |
795 |
wakaba |
1.98 |
delete $self->{id_type}; |
796 |
wakaba |
1.97 |
delete $self->{form}; |
797 |
wakaba |
1.17 |
delete $self->{usemap}; |
798 |
wakaba |
1.78 |
delete $self->{ref}; |
799 |
|
|
delete $self->{template}; |
800 |
wakaba |
1.17 |
delete $self->{map}; |
801 |
wakaba |
1.33 |
return $self->{return}; |
802 |
wakaba |
1.1 |
} # check_element |
803 |
|
|
|
804 |
wakaba |
1.60 |
sub _add_minus_elements ($$@) { |
805 |
|
|
my $self = shift; |
806 |
|
|
my $element_state = shift; |
807 |
|
|
for my $elements (@_) { |
808 |
|
|
for my $nsuri (keys %$elements) { |
809 |
|
|
for my $ln (keys %{$elements->{$nsuri}}) { |
810 |
|
|
unless ($self->{minus_elements}->{$nsuri}->{$ln}) { |
811 |
|
|
$element_state->{minus_elements_original}->{$nsuri}->{$ln} = 0; |
812 |
|
|
$self->{minus_elements}->{$nsuri}->{$ln} = 1; |
813 |
|
|
} |
814 |
|
|
} |
815 |
|
|
} |
816 |
|
|
} |
817 |
|
|
} # _add_minus_elements |
818 |
|
|
|
819 |
|
|
sub _remove_minus_elements ($$) { |
820 |
|
|
my $self = shift; |
821 |
|
|
my $element_state = shift; |
822 |
|
|
for my $nsuri (keys %{$element_state->{minus_elements_original}}) { |
823 |
|
|
for my $ln (keys %{$element_state->{minus_elements_original}->{$nsuri}}) { |
824 |
|
|
delete $self->{minus_elements}->{$nsuri}->{$ln}; |
825 |
|
|
} |
826 |
|
|
} |
827 |
|
|
} # _remove_minus_elements |
828 |
|
|
|
829 |
|
|
sub _add_plus_elements ($$@) { |
830 |
|
|
my $self = shift; |
831 |
|
|
my $element_state = shift; |
832 |
|
|
for my $elements (@_) { |
833 |
|
|
for my $nsuri (keys %$elements) { |
834 |
|
|
for my $ln (keys %{$elements->{$nsuri}}) { |
835 |
|
|
unless ($self->{plus_elements}->{$nsuri}->{$ln}) { |
836 |
|
|
$element_state->{plus_elements_original}->{$nsuri}->{$ln} = 0; |
837 |
|
|
$self->{plus_elements}->{$nsuri}->{$ln} = 1; |
838 |
|
|
} |
839 |
|
|
} |
840 |
|
|
} |
841 |
|
|
} |
842 |
|
|
} # _add_plus_elements |
843 |
|
|
|
844 |
|
|
sub _remove_plus_elements ($$) { |
845 |
|
|
my $self = shift; |
846 |
|
|
my $element_state = shift; |
847 |
|
|
for my $nsuri (keys %{$element_state->{plus_elements_original}}) { |
848 |
|
|
for my $ln (keys %{$element_state->{plus_elements_original}->{$nsuri}}) { |
849 |
|
|
delete $self->{plus_elements}->{$nsuri}->{$ln}; |
850 |
|
|
} |
851 |
|
|
} |
852 |
|
|
} # _remove_plus_elements |
853 |
|
|
|
854 |
wakaba |
1.68 |
sub _attr_status_info ($$$) { |
855 |
|
|
my ($self, $attr, $status_code) = @_; |
856 |
wakaba |
1.70 |
|
857 |
|
|
if (not ($status_code & FEATURE_ALLOWED)) { |
858 |
|
|
$self->{onerror}->(node => $attr, |
859 |
|
|
type => 'attribute not defined', |
860 |
wakaba |
1.83 |
level => $self->{level}->{must}); |
861 |
wakaba |
1.70 |
} elsif ($status_code & FEATURE_DEPRECATED_SHOULD) { |
862 |
|
|
$self->{onerror}->(node => $attr, |
863 |
|
|
type => 'deprecated:attr', |
864 |
wakaba |
1.83 |
level => $self->{level}->{should}); |
865 |
wakaba |
1.70 |
} elsif ($status_code & FEATURE_DEPRECATED_INFO) { |
866 |
|
|
$self->{onerror}->(node => $attr, |
867 |
|
|
type => 'deprecated:attr', |
868 |
wakaba |
1.83 |
level => $self->{level}->{info}); |
869 |
wakaba |
1.70 |
} |
870 |
|
|
|
871 |
wakaba |
1.68 |
my $status; |
872 |
|
|
if ($status_code & FEATURE_STATUS_REC) { |
873 |
|
|
return; |
874 |
|
|
} elsif ($status_code & FEATURE_STATUS_CR) { |
875 |
|
|
$status = 'cr'; |
876 |
|
|
} elsif ($status_code & FEATURE_STATUS_LC) { |
877 |
|
|
$status = 'lc'; |
878 |
|
|
} elsif ($status_code & FEATURE_STATUS_WD) { |
879 |
|
|
$status = 'wd'; |
880 |
|
|
} else { |
881 |
|
|
$status = 'non-standard'; |
882 |
|
|
} |
883 |
|
|
$self->{onerror}->(node => $attr, |
884 |
|
|
type => 'status:'.$status.':attr', |
885 |
wakaba |
1.83 |
level => $self->{level}->{info}); |
886 |
wakaba |
1.68 |
} # _attr_status_info |
887 |
|
|
|
888 |
wakaba |
1.2 |
sub _add_minuses ($@) { |
889 |
|
|
my $self = shift; |
890 |
|
|
my $r = {}; |
891 |
|
|
for my $list (@_) { |
892 |
|
|
for my $ns (keys %$list) { |
893 |
|
|
for my $ln (keys %{$list->{$ns}}) { |
894 |
|
|
unless ($self->{minuses}->{$ns}->{$ln}) { |
895 |
|
|
$self->{minuses}->{$ns}->{$ln} = 1; |
896 |
|
|
$r->{$ns}->{$ln} = 1; |
897 |
|
|
} |
898 |
|
|
} |
899 |
|
|
} |
900 |
|
|
} |
901 |
wakaba |
1.4 |
return {type => 'plus', list => $r}; |
902 |
wakaba |
1.2 |
} # _add_minuses |
903 |
|
|
|
904 |
wakaba |
1.50 |
sub _add_pluses ($@) { |
905 |
|
|
my $self = shift; |
906 |
|
|
my $r = {}; |
907 |
|
|
for my $list (@_) { |
908 |
|
|
for my $ns (keys %$list) { |
909 |
|
|
for my $ln (keys %{$list->{$ns}}) { |
910 |
|
|
unless ($self->{pluses}->{$ns}->{$ln}) { |
911 |
|
|
$self->{pluses}->{$ns}->{$ln} = 1; |
912 |
|
|
$r->{$ns}->{$ln} = 1; |
913 |
|
|
} |
914 |
|
|
} |
915 |
|
|
} |
916 |
|
|
} |
917 |
|
|
return {type => 'minus', list => $r}; |
918 |
|
|
} # _add_pluses |
919 |
|
|
|
920 |
wakaba |
1.2 |
sub _remove_minuses ($$) { |
921 |
wakaba |
1.4 |
my ($self, $todo) = @_; |
922 |
wakaba |
1.50 |
if ($todo->{type} eq 'minus') { |
923 |
|
|
for my $ns (keys %{$todo->{list}}) { |
924 |
|
|
for my $ln (keys %{$todo->{list}->{$ns}}) { |
925 |
|
|
delete $self->{pluses}->{$ns}->{$ln} if $todo->{list}->{$ns}->{$ln}; |
926 |
|
|
} |
927 |
wakaba |
1.2 |
} |
928 |
wakaba |
1.50 |
} elsif ($todo->{type} eq 'plus') { |
929 |
|
|
for my $ns (keys %{$todo->{list}}) { |
930 |
|
|
for my $ln (keys %{$todo->{list}->{$ns}}) { |
931 |
|
|
delete $self->{minuses}->{$ns}->{$ln} if $todo->{list}->{$ns}->{$ln}; |
932 |
|
|
} |
933 |
|
|
} |
934 |
|
|
} else { |
935 |
|
|
die "$0: Unknown +- type: $todo->{type}"; |
936 |
wakaba |
1.2 |
} |
937 |
|
|
1; |
938 |
|
|
} # _remove_minuses |
939 |
|
|
|
940 |
wakaba |
1.50 |
## NOTE: Priority for "minuses" and "pluses" are currently left |
941 |
|
|
## undefined and implemented inconsistently; it is not a problem for |
942 |
|
|
## now, since no element belongs to both lists. |
943 |
|
|
|
944 |
wakaba |
1.30 |
sub _check_get_children ($$$) { |
945 |
|
|
my ($self, $node, $parent_todo) = @_; |
946 |
wakaba |
1.4 |
my $new_todos = []; |
947 |
wakaba |
1.2 |
my $sib = []; |
948 |
|
|
TP: { |
949 |
|
|
my $node_ns = $node->namespace_uri; |
950 |
|
|
$node_ns = '' unless defined $node_ns; |
951 |
|
|
my $node_ln = $node->manakai_local_name; |
952 |
wakaba |
1.45 |
if ($HTMLTransparentElements->{$node_ns}->{$node_ln}) { |
953 |
|
|
if ($node_ns eq $HTML_NS and $node_ln eq 'noscript') { |
954 |
|
|
if ($parent_todo->{flag}->{in_head}) { |
955 |
|
|
# |
956 |
|
|
} else { |
957 |
|
|
my $end = $self->_add_minuses ({$HTML_NS, {noscript => 1}}); |
958 |
|
|
push @$sib, $end; |
959 |
|
|
|
960 |
|
|
unshift @$sib, @{$node->child_nodes}; |
961 |
|
|
push @$new_todos, {type => 'element-attributes', node => $node}; |
962 |
|
|
last TP; |
963 |
|
|
} |
964 |
wakaba |
1.58 |
} elsif ($node_ns eq $HTML_NS and $node_ln eq 'del') { |
965 |
|
|
my $sig_flag = $parent_todo->{flag}->{has_descendant}->{significant}; |
966 |
|
|
unshift @$sib, @{$node->child_nodes}; |
967 |
|
|
push @$new_todos, {type => 'element-attributes', node => $node}; |
968 |
|
|
push @$new_todos, |
969 |
|
|
{type => 'code', |
970 |
|
|
code => sub { |
971 |
|
|
$parent_todo->{flag}->{has_descendant}->{significant} = 0 |
972 |
|
|
if not $sig_flag; |
973 |
|
|
}}; |
974 |
|
|
last TP; |
975 |
wakaba |
1.45 |
} else { |
976 |
|
|
unshift @$sib, @{$node->child_nodes}; |
977 |
|
|
push @$new_todos, {type => 'element-attributes', node => $node}; |
978 |
|
|
last TP; |
979 |
wakaba |
1.2 |
} |
980 |
|
|
} |
981 |
wakaba |
1.8 |
if ($node_ns eq $HTML_NS and ($node_ln eq 'video' or $node_ln eq 'audio')) { |
982 |
wakaba |
1.2 |
if ($node->has_attribute_ns (undef, 'src')) { |
983 |
|
|
unshift @$sib, @{$node->child_nodes}; |
984 |
wakaba |
1.9 |
push @$new_todos, {type => 'element-attributes', node => $node}; |
985 |
wakaba |
1.2 |
last TP; |
986 |
|
|
} else { |
987 |
|
|
my @cn = @{$node->child_nodes}; |
988 |
|
|
CN: while (@cn) { |
989 |
|
|
my $cn = shift @cn; |
990 |
|
|
my $cnt = $cn->node_type; |
991 |
|
|
if ($cnt == 1) { |
992 |
wakaba |
1.8 |
my $cn_nsuri = $cn->namespace_uri; |
993 |
|
|
$cn_nsuri = '' unless defined $cn_nsuri; |
994 |
|
|
if ($cn_nsuri eq $HTML_NS and $cn->manakai_local_name eq 'source') { |
995 |
wakaba |
1.2 |
# |
996 |
|
|
} else { |
997 |
|
|
last CN; |
998 |
|
|
} |
999 |
|
|
} elsif ($cnt == 3 or $cnt == 4) { |
1000 |
wakaba |
1.96 |
if ($cn->data =~ /[^\x09\x0A\x0C\x0D\x20]/) { |
1001 |
wakaba |
1.2 |
last CN; |
1002 |
|
|
} |
1003 |
|
|
} |
1004 |
|
|
} # CN |
1005 |
|
|
unshift @$sib, @cn; |
1006 |
|
|
} |
1007 |
wakaba |
1.57 |
} elsif ($node_ns eq $HTML_NS and $node_ln eq 'object') { |
1008 |
|
|
my @cn = @{$node->child_nodes}; |
1009 |
|
|
CN: while (@cn) { |
1010 |
|
|
my $cn = shift @cn; |
1011 |
|
|
my $cnt = $cn->node_type; |
1012 |
|
|
if ($cnt == 1) { |
1013 |
|
|
my $cn_nsuri = $cn->namespace_uri; |
1014 |
|
|
$cn_nsuri = '' unless defined $cn_nsuri; |
1015 |
|
|
if ($cn_nsuri eq $HTML_NS and $cn->manakai_local_name eq 'param') { |
1016 |
|
|
# |
1017 |
|
|
} else { |
1018 |
|
|
last CN; |
1019 |
|
|
} |
1020 |
|
|
} elsif ($cnt == 3 or $cnt == 4) { |
1021 |
wakaba |
1.96 |
if ($cn->data =~ /[^\x09\x0A\x0C\x0D\x20]/) { |
1022 |
wakaba |
1.57 |
last CN; |
1023 |
|
|
} |
1024 |
|
|
} |
1025 |
|
|
} # CN |
1026 |
|
|
unshift @$sib, @cn; |
1027 |
wakaba |
1.2 |
} |
1028 |
wakaba |
1.4 |
push @$new_todos, {type => 'element', node => $node}; |
1029 |
wakaba |
1.2 |
} # TP |
1030 |
wakaba |
1.30 |
|
1031 |
|
|
for my $new_todo (@$new_todos) { |
1032 |
|
|
$new_todo->{flag} = {%{$parent_todo->{flag} or {}}}; |
1033 |
|
|
} |
1034 |
|
|
|
1035 |
wakaba |
1.4 |
return ($sib, $new_todos); |
1036 |
wakaba |
1.2 |
} # _check_get_children |
1037 |
|
|
|
1038 |
wakaba |
1.44 |
=head1 LICENSE |
1039 |
|
|
|
1040 |
wakaba |
1.56 |
Copyright 2007-2008 Wakaba <w@suika.fam.cx> |
1041 |
wakaba |
1.44 |
|
1042 |
|
|
This library is free software; you can redistribute it |
1043 |
|
|
and/or modify it under the same terms as Perl itself. |
1044 |
|
|
|
1045 |
|
|
=cut |
1046 |
|
|
|
1047 |
wakaba |
1.1 |
1; |
1048 |
wakaba |
1.98 |
# $Date: 2008/09/21 09:45:02 $ |