49
|
1 package IMPL::DOM::Node;
|
|
2 use strict;
|
|
3 use warnings;
|
|
4
|
|
5 use base qw(IMPL::Object);
|
|
6
|
|
7 use IMPL::Object::List;
|
|
8 use IMPL::Class::Property;
|
|
9 use IMPL::Class::Property::Direct;
|
|
10 use Scalar::Util qw(weaken);
|
|
11
|
|
12 use IMPL::Exception;
|
|
13
|
|
14 BEGIN {
|
|
15 public _direct property nodeName => prop_get;
|
|
16 public _direct property document => prop_get;
|
|
17 public _direct property isComplex => { get => \&_getIsComplex } ;
|
|
18 public _direct property nodeValue => prop_all;
|
102
|
19 public _direct property childNodes => { get => \&_getChildNodes }; # prop_list
|
49
|
20 public _direct property parentNode => prop_get ;
|
|
21 private _direct property _propertyMap => prop_all ;
|
|
22 }
|
|
23
|
|
24 sub CTOR {
|
|
25 my ($this,%args) = @_;
|
|
26
|
|
27 $this->{$nodeName} = delete $args{nodeName} or die new IMPL::InvalidArgumentException("A name is required");
|
|
28 $this->{$nodeValue} = delete $args{nodeValue} if exists $args{nodeValue};
|
|
29 if ( exists $args{document} ) {
|
|
30 $this->{$document} = delete $args{document};
|
|
31 weaken($this->{$document});
|
|
32 }
|
|
33
|
|
34 $this->{$_propertyMap} = \%args;
|
|
35 }
|
|
36
|
|
37 sub insertNode {
|
|
38 my ($this,$node,$pos) = @_;
|
|
39
|
|
40 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
41
|
|
42 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
43
|
|
44 $this->childNodes->InsertAt($pos,$node);
|
|
45
|
|
46 $node->_setParent( $this );
|
|
47
|
|
48 return $node;
|
|
49 }
|
|
50
|
|
51 sub appendChild {
|
|
52 my ($this,$node) = @_;
|
|
53
|
|
54 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
55
|
|
56 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
57
|
|
58 my $children = $this->childNodes;
|
|
59 $children->Append($node);
|
|
60
|
|
61 $node->_setParent( $this );
|
|
62
|
|
63 return $node;
|
|
64 }
|
|
65
|
|
66 sub appendNode {
|
|
67 goto &appendChild;
|
|
68 }
|
|
69
|
|
70 sub appendRange {
|
|
71 my ($this,@range) = @_;
|
|
72
|
|
73 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if grep $_ == $this, @range;
|
|
74
|
|
75 foreach my $node (@range) {
|
|
76 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
77 $node->_setParent( $this );
|
|
78 }
|
|
79
|
|
80 $this->childNodes->Append(@range);
|
|
81
|
|
82 return $this;
|
|
83 }
|
|
84
|
|
85 sub _getChildNodes {
|
|
86 my ($this) = @_;
|
|
87
|
|
88 $this->{$childNodes} = new IMPL::Object::List() unless $this->{$childNodes};
|
102
|
89 return wantarray ? @{ $this->{$childNodes} } : $this->{$childNodes};
|
49
|
90 }
|
|
91
|
|
92 sub removeNode {
|
|
93 my ($this,$node) = @_;
|
|
94
|
|
95 if ($this == $node->{$parentNode}) {
|
|
96 $this->childNodes->RemoveItem($node);
|
|
97 $node->_setParent(undef);
|
|
98 return $node;
|
|
99 } else {
|
|
100 die new IMPL::InvalidOperationException("The specified node isn't belong to this node");
|
|
101 }
|
|
102 }
|
|
103
|
|
104 sub replaceNodeAt {
|
|
105 my ($this,$index,$node) = @_;
|
|
106
|
|
107 my $nodeOld = $this->childNodes->[$index];
|
|
108
|
|
109 die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
|
|
110
|
|
111 # unlink node from previous parent
|
|
112 $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
|
|
113
|
|
114 # replace (or set) old node
|
|
115 $this->childNodes->[$index] = $node;
|
|
116
|
|
117 # set new parent
|
|
118 $node->_setParent( $this );
|
|
119
|
|
120 # unlink old node if we have one
|
|
121 $nodeOld->_setParent(undef) if $nodeOld;
|
|
122
|
|
123 # return old node
|
|
124 return $nodeOld;
|
|
125 }
|
|
126
|
|
127 sub removeAt {
|
|
128 my ($this,$pos) = @_;
|
|
129
|
|
130 if ( my $node = $this->childNodes->RemoveAt($pos) ) {
|
|
131 $node->_setParent(undef);
|
|
132 return $node;
|
|
133 } else {
|
|
134 return undef;
|
|
135 }
|
|
136 }
|
|
137
|
|
138 sub removeLast {
|
|
139 my ($this) = @_;
|
|
140
|
|
141 if ( my $node = $this->{$childNodes} ? $this->{$childNodes}->RemoveLast() : undef) {
|
|
142 $node->_setParent(undef);
|
|
143 return $node;
|
|
144 } else {
|
|
145 return undef;
|
|
146 }
|
|
147 }
|
|
148
|
|
149 sub removeSelected {
|
|
150 my ($this,$query) = @_;
|
|
151
|
|
152 my @newSet;
|
|
153 my @result;
|
|
154
|
|
155 if (ref $query eq 'CODE') {
|
|
156 &$query($_) ? push @result, $_ : push @newSet, $_ foreach @{$this->childNodes};
|
|
157 } elsif (defined $query) {
|
|
158 $_->nodeName eq $query ? push @result, $_ : push @newSet, $_ foreach @{$this->childNodes};
|
|
159 } else {
|
|
160 my $children = $this->childNodes;
|
|
161 $_->_setParent(undef) foreach @$children;
|
|
162 delete $this->{$childNodes};
|
|
163 return wantarray ? @$children : $children;
|
|
164 }
|
|
165
|
|
166 $_->_setParent(undef) foreach @result;
|
|
167
|
|
168 $this->{$childNodes} = @newSet ? bless \@newSet ,'IMPL::Object::List' : undef;
|
|
169
|
|
170 return wantarray ? @result : \@result;
|
|
171 }
|
|
172
|
|
173 sub selectNodes {
|
|
174 my ($this,$query) = @_;
|
|
175
|
|
176 my @result;
|
|
177
|
|
178 if (ref $query eq 'CODE') {
|
|
179 @result = grep &$query($_), @{$this->childNodes};
|
|
180 } elsif (ref $query eq 'ARRAY' ) {
|
|
181 my %keys = map (($_,1),@$query);
|
|
182 @result = grep $keys{$_->nodeName}, @{$this->childNodes};
|
|
183 } elsif (defined $query) {
|
|
184 @result = grep $_->nodeName eq $query, @{$this->childNodes};
|
|
185 } else {
|
|
186 if (wantarray) {
|
|
187 return @{$this->childNodes};
|
|
188 } else {
|
|
189 @result = $this->childNodes;
|
|
190 return \@result;
|
|
191 }
|
|
192 }
|
|
193
|
|
194 return wantarray ? @result : \@result;
|
|
195 }
|
|
196
|
|
197 sub firstChild {
|
|
198 @_ >=2 ? $_[0]->replaceNodeAt(0,$_[1]) : $_[0]->childNodes->[0];
|
|
199 }
|
|
200
|
|
201 sub _getIsComplex {
|
|
202 $_[0]->childNodes->Count ? 1 : 0;
|
|
203 }
|
|
204
|
|
205 sub _updateDocRefs {
|
|
206 my ($this) = @_;
|
|
207
|
|
208 # this method is called by the parent node on his children, so we need no to check parent
|
|
209 $this->{$document} = $this->{$parentNode}->document;
|
|
210
|
|
211 # prevent cyclic
|
|
212 weaken($this->{$document}) if $this->{$document};
|
|
213
|
|
214 $_->_updateDocRefs foreach @{$this->{$childNodes}};
|
|
215 }
|
|
216
|
|
217 sub _setParent {
|
|
218 my ($this,$node) = @_;
|
|
219
|
|
220
|
|
221 if (($node || 0) != ($this->{$parentNode} || 0)) {
|
|
222 my $newOwner;
|
|
223 if ($node) {
|
|
224 $this->{$parentNode} = $node;
|
|
225 $newOwner = $node->document || 0;
|
|
226
|
|
227 # prevent from creating cyclicreferences
|
|
228 weaken($this->{$parentNode});
|
|
229
|
|
230 } else {
|
|
231 delete $this->{$parentNode};
|
75
|
232
|
|
233 #keep document
|
|
234 $newOwner = $this->{$document};
|
49
|
235 }
|
|
236
|
|
237 if (($this->{$document}||0) != $newOwner) {
|
|
238 $this->{$document} = $newOwner;
|
|
239 weaken($this->{$document}) if $newOwner;
|
|
240 $_->_updateDocRefs foreach @{$this->childNodes};
|
|
241 }
|
|
242 }
|
|
243 }
|
|
244
|
|
245 sub text {
|
|
246 my ($this) = @_;
|
|
247
|
|
248 join ('', $this->nodeValue || '', map ($_->text || '', @{$this->childNodes}));
|
|
249 }
|
|
250
|
|
251 sub nodeProperty {
|
|
252 my $this = shift;
|
|
253 my $name = shift;
|
|
254
|
|
255 if (@_) {
|
|
256 # set
|
|
257 return $this->{$_propertyMap}{$name} = shift;
|
|
258 } else {
|
|
259 return $this->{$_propertyMap}{$name};
|
|
260 }
|
|
261 }
|
|
262
|
|
263 sub qname {
|
|
264 $_[0]->{$nodeName};
|
|
265 }
|
|
266
|
|
267 sub path {
|
|
268 my ($this) = @_;
|
|
269
|
|
270 if ($this->parentNode) {
|
|
271 return $this->parentNode->path.'.'.$this->qname;
|
|
272 } else {
|
|
273 return $this->qname;
|
|
274 }
|
|
275 }
|
|
276
|
|
277 1;
|
75
|
278
|
|
279 __END__
|
|
280
|
|
281 =pod
|
|
282
|
|
283 =head1 NAME
|
|
284
|
|
285 C<IMPL::DOM::Node> Элемент DOM модели
|
|
286
|
|
287 =head1 DESCRIPTION
|
|
288
|
|
289 Базовый узел DOM модели. От него можно наследовать другие элементы DOM модели.
|
|
290
|
|
291 =head1 MEMBERS
|
|
292
|
|
293 =head2 PROPERTIES
|
|
294
|
|
295 =over
|
|
296
|
|
297 =item C<[get] nodeName>
|
|
298
|
|
299 Имя узла. Задается при создании.
|
|
300
|
|
301 =item C<[get] document>
|
|
302
|
|
303 Документ к которому принадлежит узел. Задается при поздании узла.
|
|
304
|
|
305 =item C<[get] isComplex>
|
|
306
|
|
307 Определяет является ли узел сложным (тоесть есть ли дети).
|
|
308
|
|
309 C<true> - есть, C<false> - нет.
|
|
310
|
|
311 =item C<[get,set] nodeValue>
|
|
312
|
|
313 Значение узла, обычно простой скаляр, но ничто не мешает туда
|
|
314 устанавливать любое значение.
|
|
315
|
|
316 =item C<[get,list] childNodes>
|
|
317
|
|
318 Список детей, является списокм C<IMPL::Object::List>.
|
|
319
|
|
320 =item C<[get] parentNode>
|
|
321
|
|
322 Ссылка на родительский элемент, если таковой имеется.
|
|
323
|
|
324 =head2 METHODS
|
|
325
|
|
326 =cut |