Parent

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

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

Public Class Methods

new(node, mech, page) click to toggle source
    # File lib/mechanize/page/link.rb, line 18
18:   def initialize(node, mech, page)
19:     @node       = node
20:     @attributes = node
21:     @href       = node['href']
22:     @mech       = mech
23:     @page       = page
24:     @text       = nil
25:     @uri        = nil
26:   end

Public Instance Methods

click() click to toggle source

Click on this link

    # File lib/mechanize/page/link.rb, line 29
29:   def click
30:     @mech.click self
31:   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
43:   def dom_class
44:     node['class']
45:   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
36:   def dom_id
37:     node['id']
38:   end
noreferrer?() click to toggle source

Test if this link should not be traced.

    # File lib/mechanize/page/link.rb, line 67
67:   def noreferrer?
68:     rel?('noreferrer')
69:   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
57:   def rel
58:     @rel ||= (val = attributes['rel']) ? val.downcase.split(' ') : []
59:   end
rel?(kind) click to toggle source

Test if the rel attribute includes kind.

    # File lib/mechanize/page/link.rb, line 62
62:   def rel? kind
63:     rel.include? kind
64:   end
text() click to toggle source

The text content of this link

    # File lib/mechanize/page/link.rb, line 72
72:   def text
73:     return @text if @text
74: 
75:     @text = @node.inner_text
76: 
77:     # If there is no text, try to find an image and use it's alt text
78:     if (@text.nil? or @text.empty?) and imgs = @node.search('img') then
79:       @text = imgs.map do |e|
80:         e['alt']
81:       end.join
82:     end
83: 
84:     @text
85:   end
Also aliased as: to_s
to_s() click to toggle source
Alias for: text
uri() click to toggle source

A URI for the # 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
 92:   def uri
 93:     @uri ||= if @href then
 94:                begin
 95:                  URI.parse @href
 96:                rescue URI::InvalidURIError
 97:                  URI.parse WEBrick::HTTPUtils.escape @href
 98:                end
 99:              end
100:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.