From e4db955395c0d0dd73091fda35b8e04150ad55f0 Mon Sep 17 00:00:00 2001 From: Travis Grathwell Date: Sun, 3 May 2015 19:13:35 -0700 Subject: [PATCH] inject_asset_host should not raise if is missing Previously, if you had Capybara.asset_host defined and tried to `save_page` on a document without a tag (like plain text) `inject_asset_host` would crash trying to string-replace a tag onto the nonexistant . Since there's probably no need for it to successfully inject a into such documents, now it will return the original content when there is no present. --- lib/capybara/helpers.rb | 8 +++++--- lib/capybara/spec/session/save_page_spec.rb | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/capybara/helpers.rb b/lib/capybara/helpers.rb index c063c7bc..586d8e1a 100644 --- a/lib/capybara/helpers.rb +++ b/lib/capybara/helpers.rb @@ -42,10 +42,12 @@ module Capybara def inject_asset_host(html) if Capybara.asset_host && Nokogiri::HTML(html).css("base").empty? match = html.match(//) - html.clone.insert match.end(0), "" - else - html + if match + return html.clone.insert match.end(0), "" + end end + + html end ## diff --git a/lib/capybara/spec/session/save_page_spec.rb b/lib/capybara/spec/session/save_page_spec.rb index 0dbf96d8..27e8b0b5 100644 --- a/lib/capybara/spec/session/save_page_spec.rb +++ b/lib/capybara/spec/session/save_page_spec.rb @@ -79,5 +79,13 @@ Capybara::SpecHelper.spec '#save_page' do expect(result).to include('" do + @session.visit("/with_simple_html") + path = @session.save_page + + result = File.read(path) + expect(result).to include("Bar") + end end end