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
|
|
64 sub removeAt {
|
|
65 my ($this,$pos) = @_;
|
4
|
66
|
|
67 if ( my $node = $this->childNodes->RemoveAt($pos) ) {
|
|
68 $node->{$parentNode} = undef;
|
|
69 return $node;
|
|
70 } else {
|
|
71 return undef;
|
|
72 }
|
0
|
73 }
|
|
74
|
|
75 sub selectNodes {
|
|
76 my ($this,$name) = @_;
|
4
|
77
|
|
78 my @result = grep $_->nodeName eq $name, @{$this->childNodes};
|
|
79
|
|
80 return wantarray ? @result : \@result;
|
0
|
81 }
|
|
82
|
4
|
83 sub _getIsComplex {
|
|
84 $_[0]->childNodes->Count ? 1 : 0;
|
|
85 }
|
|
86
|
|
87 sub _setParent {
|
0
|
88 my ($this,$parentNode) = @_;
|
4
|
89
|
|
90 $this->{$parentNode} = $parentNode;
|
|
91 weaken($this->{$parentNode});
|
0
|
92 }
|
|
93
|
|
94 sub text {
|
|
95 my ($this) = @_;
|
4
|
96
|
|
97 join '', $this->nodeValue, map $_->nodeValue, @{$this->childNodes};
|
0
|
98 }
|
|
99
|
|
100 sub Property {
|
|
101 my $this = shift;
|
|
102 my $name = shift;
|
|
103
|
|
104 if (@_) {
|
|
105 # set
|
6
|
106 return $this->{$_propertyMap}{$name} = shift;
|
0
|
107 } else {
|
6
|
108 return $this->{$_propertyMap}{$name};
|
0
|
109 }
|
|
110 }
|
|
111
|
|
112 1;
|