/[suikacvs]/markup/html/whatpm/Whatpm/ContentChecker.pm
Suika

Contents of /markup/html/whatpm/Whatpm/ContentChecker.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.47 - (hide annotations) (download)
Sun Sep 9 07:57:32 2007 UTC (17 years, 1 month ago) by wakaba
Branch: MAIN
Changes since 1.46: +21 -4 lines
++ whatpm/Whatpm/ChangeLog	9 Sep 2007 07:57:16 -0000
	* ContentChecker.pm: Support for language tag validation.

2007-09-09  Wakaba  <wakaba@suika.fam.cx>

++ whatpm/Whatpm/ContentChecker/ChangeLog	9 Sep 2007 07:56:59 -0000
2007-09-09  Wakaba  <wakaba@suika.fam.cx>

	* HTML.pm: Support for language tag validation.

1 wakaba 1.1 package Whatpm::ContentChecker;
2     use strict;
3 wakaba 1.47 our $VERSION=do{my @r=(q$Revision: 1.46 $=~/\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.42 my $HTML_NS = q<http://www.w3.org/1999/xhtml>;
11 wakaba 1.9 my $XML_NS = q<http://www.w3.org/XML/1998/namespace>;
12     my $XMLNS_NS = q<http://www.w3.org/2000/xmlns/>;
13    
14 wakaba 1.42 my $Namespace = {
15 wakaba 1.43 q<http://www.w3.org/2005/Atom> => {module => 'Whatpm::ContentChecker::Atom'},
16 wakaba 1.42 $HTML_NS => {module => 'Whatpm::ContentChecker::HTML'},
17     $XML_NS => {loaded => 1},
18     $XMLNS_NS => {loaded => 1},
19     };
20    
21     our $AttrChecker = {
22 wakaba 1.9 $XML_NS => {
23 wakaba 1.13 space => sub {
24     my ($self, $attr) = @_;
25     my $value = $attr->value;
26     if ($value eq 'default' or $value eq 'preserve') {
27     #
28     } else {
29     ## NOTE: An XML "error"
30 wakaba 1.33 $self->{onerror}->(node => $attr, level => 'error',
31     type => 'invalid attribute value');
32 wakaba 1.13 }
33     },
34     lang => sub {
35 wakaba 1.35 my ($self, $attr) = @_;
36 wakaba 1.47 my $value = $attr->value;
37     if ($value eq '') {
38     #
39     } else {
40     require Whatpm::LangTag;
41     Whatpm::LangTag->check_rfc3066_language_tag ($value, sub {
42     my %opt = @_;
43     my $type = 'LangTag:'.$opt{type};
44     $type .= ':' . $opt{subtag} if defined $opt{subtag};
45     $self->{onerror}->(node => $attr, type => $type,
46     value => $opt{value}, level => $opt{level});
47     });
48     }
49    
50 wakaba 1.13 ## NOTE: "The values of the attribute are language identifiers
51     ## as defined by [IETF RFC 3066], Tags for the Identification
52     ## of Languages, or its successor; in addition, the empty string
53     ## may be specified." ("may" in lower case)
54 wakaba 1.47 ## NOTE: Is an RFC 3066-valid (but RFC 4647-invalid) language tag
55     ## allowed today?
56    
57     ## TODO: test data
58    
59 wakaba 1.35 if ($attr->owner_document->manakai_is_html) { # MUST NOT
60 wakaba 1.36 $self->{onerror}->(node => $attr, type => 'in HTML:xml:lang');
61 wakaba 1.35 ## TODO: Test data...
62     }
63 wakaba 1.13 },
64     base => sub {
65     my ($self, $attr) = @_;
66     my $value = $attr->value;
67     if ($value =~ /[^\x{0000}-\x{10FFFF}]/) { ## ISSUE: Should we disallow noncharacters?
68     $self->{onerror}->(node => $attr,
69 wakaba 1.33 type => 'invalid attribute value');
70 wakaba 1.13 }
71 wakaba 1.18 ## NOTE: Conformance to URI standard is not checked since there is
72     ## no author requirement on conformance in the XML Base specification.
73 wakaba 1.13 },
74     id => sub {
75     my ($self, $attr) = @_;
76     my $value = $attr->value;
77     $value =~ s/[\x09\x0A\x0D\x20]+/ /g;
78     $value =~ s/^\x20//;
79     $value =~ s/\x20$//;
80     ## TODO: NCName in XML 1.0 or 1.1
81     ## TODO: declared type is ID?
82 wakaba 1.33 if ($self->{id}->{$value}) { ## NOTE: An xml:id error
83     $self->{onerror}->(node => $attr, level => 'error',
84     type => 'duplicate ID');
85 wakaba 1.37 push @{$self->{id}->{$value}}, $attr;
86 wakaba 1.13 } else {
87 wakaba 1.37 $self->{id}->{$value} = [$attr];
88 wakaba 1.13 }
89     },
90 wakaba 1.9 },
91     $XMLNS_NS => {
92 wakaba 1.13 '' => sub {
93     my ($self, $attr) = @_;
94     my $ln = $attr->manakai_local_name;
95     my $value = $attr->value;
96     if ($value eq $XML_NS and $ln ne 'xml') {
97     $self->{onerror}
98 wakaba 1.33 ->(node => $attr, level => 'NC',
99     type => 'Reserved Prefixes and Namespace Names:=xml');
100 wakaba 1.13 } elsif ($value eq $XMLNS_NS) {
101     $self->{onerror}
102 wakaba 1.33 ->(node => $attr, level => 'NC',
103     type => 'Reserved Prefixes and Namespace Names:=xmlns');
104 wakaba 1.13 }
105     if ($ln eq 'xml' and $value ne $XML_NS) {
106     $self->{onerror}
107 wakaba 1.33 ->(node => $attr, level => 'NC',
108     type => 'Reserved Prefixes and Namespace Names:xmlns:xml=');
109 wakaba 1.13 } elsif ($ln eq 'xmlns') {
110     $self->{onerror}
111 wakaba 1.33 ->(node => $attr, level => 'NC',
112     type => 'Reserved Prefixes and Namespace Names:xmlns:xmlns=');
113 wakaba 1.13 }
114     ## TODO: If XML 1.0 and empty
115     },
116     xmlns => sub {
117     my ($self, $attr) = @_;
118     ## TODO: In XML 1.0, URI reference [RFC 3986] or an empty string
119     ## TODO: In XML 1.1, IRI reference [RFC 3987] or an empty string
120 wakaba 1.18 ## TODO: relative references are deprecated
121 wakaba 1.13 my $value = $attr->value;
122     if ($value eq $XML_NS) {
123     $self->{onerror}
124 wakaba 1.33 ->(node => $attr, level => 'NC',
125     type => 'Reserved Prefixes and Namespace Names:=xml');
126 wakaba 1.13 } elsif ($value eq $XMLNS_NS) {
127     $self->{onerror}
128 wakaba 1.33 ->(node => $attr, level => 'NC',
129     type => 'Reserved Prefixes and Namespace Names:=xmlns');
130 wakaba 1.13 }
131     },
132 wakaba 1.9 },
133     };
134    
135 wakaba 1.14 ## ISSUE: Should we really allow these attributes?
136 wakaba 1.13 $AttrChecker->{''}->{'xml:space'} = $AttrChecker->{$XML_NS}->{space};
137     $AttrChecker->{''}->{'xml:lang'} = $AttrChecker->{$XML_NS}->{lang};
138     $AttrChecker->{''}->{'xml:base'} = $AttrChecker->{$XML_NS}->{base};
139     $AttrChecker->{''}->{'xml:id'} = $AttrChecker->{$XML_NS}->{id};
140    
141 wakaba 1.3 ## ANY
142 wakaba 1.42 our $AnyChecker = sub {
143 wakaba 1.4 my ($self, $todo) = @_;
144     my $el = $todo->{node};
145     my $new_todos = [];
146 wakaba 1.3 my @nodes = (@{$el->child_nodes});
147     while (@nodes) {
148     my $node = shift @nodes;
149     $self->_remove_minuses ($node) and next if ref $node eq 'HASH';
150    
151     my $nt = $node->node_type;
152     if ($nt == 1) {
153     my $node_ns = $node->namespace_uri;
154     $node_ns = '' unless defined $node_ns;
155     my $node_ln = $node->manakai_local_name;
156     if ($self->{minuses}->{$node_ns}->{$node_ln}) {
157     $self->{onerror}->(node => $node, type => 'element not allowed');
158     }
159 wakaba 1.4 push @$new_todos, {type => 'element', node => $node};
160 wakaba 1.3 } elsif ($nt == 5) {
161     unshift @nodes, @{$node->child_nodes};
162     }
163     }
164 wakaba 1.4 return ($new_todos);
165 wakaba 1.3 }; # $AnyChecker
166    
167 wakaba 1.42 our $ElementDefault = {
168 wakaba 1.1 checker => sub {
169 wakaba 1.4 my ($self, $todo) = @_;
170 wakaba 1.33 $self->{onerror}->(node => $todo->{node}, level => 'unsupported',
171     type => 'element');
172 wakaba 1.4 return $AnyChecker->($self, $todo);
173 wakaba 1.1 },
174 wakaba 1.9 attrs_checker => sub {
175     my ($self, $todo) = @_;
176     for my $attr (@{$todo->{node}->attributes}) {
177     my $attr_ns = $attr->namespace_uri;
178     $attr_ns = '' unless defined $attr_ns;
179     my $attr_ln = $attr->manakai_local_name;
180     my $checker = $AttrChecker->{$attr_ns}->{$attr_ln}
181     || $AttrChecker->{$attr_ns}->{''};
182     if ($checker) {
183     $checker->($self, $attr);
184 wakaba 1.17 } else {
185 wakaba 1.33 $self->{onerror}->(node => $attr, level => 'unsupported',
186     type => 'attribute');
187 wakaba 1.9 }
188     }
189     },
190 wakaba 1.1 };
191    
192 wakaba 1.7 my $HTMLTransparentElements = {
193     $HTML_NS => {qw/ins 1 font 1 noscript 1/},
194 wakaba 1.29 ## NOTE: |html:noscript| is transparent if scripting is disabled
195     ## and not in |head|.
196 wakaba 1.7 };
197    
198 wakaba 1.42 our $Element = {};
199 wakaba 1.7
200 wakaba 1.42 sub check_document ($$$) {
201     my ($self, $doc, $onerror) = @_;
202     $self = bless {}, $self unless ref $self;
203     $self->{onerror} = $onerror;
204 wakaba 1.1
205 wakaba 1.42 my $docel = $doc->document_element;
206     unless (defined $docel) {
207     ## ISSUE: Should we check content of Document node?
208     $onerror->(node => $doc, type => 'no document element');
209     ## ISSUE: Is this non-conforming (to what spec)? Or just a warning?
210     return {
211     class => {},
212     id => {}, table => [], term => {},
213     };
214 wakaba 1.1 }
215    
216 wakaba 1.42 ## ISSUE: Unexpanded entity references and HTML5 conformance
217 wakaba 1.1
218 wakaba 1.42 my $docel_nsuri = $docel->namespace_uri;
219     $docel_nsuri = '' unless defined $docel_nsuri;
220 wakaba 1.43 unless ($Namespace->{$docel_nsuri}->{loaded}) {
221     if ($Namespace->{$docel_nsuri}->{module}) {
222     eval qq{ require $Namespace->{$docel_nsuri}->{module} } or die $@;
223     } else {
224     $Namespace->{$docel_nsuri}->{loaded} = 1;
225     }
226     }
227 wakaba 1.42 my $docel_def = $Element->{$docel_nsuri}->{$docel->manakai_local_name} ||
228     $Element->{$docel_nsuri}->{''} ||
229     $ElementDefault;
230     if ($docel_def->{is_root}) {
231     #
232     } else {
233     $onerror->(node => $docel, type => 'element not allowed');
234 wakaba 1.1 }
235    
236 wakaba 1.42 ## TODO: Check for other items other than document element
237     ## (second (errorous) element, text nodes, PI nodes, doctype nodes)
238 wakaba 1.2
239 wakaba 1.42 return $self->check_element ($docel, $onerror);
240     } # check_document
241 wakaba 1.1
242 wakaba 1.42 sub check_element ($$$) {
243     my ($self, $el, $onerror) = @_;
244     $self = bless {}, $self unless ref $self;
245     $self->{onerror} = $onerror;
246 wakaba 1.2
247 wakaba 1.42 $self->{minuses} = {};
248     $self->{id} = {};
249     $self->{term} = {};
250     $self->{usemap} = [];
251     $self->{contextmenu} = [];
252     $self->{map} = {};
253     $self->{menu} = {};
254     $self->{has_link_type} = {};
255 wakaba 1.46 #$self->{has_uri_attr};
256     #$self->{has_hyperlink_element};
257 wakaba 1.42 $self->{return} = {
258     class => {},
259     id => $self->{id}, table => [], term => $self->{term},
260     };
261 wakaba 1.4
262 wakaba 1.42 my @todo = ({type => 'element', node => $el});
263     while (@todo) {
264     my $todo = shift @todo;
265     if ($todo->{type} eq 'element') {
266     my $prefix = $todo->{node}->prefix;
267     if (defined $prefix and $prefix eq 'xmlns') {
268     $self->{onerror}
269     ->(node => $todo->{node}, level => 'NC',
270     type => 'Reserved Prefixes and Namespace Names:<xmlns:>');
271 wakaba 1.7 }
272 wakaba 1.42 my $nsuri = $todo->{node}->namespace_uri;
273     $nsuri = '' unless defined $nsuri;
274     unless ($Namespace->{$nsuri}->{loaded}) {
275     if ($Namespace->{$nsuri}->{module}) {
276     eval qq{ require $Namespace->{$nsuri}->{module} } or die $@;
277     } else {
278     $Namespace->{$nsuri}->{loaded} = 1;
279 wakaba 1.1 }
280     }
281 wakaba 1.42 my $ln = $todo->{node}->manakai_local_name;
282     my $eldef = $Element->{$nsuri}->{$ln} ||
283     $Element->{$nsuri}->{''} ||
284     $ElementDefault;
285     $eldef->{attrs_checker}->($self, $todo);
286     my ($new_todos) = $eldef->{checker}->($self, $todo);
287     unshift @todo, @$new_todos;
288     } elsif ($todo->{type} eq 'element-attributes') {
289     my $prefix = $todo->{node}->prefix;
290     if (defined $prefix and $prefix eq 'xmlns') {
291     $self->{onerror}
292     ->(node => $todo->{node}, level => 'NC',
293     type => 'Reserved Prefixes and Namespace Names:<xmlns:>');
294     }
295     my $nsuri = $todo->{node}->namespace_uri;
296     $nsuri = '' unless defined $nsuri;
297     unless ($Namespace->{$nsuri}->{loaded}) {
298     if ($Namespace->{$nsuri}->{module}) {
299     eval qq{ require $Namespace->{$nsuri}->{module} } or die $@;
300 wakaba 1.1 } else {
301 wakaba 1.42 $Namespace->{$nsuri}->{loaded} = 1;
302 wakaba 1.1 }
303     }
304 wakaba 1.9 my $ln = $todo->{node}->manakai_local_name;
305     my $eldef = $Element->{$nsuri}->{$ln} ||
306     $Element->{$nsuri}->{''} ||
307     $ElementDefault;
308     $eldef->{attrs_checker}->($self, $todo);
309 wakaba 1.4 } elsif ($todo->{type} eq 'plus') {
310     $self->_remove_minuses ($todo);
311 wakaba 1.30 } elsif ($todo->{type} eq 'code') {
312     $todo->{code}->();
313     } else {
314     die "$0: Internal error: Unsupported checking action type |$todo->{type}|";
315 wakaba 1.4 }
316 wakaba 1.1 }
317 wakaba 1.17
318     for (@{$self->{usemap}}) {
319     unless ($self->{map}->{$_->[0]}) {
320     $self->{onerror}->(node => $_->[1], type => 'no referenced map');
321     }
322     }
323    
324 wakaba 1.32 for (@{$self->{contextmenu}}) {
325     unless ($self->{menu}->{$_->[0]}) {
326     $self->{onerror}->(node => $_->[1], type => 'no referenced menu');
327     }
328     }
329    
330 wakaba 1.17 delete $self->{minuses};
331     delete $self->{onerror};
332     delete $self->{id};
333     delete $self->{usemap};
334     delete $self->{map};
335 wakaba 1.33 return $self->{return};
336 wakaba 1.1 } # check_element
337    
338 wakaba 1.2 sub _add_minuses ($@) {
339     my $self = shift;
340     my $r = {};
341     for my $list (@_) {
342     for my $ns (keys %$list) {
343     for my $ln (keys %{$list->{$ns}}) {
344     unless ($self->{minuses}->{$ns}->{$ln}) {
345     $self->{minuses}->{$ns}->{$ln} = 1;
346     $r->{$ns}->{$ln} = 1;
347     }
348     }
349     }
350     }
351 wakaba 1.4 return {type => 'plus', list => $r};
352 wakaba 1.2 } # _add_minuses
353    
354     sub _remove_minuses ($$) {
355 wakaba 1.4 my ($self, $todo) = @_;
356     for my $ns (keys %{$todo->{list}}) {
357     for my $ln (keys %{$todo->{list}->{$ns}}) {
358     delete $self->{minuses}->{$ns}->{$ln} if $todo->{list}->{$ns}->{$ln};
359 wakaba 1.2 }
360     }
361     1;
362     } # _remove_minuses
363    
364 wakaba 1.30 sub _check_get_children ($$$) {
365     my ($self, $node, $parent_todo) = @_;
366 wakaba 1.4 my $new_todos = [];
367 wakaba 1.2 my $sib = [];
368     TP: {
369     my $node_ns = $node->namespace_uri;
370     $node_ns = '' unless defined $node_ns;
371     my $node_ln = $node->manakai_local_name;
372 wakaba 1.45 if ($HTMLTransparentElements->{$node_ns}->{$node_ln}) {
373     if ($node_ns eq $HTML_NS and $node_ln eq 'noscript') {
374     if ($parent_todo->{flag}->{in_head}) {
375     #
376     } else {
377     my $end = $self->_add_minuses ({$HTML_NS, {noscript => 1}});
378     push @$sib, $end;
379    
380     unshift @$sib, @{$node->child_nodes};
381     push @$new_todos, {type => 'element-attributes', node => $node};
382     last TP;
383     }
384     } else {
385     unshift @$sib, @{$node->child_nodes};
386     push @$new_todos, {type => 'element-attributes', node => $node};
387     last TP;
388 wakaba 1.2 }
389     }
390 wakaba 1.8 if ($node_ns eq $HTML_NS and ($node_ln eq 'video' or $node_ln eq 'audio')) {
391 wakaba 1.2 if ($node->has_attribute_ns (undef, 'src')) {
392     unshift @$sib, @{$node->child_nodes};
393 wakaba 1.9 push @$new_todos, {type => 'element-attributes', node => $node};
394 wakaba 1.2 last TP;
395     } else {
396     my @cn = @{$node->child_nodes};
397     CN: while (@cn) {
398     my $cn = shift @cn;
399     my $cnt = $cn->node_type;
400     if ($cnt == 1) {
401 wakaba 1.8 my $cn_nsuri = $cn->namespace_uri;
402     $cn_nsuri = '' unless defined $cn_nsuri;
403     if ($cn_nsuri eq $HTML_NS and $cn->manakai_local_name eq 'source') {
404 wakaba 1.2 #
405     } else {
406     last CN;
407     }
408     } elsif ($cnt == 3 or $cnt == 4) {
409     if ($cn->data =~ /[^\x09-\x0D\x20]/) {
410     last CN;
411     }
412     }
413     } # CN
414     unshift @$sib, @cn;
415     }
416     }
417 wakaba 1.4 push @$new_todos, {type => 'element', node => $node};
418 wakaba 1.2 } # TP
419 wakaba 1.30
420     for my $new_todo (@$new_todos) {
421     $new_todo->{flag} = {%{$parent_todo->{flag} or {}}};
422     }
423    
424 wakaba 1.4 return ($sib, $new_todos);
425 wakaba 1.2 } # _check_get_children
426    
427 wakaba 1.44 =head1 LICENSE
428    
429     Copyright 2007 Wakaba <w@suika.fam.cx>
430    
431     This library is free software; you can redistribute it
432     and/or modify it under the same terms as Perl itself.
433    
434     =cut
435    
436 wakaba 1.1 1;
437 wakaba 1.47 # $Date: 2007/08/17 11:53:52 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24