static public Method

Elements.expand(ele1, ele2, excl=false)

Given two elements, attempt to gather an Elements array of everything between (and including) those two elements.

Source Code

# File hpricot/elements.rb, line 315
def self.expand(ele1, ele2, excl=false)
  ary = []
  offset = excl ? -1 : 0

  if ele1 and ele2
    # let's quickly take care of siblings
    if ele1.parent == ele2.parent
      ary = ele1.parent.children[ele1.node_position..(ele2.node_position+offset)]
    else
      # find common parent
      p, ele1_p = ele1, [ele1]
      ele1_p.unshift p while p.respond_to?(:parent) and p = p.parent
      p, ele2_p = ele2, [ele2]
      ele2_p.unshift p while p.respond_to?(:parent) and p = p.parent
      common_parent = ele1_p.zip(ele2_p).select { |p1, p2| p1 == p2 }.flatten.last

      child = nil
      if ele1 == common_parent
        child = ele2
      elsif ele2 == common_parent
        child = ele1
      end

      if child
        ary = common_parent.children[0..(child.node_position+offset)]
      end
    end
  end

  return Elements[*ary]
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.