Changeset 122

Show
Ignore:
Timestamp:
03/22/2007 22:34:49 (21 months ago)
Author:
why
Message:
  • lib/hpricot/traverse.rb: preceding_siblings and following_siblings from Brett #70. i went ahead and added preceding and following which find all nodes in either position. also, previous_node and next_node are now just previous and next, since I think these are more common than the sibling variants.
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/lib/hpricot/traverse.rb

    r116 r122  
    6060    # Returns the node neighboring this node to the south: just below it. 
    6161    # This method includes text nodes and comments and such. 
    62     def next_node 
     62    def next 
    6363      sib = parent.children 
    6464      sib[sib.index(self) + 1] if parent 
    6565    end 
     66    alias_method :next_node, :next 
    6667 
    6768    # Returns to node neighboring this node to the north: just above it. 
    6869    # This method includes text nodes and comments and such. 
    69     def previous_node 
     70    def previous 
    7071      sib = parent.children 
    7172      x = sib.index(self) - 1 
    7273      sib[x] if sib and x >= 0 
    7374    end 
     75    alias_method :previous_node, :previous 
     76 
     77    # Find all preceding nodes. 
     78    def preceding 
     79      sibs = parent.children 
     80      si = sibs.index(self)  
     81      return Elements[*sibs[0...si]]  
     82    end  
     83  
     84    # Find all nodes which follow the current one. 
     85    def following 
     86      sibs = parent.children  
     87      si = sibs.index(self) + 1  
     88      return Elements[*sibs[si...sibs.length]]  
     89    end  
    7490 
    7591    # Adds elements immediately after this element, contained in the +html+ string. 
     
    365381    end 
    366382 
     383    # Find all preceding sibling elements.   Like the other "sibling" methods, this weeds 
     384    # out text and comment nodes. 
     385    def preceding_siblings()  
     386      sibs = parent.containers  
     387      si = sibs.index(self)  
     388      return Elements[*sibs[0...si]]  
     389    end  
     390  
     391    # Find sibling elements which follow the current one.   Like the other "sibling" methods, this weeds 
     392    # out text and comment nodes. 
     393    def following_siblings()  
     394      sibs = parent.containers  
     395      si = sibs.index(self) + 1  
     396      return Elements[*sibs[si...sibs.length]]  
     397    end  
     398 
    367399    # Puts together an array of neighboring sibling elements based on their proximity 
    368400    # to this element.