0
|
1 package IMPL::DOM::Node;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
|
5 use base qw(IMPL::Object IMPL::Object::Serializable IMPL::Object::Autofill);
|
|
6
|
4
|
7 use IMPL::Object::List;
|
0
|
8 use IMPL::Class::Property;
|
|
9 use IMPL::Class::Property::Direct;
|
|
10 use Scalar::Util qw(weaken);
|
|
11
|
1
|
12 use IMPL::Exception;
|
|
13
|
0
|
14 __PACKAGE__->PassThroughArgs;
|
|
15
|
|
16 BEGIN {
|
4
|
17 public _direct property nodeName => prop_get | owner_set;
|
|
18 public _direct property isComplex => { get => \&_getIsComplex } ;
|
|
19 public _direct property nodeValue => prop_all;
|
|
20 public _direct property childNodes => { get => \&_getChildNodes };
|
|
21 public _direct property parentNode => prop_get ;
|
|
22 private _direct property _propertyMap => prop_all;
|
0
|
23 }
|
|
24
|
|
25 sub CTOR {
|
1
|
26 my ($this,$name) = @_;
|
0
|
27
|
1
|
28 $this->nodeName($name) or die new IMPL::InvalidArgumentException("A name is required");
|
0
|
29 $this->_propertyMap({});
|
|
30 }
|
|
31
|
|
32 sub insertNode {
|
|
33 my ($this,$node,$pos) = @_;
|
4
|
34
|
|
35 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
36
|
|
37 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
38
|
|
39 $this->childNodes->InsertAt($pos,$node);
|
|
40
|
|
41 $node->_setParent( $this );
|
|
42
|
|
43 return $node;
|
|
44 }
|
|
45
|
|
46 sub _getChildNodes {
|
|
47 my ($this) = @_;
|
|
48
|
|
49 $this->{$childNodes} = new IMPL::Object::List() unless $this->{$childNodes};
|
|
50 $this->{$childNodes};
|
0
|
51 }
|
|
52
|
|
53 sub removeNode {
|
|
54 my ($this,$node) = @_;
|
4
|
55
|
|
56 if ($this == $node->{$parentNode}) {
|
|
57 $this->childNodes->RemoveItem($node);
|
|
58 $node->{$parentNode} = undef;
|
|
59 return $this;
|
|
60 } else {
|
|
61 die new IMPL::InvalidOperationException("The specified node isn't belong to this node");
|
|
62 }
|
0
|
63 }
|
|
64
|
|
65 sub removeAt {
|
|
66 my ($this,$pos) = @_;
|
4
|
67
|
|
68 if ( my $node = $this->childNodes->RemoveAt($pos) ) {
|
|
69 $node->{$parentNode} = undef;
|
|
70 return $node;
|
|
71 } else {
|
|
72 return undef;
|
|
73 }
|
0
|
74 }
|
|
75
|
|
76 sub selectNodes {
|
|
77 my ($this,$name) = @_;
|
4
|
78
|
|
79 my @result = grep $_->nodeName eq $name, @{$this->childNodes};
|
|
80
|
|
81 return wantarray ? @result : \@result;
|
0
|
82 }
|
|
83
|
4
|
84 sub _getIsComplex {
|
|
85 $_[0]->childNodes->Count ? 1 : 0;
|
|
86 }
|
|
87
|
|
88 sub _setParent {
|
0
|
89 my ($this,$parentNode) = @_;
|
4
|
90
|
|
91 $this->{$parentNode} = $parentNode;
|
|
92 weaken($this->{$parentNode});
|
0
|
93 }
|
|
94
|
|
95 sub text {
|
|
96 my ($this) = @_;
|
4
|
97
|
|
98 join '', $this->nodeValue, map $_->nodeValue, @{$this->childNodes};
|
0
|
99 }
|
|
100
|
|
101 sub Property {
|
|
102 my $this = shift;
|
|
103 my $name = shift;
|
|
104
|
|
105 if (@_) {
|
|
106 # set
|
|
107 return $this->_propertyMap->{$name} = shift;
|
|
108 } else {
|
|
109 return $this->_propertyMap->{$name};
|
|
110 }
|
|
111 }
|
|
112
|
|
113 1;
|