Use Nokogiri::HTML5 to parse strings if nokogumbo is included

This commit is contained in:
Thomas Walpole 2017-07-27 01:05:13 -07:00
parent 19100ab01b
commit 7aa04bb5cf
5 changed files with 39 additions and 4 deletions

View File

@ -89,6 +89,9 @@ matrix:
- gemfile: Gemfile
rvm: 2.5.1
env: W3C=true HEADLESS=true
- gemfile: gemfiles/Gemfile.gumbo
rvm: 2.5.3
script: bundle exec rake spec_rack
allow_failures:
- gemfile: gemfiles/Gemfile.beta-versions
- gemfile: gemfiles/Gemfile.edge-marionette

7
gemfiles/Gemfile.gumbo Normal file
View File

@ -0,0 +1,7 @@
source 'https://rubygems.org'
gem 'bundler', '~> 1.1'
gemspec path: '..'
gem 'xpath', :git => 'git://github.com/teamcapybara/xpath.git'
gem 'nokogumbo'

View File

@ -366,9 +366,18 @@ module Capybara
# @return [Nokogiri::HTML::Document] HTML document
#
def HTML(html) # rubocop:disable Naming/MethodName
Nokogiri::HTML(html).tap do |document|
document.xpath('//textarea').each do |textarea|
textarea['_capybara_raw_value'] = textarea.content.sub(/\A\n/, '')
if Nokogiri.respond_to?(:HTML5) # Nokogumbo installed
Nokogiri::HTML5(html).tap do |document|
document.xpath('//textarea').each do |textarea|
# The Nokogumbo HTML5 parser already returns spec compliant contents
textarea['_capybara_raw_value'] = textarea.content
end
end
else
Nokogiri::HTML(html).tap do |document|
document.xpath('//textarea').each do |textarea|
textarea['_capybara_raw_value'] = textarea.content.sub(/\A\n/, '')
end
end
end
end

View File

@ -117,7 +117,8 @@ module Capybara
def extract_results(session)
expect(session).to have_xpath("//pre[@id='results']")
YAML.load Nokogiri::HTML(session.body).xpath("//pre[@id='results']").first.inner_html.lstrip
# YAML.load Nokogiri::HTML(session.body).xpath("//pre[@id='results']").first.inner_html.lstrip
YAML.load Capybara::HTML(session.body).xpath("//pre[@id='results']").first.inner_html.lstrip
end
end
end

View File

@ -1,6 +1,12 @@
# frozen_string_literal: true
require 'spec_helper'
nokogumbo_required = begin
require 'nokogumbo'
true
rescue LoadError
false
end
module TestSessions
RackTest = Capybara::Session.new(:rack_test, TestApp)
@ -231,6 +237,15 @@ RSpec.describe Capybara::RackTest::Driver do
end
end
RSpec.describe 'Capybara::String' do
it 'should use gumbo' do
skip 'Only valid if gumbo is included' unless nokogumbo_required
allow(Nokogiri).to receive(:HTML5).and_call_original
Capybara.string('<div id=test_div></div>')
expect(Nokogiri).to have_received(:HTML5)
end
end
module CSSHandlerIncludeTester
def dont_extend_css_handler
raise 'should never be called'