diff --git a/lib/mechanize/page/frame.rb b/lib/mechanize/page/frame.rb index 401b22cd..62bdf2b0 100644 --- a/lib/mechanize/page/frame.rb +++ b/lib/mechanize/page/frame.rb @@ -1,5 +1,5 @@ class Mechanize - class Page < Mechanize::File + class Page # This class encapsulates a 'frame' tag. Frame objects can be treated # just like Link objects. They contain src, the link they refer to, # name, the name of the frame. 'src' and 'name' are aliased to 'href' @@ -7,6 +7,8 @@ class Page < Mechanize::File # like a Link. class Frame < Link alias :src :href + + attr_reader :text alias :name :text def initialize(node, mech, referer) diff --git a/lib/mechanize/page/link.rb b/lib/mechanize/page/link.rb index 061bcb59..9cefba75 100644 --- a/lib/mechanize/page/link.rb +++ b/lib/mechanize/page/link.rb @@ -11,32 +11,28 @@ class Mechanize::Page::Link attr_reader :node attr_reader :href - attr_reader :text attr_reader :attributes attr_reader :page - alias :to_s :text alias :referer :page def initialize(node, mech, page) @node = node @href = node['href'] - @text = node.inner_text + @text = nil @page = page @mech = mech @attributes = node + end - # If there is no text, try to find an image and use it's alt text - if (@text.nil? || @text.length == 0) && node.search('img').length > 0 - @text = '' - node.search('img').each do |e| - @text << ( e['alt'] || '') - end - end - + # Click on this link + def click + @mech.click self end - def uri - @href && URI.parse(WEBrick::HTTPUtils.escape(@href)) + # This method is a shorthand to get link's DOM id. + # Common usage: page.link_with(:dom_id => "links_exact_id") + def dom_id + node['id'] end # A list of words in the rel attribute, all lower-cased. @@ -49,15 +45,28 @@ def rel?(kind) rel.include?(kind) end - # Click on this link - def click - @mech.click self + # The text content of this link + 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 not node.search('img').empty? then + @text = '' + + @node.search('img').each do |e| + @text << ( e['alt'] || '') + end + end + + @text end - # This method is a shorthand to get link's DOM id. - # Common usage: page.link_with(:dom_id => "links_exact_id") - def dom_id - node['id'] + alias :to_s :text + + def uri + @href && URI.parse(WEBrick::HTTPUtils.escape(@href)) end end diff --git a/test/test_pretty_print.rb b/test/test_pretty_print.rb index 26ddac75..0e6786c4 100644 --- a/test/test_pretty_print.rb +++ b/test/test_pretty_print.rb @@ -4,7 +4,7 @@ class TestPrettyPrint < Test::Unit::TestCase def setup @agent = Mechanize.new end - + def test_pretty_print @agent.get("http://localhost/tc_pretty_print.html") pretty_string = @agent.pretty_print_inspect