Skip to content

Commit

Permalink
Fix images.best for malformed img tags
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwindt committed Feb 14, 2015
1 parent fd13062 commit 3c8835d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/meta_inspector/parsers/images.rb
Expand Up @@ -35,7 +35,7 @@ def owner_suggested
# filtered for images that are more square than 10:1 or 1:10
def largest()
@larget_image ||= begin
img_nodes = parsed.search('//img')
img_nodes = parsed.search('//img').select{ |img_node| img_node['src'] }
sizes = img_nodes.map { |img_node| [URL.absolutify(img_node['src'], base_url), img_node['width'], img_node['height']] }
sizes.uniq! { |url, width, height| url }
if @download_images
Expand Down
23 changes: 23 additions & 0 deletions spec/fixtures/malformed_image_in_html.response
@@ -0,0 +1,23 @@
HTTP/1.1 200 OK
Server: nginx/0.7.67
Date: Fri, 18 Nov 2011 21:46:46 GMT
Content-Type: text/html
Connection: keep-alive
Last-Modified: Mon, 14 Nov 2011 16:53:18 GMT
Content-Length: 4987
X-Varnish: 2000423390
Age: 0
Via: 1.1 varnish

<html>
<head>
<title>An example page</title>
</head>
<body>
<img src="/too_narrow" width="10" height="100" />
<img src="/smaller" width="10" height="10" />
<img src="/largest" width="100" height="100" />
<img width="100" height="10" />
<img src="/smallest" width="1" height="1" />
</body>
</html>
7 changes: 7 additions & 0 deletions spec/meta_inspector/images_spec.rb
Expand Up @@ -88,6 +88,13 @@

expect(page.images.best).to eq("http://example.com/100x100")
end

it "should find image when some img tag has no src attribute" do
page = MetaInspector.new('http://example.com/malformed_image_in_html')

expect(page.images.best).to eq("http://example.com/largest")
end

end

describe "images.owner_suggested" do
Expand Down
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Expand Up @@ -36,6 +36,7 @@ def fixture_file(filename)
# Used to test largest image in page logic
FakeWeb.register_uri(:get, "http://example.com/largest_image_in_html", :response => fixture_file("largest_image_in_html.response"))
FakeWeb.register_uri(:get, "http://example.com/largest_image_using_image_size", :response => fixture_file("largest_image_using_image_size.response"))
FakeWeb.register_uri(:get, "http://example.com/malformed_image_in_html", :response => fixture_file("malformed_image_in_html.response"))
FakeWeb.register_uri(:get, "http://example.com/10x10", :response => fixture_file("10x10.jpg.response"))
FakeWeb.register_uri(:get, "http://example.com/100x100", :response => fixture_file("100x100.jpg.response"))

Expand Down

0 comments on commit 3c8835d

Please sign in to comment.