Skip to content

Commit

Permalink
Ensure images with no src attribute still returns urls
Browse files Browse the repository at this point in the history
If a website has images with no "src" attribute, Mechanize
will break when attempting to build relative URLs for images
where image.src returns nil, URI("host") + src will fail

closes sparklemotion#317
  • Loading branch information
leejarvis committed May 27, 2013
1 parent 8db5993 commit 2095aa1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/mechanize/page/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ def to_s
def url
if relative? then
if page.bases[0] then
page.bases[0].href + src
page.bases[0].href + src.to_s
else
page.uri + Mechanize::Util.uri_escape(src)
page.uri + Mechanize::Util.uri_escape(src.to_s)
end
else
URI Mechanize::Util.uri_escape(src)
Expand Down
6 changes: 6 additions & 0 deletions test/test_mechanize_page_image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,11 @@ def test_image_referer_https_page_rel_src
assert_equal URI('https://example/'), page.images.first.image_referer.uri
end

def test_no_src_attribute
page = html_page '<img width="10" height="10" class="foo" />'
page.uri = URI 'https://example/'
assert_equal URI('https://example/'), page.images.first.url
end

end

0 comments on commit 2095aa1

Please sign in to comment.