Class: Mechanize::Page::Link

### Home Classes Methods

### In Files

- lib/mechanize/page/link.rb

### Parent

Object

### Methods

- ::new
- #click
- #dom_class
- #dom_id
- #noreferrer?
- #rel
- #rel?
- #text
- #to_s
- #uri

### Files

- EXAMPLES.rdoc
- GUIDE.rdoc
- LICENSE.rdoc
- Manifest.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Mechanize
- Mechanize::ChunkedTerminationError
- Mechanize::ContentTypeError
- Mechanize::Cookie
- Mechanize::CookieCMethods
- Mechanize::CookieDeprecated
- Mechanize::CookieIMethods
- Mechanize::CookieJar
- Mechanize::CookieJarIMethods
- Mechanize::DirectorySaver
- Mechanize::Download
- Mechanize::ElementMatcher
- Mechanize::ElementNotFoundError
- Mechanize::Error
- Mechanize::File
- Mechanize::FileConnection
- Mechanize::FileRequest
- Mechanize::FileResponse
- Mechanize::FileSaver
- Mechanize::Form
- Mechanize::Form::Button
- Mechanize::Form::CheckBox
- Mechanize::Form::Field
- Mechanize::Form::FileUpload
- Mechanize::Form::Hidden
- Mechanize::Form::ImageButton
- Mechanize::Form::Keygen
- Mechanize::Form::MultiSelectList
- Mechanize::Form::Option
- Mechanize::Form::RadioButton
- Mechanize::Form::Reset
- Mechanize::Form::SelectList
- Mechanize::Form::Submit
- Mechanize::Form::Text
- Mechanize::Form::Textarea
- Mechanize::HTTP
- Mechanize::HTTP::Agent
- Mechanize::HTTP::AuthChallenge
- Mechanize::HTTP::AuthRealm
- Mechanize::HTTP::AuthStore
- Mechanize::HTTP::ContentDispositionParser
- Mechanize::HTTP::WWWAuthenticateParser
- Mechanize::Headers
- Mechanize::History
- Mechanize::Image
- Mechanize::Page
- Mechanize::Page::Base
- Mechanize::Page::Frame
- Mechanize::Page::Image
- Mechanize::Page::Label
- Mechanize::Page::Link
- Mechanize::Page::MetaRefresh
- Mechanize::Parser
- Mechanize::PluggableParser
- Mechanize::Prependable
- Mechanize::RedirectLimitReachedError
- Mechanize::RedirectNotGetOrHeadError
- Mechanize::ResponseCodeError
- Mechanize::ResponseReadError
- Mechanize::RobotsDisallowedError
- Mechanize::TestCase
- Mechanize::UnauthorizedError
- Mechanize::UnsupportedSchemeError
- Mechanize::Util
- Mechanize::XmlFile
- HTTP
- HTTP::CookieJar

No matching classes.

# Mechanize::Page::Link

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://example">Hello World</a>
    <a href="http://example"><img src="test.jpg" alt="Hello World"></a>

### Attributes

attributes[R]
href[R]
node[R]
page[R]
referer[R]

### Public Class Methods

new(node, mech, page) click to toggle source

    # File lib/mechanize/page/link.rb, line 18
    def initialize(node, mech, page)
      @node       = node
      @attributes = node
      @href       = node['href']
      @mech       = mech
      @page       = page
      @text       = nil
      @uri        = nil
    end

### Public Instance Methods

click() click to toggle source

Click on this link

    # File lib/mechanize/page/link.rb, line 29
    def click
      @mech.click self
    end

dom_class() click to toggle source

This method is a shorthand to get a link's DOM class Common usage:

    page.link_with(:dom_class => "links_exact_class")

    # File lib/mechanize/page/link.rb, line 43
    def dom_class
      node['class']
    end

dom_id() click to toggle source

This method is a shorthand to get link's DOM id. Common usage:

    page.link_with(:dom_id => "links_exact_id")

    # File lib/mechanize/page/link.rb, line 36
    def dom_id
      node['id']
    end

noreferrer?() click to toggle source

Test if this link should not be traced.

    # File lib/mechanize/page/link.rb, line 67
    def noreferrer?
      rel?('noreferrer')
    end

rel() click to toggle source

A list of words in the rel attribute, all lower-cased.

    # File lib/mechanize/page/link.rb, line 57
    def rel
      @rel ||= (val = attributes['rel']) ? val.downcase.split(' ') : []
    end

rel?(kind) click to toggle source

Test if the rel attribute includes `kind`.

    # File lib/mechanize/page/link.rb, line 62
    def rel? kind
      rel.include? kind
    end

text() click to toggle source

The text content of this link

    # File lib/mechanize/page/link.rb, line 72
    def text
      return @text if @text

      @text = @node.inner_text

      # If there is no text, try to find an image and use it's alt text
      if (@text.nil? or @text.empty?) and imgs = @node.search('img') then
        @text = imgs.map do |e|
          e['alt']
        end.join
      end

      @text
    end

Also aliased as: to_s
to_s() click to toggle source
Alias for: text
uri() click to toggle source

A URI for the href for this link. The link is first parsed as a raw link. If that fails parsing an escaped link is attepmted.

    # File lib/mechanize/page/link.rb, line 92
    def uri
      @uri ||= if @href then
                 begin
                   URI.parse @href
                 rescue URI::InvalidURIError
                   URI.parse WEBrick::HTTPUtils.escape @href
                 end
               end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.