diff --git a/lib/capybara/helpers.rb b/lib/capybara/helpers.rb index 9d1e83cb..6c8d0ec4 100644 --- a/lib/capybara/helpers.rb +++ b/lib/capybara/helpers.rb @@ -27,6 +27,24 @@ module Capybara def to_regexp(text) text.is_a?(Regexp) ? text : Regexp.escape(normalize_whitespace(text)) end + + ## + # + # Injects a `` tag into the given HTML code, pointing to + # `Capybara.asset_host`. + # + # @param [String] html HTML code to inject into + # @param [String] The modified HTML code + # + def inject_asset_host(html) + if Capybara.asset_host + if Nokogiri::HTML(html).css("base").empty? and match = html.match(//) + html.insert match.end(0), "" + end + else + html + end + end end end end diff --git a/lib/capybara/session.rb b/lib/capybara/session.rb index 663726b7..4ba1df78 100644 --- a/lib/capybara/session.rb +++ b/lib/capybara/session.rb @@ -313,18 +313,10 @@ module Capybara def save_page(path=nil) path ||= "capybara-#{Time.new.strftime("%Y%m%d%H%M%S")}#{rand(10**10)}.html" path = File.expand_path(path, Capybara.save_and_open_page_path) if Capybara.save_and_open_page_path - body_to_write = body - - if Capybara.asset_host - parsed = Nokogiri::HTML(body_to_write) - if parsed.css("base").empty? && match = body_to_write.match(//) - body_to_write.insert match.end(0), "" - end - end FileUtils.mkdir_p(File.dirname(path)) - File.open(path,'w') { |f| f.write(body_to_write) } + File.open(path,'w') { |f| f.write(Capybara::Helpers.inject_asset_host(body)) } path end @@ -393,7 +385,7 @@ module Capybara end private - + def scopes @scopes ||= [document] end