lib/watir/element_collections.rb in watir-2.0.4 vs lib/watir/element_collections.rb in watir-3.0.0.rc1
- old
+ new
@@ -2,11 +2,11 @@
# this class is the super class for the iterator classes (buttons, links, spans etc
# it would normally only be accessed by the iterator methods (spans, links etc) of IE
class ElementCollections
include Enumerable
- # Super class for all the iteractor classes
+ # Super class for all the iterator classes
# * container - an instance of an IE object
def initialize(container, how, what)
if how == :index || (how.is_a?(Hash) && how[:index])
_how = what ? "#{how.inspect}, #{what.inspect}" : "#{how.inspect}"
raise Exception::MissingWayOfFindingObjectException,
@@ -28,23 +28,18 @@
alias_method :size, :length
# iterate through each of the elements in the collection in turn
def each
- @container.tagged_element_locator(element_tag, @how, @what, element_class).each {|element| yield element}
+ @container.locator_for(TaggedElementLocator, element_tags, @how, @what, element_class).each {|element| yield element}
end
# allows access to a specific item in the collection
def [](n)
number = n - Watir::IE.base_index
offset = Watir::IE.zero_based_indexing ? (length - 1) : length
-
- unless number.between?(0, offset)
- raise Exception::MissingWayOfFindingObjectException,
- "Can't find #{element_tag.downcase} with :index #{n} from #{self.class} with size of #{length}"
- end
- return iterator_object(number)
+ iterator_object(number) || element_class.new(@container, :index, n)
end
def first
iterator_object(0)
end
@@ -63,17 +58,24 @@
private
def iterator_object(i)
count = 0
- each {|e| return e if count == i; count += 1}
+ each do |e|
+ return e if (i >= 0 && count == i) || (i < 0 && count == length + i)
+ count += 1
+ end
end
def element_class
Watir.const_get self.class.name.split("::").last.chop
end
- def element_tag
- element_class.const_defined?(:TAG) ? element_class::TAG : element_class.name.split("::").last
+ def element_tags
+ tags = @how.is_a?(Hash) && @how[:tag_name] ? [@how[:tag_name].upcase] :
+ element_class.const_defined?(:TAG) ? [element_class::TAG] :
+ element_class.const_defined?(:TAGS) ? element_class::TAGS :
+ [element_class.name.split("::").last.upcase]
+ tags
end
end
end