0
|
1 package IMPL::DOM::Node;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
11
|
5 use base qw(IMPL::Object);
|
0
|
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 BEGIN {
|
4
|
15 public _direct property nodeName => prop_get | owner_set;
|
|
16 public _direct property isComplex => { get => \&_getIsComplex } ;
|
|
17 public _direct property nodeValue => prop_all;
|
|
18 public _direct property childNodes => { get => \&_getChildNodes };
|
|
19 public _direct property parentNode => prop_get ;
|
6
|
20 private _direct property _propertyMap => prop_get ;
|
0
|
21 }
|
|
22
|
|
23 sub CTOR {
|
11
|
24 my ($this,%args) = @_;
|
0
|
25
|
11
|
26 $this->nodeName($args{nodeName}) or die new IMPL::InvalidArgumentException("A name is required");
|
0
|
27 }
|
|
28
|
|
29 sub insertNode {
|
|
30 my ($this,$node,$pos) = @_;
|
4
|
31
|
|
32 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
33
|
|
34 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
35
|
|
36 $this->childNodes->InsertAt($pos,$node);
|
|
37
|
|
38 $node->_setParent( $this );
|
|
39
|
|
40 return $node;
|
|
41 }
|
|
42
|
14
|
43 sub appendNode {
|
|
44 my ($this,$node,$pos) = @_;
|
|
45
|
|
46 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
47
|
|
48 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
49
|
|
50 $this->childNodes->Append($node);
|
|
51
|
|
52 $node->_setParent( $this );
|
|
53
|
|
54 return $node;
|
|
55 }
|
|
56
|
4
|
57 sub _getChildNodes {
|
|
58 my ($this) = @_;
|
|
59
|
|
60 $this->{$childNodes} = new IMPL::Object::List() unless $this->{$childNodes};
|
|
61 $this->{$childNodes};
|
0
|
62 }
|
|
63
|
|
64 sub removeNode {
|
|
65 my ($this,$node) = @_;
|
4
|
66
|
|
67 if ($this == $node->{$parentNode}) {
|
|
68 $this->childNodes->RemoveItem($node);
|
|
69 $node->{$parentNode} = undef;
|
|
70 return $this;
|
|
71 } else {
|
|
72 die new IMPL::InvalidOperationException("The specified node isn't belong to this node");
|
|
73 }
|
0
|
74 }
|
|
75
|
7
|
76 sub replaceNodeAt {
|
|
77 my ($this,$index,$node) = @_;
|
|
78
|
|
79 my $nodeOld = $this->childNodes->[$index];
|
|
80
|
|
81 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
82
|
|
83 # unlink node from previous parent
|
|
84 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
85
|
|
86 # replace (or set) old node
|
|
87 $this->childNodes->[$index] = $node;
|
|
88
|
|
89 # save new parent
|
|
90 $node->_setParent( $this );
|
|
91
|
|
92 # unlink old node if we have one
|
|
93 $nodeOld->{$parentNode} = undef if $nodeOld;
|
|
94
|
|
95 # return old node
|
|
96 return $nodeOld;
|
|
97 }
|
|
98
|
0
|
99 sub removeAt {
|
|
100 my ($this,$pos) = @_;
|
4
|
101
|
|
102 if ( my $node = $this->childNodes->RemoveAt($pos) ) {
|
|
103 $node->{$parentNode} = undef;
|
|
104 return $node;
|
|
105 } else {
|
|
106 return undef;
|
|
107 }
|
0
|
108 }
|
|
109
|
|
110 sub selectNodes {
|
14
|
111 my ($this,$query) = @_;
|
|
112 my @result;
|
4
|
113
|
14
|
114 if (ref $query eq 'CODE') {
|
|
115 @result = grep &$query($_), @{$this->childNodes};
|
|
116 } else {
|
|
117 @result = grep $_->nodeName eq $query, @{$this->childNodes};
|
|
118 }
|
4
|
119
|
|
120 return wantarray ? @result : \@result;
|
0
|
121 }
|
|
122
|
7
|
123 sub firstChild {
|
|
124 @_ >=2 ? $_[0]->replaceNodeAt(0,$_[1]) : $_[0]->childNodes->[0];
|
|
125 }
|
|
126
|
4
|
127 sub _getIsComplex {
|
|
128 $_[0]->childNodes->Count ? 1 : 0;
|
|
129 }
|
|
130
|
|
131 sub _setParent {
|
11
|
132 my ($this,$node) = @_;
|
4
|
133
|
11
|
134 $this->{$parentNode} = $node;
|
14
|
135 # prevent from creating cyclicreferences
|
4
|
136 weaken($this->{$parentNode});
|
0
|
137 }
|
|
138
|
|
139 sub text {
|
|
140 my ($this) = @_;
|
4
|
141
|
|
142 join '', $this->nodeValue, map $_->nodeValue, @{$this->childNodes};
|
0
|
143 }
|
|
144
|
|
145 sub Property {
|
|
146 my $this = shift;
|
|
147 my $name = shift;
|
|
148
|
|
149 if (@_) {
|
|
150 # set
|
6
|
151 return $this->{$_propertyMap}{$name} = shift;
|
0
|
152 } else {
|
6
|
153 return $this->{$_propertyMap}{$name};
|
0
|
154 }
|
|
155 }
|
|
156
|
|
157 1;
|