/[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.44 - (hide annotations) (download)
Mon Aug 6 10:56:50 2007 UTC (17 years, 3 months ago) by wakaba
Branch: MAIN
Changes since 1.43: +11 -1 lines
++ whatpm/Whatpm/ChangeLog	6 Aug 2007 10:56:45 -0000
2007-08-06  Wakaba  <wakaba@suika.fam.cx>

	* ContentChecker.pod: New documentation.

	* Makefile: A rule for |ContentChecker.html| is added.

	* ContentChecker.pm: A pod "LICENSE" section is added.

	* NanoDOM.pm ($VERSION): New variable.

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

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24