123456789_123456789_123456789_123456789_123456789_

Class: Capybara::Queries::SelectorQuery::Rectangle Private

Relationships & Source Files
Inherits: Object
Defined in: lib/capybara/queries/selector_query.rb

Class Method Summary

Instance Attribute Summary

  • #bottom readonly Internal use only
  • #left readonly Internal use only
  • #right readonly Internal use only
  • #top readonly Internal use only

Instance Method Summary

Constructor Details

.new(position) ⇒ Rectangle

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 629

def initialize(position)
  # rubocop:disable Style/RescueModifier
  @top = position['top'] rescue position['y']
  @bottom = position['bottom'] rescue (@top + position['height'])
  @left = position['left'] rescue position['x']
  @right = position['right'] rescue (@left + position['width'])
  # rubocop:enable Style/RescueModifier
end

Instance Attribute Details

#bottom (readonly)

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 627

attr_reader :top, :bottom, :left, :right

#left (readonly)

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 627

attr_reader :top, :bottom, :left, :right

#right (readonly)

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 627

attr_reader :top, :bottom, :left, :right

#top (readonly)

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 627

attr_reader :top, :bottom, :left, :right

Instance Method Details

#above?(other) ⇒ Boolean

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 653

def above?(other)
  bottom <= other.top
end

#below?(other) ⇒ Boolean

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 657

def below?(other)
  top >= other.bottom
end

#distance(other)

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 638

def distance(other)
  distance = Float::INFINITY

  line_segments.each do |ls1|
    other.line_segments.each do |ls2|
      distance = [
        distance,
        distance_segment_segment(*ls1, *ls2)
      ].min
    end
  end

  distance
end

#distance_segment_segment(l1p1, l1p2, l2p1, l2p2) (private)

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 686

def distance_segment_segment(l1p1, l1p2, l2p1, l2p2)
  # See http://geomalgorithms.com/a07-_distance.html
  # rubocop:disable Naming/VariableName
  u = l1p2 - l1p1
  v = l2p2 - l2p1
  w = l1p1 - l2p1

  a = u.dot u
  b = u.dot v
  c = v.dot v

  d = u.dot w
  e = v.dot w
  cap_d = (a * c) - (b**2)
  sD = tD = cap_d

  # compute the line parameters of the two closest points
  if cap_d < Float::EPSILON # the lines are almost parallel
    sN = 0.0 # force using point P0 on segment S1
    sD = 1.0 # to prevent possible division by 0.0 later
    tN = e
    tD = c
  else # get the closest points on the infinite lines
    sN = (b * e) - (c * d)
    tN = (a * e) - (b * d)
    if sN.negative? # sc < 0 => the s=0 edge is visible
      sN = 0
      tN = e
      tD = c
    elsif sN > sD # sc > 1 => the s=1 edge is visible
      sN = sD
      tN = e + b
      tD = c
    end
  end

  if tN.negative? # tc < 0 => the t=0 edge is visible
    tN = 0
    # recompute sc for this edge
    if (-d).negative?
      sN = 0.0
    elsif -d > a
      sN = sD
    else
      sN = -d
      sD = a
    end
  elsif tN > tD # tc > 1 => the t=1 edge is visible
    tN = tD
    # recompute sc for this edge
    if (-d + b).negative?
      sN = 0.0
    elsif (-d + b) > a
      sN = sD
    else
      sN = (-d + b)
      sD = a
    end
  end

  # finally do the division to get sc and tc
  sc = sN.abs < Float::EPSILON ? 0.0 : sN / sD
  tc = tN.abs < Float::EPSILON ? 0.0 : tN / tD

  # difference of the two closest points
  dP = w + (u * sc) - (v * tc)

  Math.sqrt(dP.dot(dP))
  # rubocop:enable Naming/VariableName
end

#left_of?(other) ⇒ Boolean

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 661

def left_of?(other)
  right <= other.left
end

#near?(other) ⇒ Boolean

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 669

def near?(other)
  distance(other) <= 50
end

#right_of?(other) ⇒ Boolean

[ GitHub ]

  
# File 'lib/capybara/queries/selector_query.rb', line 665

def right_of?(other)
  left >= other.right
end