inject_asset_host should not raise if <head> is missing

Previously, if you had Capybara.asset_host defined and tried to
`save_page` on a document without a <head> tag (like plain text)
`inject_asset_host` would crash trying to string-replace a <base>
tag onto the nonexistant <head>.

Since there's probably no need for it to successfully inject a
<base> into such documents, now it will return the original content
when there is no <head> present.
This commit is contained in:
Travis Grathwell 2015-05-03 19:13:35 -07:00
parent 90f5d374fd
commit e4db955395
2 changed files with 13 additions and 3 deletions

View File

@ -42,10 +42,12 @@ module Capybara
def inject_asset_host(html)
if Capybara.asset_host && Nokogiri::HTML(html).css("base").empty?
match = html.match(/<head[^<]*?>/)
html.clone.insert match.end(0), "<base href='#{Capybara.asset_host}' />"
else
html
if match
return html.clone.insert match.end(0), "<base href='#{Capybara.asset_host}' />"
end
end
html
end
##

View File

@ -79,5 +79,13 @@ Capybara::SpecHelper.spec '#save_page' do
expect(result).to include('<html')
expect(result).not_to include("http://example.com")
end
it "executes successfully even if the page is missing a <head>" do
@session.visit("/with_simple_html")
path = @session.save_page
result = File.read(path)
expect(result).to include("Bar")
end
end
end