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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.19 - (hide annotations) (download)
Sun Feb 17 06:36:28 2008 UTC (16 years, 8 months ago) by wakaba
Branch: MAIN
Changes since 1.18: +27 -2 lines
++ whatpm/t/ChangeLog	17 Feb 2008 06:35:24 -0000
2008-02-17  Wakaba  <wakaba@suika.fam.cx>

	* content-model-1.dat, content-model-2.dat, content-model-5.dat:
	Test results are updated; new tests are added.

++ whatpm/Whatpm/ChangeLog	17 Feb 2008 06:34:11 -0000
2008-02-17  Wakaba  <wakaba@suika.fam.cx>

	* ContenteChecker.pm ($HTMLTransparentElements): More
	elements are added.
	(_get_children): HTML |object| elements are now semi-transparent.

	* NanoDOM.pm (manakai_html, manakai_head): New methods.

++ whatpm/Whatpm/ContentChecker/ChangeLog	17 Feb 2008 06:32:44 -0000
2008-02-17  Wakaba  <wakaba@suika.fam.cx>

	* HTML.pm: Most part of December 2007 Content Model is implemented.

1 wakaba 1.1 =head1 NAME
2    
3 wakaba 1.2 Whatpm::NanoDOM - A Non-Conforming Implementation of DOM Subset
4 wakaba 1.1
5     =head1 DESCRIPTION
6    
7 wakaba 1.2 The C<Whatpm::NanoDOM> module contains a non-conforming implementation
8 wakaba 1.1 of a subset of DOM. It is the intention that this module is
9 wakaba 1.2 used only for the purpose of testing the C<Whatpm::HTML> module.
10 wakaba 1.1
11     See source code if you would like to know what it does.
12    
13     =cut
14    
15 wakaba 1.2 package Whatpm::NanoDOM;
16 wakaba 1.1 use strict;
17 wakaba 1.19 our $VERSION=do{my @r=(q$Revision: 1.18 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
18 wakaba 1.1
19 wakaba 1.3 require Scalar::Util;
20    
21     package Whatpm::NanoDOM::DOMImplementation;
22    
23     sub create_document ($) {
24     return Whatpm::NanoDOM::Document->new;
25     } # create_document
26    
27 wakaba 1.2 package Whatpm::NanoDOM::Node;
28 wakaba 1.1
29     sub new ($) {
30     my $class = shift;
31     my $self = bless {}, $class;
32     return $self;
33     } # new
34    
35     sub parent_node ($) {
36     return shift->{parent_node};
37     } # parent_node
38    
39 wakaba 1.4 sub manakai_parent_element ($) {
40     my $self = shift;
41     my $parent = $self->{parent_node};
42     while (defined $parent) {
43     if ($parent->node_type == 1) {
44     return $parent;
45     } else {
46     $parent = $parent->{parent_node};
47     }
48     }
49     return undef;
50     } # manakai_parent_element
51    
52 wakaba 1.1 sub child_nodes ($) {
53 wakaba 1.7 return shift->{child_nodes} || [];
54 wakaba 1.1 } # child_nodes
55    
56     ## NOTE: Only applied to Elements and Documents
57     sub append_child ($$) {
58     my ($self, $new_child) = @_;
59     if (defined $new_child->{parent_node}) {
60     my $parent_list = $new_child->{parent_node}->{child_nodes};
61     for (0..$#$parent_list) {
62     if ($parent_list->[$_] eq $new_child) {
63     splice @$parent_list, $_, 1;
64     }
65     }
66     }
67     push @{$self->{child_nodes}}, $new_child;
68 wakaba 1.3 $new_child->{parent_node} = $self;
69     Scalar::Util::weaken ($new_child->{parent_node});
70 wakaba 1.1 return $new_child;
71     } # append_child
72    
73     ## NOTE: Only applied to Elements and Documents
74     sub insert_before ($$;$) {
75     my ($self, $new_child, $ref_child) = @_;
76     if (defined $new_child->{parent_node}) {
77     my $parent_list = $new_child->{parent_node}->{child_nodes};
78     for (0..$#$parent_list) {
79     if ($parent_list->[$_] eq $new_child) {
80     splice @$parent_list, $_, 1;
81     }
82     }
83     }
84     my $i = @{$self->{child_nodes}};
85     if (defined $ref_child) {
86     for (0..$#{$self->{child_nodes}}) {
87     if ($self->{child_nodes}->[$_] eq $ref_child) {
88     $i = $_;
89     last;
90     }
91     }
92     }
93     splice @{$self->{child_nodes}}, $i, 0, $new_child;
94 wakaba 1.5 $new_child->{parent_node} = $self;
95     Scalar::Util::weaken ($new_child->{parent_node});
96 wakaba 1.1 return $new_child;
97     } # insert_before
98    
99     ## NOTE: Only applied to Elements and Documents
100     sub remove_child ($$) {
101     my ($self, $old_child) = @_;
102     my $parent_list = $self->{child_nodes};
103     for (0..$#$parent_list) {
104     if ($parent_list->[$_] eq $old_child) {
105     splice @$parent_list, $_, 1;
106     }
107     }
108     delete $old_child->{parent_node};
109     return $old_child;
110     } # remove_child
111    
112     ## NOTE: Only applied to Elements and Documents
113     sub has_child_nodes ($) {
114     return @{shift->{child_nodes}} > 0;
115     } # has_child_nodes
116    
117     ## NOTE: Only applied to Elements and Documents
118 wakaba 1.8 sub first_child ($) {
119     my $self = shift;
120     return $self->{child_nodes}->[0];
121     } # first_child
122    
123     ## NOTE: Only applied to Elements and Documents
124 wakaba 1.1 sub last_child ($) {
125     my $self = shift;
126     return @{$self->{child_nodes}} ? $self->{child_nodes}->[-1] : undef;
127     } # last_child
128    
129     ## NOTE: Only applied to Elements and Documents
130     sub previous_sibling ($) {
131     my $self = shift;
132     my $parent = $self->{parent_node};
133     return undef unless defined $parent;
134     my $r;
135     for (@{$parent->{child_nodes}}) {
136     if ($_ eq $self) {
137     return $r;
138     } else {
139     $r = $_;
140     }
141     }
142     return undef;
143     } # previous_sibling
144    
145 wakaba 1.6 sub prefix ($;$) {
146     my $self = shift;
147     if (@_) {
148     $self->{prefix} = shift;
149     }
150     return $self->{prefix};
151     } # prefix
152    
153 wakaba 1.18 sub get_user_data ($$) {
154     return $_[0]->{$_[1]};
155     } # get_user_data
156    
157     sub set_user_data ($$;$$) {
158     $_[0]->{$_[1]} = $_[2];
159     } # set_user_data
160    
161 wakaba 1.1 sub ELEMENT_NODE () { 1 }
162     sub ATTRIBUTE_NODE () { 2 }
163     sub TEXT_NODE () { 3 }
164     sub CDATA_SECTION_NODE () { 4 }
165     sub ENTITY_REFERENCE_NODE () { 5 }
166     sub ENTITY_NODE () { 6 }
167     sub PROCESSING_INSTRUCTION_NODE () { 7 }
168     sub COMMENT_NODE () { 8 }
169     sub DOCUMENT_NODE () { 9 }
170     sub DOCUMENT_TYPE_NODE () { 10 }
171     sub DOCUMENT_FRAGMENT_NODE () { 11 }
172     sub NOTATION_NODE () { 12 }
173    
174 wakaba 1.2 package Whatpm::NanoDOM::Document;
175     push our @ISA, 'Whatpm::NanoDOM::Node';
176 wakaba 1.1
177     sub new ($) {
178     my $self = shift->SUPER::new;
179     $self->{child_nodes} = [];
180     return $self;
181     } # new
182    
183     ## A manakai extension
184     sub manakai_append_text ($$) {
185     my $self = shift;
186     if (@{$self->{child_nodes}} and
187     $self->{child_nodes}->[-1]->node_type == 3) {
188     $self->{child_nodes}->[-1]->manakai_append_text (shift);
189     } else {
190     my $text = $self->create_text_node (shift);
191     $self->append_child ($text);
192     }
193     } # manakai_append_text
194    
195     sub node_type () { 9 }
196    
197     sub strict_error_checking {
198     return 0;
199     } # strict_error_checking
200    
201     sub create_text_node ($$) {
202     shift;
203 wakaba 1.2 return Whatpm::NanoDOM::Text->new (shift);
204 wakaba 1.1 } # create_text_node
205    
206     sub create_comment ($$) {
207     shift;
208 wakaba 1.2 return Whatpm::NanoDOM::Comment->new (shift);
209 wakaba 1.1 } # create_comment
210    
211     ## The second parameter only supports manakai extended way
212     ## to specify qualified name - "[$prefix, $local_name]"
213     sub create_element_ns ($$$) {
214     my ($self, $nsuri, $qn) = @_;
215 wakaba 1.3 return Whatpm::NanoDOM::Element->new ($self, $nsuri, $qn->[0], $qn->[1]);
216 wakaba 1.1 } # create_element_ns
217    
218     ## A manakai extension
219     sub create_document_type_definition ($$) {
220     shift;
221 wakaba 1.2 return Whatpm::NanoDOM::DocumentType->new (shift);
222 wakaba 1.1 } # create_document_type_definition
223    
224 wakaba 1.3 sub implementation ($) {
225     return 'Whatpm::NanoDOM::DOMImplementation';
226     } # implementation
227    
228 wakaba 1.4 sub document_element ($) {
229     my $self = shift;
230     for (@{$self->child_nodes}) {
231     if ($_->node_type == 1) {
232     return $_;
233     }
234     }
235     return undef;
236     } # document_element
237    
238 wakaba 1.12 sub adopt_node ($$) {
239 wakaba 1.11 my @node = ($_[1]);
240     while (@node) {
241     my $node = shift @node;
242     $node->{owner_document} = $_[0];
243     Scalar::Util::weaken ($node->{owner_document});
244     push @node, @{$node->child_nodes};
245     push @node, @{$node->attributes or []} if $node->can ('attributes');
246     }
247     return $_[1];
248     } # adopt_node
249    
250 wakaba 1.12 sub manakai_is_html ($;$) {
251     if (@_ > 1) {
252     if ($_[1]) {
253     $_[0]->{manakai_is_html} = 1;
254     } else {
255 wakaba 1.13 delete $_[0]->{manakai_is_html};
256 wakaba 1.12 delete $_[0]->{manakai_compat_mode};
257     }
258     }
259     return $_[0]->{manakai_is_html};
260     } # manakai_is_html
261    
262     sub compat_mode ($) {
263     if ($_[0]->{manakai_is_html}) {
264     if ($_[0]->{manakai_compat_mode} eq 'quirks') {
265     return 'BackCompat';
266     }
267     }
268     return 'CSS1Compat';
269     } # compat_mode
270    
271     sub manakai_compat_mode ($;$) {
272     if ($_[0]->{manakai_is_html}) {
273     if (@_ > 1 and defined $_[1] and
274     {'no quirks' => 1, 'limited quirks' => 1, 'quirks' => 1}->{$_[1]}) {
275     $_[0]->{manakai_compat_mode} = $_[1];
276     }
277     return $_[0]->{manakai_compat_mode} || 'no quirks';
278     } else {
279     return 'no quirks';
280     }
281     } # manakai_compat_mode
282    
283 wakaba 1.19 sub manakai_head ($) {
284     my $html = $_[0]->manakai_html;
285     return undef unless defined $html;
286     for my $el (@{$html->child_nodes}) {
287     next unless $el->node_type == 1; # ELEMENT_NODE
288     my $nsuri = $el->namespace_uri;
289     next unless defined $nsuri;
290     next unless $nsuri eq q<http://www.w3.org/1999/xhtml>;
291     next unless $el->manakai_local_name eq 'head';
292     return $el;
293     }
294     return undef;
295     } # manakai_head
296    
297     sub manakai_html ($) {
298     my $de = $_[0]->document_element;
299     my $nsuri = $de->namespace_uri;
300     if (defined $nsuri and $nsuri eq q<http://www.w3.org/1999/xhtml> and
301     $de->manakai_local_name eq 'html') {
302     return $de;
303     } else {
304     return undef;
305     }
306     } # manakai_html
307    
308 wakaba 1.17 sub input_encoding ($;$) {
309     $_[0]->{input_encoding} = $_[1] if @_ > 1;
310     return $_[0]->{input_encoding};
311     }
312    
313     sub manakai_charset ($;$) {
314     $_[0]->{manakai_charset} = $_[1] if @_ > 1;
315     return $_[0]->{manakai_charset};
316     }
317    
318     sub manakai_has_bom ($;$) {
319     $_[0]->{manakai_has_bom} = $_[1] if @_ > 1;
320     return $_[0]->{manakai_has_bom};
321     }
322    
323 wakaba 1.2 package Whatpm::NanoDOM::Element;
324     push our @ISA, 'Whatpm::NanoDOM::Node';
325 wakaba 1.1
326 wakaba 1.3 sub new ($$$$$) {
327 wakaba 1.1 my $self = shift->SUPER::new;
328 wakaba 1.3 $self->{owner_document} = shift;
329     Scalar::Util::weaken ($self->{owner_document});
330 wakaba 1.1 $self->{namespace_uri} = shift;
331     $self->{prefix} = shift;
332     $self->{local_name} = shift;
333     $self->{attributes} = {};
334     $self->{child_nodes} = [];
335     return $self;
336     } # new
337    
338 wakaba 1.3 sub owner_document ($) {
339     return shift->{owner_document};
340     } # owner_document
341    
342 wakaba 1.1 sub clone_node ($$) {
343     my ($self, $deep) = @_; ## NOTE: Deep cloning is not supported
344     my $clone = bless {
345     namespace_uri => $self->{namespace_uri},
346     prefix => $self->{prefix},
347     local_name => $self->{local_name},
348     child_nodes => [],
349     }, ref $self;
350     for my $ns (keys %{$self->{attributes}}) {
351     for my $ln (keys %{$self->{attributes}->{$ns}}) {
352     my $attr = $self->{attributes}->{$ns}->{$ln};
353     $clone->{attributes}->{$ns}->{$ln} = bless {
354     namespace_uri => $attr->{namespace_uri},
355     prefix => $attr->{prefix},
356     local_name => $attr->{local_name},
357     value => $attr->{value},
358     }, ref $self->{attributes}->{$ns}->{$ln};
359     }
360     }
361     return $clone;
362     } # clone
363    
364     ## A manakai extension
365     sub manakai_append_text ($$) {
366     my $self = shift;
367     if (@{$self->{child_nodes}} and
368     $self->{child_nodes}->[-1]->node_type == 3) {
369     $self->{child_nodes}->[-1]->manakai_append_text (shift);
370     } else {
371 wakaba 1.2 my $text = Whatpm::NanoDOM::Text->new (shift);
372 wakaba 1.1 $self->append_child ($text);
373     }
374     } # manakai_append_text
375    
376 wakaba 1.7 sub text_content ($) {
377     my $self = shift;
378     my $r = '';
379     for my $child (@{$self->child_nodes}) {
380     if ($child->can ('data')) {
381     $r .= $child->data;
382     } else {
383     $r .= $child->text_content;
384     }
385     }
386     return $r;
387     } # text_content
388    
389 wakaba 1.1 sub attributes ($) {
390     my $self = shift;
391     my $r = [];
392     ## Order MUST be stable
393     for my $ns (sort {$a cmp $b} keys %{$self->{attributes}}) {
394     for my $ln (sort {$a cmp $b} keys %{$self->{attributes}->{$ns}}) {
395     push @$r, $self->{attributes}->{$ns}->{$ln}
396     if defined $self->{attributes}->{$ns}->{$ln};
397     }
398     }
399     return $r;
400     } # attributes
401    
402 wakaba 1.3 sub local_name ($) { # TODO: HTML5 case
403     return shift->{local_name};
404     } # local_name
405    
406 wakaba 1.4 sub manakai_local_name ($) {
407     return shift->{local_name}; # no case fixing for HTML5
408     } # manakai_local_name
409    
410 wakaba 1.3 sub namespace_uri ($) {
411     return shift->{namespace_uri};
412     } # namespace_uri
413    
414 wakaba 1.4 sub manakai_element_type_match ($$$) {
415     my ($self, $nsuri, $ln) = @_;
416     if (defined $nsuri) {
417     if (defined $self->{namespace_uri} and $nsuri eq $self->{namespace_uri}) {
418     return ($ln eq $self->{local_name});
419     } else {
420     return 0;
421     }
422     } else {
423     if (not defined $self->{namespace_uri}) {
424     return ($ln eq $self->{local_name});
425     } else {
426     return 0;
427     }
428     }
429     } # manakai_element_type_match
430    
431 wakaba 1.1 sub node_type { 1 }
432    
433     ## TODO: HTML5 capitalization
434     sub tag_name ($) {
435     my $self = shift;
436     if (defined $self->{prefix}) {
437     return $self->{prefix} . ':' . $self->{local_name};
438     } else {
439     return $self->{local_name};
440     }
441     } # tag_name
442    
443 wakaba 1.8 sub get_attribute_ns ($$$) {
444     my ($self, $nsuri, $ln) = @_;
445     $nsuri = '' unless defined $nsuri;
446     return defined $self->{attributes}->{$nsuri}->{$ln}
447     ? $self->{attributes}->{$nsuri}->{$ln}->value : undef;
448     } # get_attribute_ns
449    
450 wakaba 1.9 sub get_attribute_node_ns ($$$) {
451     my ($self, $nsuri, $ln) = @_;
452     $nsuri = '' unless defined $nsuri;
453     return $self->{attributes}->{$nsuri}->{$ln};
454     } # get_attribute_node_ns
455    
456 wakaba 1.1 sub has_attribute_ns ($$$) {
457     my ($self, $nsuri, $ln) = @_;
458 wakaba 1.8 $nsuri = '' unless defined $nsuri;
459 wakaba 1.1 return defined $self->{attributes}->{$nsuri}->{$ln};
460     } # has_attribute_ns
461    
462     ## The second parameter only supports manakai extended way
463     ## to specify qualified name - "[$prefix, $local_name]"
464     sub set_attribute_ns ($$$$) {
465     my ($self, $nsuri, $qn, $value) = @_;
466     $self->{attributes}->{$nsuri}->{$qn->[1]}
467 wakaba 1.5 = Whatpm::NanoDOM::Attr->new ($self, $nsuri, $qn->[0], $qn->[1], $value);
468 wakaba 1.1 } # set_attribute_ns
469    
470 wakaba 1.2 package Whatpm::NanoDOM::Attr;
471     push our @ISA, 'Whatpm::NanoDOM::Node';
472 wakaba 1.1
473 wakaba 1.5 sub new ($$$$$$) {
474 wakaba 1.1 my $self = shift->SUPER::new;
475 wakaba 1.5 $self->{owner_element} = shift;
476     Scalar::Util::weaken ($self->{owner_element});
477 wakaba 1.1 $self->{namespace_uri} = shift;
478     $self->{prefix} = shift;
479     $self->{local_name} = shift;
480     $self->{value} = shift;
481     return $self;
482     } # new
483    
484 wakaba 1.5 sub namespace_uri ($) {
485     return shift->{namespace_uri};
486     } # namespace_uri
487    
488     sub manakai_local_name ($) {
489     return shift->{local_name};
490     } # manakai_local_name
491    
492 wakaba 1.1 sub node_type { 2 }
493    
494 wakaba 1.14 sub owner_document ($) {
495     return shift->owner_element->owner_document;
496     } # owner_document
497    
498 wakaba 1.1 ## TODO: HTML5 case stuff?
499     sub name ($) {
500     my $self = shift;
501     if (defined $self->{prefix}) {
502     return $self->{prefix} . ':' . $self->{local_name};
503     } else {
504     return $self->{local_name};
505     }
506     } # name
507    
508     sub value ($) {
509     return shift->{value};
510     } # value
511    
512 wakaba 1.5 sub owner_element ($) {
513     return shift->{owner_element};
514     } # owner_element
515    
516 wakaba 1.2 package Whatpm::NanoDOM::CharacterData;
517     push our @ISA, 'Whatpm::NanoDOM::Node';
518 wakaba 1.1
519     sub new ($$) {
520     my $self = shift->SUPER::new;
521     $self->{data} = shift;
522     return $self;
523     } # new
524    
525     ## A manakai extension
526     sub manakai_append_text ($$) {
527     my ($self, $s) = @_;
528     $self->{data} .= $s;
529     } # manakai_append_text
530    
531     sub data ($) {
532     return shift->{data};
533     } # data
534    
535 wakaba 1.2 package Whatpm::NanoDOM::Text;
536     push our @ISA, 'Whatpm::NanoDOM::CharacterData';
537 wakaba 1.1
538     sub node_type () { 3 }
539    
540 wakaba 1.2 package Whatpm::NanoDOM::Comment;
541     push our @ISA, 'Whatpm::NanoDOM::CharacterData';
542 wakaba 1.1
543     sub node_type () { 8 }
544    
545 wakaba 1.2 package Whatpm::NanoDOM::DocumentType;
546     push our @ISA, 'Whatpm::NanoDOM::Node';
547 wakaba 1.1
548     sub new ($$) {
549     my $self = shift->SUPER::new;
550     $self->{name} = shift;
551     return $self;
552     } # new
553    
554     sub node_type () { 10 }
555    
556     sub name ($) {
557     return shift->{name};
558     } # name
559    
560 wakaba 1.16 sub public_id ($;$) {
561     $_[0]->{public_id} = $_[1] if @_ > 1;
562     return $_[0]->{public_id};
563     } # public_id
564    
565     sub system_id ($;$) {
566     $_[0]->{system_id} = $_[1] if @_ > 1;
567     return $_[0]->{system_id};
568     } # system_id
569    
570 wakaba 1.1 =head1 SEE ALSO
571    
572 wakaba 1.2 L<Whatpm::HTML>
573 wakaba 1.1
574     =head1 AUTHOR
575    
576     Wakaba <w@suika.fam.cx>.
577    
578     =head1 LICENSE
579    
580     Copyright 2007 Wakaba <w@suika.fam.cx>
581    
582     This library is free software; you can redistribute it
583     and/or modify it under the same terms as Perl itself.
584    
585     =cut
586    
587     1;
588 wakaba 1.19 # $Date: 2007/11/23 07:35:03 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24