/[suikacvs]/messaging/manakai/lib/Message/DOM/Node.pm
Suika

Contents of /messaging/manakai/lib/Message/DOM/Node.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.18 - (show annotations) (download)
Sat Dec 22 06:29:32 2007 UTC (16 years, 11 months ago) by wakaba
Branch: MAIN
Changes since 1.17: +6 -16 lines
++ manakai/lib/Message/DOM/ChangeLog	22 Dec 2007 06:29:10 -0000
2007-12-22  Wakaba  <wakaba@suika.fam.cx>

	* CSSStyleSheet.pm, CSSRule.pm, CSSRuleList.pm, CSSStyleDeclaration.pm:
	First version of CSSOM implementation.

++ manakai/lib/Message/Charset/ChangeLog	23 Nov 2007 04:23:29 -0000
2007-11-23  Wakaba  <wakaba@suika.fam.cx>

	* Info.pm (is_syntactically_valid_iana_charset_name): Function
	name was misspelled.

1 package Message::DOM::Node;
2 use strict;
3 our $VERSION=do{my @r=(q$Revision: 1.17 $=~/\d+/g);sprintf "%d."."%02d" x $#r,@r};
4 push our @ISA, 'Message::IF::Node',
5 'Message::IF::NSResolver';
6 require Scalar::Util;
7 require Message::DOM::DOMException;
8
9 ## NOTE:
10 ## Node
11 ## + Attr (2)
12 ## + AttributeDefinition (81002)
13 ## + CharacterData
14 ## + Comment (8)
15 ## + Text (3)
16 ## + CDATASection (4)
17 ## + Document (9)
18 ## + DocumentFragment (11)
19 ## + DocumentType (10)
20 ## + Element (1)
21 ## + ElementTypeDefinition (81001)
22 ## + Entity (6)
23 ## + EntityReference (5)
24 ## + Notation (12)
25 ## + ProcessingInstruction (7)
26
27 use overload
28 '==' => 'is_equal_node',
29 '!=' => sub {
30 return not ($_[0] == $_[1]);
31 },
32 #eq => sub { $_[0] eq $_[1] }, ## is_same_node
33 #ne => sub { $_[0] ne $_[1] }, ## not is_same_node
34 fallback => 1;
35
36 ## The |Node| interface - constants
37
38 ## Definition group NodeType
39
40 ## NOTE: Numeric codes up to 200 are reserved by W3C [DOM1SE, DOM2, DOM3].
41
42 sub ELEMENT_NODE () { 1 }
43 sub ATTRIBUTE_NODE () { 2 }
44 sub TEXT_NODE () { 3 }
45 sub CDATA_SECTION_NODE () { 4 }
46 sub ENTITY_REFERENCE_NODE () { 5 }
47 sub ENTITY_NODE () { 6 }
48 sub PROCESSING_INSTRUCTION_NODE () { 7 }
49 sub COMMENT_NODE () { 8 }
50 sub DOCUMENT_NODE () { 9 }
51 sub DOCUMENT_TYPE_NODE () { 10 }
52 sub DOCUMENT_FRAGMENT_NODE () { 11 }
53 sub NOTATION_NODE () { 12 }
54 sub ELEMENT_TYPE_DEFINITION_NODE () { 81001 }
55 sub ATTRIBUTE_DEFINITION_NODE () { 81002 }
56
57 ## Definition group DocumentPosition
58
59 sub DOCUMENT_POSITION_DISCONNECTED () { 0x01 }
60 sub DOCUMENT_POSITION_PRECEDING () { 0x02 }
61 sub DOCUMENT_POSITION_FOLLOWING () { 0x04 }
62 sub DOCUMENT_POSITION_CONTAINS () { 0x08 }
63 sub DOCUMENT_POSITION_CONTAINED_BY () { 0x10 }
64 sub DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC () { 0x20 }
65
66 ## |OperationType| from the |UserDataHandler| interface.
67 sub NODE_CLONED () { 1 }
68 sub NODE_IMPORTED () { 2 }
69 sub NODE_DELETED () { 3 }
70 sub NODE_RENAMED () { 4 }
71 sub NODE_ADOPTED () { 5 }
72
73 sub ____new ($$) {
74 my $self = bless \({}), shift;
75 $$self->{owner_document} = shift;
76 Scalar::Util::weaken ($$self->{owner_document});
77 return $self;
78 } # ____new
79
80 sub ___report_error ($$) {
81 $_[1]->throw;
82 } # ___report_error
83
84 sub AUTOLOAD {
85 my $method_name = our $AUTOLOAD;
86 $method_name =~ s/.*:://;
87 return if $method_name eq 'DESTROY';
88
89 if ({
90 ## Read-only attributes (trivial accessors)
91 owner_document => 1,
92 parent_node => 1,
93 manakai_read_only => 1,
94 }->{$method_name}) {
95 no strict 'refs';
96 eval qq{
97 sub $method_name (\$) {
98 return \${\$_[0]}->{$method_name};
99 }
100 };
101 goto &{ $AUTOLOAD };
102 } elsif (my $module_name = {
103 add_event_listener => 'Message::DOM::EventTargetNode',
104 add_event_listener_ns => 'Message::DOM::EventTargetNode',
105 dispatch_event => 'Message::DOM::EventTargetNode',
106 remove_event_listener => 'Message::DOM::EventTargetNode',
107 remove_event_listener_ns => 'Message::DOM::EventTargetNode',
108 }->{$method_name}) {
109 eval qq{ require $module_name } or die $@;
110 goto &{ $AUTOLOAD };
111 } else {
112 require Carp;
113 Carp::croak (qq<Can't locate method "$AUTOLOAD">);
114 }
115 } # AUTOLOAD
116
117 ## |Node| attributes
118
119 ## NOTE: Overridden by |Element|.
120 sub attributes () { undef }
121
122 sub base_uri ($) {
123 ## NOTE: Overridden by |Attr|, |CharacterData|, |Document|, |DocumentType|,
124 ## |Element|, |EntityReference|, and |ProcessingInstruction|.
125
126 local $Error::Depth = $Error::Depth + 1;
127 return $_[0]->owner_document->base_uri;
128 } # base_uri
129
130 sub child_nodes ($) {
131 ## NOTE: Overridden by |CharacterData|, |ElementTypeDefinition|,
132 ## |Notation|, and |ProcessingInstruction|.
133 require Message::DOM::NodeList;
134 return bless \\($_[0]), 'Message::DOM::NodeList::ChildNodeList';
135 } # child_nodes
136
137 sub manakai_expanded_uri ($) {
138 my $self = shift;
139 local $Error::Depth = $Error::Depth + 1;
140 my $ln = $self->local_name;
141 if (defined $ln) {
142 my $nsuri = $self->namespace_uri;
143 if (defined $nsuri) {
144 return $nsuri . $ln;
145 } else {
146 return $ln;
147 }
148 } else {
149 return undef;
150 }
151 } # manakai_expanded_uri
152
153 sub first_child ($) {
154 my $self = shift;
155 return $$self->{child_nodes} ? $$self->{child_nodes}->[0] : undef;
156 } # first_child
157
158 sub manakai_language ($;$) {
159 my $self = $_[0];
160 local $Error::Depth = $Error::Depth + 1;
161
162 if (@_ > 1) {
163 if ($self->node_type == 1) { # ELEMENT_NODE
164 if (defined $_[1]) {
165 if ($self->has_attribute_ns (undef, 'xml:lang')) {
166 $self->set_attribute_ns (undef, [undef, 'xml:lang'] => $_[1]);
167 # or exception
168 } else {
169 $self->set_attribute_ns
170 (q<http://www.w3.org/XML/1998/namespace>, 'xml:lang', $_[1]);
171 }
172 } else {
173 $self->remove_attribute_ns
174 (q<http://www.w3.org/XML/1998/namespace>, 'lang');
175 $self->remove_attribute_ns (undef, 'xml:lang');
176 }
177 }
178
179 return undef unless defined wantarray;
180 }
181
182 my $target = $self;
183 while (defined $target) {
184 if ($target->node_type == 1) { # ELEMENT_NODE
185 ## Step 1
186
187 ## Step 1.1
188 my $r = $target->get_attribute_ns
189 (q<http://www.w3.org/XML/1998/namespace>, 'lang');
190 return $r if defined $r;
191
192 ## Step 1.2
193 $r = $target->get_attribute_ns (undef, 'xml:lang');
194 return $r if defined $r;
195 }
196
197 ## Step 2
198 $target = $target->parent_node;
199 }
200
201 ## Step 3
202 my $od = $self->owner_document;
203 if (defined $od) {
204 return $od->manakai_language;
205 }
206
207 ## Step 4
208 ## TODO: from upper-level protocol, if $self isa Document
209
210 ## Step 5
211 return '';
212 } # manakai_language
213
214 sub last_child ($) {
215 my $self = shift;
216 return $$self->{child_nodes} && $$self->{child_nodes}->[0]
217 ? $$self->{child_nodes}->[-1] : undef;
218 } # last_child
219
220 sub local_name { undef }
221
222 sub manakai_local_name { undef }
223
224 sub namespace_uri { undef }
225
226 sub next_sibling ($) {
227 my $self = shift;
228 my $parent = $$self->{parent_node};
229 return undef unless defined $parent;
230 my $has_self;
231 for (@{$parent->child_nodes}) {
232 if ($_ eq $self) {
233 $has_self = 1;
234 } elsif ($has_self) {
235 return $_;
236 }
237 }
238 return undef;
239 } # next_sibling
240
241 ## NOTE: Overridden by subclasses.
242 sub node_name () { undef }
243
244 ## NOTE: Overridden by subclasses.
245 sub node_type () { }
246
247 ## NOTE: Overridden by |Attr|, |AttributeDefinition|,
248 ## |CharacterData|, and |ProcessingInstruction|.
249 sub node_value () { undef }
250
251 sub owner_document ($);
252
253 sub manakai_parent_element ($) {
254 my $self = shift;
255 my $parent = $$self->{parent_node};
256 while (defined $parent) {
257 if ($parent->node_type == ELEMENT_NODE) {
258 return $parent;
259 } else {
260 $parent = $$parent->{parent_node};
261 }
262 }
263 return undef;
264 } # manakai_parent_element
265
266 sub parent_node ($);
267
268 ## NOTE: Overridden by |Element| and |Attr|.
269 sub prefix ($;$) { undef }
270
271 sub previous_sibling ($) {
272 my $self = shift;
273 my $parent = $$self->{parent_node};
274 return undef unless defined $parent;
275 my $prev;
276 for (@{$parent->child_nodes}) {
277 if ($_ eq $self) {
278 return $prev;
279 } else {
280 $prev = $_;
281 }
282 }
283 return undef;
284 } # previous_sibling
285
286 sub manakai_read_only ($);
287
288 sub text_content ($;$) {
289 ## NOTE: For |Element|, |Attr|, |Entity|, |EntityReference|,
290 ## |DocumentFragment|, and |AttributeDefinition|. In addition,
291 ## |Document|'s |text_content| might call this attribute.
292
293 ## NOTE: Overridden by |Document|, |DocumentType|, |Notation|,
294 ## |CharacterData|, |ProcessingInstruction|, and |ElementTypeDefinition|.
295
296 my $self = $_[0];
297
298 if (@_ > 1) {
299 if (${$$self->{owner_document} or $self}->{strict_error_checking} and
300 $$self->{manakai_read_only}) {
301 report Message::DOM::DOMException
302 -object => $self,
303 -type => 'NO_MODIFICATION_ALLOWED_ERR',
304 -subtype => 'READ_ONLY_NODE_ERR';
305 }
306
307 local $Error::Depth = $Error::Depth + 1;
308 @{$self->child_nodes} = ();
309 if (defined $_[1] and length $_[1]) {
310 ## NOTE: |DocumentType| don't use this code.
311 my $text = ($$self->{owner_document} || $self)->create_text_node ($_[1]);
312 $self->append_child ($text);
313 }
314 }
315
316 if (defined wantarray) {
317 local $Error::Depth = $Error::Depth + 1;
318 my $r = '';
319 my @node = @{$self->child_nodes};
320 while (@node) {
321 my $child = shift @node;
322 my $child_nt = $child->node_type;
323 if ($child_nt == TEXT_NODE or $child_nt == CDATA_SECTION_NODE) {
324 $r .= $child->node_value unless $child->is_element_content_whitespace;
325 } elsif ($child_nt == COMMENT_NODE or
326 $child_nt == PROCESSING_INSTRUCTION_NODE or
327 $child_nt == DOCUMENT_TYPE_NODE) {
328 #
329 } else {
330 unshift @node, @{$child->child_nodes};
331 }
332 }
333 return $r;
334 }
335 } # text_content
336
337 ## |Node| methods
338
339 sub append_child ($$) {
340 ## NOTE: |Element|, |Entity|, |DocumentFragment|, |EntityReference|.
341 ## NOTE: |Document|, |Attr|, |CharacterData|, |AttributeDefinition|,
342 ## |Notation|, |ProcessingInstruction| |ElementTypeDefinition|,
343 ## and |DocumentType| define their own implementations.
344 my $self = $_[0];
345
346 ## NOTE: Depends on $self->node_type:
347 my $self_od = $$self->{owner_document};
348
349 ## -- Node Type check
350 my @new_child;
351 my $new_child_parent;
352 if ($_[1]->node_type == DOCUMENT_FRAGMENT_NODE) {
353 push @new_child, @{$_[1]->child_nodes};
354 $new_child_parent = $_[1];
355 } else {
356 @new_child = ($_[1]);
357 $new_child_parent = $_[1]->parent_node;
358 }
359
360 ## NOTE: Depends on $self->node_type:
361 if ($$self_od->{strict_error_checking}) {
362 my $child_od = $_[1]->owner_document || $_[1]; # might be DocumentType
363 if ($self_od ne $child_od and $child_od->node_type != DOCUMENT_TYPE_NODE) {
364 report Message::DOM::DOMException
365 -object => $self,
366 -type => 'WRONG_DOCUMENT_ERR',
367 -subtype => 'EXTERNAL_OBJECT_ERR';
368 }
369
370 if ($$self->{manakai_read_only} or
371 (@new_child and defined $new_child_parent and
372 $$new_child_parent->{manakai_read_only})) {
373 report Message::DOM::DOMException
374 -object => $self,
375 -type => 'NO_MODIFICATION_ALLOWED_ERR',
376 -subtype => 'READ_ONLY_NODE_ERR';
377 }
378
379 ## NOTE: |Document| has children order check here.
380
381 for my $cn (@new_child) {
382 unless ({
383 TEXT_NODE, 1, ENTITY_REFERENCE_NODE, 1,
384 ELEMENT_NODE, 1, CDATA_SECTION_NODE, 1,
385 PROCESSING_INSTRUCTION_NODE, 1, COMMENT_NODE, 1,
386 }->{$cn->node_type}) {
387 report Message::DOM::DOMException
388 -object => $self,
389 -type => 'HIERARCHY_REQUEST_ERR',
390 -subtype => 'CHILD_NODE_TYPE_ERR';
391 }
392 }
393
394 my $anode = $self;
395 while (defined $anode) {
396 if ($anode eq $_[1]) {
397 report Message::DOM::DOMException
398 -object => $self,
399 -type => 'HIERARCHY_REQUEST_ERR',
400 -subtype => 'ANCESTOR_NODE_ERR';
401 }
402 $anode = $$anode->{parent_node};
403 }
404 }
405
406 ## NOTE: "Insert at" code only in insert_before and replace_child
407
408 ## -- Removes from parent
409 if ($new_child_parent) {
410 if (@new_child == 1) {
411 my $v = $$new_child_parent->{child_nodes};
412 RP: for my $i (0..$#$v) {
413 if ($v->[$i] eq $new_child[0]) {
414 splice @$v, $i, 1, ();
415 last RP;
416 }
417 } # RP
418 } else {
419 @{$$new_child_parent->{child_nodes}} = ();
420 }
421 }
422
423 ## -- Rewrite the |parentNode| properties
424 for my $nc (@new_child) {
425 $$nc->{parent_node} = $self;
426 Scalar::Util::weaken ($$nc->{parent_node});
427 }
428
429 ## NOTE: Depends on method:
430 push @{$$self->{child_nodes}}, @new_child;
431
432 ## NOTE: Setting |owner_document| in |Document|.
433
434 return $_[1];
435 } # apepnd_child
436
437 sub clone_node ($;$) {
438 my ($self, $deep) = @_;
439
440 ## TODO: Cloning operation
441 ## for ElementTypeDefinition, and AttributeDefinition nodes,
442 ## as well as new attributes introduced in DOM XML Document Type Definition
443 ## module.
444 ## TODO: if default attributes and attributedefinition are inconsistent
445
446 local $Error::Depth = $Error::Depth + 1;
447 my $od = $self->owner_document;
448 my $strict_check = $od->strict_error_checking;
449 $od->strict_error_checking (0);
450 my $cfg = $od->dom_config;
451 my $er_copy_asis
452 = $cfg->get_parameter
453 (q<http://suika.fam.cx/www/2006/dom-config/clone-entity-reference-subtree>);
454
455 my $r;
456 my @udh;
457 my @node = ([$self]);
458 while (@node) {
459 my ($node, $parent) = @{shift @node};
460 my $nt = $node->node_type;
461 my $clone;
462 if ($nt == ELEMENT_NODE) {
463 $clone = $od->create_element_ns
464 ($node->namespace_uri, [$node->prefix, $node->local_name]);
465 if ($parent) {
466 $parent->append_child ($clone);
467 } else {
468 $r = $clone;
469 }
470 my $attrs = $node->attributes;
471 my $attrsMax = @$attrs - 1;
472 for my $i (0..$attrsMax) {
473 my $attr = $attrs->[$i];
474 push @node, [$attr, $clone] if $attr->specified;
475 }
476 if ($deep) {
477 push @node, map {[$_, $clone]} @{$node->child_nodes};
478 }
479 } elsif ($nt == TEXT_NODE) {
480 $clone = $od->create_text_node ($node->data);
481 if ($parent) {
482 $parent->append_child ($clone);
483 } else {
484 $r = $clone;
485 }
486 $clone->is_element_content_whitespace (1)
487 if $node->is_element_content_whitespace;
488 } elsif ($nt == ATTRIBUTE_NODE) {
489 $clone = $od->create_attribute_ns
490 ($node->namespace_uri, [$node->prefix, $node->local_name]);
491 if ($parent) {
492 $parent->set_attribute_node_ns ($clone);
493 } else {
494 $r = $clone;
495 }
496 $clone->specified (1);
497 push @node, map {[$_, $clone]} @{$node->child_nodes};
498 } elsif ($nt == COMMENT_NODE) {
499 $clone = $od->create_comment ($node->data);
500 if ($parent) {
501 $parent->append_child ($clone);
502 } else {
503 $r = $clone;
504 }
505 } elsif ($nt == CDATA_SECTION_NODE) {
506 $clone = $od->create_cdata_section ($node->data);
507 if ($parent) {
508 $parent->append_child ($clone);
509 } else {
510 $r = $clone;
511 }
512 } elsif ($nt == PROCESSING_INSTRUCTION_NODE) {
513 $clone = $od->create_processing_instruction
514 ($node->target, $node->data);
515 if ($parent) {
516 $parent->append_child ($clone);
517 } else {
518 $r = $clone;
519 }
520 } elsif ($nt == ENTITY_REFERENCE_NODE) {
521 $clone = $od->create_entity_reference ($node->node_name);
522 if ($er_copy_asis) {
523 $clone->manakai_set_read_only (0);
524 $clone->text_content (0);
525 for (@{$node->child_nodes}) {
526 $clone->append_child ($_->clone_node (1));
527 }
528 $clone->manakai_expanded ($node->manakai_expanded);
529 $clone->manakai_set_read_only (1, 1);
530 } # copy asis
531 if ($parent) {
532 $parent->append_child ($clone);
533 } else {
534 $r = $clone;
535 }
536 } elsif ($nt == DOCUMENT_FRAGMENT_NODE) {
537 $clone = $od->create_document_fragment;
538 $r = $clone;
539 push @node, map {[$_, $clone]} @{$node->child_nodes};
540 } elsif ($nt == DOCUMENT_NODE) {
541 $od->strict_error_checking ($strict_check);
542 report Message::DOM::DOMException
543 -object => $self,
544 -type => 'NOT_SUPPORTED_ERR',
545 -subtype => 'CLONE_NODE_NOT_SUPPORTED_ERR';
546 } elsif ($nt == DOCUMENT_TYPE_NODE) {
547 $od->strict_error_checking ($strict_check);
548 report Message::DOM::DOMException
549 -object => $self,
550 -type => 'NOT_SUPPORTED_ERR',
551 -subtype => 'CLONE_NODE_NOT_SUPPORTED_ERR';
552 } elsif ($nt == ENTITY_NODE) {
553 $od->strict_error_checking ($strict_check);
554 report Message::DOM::DOMException
555 -object => $self,
556 -type => 'NOT_SUPPORTED_ERR',
557 -subtype => 'CLONE_NODE_NOT_SUPPORTED_ERR';
558 } elsif ($nt == NOTATION_NODE) {
559 $od->strict_error_checking ($strict_check);
560 report Message::DOM::DOMException
561 -object => $self,
562 -type => 'NOT_SUPPORTED_ERR',
563 -subtype => 'CLONE_NODE_NOT_SUPPORTED_ERR';
564 } else {
565 $od->strict_error_checking ($strict_check);
566 report Message::DOM::DOMException
567 -object => $self,
568 -type => 'NOT_SUPPORTED_ERR',
569 -subtype => 'CLONE_NODE_NOT_SUPPORTED_ERR';
570 }
571
572 my $udhs = $$self->{user_data};
573 push @udh, [$node => $clone, $udhs] if $udhs and %$udhs;
574 } # @node
575 $od->strict_error_checking (1) if $strict_check;
576
577 ## Calling user data handlers if any
578 for my $sd (@udh) {
579 my $src = $sd->[0];
580 my $src_ud = $sd->[2];
581 for my $key (keys %{$src_ud}) {
582 my $dh = $src_ud->{$key}->[1];
583 if ($dh) { ## NODE_CLONED
584 $dh->handle (1, $key, $src_ud->{$key}->[0], $src, $sd->[1]);
585 ## ISSUE: |handler| method? CODE?
586 }
587 }
588 }
589
590 return $r;
591 } # clone_node
592
593 sub compare_document_position ($$) {
594 ## ISSUE: There are implementation specifics
595 ## (see what Gecko does if it implement this method...)
596
597 ## ISSUE: Maybe we should overload <=> or cmp
598
599 ## TODO: Too long method name! Too long constant names!
600 ## Too many thing to be done by a method!
601 ## Maybe we should import simpler method implemented by IE.
602
603 ## TODO: ElementTypeDefinition and AttributeDefinition
604
605 my @acontainer = ($_[0]);
606 my @bcontainer = ($_[1]);
607 F: {
608 A: while (1) {
609 if ($acontainer[-1] eq $bcontainer[-1]) {
610 last F;
611 } else {
612 my $ap;
613 my $atype = $acontainer[-1]->node_type;
614 if ($atype == ATTRIBUTE_NODE) {
615 $ap = $acontainer[-1]->owner_element;
616 } elsif ($atype == ENTITY_NODE or $atype == NOTATION_NODE or
617 $atype == ELEMENT_TYPE_DEFINITION_NODE) {
618 $ap = $acontainer[-1]->owner_document_type_definition;
619 } elsif ($atype == ATTRIBUTE_DEFINITION_NODE) {
620 $ap = $acontainer[-1]->owner_element_type_definition;
621 } else {
622 $ap = $acontainer[-1]->parent_node;
623 }
624 if (defined $ap) {
625 push @acontainer, $ap;
626 } else {
627 last A;
628 }
629 }
630 } # A
631
632 B: while (1) {
633 if ($acontainer[-1] eq $bcontainer[-1]) {
634 last F;
635 } else {
636 my $bp;
637 my $btype = $bcontainer[-1]->node_type;
638 if ($btype == ATTRIBUTE_NODE) {
639 $bp = $bcontainer[-1]->owner_element;
640 } elsif ($btype == ENTITY_NODE or $btype == NOTATION_NODE or
641 $btype == ELEMENT_TYPE_DEFINITION_NODE) {
642 $bp = $bcontainer[-1]->owner_document_type_definition;
643 } elsif ($btype == ATTRIBUTE_DEFINITION_NODE) {
644 $bp = $bcontainer[-1]->owner_element_type_definition;
645 } else {
646 $bp = $bcontainer[-1]->parent_node;
647 }
648 if (defined $bp) {
649 push @bcontainer, $bp;
650 } else {
651 last B;
652 }
653 }
654 } # B
655
656 ## Disconnected
657 if ($bcontainer[-1]->isa ('Message::IF::Node')) {
658 ## ISSUE: Document this in manakai's DOM Perl Binding?
659 return DOCUMENT_POSITION_DISCONNECTED
660 | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
661 | ((${$acontainer[-1]} cmp ${$bcontainer[-1]}) > 0
662 ? DOCUMENT_POSITION_FOLLOWING
663 : DOCUMENT_POSITION_PRECEDING);
664 } else {
665 ## TODO: Is there test cases for this?
666 return DOCUMENT_POSITION_DISCONNECTED
667 | DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
668 | DOCUMENT_POSITION_FOLLOWING;
669 }
670 } # F
671
672 ## Common container found
673 if (@acontainer >= 2) {
674 if (@bcontainer >= 2) {
675 my $acnt = $acontainer[-2]->node_type;
676 my $bcnt = $bcontainer[-2]->node_type;
677 if ($acnt == ATTRIBUTE_NODE or
678 $acnt == NOTATION_NODE or
679 $acnt == ELEMENT_TYPE_DEFINITION_NODE or
680 $acnt == ATTRIBUTE_DEFINITION_NODE) {
681 if ($acnt == $bcnt) {
682 return DOCUMENT_POSITION_IMPLEMENTATION_SPECIFIC
683 | (($acontainer[-2]->node_name cmp
684 $bcontainer[-2]->node_name) > 0
685 ? DOCUMENT_POSITION_FOLLOWING
686 : DOCUMENT_POSITION_PRECEDING);
687 } elsif ($bcnt == ATTRIBUTE_NODE or
688 $bcnt == NOTATION_NODE or
689 $bcnt == ELEMENT_TYPE_DEFINITION_NODE or
690 $bcnt == ATTRIBUTE_DEFINITION_NODE) {
691 return (($acnt < $bcnt)
692 ? DOCUMENT_POSITION_FOLLOWING
693 : DOCUMENT_POSITION_PRECEDING);
694 } else {
695 ## A: Non-child and B: child
696 return DOCUMENT_POSITION_FOLLOWING;
697 }
698 } elsif ($bcnt == ATTRIBUTE_NODE or
699 $bcnt == NOTATION_NODE or
700 $bcnt == ELEMENT_TYPE_DEFINITION_NODE or
701 $bcnt == ATTRIBUTE_DEFINITION_NODE) {
702 ## A: Child and B: non-child
703 return DOCUMENT_POSITION_PRECEDING;
704 } else {
705 ## A and B are both children
706 for my $cn (@{$acontainer[-1]->child_nodes}) {
707 if ($cn eq $acontainer[-2]) {
708 return DOCUMENT_POSITION_FOLLOWING;
709 } elsif ($cn eq $bcontainer[-2]) {
710 return DOCUMENT_POSITION_PRECEDING;
711 }
712 }
713 die "compare_document_position: Something wrong (1)";
714 }
715 } else {
716 ## B contains A
717 return DOCUMENT_POSITION_CONTAINS
718 | DOCUMENT_POSITION_PRECEDING;
719 }
720 } else {
721 if (@bcontainer >= 2) {
722 ## A contains B
723 return DOCUMENT_POSITION_CONTAINED_BY
724 | DOCUMENT_POSITION_FOLLOWING;
725 } else {
726 ## A eq B
727 return 0;
728 }
729 }
730 die "compare_document_position: Something wrong (2)";
731 } # compare_document_position
732
733 sub get_feature ($$;$) {
734 my $feature = lc $_[1]; ## TODO: |lc|?
735 $feature =~ s/^\+//;
736 my $version = defined $_[2] ? $_[2] : '';
737 if ($Message::DOM::DOMImplementation::HasFeature->{$feature}->{$version}) {
738 return $_[0];
739 } else {
740 return undef;
741 }
742 } # get_feature
743
744 sub get_user_data ($$) {
745 if (${$_[0]}->{user_data}->{$_[1]}) {
746 return ${$_[0]}->{user_data}->{$_[1]}->[0];
747 } else {
748 return undef;
749 }
750 } # get_user_data
751
752 sub has_attributes ($) {
753 for (values %{${$_[0]}->{attributes} or {}}) {
754 return 1 if keys %$_;
755 }
756 return 0;
757 } # has_attributes
758
759 sub has_child_nodes ($) {
760 return (@{${$_[0]}->{child_nodes} or []} > 0);
761 } # has_child_nodes
762
763 sub insert_before ($$) {
764 ## NOTE: |Element|, |Entity|, |DocumentFragment|, |EntityReference|.
765 ## NOTE: |Document|, |Attr|, |CharacterData|, |AttributeDefinition|,
766 ## |Notation|, |ProcessingInstruction|, |ElementTypeDefinition|,
767 ## and |DocumentType| define their own implementations.
768 my $self = $_[0];
769
770 ## NOTE: Depends on $self->node_type:
771 my $self_od = $$self->{owner_document};
772
773 ## -- Node Type check
774 my @new_child;
775 my $new_child_parent;
776 if ($_[1]->node_type == DOCUMENT_FRAGMENT_NODE) {
777 push @new_child, @{$_[1]->child_nodes};
778 $new_child_parent = $_[1];
779 } else {
780 @new_child = ($_[1]);
781 $new_child_parent = $_[1]->parent_node;
782 }
783
784 ## NOTE: Depends on $self->node_type:
785 if ($$self_od->{strict_error_checking}) {
786 my $child_od = $_[1]->owner_document || $_[1]; # might be DocumentType
787 if ($self_od ne $child_od and $child_od->node_type != DOCUMENT_TYPE_NODE) {
788 report Message::DOM::DOMException
789 -object => $self,
790 -type => 'WRONG_DOCUMENT_ERR',
791 -subtype => 'EXTERNAL_OBJECT_ERR';
792 }
793
794 if ($$self->{manakai_read_only} or
795 (@new_child and defined $new_child_parent and
796 $$new_child_parent->{manakai_read_only})) {
797 report Message::DOM::DOMException
798 -object => $self,
799 -type => 'NO_MODIFICATION_ALLOWED_ERR',
800 -subtype => 'READ_ONLY_NODE_ERR';
801 }
802
803 ## NOTE: |Document| has children order check here.
804
805 for my $cn (@new_child) {
806 unless ({
807 TEXT_NODE, 1, ENTITY_REFERENCE_NODE, 1,
808 ELEMENT_NODE, 1, CDATA_SECTION_NODE, 1,
809 PROCESSING_INSTRUCTION_NODE, 1, COMMENT_NODE, 1,
810 }->{$cn->node_type}) {
811 report Message::DOM::DOMException
812 -object => $self,
813 -type => 'HIERARCHY_REQUEST_ERR',
814 -subtype => 'CHILD_NODE_TYPE_ERR';
815 }
816 }
817
818 my $anode = $self;
819 while (defined $anode) {
820 if ($anode eq $_[1]) {
821 report Message::DOM::DOMException
822 -object => $self,
823 -type => 'HIERARCHY_REQUEST_ERR',
824 -subtype => 'ANCESTOR_NODE_ERR';
825 }
826 $anode = $$anode->{parent_node};
827 }
828 }
829
830 ## -- Insert at... ## NOTE: Only in insert_before and replace_child
831 my $index = -1; # last
832 if (defined $_[2]) {
833 ## error if $_[1] eq $_[2];
834
835 my $cns = $self->child_nodes;
836 my $cnsl = @$cns;
837 C: {
838 $index = 0;
839 for my $i (0..($cnsl-1)) {
840 my $cn = $cns->[$i];
841 if ($cn eq $_[2]) {
842 $index += $i;
843 last C;
844 } elsif ($cn eq $_[1]) {
845 $index = -1; # offset
846 }
847 }
848
849 report Message::DOM::DOMException
850 -object => $self,
851 -type => 'NOT_FOUND_ERR',
852 -subtype => 'NOT_CHILD_ERR';
853 } # C
854 }
855 ## NOTE: "else" only in replace_child
856
857 ## -- Removes from parent
858 if ($new_child_parent) {
859 if (@new_child == 1) {
860 my $v = $$new_child_parent->{child_nodes};
861 RP: for my $i (0..$#$v) {
862 if ($v->[$i] eq $new_child[0]) {
863 splice @$v, $i, 1, ();
864 last RP;
865 }
866 } # RP
867 } else {
868 @{$$new_child_parent->{child_nodes}} = ();
869 }
870 }
871
872 ## -- Rewrite the |parentNode| properties
873 for my $nc (@new_child) {
874 $$nc->{parent_node} = $self;
875 Scalar::Util::weaken ($$nc->{parent_node});
876 }
877
878 ## NOTE: Depends on method:
879 if ($index == -1) {
880 push @{$$self->{child_nodes}}, @new_child;
881 } else {
882 splice @{$$self->{child_nodes}}, $index, 0, @new_child;
883 }
884
885 ## NOTE: Setting |owner_document| in |Document|.
886
887 return $_[1];
888 } # insert_before
889
890 sub is_equal_node ($$) {
891 local $Error::Depth = $Error::Depth + 1;
892
893 return 0 unless UNIVERSAL::isa ($_[1], 'Message::IF::Node');
894
895 my $nt = $_[0]->node_type;
896 return 0 unless $nt == $_[1]->node_type;
897
898 my @str_attr = qw/node_name local_name namespace_uri
899 prefix node_value/;
900 push @str_attr, qw/public_id system_id internal_subset/
901 if $nt == DOCUMENT_TYPE_NODE;
902 for my $attr_name (@str_attr) {
903 my $v1 = $_[0]->can ($attr_name) ? $_[0]->$attr_name : undef;
904 my $v2 = $_[1]->can ($attr_name) ? $_[1]->$attr_name : undef;
905 if (defined $v1 and defined $v2) {
906 return 0 unless ''.$v1 eq ''.$v2;
907 } elsif (defined $v1 or defined $v2) {
908 return 0;
909 }
910 }
911
912 my @num_eq_attr = qw/child_nodes attributes/;
913 push @num_eq_attr, qw/entities notations element_types/
914 if $nt == DOCUMENT_TYPE_NODE;
915 push @num_eq_attr, qw/attribute_definitions/
916 if $nt == ELEMENT_TYPE_DEFINITION_NODE;
917 push @num_eq_attr, qw/declared_type default_type allowed_tokens/
918 if $nt == ATTRIBUTE_DEFINITION_NODE;
919 for my $attr_name (@num_eq_attr) {
920 my $v1 = $_[0]->can ($attr_name) ? $_[0]->$attr_name : undef;
921 my $v2 = $_[1]->can ($attr_name) ? $_[1]->$attr_name : undef;
922 if (defined $v1 and defined $v2) {
923 return 0 unless $v1 == $v2;
924 } elsif (defined $v1 or defined $v2) {
925 return 0;
926 }
927 }
928
929 return 1;
930 } # is_equal_node
931
932 sub is_same_node ($$) { $_[0] eq $_[1] }
933
934 sub is_supported ($$;$) {
935 my $feature = lc $_[1]; ## TODO: |lc|?
936 my $plus = ($feature =~ s/^\+//);
937 my $version = defined $_[2] ? $_[2] : '';
938 return $Message::DOM::DOMImplementation::HasFeature->{$feature}->{$version};
939 } # is_supported;
940
941 sub manakai_append_text ($$) {
942 ## NOTE: For |Element|, |Attr|, |Entity|, |EntityReference|,
943 ## |DocumentFragment|, and |AttributeDefinition|. In addition,
944 ## |Document|'s |text_content| might call this attribute.
945
946 ## NOTE: Overridden by |Document|, |DocumentType|, |CharacterData|,
947 ## |ElementTypeDefinition|, |Notation|, and |ProcessingInstruction|.
948
949 my $self = $_[0];
950 local $Error::Depth = $Error::Depth + 1;
951 if (@{$$self->{child_nodes}} and
952 $$self->{child_nodes}->[-1]->node_type == TEXT_NODE) {
953 $$self->{child_nodes}->[-1]->manakai_append_text ($_[1]);
954 } else {
955 my $text = ($$self->{owner_document} or $self)->create_text_node ($_[1]);
956 $self->append_child ($text);
957 }
958 } # manakai_append_text
959
960 sub is_default_namespace ($$) {
961 local $Error::Depth = $Error::Depth + 1;
962 my $namespace_uri = defined $_[1] ? $_[1] : '';
963 my $nt = $_[0]->node_type;
964 if ($nt == ELEMENT_NODE) {
965 my $el = $_[0];
966 EL: {
967 unless (defined $el->prefix) {
968 my $elns = $el->namespace_uri;
969 if ($namespace_uri ne '' and defined $elns) {
970 return $namespace_uri eq $elns;
971 } else {
972 return not ($namespace_uri eq '' or defined $elns);
973 }
974 }
975 my $xmlns = $el->get_attribute_ns
976 ('http://www.w3.org/2000/xmlns/', 'xmlns');
977 if (defined $xmlns) {
978 if ($namespace_uri ne '') {
979 return ($namespace_uri eq $xmlns);
980 } else {
981 return ($xmlns eq '');
982 }
983 }
984 $el = $el->manakai_parent_element;
985 redo EL if defined $el;
986 return 0;
987 } # EL;
988 } else {
989 my $el = $nt == DOCUMENT_NODE
990 ? $_[0]->document_element
991 : $nt == ATTRIBUTE_NODE
992 ? $_[0]->owner_element
993 : $_[0]->manakai_parent_element;
994 if (defined $el) {
995 return $el->is_default_namespace ($_[1]);
996 } else {
997 return 0;
998 }
999 }
1000 } # is_default_namespace
1001
1002 sub lookup_namespace_uri ($$) {
1003 my ($self, $prefix) = @_;
1004 $prefix = undef if defined $prefix and $prefix eq '';
1005 ## NOTE: Implementation dependent.
1006 ## TODO: Check what Gecko does.
1007 local $Error::Depth = $Error::Depth + 1;
1008 my $nt = $self->node_type;
1009 if ($nt == ELEMENT_NODE) {
1010 my $el = $self;
1011 EL: {
1012 my $elns = $el->namespace_uri;
1013 if (defined $elns) {
1014 my $elpfx = $el->prefix;
1015 if ((not defined $prefix and not defined $elpfx) or
1016 (defined $prefix and defined $elpfx and $prefix eq $elpfx)) {
1017 return $elns;
1018 }
1019 }
1020 AT: for my $attr (@{$el->attributes}) {
1021 my $attrns = $attr->namespace_uri;
1022 next AT if not defined $attrns or
1023 $attrns ne 'http://www.w3.org/2000/xmlns/';
1024 my $attrpfx = $attr->prefix;
1025 if (not defined $prefix) {
1026 my $attrln = $attr->local_name;
1027 if ($attrln eq 'xmlns') {
1028 my $attrval = $attr->value;
1029 return length $attrval ? $attrval : undef;
1030 }
1031 } elsif (defined $prefix and
1032 defined $attrpfx and $attrpfx eq 'xmlns') {
1033 my $attrln = $attr->local_name;
1034 if ($attrln eq $prefix) {
1035 my $attrval = $attr->value;
1036 return length $attrval ? $attrval : undef;
1037 }
1038 }
1039 } # AT
1040 $el = $el->manakai_parent_element;
1041 redo EL if defined $el;
1042 return undef;
1043 } # EL;
1044 } else {
1045 my $el = $nt == DOCUMENT_NODE
1046 ? $self->document_element
1047 : $nt == ATTRIBUTE_NODE
1048 ? $self->owner_element
1049 : $self->manakai_parent_element;
1050 if (defined $el) {
1051 return $el->lookup_namespace_uri ($prefix);
1052 } else {
1053 return undef;
1054 }
1055 }
1056 } # lookup_namespace_uri
1057
1058 sub lookup_prefix ($$) {
1059 my $namespace_uri = defined $_[1] ? $_[1] : '';
1060 if ($namespace_uri eq '') {
1061 return undef;
1062 }
1063
1064 local $Error::Depth = $Error::Depth + 1;
1065 my $nt = $_[0]->node_type;
1066 if ($nt == ELEMENT_NODE) {
1067 my $el = $_[0];
1068 EL: {
1069 my $elns = $el->namespace_uri;
1070 if (defined $elns and $elns eq $namespace_uri) {
1071 my $elpfx = $el->prefix;
1072 if (defined $elpfx) {
1073 my $oeluri = $_[0]->lookup_namespace_uri ($elpfx);
1074 if (defined $oeluri and $oeluri eq $namespace_uri) {
1075 return $elpfx;
1076 }
1077 }
1078 }
1079 AT: for my $attr (@{$el->attributes}) {
1080 my $attrpfx = $attr->prefix;
1081 next AT if not defined $attrpfx or $attrpfx ne 'xmlns';
1082 my $attrns = $attr->namespace_uri;
1083 next AT if not defined $attrns or
1084 $attrns ne 'http://www.w3.org/2000/xmlns/';
1085 next AT unless $attr->value eq $namespace_uri;
1086 my $attrln = $attr->local_name;
1087 my $oeluri = $el->lookup_namespace_uri ($attrln);
1088 next AT unless defined $oeluri;
1089 if ($oeluri eq $namespace_uri) {
1090 return $attrln;
1091 }
1092 }
1093 $el = $el->manakai_parent_element;
1094 redo EL if defined $el;
1095 return undef;
1096 } # EL
1097 } else {
1098 my $el = $nt == DOCUMENT_NODE
1099 ? $_[0]->document_element
1100 : $nt == ATTRIBUTE_NODE
1101 ? $_[0]->owner_element
1102 : $_[0]->manakai_parent_element;
1103 if (defined $el) {
1104 return $el->lookup_prefix ($_[1]);
1105 } else {
1106 return undef;
1107 }
1108 }
1109 } # lookup_prefix
1110
1111 sub normalize ($) {
1112 my $self = shift;
1113 my $ptext;
1114 local $Error::Depth = $Error::Depth + 1;
1115
1116 ## Children
1117 my @remove;
1118 for my $cn (@{$self->child_nodes}) {
1119 if ($cn->node_type == TEXT_NODE) {
1120 my $nv = $cn->node_value;
1121 if (length $nv) {
1122 if (defined $ptext) {
1123 $ptext->manakai_append_text ($nv);
1124 $ptext->is_element_content_whitespace (1)
1125 if $cn->is_element_content_whitespace and
1126 $ptext->is_element_content_whitespace;
1127 push @remove, $cn;
1128 } else {
1129 $ptext = $cn;
1130 }
1131 } else {
1132 push @remove, $cn;
1133 }
1134 } else {
1135 $cn->normalize;
1136 undef $ptext;
1137 }
1138 }
1139 $self->remove_child ($_) for @remove;
1140
1141 my $nt = $self->node_type;
1142 if ($nt == ELEMENT_NODE) {
1143 ## Attributes
1144 $_->normalize for @{$self->attributes};
1145 } elsif ($nt == DOCUMENT_TYPE_NODE) {
1146 ## Element type definitions
1147 $_->normalize for @{$self->element_types};
1148 ## General entities
1149 $_->normalize for @{$self->general_entities};
1150 } elsif ($nt == ELEMENT_TYPE_DEFINITION_NODE) {
1151 ## Attribute definitions
1152 $_->normalize for @{$self->attribute_definitions};
1153 }
1154 ## TODO: normalize-characters
1155
1156 ## TODO: In this implementation, if a modification raises a
1157 ## |NO_MODIFICATION_ALLOWED_ERR|, then any modification before it
1158 ## is not reverted.
1159 } # normalize
1160
1161 sub remove_child ($$) {
1162 my ($self, $old_child) = @_;
1163
1164 if ($$self->{manakai_read_only} and
1165 ${$$self->{owner_document} or $self}->{strict_error_checking}) {
1166 report Message::DOM::DOMException
1167 -object => $self,
1168 -type => 'NO_MODIFICATION_ALLOWED_ERR',
1169 -subtype => 'READ_ONLY_NODE_ERR';
1170 }
1171
1172 my $parent_list = $$self->{child_nodes} || [];
1173 for (0..$#$parent_list) {
1174 if ($parent_list->[$_] eq $old_child) {
1175 splice @$parent_list, $_, 1, ();
1176 delete $$old_child->{parent_node};
1177 return $old_child;
1178 }
1179 }
1180
1181 report Message::DOM::DOMException
1182 -object => $self,
1183 -type => 'NOT_FOUND_ERR',
1184 -subtype => 'NOT_CHILD_ERR';
1185 } # remove_child
1186
1187 sub replace_child ($$) {
1188 ## NOTE: |Element|, |Entity|, |DocumentFragment|, |EntityReference|.
1189 ## NOTE: |Document|, |Attr|, |CharacterData|, |AttributeDefinition|,
1190 ## |Notation|, |ProcessingInstruction|, |ElementTypeDefinition|,
1191 ## and |DocumentType| define their own implementations.
1192 my $self = $_[0];
1193
1194 ## NOTE: Depends on $self->node_type:
1195 my $self_od = $$self->{owner_document};
1196
1197 ## -- Node Type check
1198 my @new_child;
1199 my $new_child_parent;
1200 if ($_[1]->node_type == DOCUMENT_FRAGMENT_NODE) {
1201 push @new_child, @{$_[1]->child_nodes};
1202 $new_child_parent = $_[1];
1203 } else {
1204 @new_child = ($_[1]);
1205 $new_child_parent = $_[1]->parent_node;
1206 }
1207
1208 ## NOTE: Depends on $self->node_type:
1209 if ($$self_od->{strict_error_checking}) {
1210 my $child_od = $_[1]->owner_document || $_[1]; # might be DocumentType
1211 if ($self_od ne $child_od and $child_od->node_type != DOCUMENT_TYPE_NODE) {
1212 report Message::DOM::DOMException
1213 -object => $self,
1214 -type => 'WRONG_DOCUMENT_ERR',
1215 -subtype => 'EXTERNAL_OBJECT_ERR';
1216 }
1217
1218 if ($$self->{manakai_read_only} or
1219 (@new_child and defined $new_child_parent and
1220 $$new_child_parent->{manakai_read_only})) {
1221 report Message::DOM::DOMException
1222 -object => $self,
1223 -type => 'NO_MODIFICATION_ALLOWED_ERR',
1224 -subtype => 'READ_ONLY_NODE_ERR';
1225 }
1226
1227 ## NOTE: |Document| has children order check here.
1228
1229 for my $cn (@new_child) {
1230 unless ({
1231 TEXT_NODE, 1, ENTITY_REFERENCE_NODE, 1,
1232 ELEMENT_NODE, 1, CDATA_SECTION_NODE, 1,
1233 PROCESSING_INSTRUCTION_NODE, 1, COMMENT_NODE, 1,
1234 }->{$cn->node_type}) {
1235 report Message::DOM::DOMException
1236 -object => $self,
1237 -type => 'HIERARCHY_REQUEST_ERR',
1238 -subtype => 'CHILD_NODE_TYPE_ERR';
1239 }
1240 }
1241
1242 my $anode = $self;
1243 while (defined $anode) {
1244 if ($anode eq $_[1]) {
1245 report Message::DOM::DOMException
1246 -object => $self,
1247 -type => 'HIERARCHY_REQUEST_ERR',
1248 -subtype => 'ANCESTOR_NODE_ERR';
1249 }
1250 $anode = $$anode->{parent_node};
1251 }
1252 }
1253
1254 ## -- Insert at... ## NOTE: Only in insertBefore and replaceChild
1255 my $index = -1; # last
1256 if (defined $_[2]) {
1257 ## error if $_[1] eq $_[2];
1258
1259 my $cns = $self->child_nodes;
1260 my $cnsl = @$cns;
1261 C: {
1262 $index = 0;
1263 for my $i (0..($cnsl-1)) {
1264 my $cn = $cns->[$i];
1265 if ($cn eq $_[2]) {
1266 $index += $i;
1267 last C;
1268 } elsif ($cn eq $_[1]) {
1269 $index = -1; # offset
1270 }
1271 }
1272
1273 report Message::DOM::DOMException
1274 -object => $self,
1275 -type => 'NOT_FOUND_ERR',
1276 -subtype => 'NOT_CHILD_ERR';
1277 } # C
1278 } else {
1279 ## NOTE: Only in replaceChild
1280 report Message::DOM::DOMException
1281 -object => $self,
1282 -type => 'NOT_FOUND_ERR',
1283 -subtype => 'NOT_CHILD_ERR';
1284 }
1285
1286 ## -- Removes from parent
1287 if ($new_child_parent) {
1288 if (@new_child == 1) {
1289 my $v = $$new_child_parent->{child_nodes};
1290 RP: for my $i (0..$#$v) {
1291 if ($v->[$i] eq $new_child[0]) {
1292 splice @$v, $i, 1, ();
1293 last RP;
1294 }
1295 } # RP
1296 } else {
1297 @{$$new_child_parent->{child_nodes}} = ();
1298 }
1299 }
1300
1301 ## -- Rewrite the |parentNode| properties
1302 for my $nc (@new_child) {
1303 $$nc->{parent_node} = $self;
1304 Scalar::Util::weaken ($$nc->{parent_node});
1305 }
1306
1307 ## NOTE: Depends on method:
1308 splice @{$$self->{child_nodes}}, $index, 1, @new_child;
1309 delete ${$_[2]}->{parent_node};
1310
1311 ## NOTE: Setting |owner_document| in |Document|.
1312
1313 return $_[2];
1314 } # replace_child
1315
1316 sub manakai_set_read_only ($;$$) {
1317 my $value = 1 if $_[1];
1318 if ($_[2]) {
1319 my @target = ($_[0]);
1320 while (@target) {
1321 my $target = shift @target;
1322 if ($value) {
1323 $$target->{manakai_read_only} = 1;
1324 } else {
1325 delete $$target->{manakai_read_only};
1326 }
1327 push @target, @{$target->child_nodes};
1328
1329 my $nt = $target->node_type;
1330 if ($nt == ELEMENT_NODE) {
1331 push @target, @{$target->attributes};
1332 } elsif ($nt == ELEMENT_TYPE_DEFINITION_NODE) {
1333 push @target, @{$target->attribute_definitions};
1334 } elsif ($nt == DOCUMENT_TYPE_NODE) {
1335 push @target, @{$target->element_types};
1336 push @target, @{$target->general_entities};
1337 push @target, @{$target->notations};
1338 }
1339 }
1340 } else { # not deep
1341 if ($value) {
1342 ${$_[0]}->{manakai_read_only} = 1;
1343 } else {
1344 delete ${$_[0]}->{manakai_read_only};
1345 }
1346 }
1347 } # manakai_set_read_only
1348
1349 # {NOTE:: Perl application developers are advised to be careful
1350 # to include direct or indirect references to the node
1351 # itself as user data or in user data handlers.
1352 # They would result in memory leak problems unless
1353 # the circular references are removed later.
1354 #
1355 # It would be a good practive to eusure that every user data
1356 # registered to a node is later unregistered by setting
1357 # <DOM::null> as a data for the same key.
1358 #
1359 sub set_user_data ($$;$$) {
1360 my ($self, $key, $data, $handler) = @_;
1361
1362 my $v = ($$self->{user_data} ||= {});
1363 my $r = $v->{$key}->[0];
1364
1365 if (defined $data) {
1366 $v->{$key} = [$data, $handler];
1367
1368 if (defined $handler) {
1369 eval q{
1370 no warnings;
1371 sub DESTROY {
1372 my $uds = ${$_[0]}->{user_data};
1373 for my $key (keys %$uds) {
1374 if (defined $uds->{$key}->[1]) {
1375 local $Error::Depth = $Error::Depth + 1;
1376 $uds->{$key}->[1]->(3, $key, $uds->{$key}->[0]); # NODE_DELETED
1377 }
1378 }
1379 }
1380 };
1381 }
1382 } else {
1383 delete $v->{$key};
1384 }
1385 return $r;
1386 } # set_user_data
1387
1388 package Message::IF::Node;
1389 package Message::IF::NSResolver;
1390
1391 =head1 LICENSE
1392
1393 Copyright 2007 Wakaba <w@suika.fam.cx>
1394
1395 This program is free software; you can redistribute it and/or
1396 modify it under the same terms as Perl itself.
1397
1398 =cut
1399
1400 1;
1401 ## $Date: 2007/10/08 07:17:18 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24