Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion lib/spidr/page/html.rb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ def urls
def to_absolute(link)
link = link.to_s
new_url = begin
url.merge(link)
base_uri.merge(link)
rescue Exception
return
end
Expand All @@ -285,5 +285,14 @@ def to_absolute(link)

return new_url
end

def base_uri
if (html? && doc)
base_tag = doc.search('//base[@href]').first
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid pulling in all matching nodes, should use doc.at(...) instead of .search.

base_tag ? URI(base_tag.get_attribute('href')) : url
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I try to avoid using trinary flip-flop expressions. I would use a standard if (foo = ...) then foo.bar / else baz here.

else
url
end
end
end
end
12 changes: 12 additions & 0 deletions spec/page/html_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -520,5 +520,17 @@
end
end
end

context "when the page has a base tag" do
let(:base_href) { "http://www.google.com/" }
let(:body) { %{<html><head><base href="#{base_href}"><title>example</title></head><body><p>hello</p></body></html>} }
let(:link) { "/foo/" }

subject { super().to_absolute(link) }

it "should set the hostname to that of the base tag instead of the page's URL" do
expect(subject).to be == URI("#{base_href}").merge(link)
end
end
end
end