Changeset 90

Show
Ignore:
Timestamp:
12/23/2006 00:24:14 (2 years ago)
Author:
why
Message:
  • lib/hpricot/traverse.rb: added siblings_at and nodes_at, recommended by #37.
Files:
1 modified

Legend:

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

    r87 r90  
    3333    def to_original_html 
    3434      output("", :preserve => true) 
     35    end 
     36 
     37    # Puts together an array of neighboring nodes based on their proximity 
     38    # to this node.  So, for example, to get the next node, you could use 
     39    # <tt>nodes_at(1).  Or, to get the previous node, use <tt>nodes_at(1)</tt>. 
     40    # 
     41    # This method also accepts ranges and sets of numbers. 
     42    # 
     43    #    ele.nodes_at(-3..-1, 1..3) # gets three nodes before and three after 
     44    #    ele.nodes_at(1, 5, 7) # gets three nodes at offsets below the current node 
     45    #    ele.nodes_at(0, 5..6) # the current node and two others 
     46    def nodes_at(*pos) 
     47      sib = parent.children 
     48      i, si = 0, sib.index(self) 
     49      Elements[* 
     50        sib.select do |x| 
     51          sel = case i - si when *pos 
     52                  true 
     53                end 
     54          i += 1 
     55          sel 
     56        end 
     57      ] 
    3558    end 
    3659 
     
    301324    end 
    302325 
    303     # Returns the node neighboring this node to the south: just below it. 
    304     # This method does not find text nodes or comments or cdata or any of that. 
     326    # Returns the container node neighboring this node to the south: just below it. 
     327    # By "container" node, I mean: this method does not find text nodes or comments or cdata or any of that. 
    305328    # See Hpricot::Traverse#next_node if you need to hunt out all kinds of nodes. 
    306329    def next_sibling 
     
    309332    end 
    310333 
    311     # Returns to node neighboring this node to the north: just above it. 
    312     # This method does not find text nodes or comments or cdata or any of that. 
     334    # Returns the container node neighboring this node to the north: just above it. 
     335    # By "container" node, I mean: this method does not find text nodes or comments or cdata or any of that. 
    313336    # See Hpricot::Traverse#previous_node if you need to hunt out all kinds of nodes. 
    314337    def previous_sibling 
     
    316339      x = sib.index(self) - 1 
    317340      sib[x] if sib and x >= 0 
     341    end 
     342 
     343    # Puts together an array of neighboring sibling elements based on their proximity 
     344    # to this element. 
     345    # 
     346    # This method accepts ranges and sets of numbers. 
     347    # 
     348    #    ele.siblings_at(-3..-1, 1..3) # gets three elements before and three after 
     349    #    ele.siblings_at(1, 5, 7) # gets three elements at offsets below the current element 
     350    #    ele.siblings_at(0, 5..6) # the current element and two others 
     351    # 
     352    # Like the other "sibling" methods, this doesn't find text and comment nodes. 
     353    # Use nodes_at to include those nodes. 
     354    def siblings_at(*pos) 
     355      sib = parent.containers 
     356      i, si = 0, sib.index(self) 
     357      Elements[* 
     358        sib.select do |x| 
     359          sel = case i - si when *pos 
     360                  true 
     361                end 
     362          i += 1 
     363          sel 
     364        end 
     365      ] 
    318366    end 
    319367