diff Lib/IMPL/DOM/Node.pm @ 4:e59f44f75f20

DOM - в разработке Testing - по мелочи Property - изменен механизм выбора имплементора
author Sergey
date Wed, 12 Aug 2009 17:36:07 +0400
parents 3b418b134d8c
children e2cd73ccc5bd
line wrap: on
line diff
--- a/Lib/IMPL/DOM/Node.pm	Tue Aug 11 17:45:52 2009 +0400
+++ b/Lib/IMPL/DOM/Node.pm	Wed Aug 12 17:36:07 2009 +0400
@@ -4,6 +4,7 @@
 
 use base qw(IMPL::Object IMPL::Object::Serializable IMPL::Object::Autofill);
 
+use IMPL::Object::List;
 use IMPL::Class::Property;
 use IMPL::Class::Property::Direct;
 use Scalar::Util qw(weaken);
@@ -13,12 +14,12 @@
 __PACKAGE__->PassThroughArgs;
 
 BEGIN {
-    public property nodeName => prop_get | owner_set;
-    public property isComplex => prop_get | owner_set;
-    public property nodeValue => prop_get | owner_set;
-    public property childNodes => prop_get | owner_set| prop_list;
-    public property parentNode => prop_get | owner_set;
-    private property _propertyMap => prop_all;
+    public _direct property nodeName => prop_get | owner_set;
+    public _direct property isComplex => { get => \&_getIsComplex } ;
+    public _direct property nodeValue => prop_all;
+    public _direct property childNodes => { get => \&_getChildNodes };
+    public _direct property parentNode => prop_get ;
+    private _direct property _propertyMap => prop_all;
 }
 
 sub CTOR {
@@ -30,26 +31,71 @@
 
 sub insertNode {
     my ($this,$node,$pos) = @_;
+    
+    die new IMPL::InvalidOperationException("You can't insert the node to itselft") if $this == $node;
+    
+    $node->{$parentNode}->removeNode($node) if ($node->{$parentNode});
+    
+    $this->childNodes->InsertAt($pos,$node);
+    
+    $node->_setParent( $this );
+    
+    return $node;
+}
+
+sub _getChildNodes {
+    my ($this) = @_;
+    
+    $this->{$childNodes} = new IMPL::Object::List() unless $this->{$childNodes};
+    $this->{$childNodes};
 }
 
 sub removeNode {
     my ($this,$node) = @_;
+    
+    if ($this == $node->{$parentNode}) {
+        $this->childNodes->RemoveItem($node);
+        $node->{$parentNode} = undef;
+        return $this;
+    } else {
+        die new IMPL::InvalidOperationException("The specified node isn't belong to this node");
+    }
 }
 
 sub removeAt {
     my ($this,$pos) = @_;
+    
+    if ( my $node = $this->childNodes->RemoveAt($pos) ) {
+        $node->{$parentNode} = undef;
+        return $node;
+    } else {
+        return undef;
+    }
 }
 
 sub selectNodes {
     my ($this,$name) = @_;
+    
+    my @result = grep $_->nodeName eq $name, @{$this->childNodes};
+    
+    return wantarray ? @result : \@result;
 }
 
-sub setParent {
+sub _getIsComplex {
+    $_[0]->childNodes->Count ? 1 : 0;
+}
+
+sub _setParent {
     my ($this,$parentNode) = @_;
+    
+    $this->{$parentNode} = $parentNode;
+    weaken($this->{$parentNode});
 }
 
 sub text {
     my ($this) = @_;
+    
+    join '', $this->nodeValue, map $_->nodeValue, @{$this->childNodes};
 }
 
 sub Property {