Sha256: bf6356ab8ad5ec7060e0365d5310ebe6f5c4801cdf1e5beea30822f86449dddf

Contents?: true

Size: 1.65 KB

Versions: 41

Compression:

Stored size: 1.65 KB

Contents

require "webrat/core/locators/locator"

module Webrat
  module Locators
    
    class LinkLocator < Locator # :nodoc:
  
      def locate
        Link.load(@session, link_element)
      end
  
      def link_element
        matching_links.min { |a, b| Webrat::XML.all_inner_text(a).length <=> Webrat::XML.all_inner_text(b).length }
      end
  
      def matching_links
        @matching_links ||= link_elements.select do |link_element|
          matches_text?(link_element) ||
          matches_id?(link_element)
        end
      end
  
      def matches_text?(link)
        if @value.is_a?(Regexp)
          matcher = @value
        else
          matcher = /#{Regexp.escape(@value.to_s)}/i
        end

        replace_nbsp(Webrat::XML.all_inner_text(link)) =~ matcher ||
        replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher ||
        Webrat::XML.attribute(link, "title")=~ matcher
      end

      def matches_id?(link)
        if @value.is_a?(Regexp)
          (Webrat::XML.attribute(link, "id") =~ @value) ? true : false
        else
          (Webrat::XML.attribute(link, "id") == @value) ? true : false
        end
      end
  
      def link_elements
        Webrat::XML.xpath_search(@dom, *Link.xpath_search)
      end
  
      def replace_nbsp(str)
        str.gsub([0xA0].pack('U'), ' ')
      end

      def replace_nbsp_ref(str)
        str.gsub('&#xA0;',' ').gsub('&nbsp;', ' ')
      end
  
      def error_message
        "Could not find link with text or title or id #{@value.inspect}"
      end
      
    end
    
    def find_link(text_or_title_or_id) #:nodoc:
      LinkLocator.new(@session, dom, text_or_title_or_id).locate!
    end
    
  end
end

Version data entries

41 entries across 41 versions & 12 rubygems

Version Path
adva-0.3.2 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.3.1 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.3.0 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.2.4 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.2.3 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.2.2 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.2.1 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.2.0 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.1.4 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.1.3 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.1.2 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.1.1 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.1.0 test/webrat/lib/webrat/core/locators/link_locator.rb
adva-0.0.1 test/webrat/lib/webrat/core/locators/link_locator.rb
aslakhellesoy-webrat-0.3.2.1 lib/webrat/core/locators/link_locator.rb
aslakhellesoy-webrat-0.3.2.2 lib/webrat/core/locators/link_locator.rb
auxesis-webrat-0.4.1 lib/webrat/core/locators/link_locator.rb
brynary-webrat-0.3.2.2 lib/webrat/core/locators/link_locator.rb
brynary-webrat-0.4.0 lib/webrat/core/locators/link_locator.rb
flazz-webrat-0.3.2.1 lib/webrat/core/locators/link_locator.rb