Class: Mechanize::Page::Link
- Inherits:
-
Object
- Object
- Mechanize::Page::Link
- Defined in:
- lib/mechanize/page/link.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_class ⇒ Object
This method is a shorthand to get a link’s DOM class Common usage: page.link_with(:dom_class => “links_exact_class”).
-
#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
:nodoc:.
-
#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_class ⇒ Object
This method is a shorthand to get a link’s DOM class Common usage:
page.link_with(:dom_class => "links_exact_class")
43 44 45 |
# File 'lib/mechanize/page/link.rb', line 43 def dom_class node['class'] 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
:nodoc:
47 48 49 50 51 52 |
# File 'lib/mechanize/page/link.rb', line 47 def pretty_print(q) # :nodoc: 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.
57 58 59 |
# File 'lib/mechanize/page/link.rb', line 57 def rel @rel ||= (val = attributes['rel']) ? val.downcase.split(' ') : [] end |
#rel?(kind) ⇒ Boolean
Test if the rel attribute includes kind
.
62 63 64 |
# File 'lib/mechanize/page/link.rb', line 62 def rel? kind rel.include? kind end |
#text ⇒ Object Also known as: to_s
The text content of this link
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/mechanize/page/link.rb', line 67 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.
87 88 89 90 91 92 93 94 95 |
# File 'lib/mechanize/page/link.rb', line 87 def uri @uri ||= if @href then begin URI.parse @href rescue URI::InvalidURIError URI.parse WEBrick::HTTPUtils.escape @href end end end |