Class: Mechanize::Page::Link
- Inherits:
-
Object
- Object
- Mechanize::Page::Link
- Defined in:
- lib/mechanize/page/link.rb,
lib/mechanize/inspect.rb,
lib/mechanize/monkey_patch.rb
Overview
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>
Direct Known Subclasses
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#href ⇒ Object
readonly
Returns the value of attribute href.
-
#node ⇒ Object
readonly
Returns the value of attribute node.
-
#page ⇒ Object
(also: #referer)
readonly
Returns the value of attribute page.
Instance Method Summary collapse
-
#click ⇒ Object
Click on this link.
-
#dom_id ⇒ Object
This method is a shorthand to get link’s DOM id.
-
#initialize(node, mech, page) ⇒ Link
constructor
A new instance of Link.
- #pretty_print(q) ⇒ Object
-
#rel ⇒ Object
A list of words in the rel attribute, all lower-cased.
-
#rel?(kind) ⇒ Boolean
Test if the rel attribute includes
kind
. -
#text ⇒ Object
(also: #to_s)
The text content of this link.
-
#uri ⇒ Object
A URI for the #href for this link.
Constructor Details
#initialize(node, mech, page) ⇒ Link
Returns a new instance of Link.
18 19 20 21 22 23 24 25 26 |
# 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 |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
14 15 16 |
# File 'lib/mechanize/page/link.rb', line 14 def attributes @attributes end |
#href ⇒ Object (readonly)
Returns the value of attribute href.
13 14 15 |
# File 'lib/mechanize/page/link.rb', line 13 def href @href end |
#node ⇒ Object (readonly)
Returns the value of attribute node.
12 13 14 |
# File 'lib/mechanize/page/link.rb', line 12 def node @node end |
#page ⇒ Object (readonly) Also known as: referer
Returns the value of attribute page.
15 16 17 |
# File 'lib/mechanize/page/link.rb', line 15 def page @page end |
Instance Method Details
#click ⇒ Object
Click on this link
29 30 31 |
# File 'lib/mechanize/page/link.rb', line 29 def click @mech.click self end |
#dom_id ⇒ Object
This method is a shorthand to get link’s DOM id. Common usage:
page.link_with(:dom_id => "links_exact_id")
36 37 38 |
# File 'lib/mechanize/page/link.rb', line 36 def dom_id node['id'] end |
#pretty_print(q) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/mechanize/inspect.rb', line 45 def pretty_print(q) q.object_group(self) { q.breakable; q.pp text q.breakable; q.pp href } end |
#rel ⇒ Object
A list of words in the rel attribute, all lower-cased.
41 42 43 |
# File 'lib/mechanize/page/link.rb', line 41 def rel @rel ||= (val = attributes['rel']) ? val.downcase.split(' ') : [] end |
#rel?(kind) ⇒ Boolean
Test if the rel attribute includes kind
.
46 47 48 |
# File 'lib/mechanize/page/link.rb', line 46 def rel? kind rel.include? kind end |
#text ⇒ Object Also known as: to_s
The text content of this link
51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/mechanize/page/link.rb', line 51 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 |
#uri ⇒ Object
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.
71 72 73 74 75 76 77 78 79 |
# File 'lib/mechanize/page/link.rb', line 71 def uri @uri ||= if @href then begin URI.parse @href rescue URI::InvalidURIError URI.parse WEBrick::HTTPUtils.escape @href end end end |