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 ;
|
6
|
22 private _direct property _propertyMap => prop_get ;
|
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 }
|
|
30
|
|
31 sub insertNode {
|
|
32 my ($this,$node,$pos) = @_;
|
4
|
33
|
|
34 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
35
|
|
36 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
37
|
|
38 $this->childNodes->InsertAt($pos,$node);
|
|
39
|
|
40 $node->_setParent( $this );
|
|
41
|
|
42 return $node;
|
|
43 }
|
|
44
|
|
45 sub _getChildNodes {
|
|
46 my ($this) = @_;
|
|
47
|
|
48 $this->{$childNodes} = new IMPL::Object::List() unless $this->{$childNodes};
|
|
49 $this->{$childNodes};
|
0
|
50 }
|
|
51
|
|
52 sub removeNode {
|
|
53 my ($this,$node) = @_;
|
4
|
54
|
|
55 if ($this == $node->{$parentNode}) {
|
|
56 $this->childNodes->RemoveItem($node);
|
|
57 $node->{$parentNode} = undef;
|
|
58 return $this;
|
|
59 } else {
|
|
60 die new IMPL::InvalidOperationException("The specified node isn't belong to this node");
|
|
61 }
|
0
|
62 }
|
|
63
|
7
|
64 sub replaceNodeAt {
|
|
65 my ($this,$index,$node) = @_;
|
|
66
|
|
67 my $nodeOld = $this->childNodes->[$index];
|
|
68
|
|
69 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
70
|
|
71 # unlink node from previous parent
|
|
72 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
73
|
|
74 # replace (or set) old node
|
|
75 $this->childNodes->[$index] = $node;
|
|
76
|
|
77 # save new parent
|
|
78 $node->_setParent( $this );
|
|
79
|
|
80 # unlink old node if we have one
|
|
81 $nodeOld->{$parentNode} = undef if $nodeOld;
|
|
82
|
|
83 # return old node
|
|
84 return $nodeOld;
|
|
85 }
|
|
86
|
0
|
87 sub removeAt {
|
|
88 my ($this,$pos) = @_;
|
4
|
89
|
|
90 if ( my $node = $this->childNodes->RemoveAt($pos) ) {
|
|
91 $node->{$parentNode} = undef;
|
|
92 return $node;
|
|
93 } else {
|
|
94 return undef;
|
|
95 }
|
0
|
96 }
|
|
97
|
|
98 sub selectNodes {
|
|
99 my ($this,$name) = @_;
|
4
|
100
|
|
101 my @result = grep $_->nodeName eq $name, @{$this->childNodes};
|
|
102
|
|
103 return wantarray ? @result : \@result;
|
0
|
104 }
|
|
105
|
7
|
106 sub firstChild {
|
|
107 @_ >=2 ? $_[0]->replaceNodeAt(0,$_[1]) : $_[0]->childNodes->[0];
|
|
108 }
|
|
109
|
4
|
110 sub _getIsComplex {
|
|
111 $_[0]->childNodes->Count ? 1 : 0;
|
|
112 }
|
|
113
|
|
114 sub _setParent {
|
0
|
115 my ($this,$parentNode) = @_;
|
4
|
116
|
|
117 $this->{$parentNode} = $parentNode;
|
|
118 weaken($this->{$parentNode});
|
0
|
119 }
|
|
120
|
|
121 sub text {
|
|
122 my ($this) = @_;
|
4
|
123
|
|
124 join '', $this->nodeValue, map $_->nodeValue, @{$this->childNodes};
|
0
|
125 }
|
|
126
|
|
127 sub Property {
|
|
128 my $this = shift;
|
|
129 my $name = shift;
|
|
130
|
|
131 if (@_) {
|
|
132 # set
|
6
|
133 return $this->{$_propertyMap}{$name} = shift;
|
0
|
134 } else {
|
6
|
135 return $this->{$_propertyMap}{$name};
|
0
|
136 }
|
|
137 }
|
|
138
|
|
139 1;
|