From 1777f7a31d5ff365019f7c78a79b40de0d131799 Mon Sep 17 00:00:00 2001 From: Eric Hodel Date: Fri, 24 Jun 2011 15:09:45 -0700 Subject: [PATCH] Sort and cleanup Link#text --- lib/mechanize/page/link.rb | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/mechanize/page/link.rb b/lib/mechanize/page/link.rb index 9cefba75..8e42d225 100644 --- a/lib/mechanize/page/link.rb +++ b/lib/mechanize/page/link.rb @@ -16,12 +16,12 @@ class Mechanize::Page::Link alias :referer :page def initialize(node, mech, page) - @node = node - @href = node['href'] - @text = nil - @page = page - @mech = mech + @node = node @attributes = node + @href = node['href'] + @mech = mech + @page = page + @text = nil end # Click on this link @@ -30,7 +30,8 @@ def click end # This method is a shorthand to get link's DOM id. - # Common usage: page.link_with(:dom_id => "links_exact_id") + # Common usage: + # page.link_with(:dom_id => "links_exact_id") def dom_id node['id'] end @@ -41,23 +42,21 @@ def rel end # Test if the rel attribute includes +kind+. - def rel?(kind) - rel.include?(kind) + def rel? kind + rel.include? kind end # The text content of this link def text return @text if @text - @text = node.inner_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 not node.search('img').empty? then - @text = '' - - @node.search('img').each do |e| - @text << ( e['alt'] || '') - end + if (@text.nil? or @text.empty?) and imgs = @node.search('img') then + @text = imgs.map do |e| + e['alt'] + end.join end @text