/[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.20 - (hide annotations) (download)
Thu Mar 20 03:37:19 2008 UTC (16 years, 7 months ago) by wakaba
Branch: MAIN
Changes since 1.19: +26 -3 lines
++ whatpm/Whatpm/ChangeLog	20 Mar 2008 03:37:11 -0000
	* HTML.pm.src: Set line/column numbers to attributes.

	* NanoDOM.pm (create_attribute_ns, set_attribute_node_ns): Added.
	(value): Setter implemented.

	* mkhtmlparser.pl: Set line/column numbers to Attr nodes.

2008-03-20  Wakaba  <wakaba@suika.fam.cx>

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.20 our $VERSION=do{my @r=(q$Revision: 1.19 $=~/\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 wakaba 1.20 sub create_attribute_ns ($$$) {
214     my ($self, $nsuri, $qn) = @_;
215     return Whatpm::NanoDOM::Attr->new (undef, $nsuri, $qn->[0], $qn->[1], '');
216    
217     ## NOTE: Created attribute node should be set to an element node
218     ## as far as possible. |onwer_document| of the attribute node, for
219     ## example, depends on the definedness of the |owner_element| attribute.
220     } # create_attribute_ns
221    
222     ## The second parameter only supports manakai extended way
223     ## to specify qualified name - "[$prefix, $local_name]"
224 wakaba 1.1 sub create_element_ns ($$$) {
225     my ($self, $nsuri, $qn) = @_;
226 wakaba 1.3 return Whatpm::NanoDOM::Element->new ($self, $nsuri, $qn->[0], $qn->[1]);
227 wakaba 1.1 } # create_element_ns
228    
229     ## A manakai extension
230     sub create_document_type_definition ($$) {
231     shift;
232 wakaba 1.2 return Whatpm::NanoDOM::DocumentType->new (shift);
233 wakaba 1.1 } # create_document_type_definition
234    
235 wakaba 1.3 sub implementation ($) {
236     return 'Whatpm::NanoDOM::DOMImplementation';
237     } # implementation
238    
239 wakaba 1.4 sub document_element ($) {
240     my $self = shift;
241     for (@{$self->child_nodes}) {
242     if ($_->node_type == 1) {
243     return $_;
244     }
245     }
246     return undef;
247     } # document_element
248    
249 wakaba 1.12 sub adopt_node ($$) {
250 wakaba 1.11 my @node = ($_[1]);
251     while (@node) {
252     my $node = shift @node;
253     $node->{owner_document} = $_[0];
254     Scalar::Util::weaken ($node->{owner_document});
255     push @node, @{$node->child_nodes};
256     push @node, @{$node->attributes or []} if $node->can ('attributes');
257     }
258     return $_[1];
259     } # adopt_node
260    
261 wakaba 1.12 sub manakai_is_html ($;$) {
262     if (@_ > 1) {
263     if ($_[1]) {
264     $_[0]->{manakai_is_html} = 1;
265     } else {
266 wakaba 1.13 delete $_[0]->{manakai_is_html};
267 wakaba 1.12 delete $_[0]->{manakai_compat_mode};
268     }
269     }
270     return $_[0]->{manakai_is_html};
271     } # manakai_is_html
272    
273     sub compat_mode ($) {
274     if ($_[0]->{manakai_is_html}) {
275     if ($_[0]->{manakai_compat_mode} eq 'quirks') {
276     return 'BackCompat';
277     }
278     }
279     return 'CSS1Compat';
280     } # compat_mode
281    
282     sub manakai_compat_mode ($;$) {
283     if ($_[0]->{manakai_is_html}) {
284     if (@_ > 1 and defined $_[1] and
285     {'no quirks' => 1, 'limited quirks' => 1, 'quirks' => 1}->{$_[1]}) {
286     $_[0]->{manakai_compat_mode} = $_[1];
287     }
288     return $_[0]->{manakai_compat_mode} || 'no quirks';
289     } else {
290     return 'no quirks';
291     }
292     } # manakai_compat_mode
293    
294 wakaba 1.19 sub manakai_head ($) {
295     my $html = $_[0]->manakai_html;
296     return undef unless defined $html;
297     for my $el (@{$html->child_nodes}) {
298     next unless $el->node_type == 1; # ELEMENT_NODE
299     my $nsuri = $el->namespace_uri;
300     next unless defined $nsuri;
301     next unless $nsuri eq q<http://www.w3.org/1999/xhtml>;
302     next unless $el->manakai_local_name eq 'head';
303     return $el;
304     }
305     return undef;
306     } # manakai_head
307    
308     sub manakai_html ($) {
309     my $de = $_[0]->document_element;
310     my $nsuri = $de->namespace_uri;
311     if (defined $nsuri and $nsuri eq q<http://www.w3.org/1999/xhtml> and
312     $de->manakai_local_name eq 'html') {
313     return $de;
314     } else {
315     return undef;
316     }
317     } # manakai_html
318    
319 wakaba 1.17 sub input_encoding ($;$) {
320     $_[0]->{input_encoding} = $_[1] if @_ > 1;
321     return $_[0]->{input_encoding};
322     }
323    
324     sub manakai_charset ($;$) {
325     $_[0]->{manakai_charset} = $_[1] if @_ > 1;
326     return $_[0]->{manakai_charset};
327     }
328    
329     sub manakai_has_bom ($;$) {
330     $_[0]->{manakai_has_bom} = $_[1] if @_ > 1;
331     return $_[0]->{manakai_has_bom};
332     }
333    
334 wakaba 1.2 package Whatpm::NanoDOM::Element;
335     push our @ISA, 'Whatpm::NanoDOM::Node';
336 wakaba 1.1
337 wakaba 1.3 sub new ($$$$$) {
338 wakaba 1.1 my $self = shift->SUPER::new;
339 wakaba 1.3 $self->{owner_document} = shift;
340     Scalar::Util::weaken ($self->{owner_document});
341 wakaba 1.1 $self->{namespace_uri} = shift;
342     $self->{prefix} = shift;
343     $self->{local_name} = shift;
344     $self->{attributes} = {};
345     $self->{child_nodes} = [];
346     return $self;
347     } # new
348    
349 wakaba 1.3 sub owner_document ($) {
350     return shift->{owner_document};
351     } # owner_document
352    
353 wakaba 1.1 sub clone_node ($$) {
354     my ($self, $deep) = @_; ## NOTE: Deep cloning is not supported
355     my $clone = bless {
356     namespace_uri => $self->{namespace_uri},
357     prefix => $self->{prefix},
358     local_name => $self->{local_name},
359     child_nodes => [],
360     }, ref $self;
361     for my $ns (keys %{$self->{attributes}}) {
362     for my $ln (keys %{$self->{attributes}->{$ns}}) {
363     my $attr = $self->{attributes}->{$ns}->{$ln};
364     $clone->{attributes}->{$ns}->{$ln} = bless {
365     namespace_uri => $attr->{namespace_uri},
366     prefix => $attr->{prefix},
367     local_name => $attr->{local_name},
368     value => $attr->{value},
369     }, ref $self->{attributes}->{$ns}->{$ln};
370     }
371     }
372     return $clone;
373     } # clone
374    
375     ## A manakai extension
376     sub manakai_append_text ($$) {
377     my $self = shift;
378     if (@{$self->{child_nodes}} and
379     $self->{child_nodes}->[-1]->node_type == 3) {
380     $self->{child_nodes}->[-1]->manakai_append_text (shift);
381     } else {
382 wakaba 1.2 my $text = Whatpm::NanoDOM::Text->new (shift);
383 wakaba 1.1 $self->append_child ($text);
384     }
385     } # manakai_append_text
386    
387 wakaba 1.7 sub text_content ($) {
388     my $self = shift;
389     my $r = '';
390     for my $child (@{$self->child_nodes}) {
391     if ($child->can ('data')) {
392     $r .= $child->data;
393     } else {
394     $r .= $child->text_content;
395     }
396     }
397     return $r;
398     } # text_content
399    
400 wakaba 1.1 sub attributes ($) {
401     my $self = shift;
402     my $r = [];
403     ## Order MUST be stable
404     for my $ns (sort {$a cmp $b} keys %{$self->{attributes}}) {
405     for my $ln (sort {$a cmp $b} keys %{$self->{attributes}->{$ns}}) {
406     push @$r, $self->{attributes}->{$ns}->{$ln}
407     if defined $self->{attributes}->{$ns}->{$ln};
408     }
409     }
410     return $r;
411     } # attributes
412    
413 wakaba 1.3 sub local_name ($) { # TODO: HTML5 case
414     return shift->{local_name};
415     } # local_name
416    
417 wakaba 1.4 sub manakai_local_name ($) {
418     return shift->{local_name}; # no case fixing for HTML5
419     } # manakai_local_name
420    
421 wakaba 1.3 sub namespace_uri ($) {
422     return shift->{namespace_uri};
423     } # namespace_uri
424    
425 wakaba 1.4 sub manakai_element_type_match ($$$) {
426     my ($self, $nsuri, $ln) = @_;
427     if (defined $nsuri) {
428     if (defined $self->{namespace_uri} and $nsuri eq $self->{namespace_uri}) {
429     return ($ln eq $self->{local_name});
430     } else {
431     return 0;
432     }
433     } else {
434     if (not defined $self->{namespace_uri}) {
435     return ($ln eq $self->{local_name});
436     } else {
437     return 0;
438     }
439     }
440     } # manakai_element_type_match
441    
442 wakaba 1.1 sub node_type { 1 }
443    
444     ## TODO: HTML5 capitalization
445     sub tag_name ($) {
446     my $self = shift;
447     if (defined $self->{prefix}) {
448     return $self->{prefix} . ':' . $self->{local_name};
449     } else {
450     return $self->{local_name};
451     }
452     } # tag_name
453    
454 wakaba 1.8 sub get_attribute_ns ($$$) {
455     my ($self, $nsuri, $ln) = @_;
456     $nsuri = '' unless defined $nsuri;
457     return defined $self->{attributes}->{$nsuri}->{$ln}
458     ? $self->{attributes}->{$nsuri}->{$ln}->value : undef;
459     } # get_attribute_ns
460    
461 wakaba 1.9 sub get_attribute_node_ns ($$$) {
462     my ($self, $nsuri, $ln) = @_;
463     $nsuri = '' unless defined $nsuri;
464     return $self->{attributes}->{$nsuri}->{$ln};
465     } # get_attribute_node_ns
466    
467 wakaba 1.1 sub has_attribute_ns ($$$) {
468     my ($self, $nsuri, $ln) = @_;
469 wakaba 1.8 $nsuri = '' unless defined $nsuri;
470 wakaba 1.1 return defined $self->{attributes}->{$nsuri}->{$ln};
471     } # has_attribute_ns
472    
473     ## The second parameter only supports manakai extended way
474     ## to specify qualified name - "[$prefix, $local_name]"
475     sub set_attribute_ns ($$$$) {
476     my ($self, $nsuri, $qn, $value) = @_;
477     $self->{attributes}->{$nsuri}->{$qn->[1]}
478 wakaba 1.5 = Whatpm::NanoDOM::Attr->new ($self, $nsuri, $qn->[0], $qn->[1], $value);
479 wakaba 1.1 } # set_attribute_ns
480    
481 wakaba 1.20 sub set_attribute_node_ns ($$) {
482     my $self = shift;
483     my $attr = shift;
484     $self->{attributes}->{$attr->namespace_uri}->{$attr->manakai_local_name}
485     = $attr;
486     $attr->{owner_element} = $self;
487     Scalar::Util::weaken ($attr->{owner_element});
488     } # set_attribute_node_ns
489    
490 wakaba 1.2 package Whatpm::NanoDOM::Attr;
491     push our @ISA, 'Whatpm::NanoDOM::Node';
492 wakaba 1.1
493 wakaba 1.5 sub new ($$$$$$) {
494 wakaba 1.1 my $self = shift->SUPER::new;
495 wakaba 1.5 $self->{owner_element} = shift;
496     Scalar::Util::weaken ($self->{owner_element});
497 wakaba 1.1 $self->{namespace_uri} = shift;
498     $self->{prefix} = shift;
499     $self->{local_name} = shift;
500     $self->{value} = shift;
501     return $self;
502     } # new
503    
504 wakaba 1.5 sub namespace_uri ($) {
505     return shift->{namespace_uri};
506     } # namespace_uri
507    
508     sub manakai_local_name ($) {
509     return shift->{local_name};
510     } # manakai_local_name
511    
512 wakaba 1.1 sub node_type { 2 }
513    
514 wakaba 1.14 sub owner_document ($) {
515     return shift->owner_element->owner_document;
516     } # owner_document
517    
518 wakaba 1.1 ## TODO: HTML5 case stuff?
519     sub name ($) {
520     my $self = shift;
521     if (defined $self->{prefix}) {
522     return $self->{prefix} . ':' . $self->{local_name};
523     } else {
524     return $self->{local_name};
525     }
526     } # name
527    
528 wakaba 1.20 sub value ($;$) {
529     if (@_ > 1) {
530     $_[0]->{value} = $_[1];
531     }
532 wakaba 1.1 return shift->{value};
533     } # value
534    
535 wakaba 1.5 sub owner_element ($) {
536     return shift->{owner_element};
537     } # owner_element
538    
539 wakaba 1.2 package Whatpm::NanoDOM::CharacterData;
540     push our @ISA, 'Whatpm::NanoDOM::Node';
541 wakaba 1.1
542     sub new ($$) {
543     my $self = shift->SUPER::new;
544     $self->{data} = shift;
545     return $self;
546     } # new
547    
548     ## A manakai extension
549     sub manakai_append_text ($$) {
550     my ($self, $s) = @_;
551     $self->{data} .= $s;
552     } # manakai_append_text
553    
554     sub data ($) {
555     return shift->{data};
556     } # data
557    
558 wakaba 1.2 package Whatpm::NanoDOM::Text;
559     push our @ISA, 'Whatpm::NanoDOM::CharacterData';
560 wakaba 1.1
561     sub node_type () { 3 }
562    
563 wakaba 1.2 package Whatpm::NanoDOM::Comment;
564     push our @ISA, 'Whatpm::NanoDOM::CharacterData';
565 wakaba 1.1
566     sub node_type () { 8 }
567    
568 wakaba 1.2 package Whatpm::NanoDOM::DocumentType;
569     push our @ISA, 'Whatpm::NanoDOM::Node';
570 wakaba 1.1
571     sub new ($$) {
572     my $self = shift->SUPER::new;
573     $self->{name} = shift;
574     return $self;
575     } # new
576    
577     sub node_type () { 10 }
578    
579     sub name ($) {
580     return shift->{name};
581     } # name
582    
583 wakaba 1.16 sub public_id ($;$) {
584     $_[0]->{public_id} = $_[1] if @_ > 1;
585     return $_[0]->{public_id};
586     } # public_id
587    
588     sub system_id ($;$) {
589     $_[0]->{system_id} = $_[1] if @_ > 1;
590     return $_[0]->{system_id};
591     } # system_id
592    
593 wakaba 1.1 =head1 SEE ALSO
594    
595 wakaba 1.2 L<Whatpm::HTML>
596 wakaba 1.1
597     =head1 AUTHOR
598    
599     Wakaba <w@suika.fam.cx>.
600    
601     =head1 LICENSE
602    
603     Copyright 2007 Wakaba <w@suika.fam.cx>
604    
605     This library is free software; you can redistribute it
606     and/or modify it under the same terms as Perl itself.
607    
608     =cut
609    
610     1;
611 wakaba 1.20 # $Date: 2008/02/17 06:36:28 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24