diff Lib/IMPL/DOM/Navigator.pm @ 36:1828103371d0

DOM in works
author Sergey
date Fri, 20 Nov 2009 16:48:08 +0300
parents a8086f85a571
children 16ada169ca75
line wrap: on
line diff
--- a/Lib/IMPL/DOM/Navigator.pm	Tue Nov 17 17:46:24 2009 +0300
+++ b/Lib/IMPL/DOM/Navigator.pm	Fri Nov 20 16:48:08 2009 +0300
@@ -20,6 +20,16 @@
     $this->{$_state} = { alternatives => [ $CurrentNode ], current => 0 };
 }
 
+sub _initNavigator {
+    my ($this,$CurrentNode) = @_;
+    
+    die IMPL::InvalidArgumentException("A starting node is a required paramater") unless $CurrentNode;
+    
+    $this->{$_state} = { alternatives => [ $CurrentNode ], current => 0 };
+    delete $this->{$_path};
+    delete $this->{$_savedstates};
+}
+
 sub _getCurrent {
     $_[0]->{$_state}{alternatives}[$_[0]->{$_state}{current}]
 }
@@ -29,14 +39,16 @@
     
     return unless @path;
     
+    my $node;
+    
     foreach my $query (@path) {
         if (my $current = $this->Current) {
             
-            my @alternatives = $this->Current->selectNodes($query);
+            my @alternatives = $current->selectNodes($query);
             
             unless (@alternatives) {
-                $this->advanceNavigator or return undef;
-                @alternatives = $this->Current->selectNodes($query);
+                $current = $this->advanceNavigator or return undef;
+                @alternatives = $current->selectNodes($query);
             }
             
             push @{$this->{$_path}},$this->{$_state};
@@ -44,13 +56,15 @@
                 alternatives => \@alternatives,
                 current => 0,
                 query => $query
-            }
+            };
+            
+            $node = $alternatives[0];
         } else {
             return undef;
         }
     }
     
-    return $this->Current;
+    $node;
 }
 
 sub selectNodes {
@@ -80,7 +94,7 @@
         current => 0
     };
     
-    return $this->Current;
+    $nodeSet[0];
 }
 
 sub fetch {
@@ -100,16 +114,16 @@
         if ( exists $this->{$_state}{query} ) {
             my $query = $this->{$_state}{query};
   
-            $this->Back or return 0; # that meams the end of the history
+            $this->Back or return undef; # that meams the end of the history
 
             undef while ( $this->advanceNavigator and not $this->Navigate($query));
 
-            return $this->Current ? 1 : 0;
+            return $this->Current;
         }
-        return 0;
+        return undef;
     }
     
-    return 1;
+    return $this->Current;
 }
 
 sub doeach {
@@ -121,22 +135,23 @@
             $_ = $this->{$_state}{alternatives}[$i];
             $code->();
         }
-         $this->{$_state}{current} = @{$this->{$_state}{alternatives}};
+        $this->{$_state}{current} = @{$this->{$_state}{alternatives}};
     } while ($this->advanceNavigator);
 }
 
 sub Back {
     my ($this,$steps) = @_;
-    
-    $steps ||= 1;
-    
     if ($this->{$_path} and @{$this->{$_path}}) {
-        
-        $steps = @{$this->{$_path}} - 1 if $steps >= @{$this->{$_path}};
-        
-        ($this->{$_state}) = splice @{$this->{$_path}},-$steps;
-        
-        $this->Current;
+        if ( (not $steps) || $steps == 1) {
+            $this->{$_state} = pop @{$this->{$_path}};
+        } else {
+            $steps ||= 1;
+            
+            $steps = @{$this->{$_path}} - 1 if $steps >= @{$this->{$_path}};
+            
+            $this->{$_state} = (splice @{$this->{$_path}},-$steps)[0];
+        }
+        $this->Current if defined wantarray;
     } else {
         return undef;
     }