Sha256: 4787e5248ee5b64eae7023ce97a1ce24a8d2b42c5ba69f8c78ba31ddc5535692

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

class Mechanize
  class Page < Mechanize::File
    # This class encapsulates links.  It contains the text and the URI for
    # 'a' tags parsed out of an HTML page.  If the link contains an image,
    # the alt text will be used for that image.
    #
    # For example, the text for the following links with both be 'Hello World':
    #
    # <a href="http://rubyforge.org">Hello World</a>
    # <a href="http://rubyforge.org"><img src="test.jpg" alt="Hello World"></a>
    class Link
      attr_reader :node
      attr_reader :href
      attr_reader :text
      attr_reader :attributes
      attr_reader :page
      alias :to_s :text
      alias :referer :page

      def initialize(node, mech, page)
        @node = node
        @href = node['href']
        @text = node.inner_text
        @page = page
        @mech = mech
        @attributes = node

        # If there is no text, try to find an image and use it's alt text
        if (@text.nil? || @text.length == 0) && node.search('img').length > 0
          @text = ''
          node.search('img').each do |e|
            @text << ( e['alt'] || '')
          end
        end

      end

      # Returns class attribute (<a class="***"> of ***). If no class, returns nil.
      def attribute_class; node['class']; end
      # Returns id attribute (<a id="***"> of ***). If no id, returns nil.
      def attribute_id; node['id']; end

      def uri
        @href && URI.parse(@href)
      end

      # Click on this link
      def click
        @mech.click self
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kitamomonga-mechanize-0.9.3.20090724215219 lib/mechanize/page/link.rb