Refactoring nokogiri usage
This commit is contained in:
parent
3e0f10bf33
commit
c01f44a32c
@ -1,3 +1,4 @@
|
||||
require "webrat/core/nokogiri"
|
||||
require "webrat/core/logging"
|
||||
require "webrat/core/flunk"
|
||||
require "webrat/core/form"
|
||||
|
@ -3,15 +3,15 @@ module Webrat
|
||||
|
||||
class HasContent
|
||||
def initialize(content)
|
||||
# Require nokogiri and fall back on rexml
|
||||
# We need Nokogiri's CSS to XPath support, even if using REXML
|
||||
require "nokogiri/css"
|
||||
|
||||
begin
|
||||
require "nokogiri"
|
||||
require "webrat/nokogiri"
|
||||
require "webrat/core/nokogiri"
|
||||
rescue LoadError => e
|
||||
if require "rexml/document"
|
||||
require "webrat/vendor/nokogiri/css"
|
||||
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
|
||||
end
|
||||
require "rexml/document"
|
||||
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
|
||||
end
|
||||
|
||||
@content = content
|
||||
@ -38,7 +38,7 @@ module Webrat
|
||||
end
|
||||
|
||||
def matches_nokogiri?(stringlike)
|
||||
@document = nokogiri_document(stringlike)
|
||||
@document = Webrat.nokogiri_document(stringlike)
|
||||
@element = @document.inner_text
|
||||
|
||||
case @content
|
||||
@ -70,20 +70,6 @@ module Webrat
|
||||
end
|
||||
end
|
||||
|
||||
def nokogiri_document(stringlike)
|
||||
return stringlike.dom if stringlike.respond_to?(:dom)
|
||||
stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)
|
||||
|
||||
case stringlike
|
||||
when Nokogiri::HTML::Document, Nokogiri::XML::NodeSet
|
||||
stringlike
|
||||
when StringIO
|
||||
Nokogiri::HTML(stringlike.string)
|
||||
else
|
||||
Nokogiri::HTML(stringlike.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
# ==== Returns
|
||||
# String:: The failure message.
|
||||
def failure_message
|
||||
|
@ -3,15 +3,16 @@ module Webrat
|
||||
|
||||
class HaveXpath
|
||||
def initialize(expected, &block)
|
||||
# We need Nokogiri's CSS to XPath support, even if using REXML
|
||||
require "nokogiri/css"
|
||||
|
||||
# Require nokogiri and fall back on rexml
|
||||
begin
|
||||
require "nokogiri"
|
||||
require "webrat/nokogiri"
|
||||
require "webrat/core/nokogiri"
|
||||
rescue LoadError => e
|
||||
if require "rexml/document"
|
||||
require "webrat/vendor/nokogiri/css"
|
||||
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
|
||||
end
|
||||
require "rexml/document"
|
||||
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
|
||||
end
|
||||
|
||||
@expected = expected
|
||||
@ -36,7 +37,7 @@ module Webrat
|
||||
end
|
||||
|
||||
def matches_nokogiri?(stringlike)
|
||||
@document = nokogiri_document(stringlike)
|
||||
@document = Webrat.nokogiri_document(stringlike)
|
||||
@document.xpath(*query).any?
|
||||
end
|
||||
|
||||
@ -61,20 +62,6 @@ module Webrat
|
||||
end
|
||||
end
|
||||
|
||||
def nokogiri_document(stringlike)
|
||||
return stringlike.dom if stringlike.respond_to?(:dom)
|
||||
stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)
|
||||
|
||||
case stringlike
|
||||
when Nokogiri::HTML::Document, Nokogiri::XML::NodeSet
|
||||
stringlike
|
||||
when StringIO
|
||||
Nokogiri::HTML(stringlike.string)
|
||||
else
|
||||
Nokogiri::HTML(stringlike.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
def query
|
||||
[@expected].flatten.compact
|
||||
end
|
||||
|
34
lib/webrat/core/nokogiri.rb
Normal file
34
lib/webrat/core/nokogiri.rb
Normal file
@ -0,0 +1,34 @@
|
||||
module Webrat
|
||||
|
||||
def self.nokogiri_document(stringlike)
|
||||
return stringlike.dom if stringlike.respond_to?(:dom)
|
||||
|
||||
if stringlike === Nokogiri::HTML::Document || stringlike === Nokogiri::XML::NodeSet
|
||||
stringlike
|
||||
elsif stringlike === StringIO
|
||||
Nokogiri::HTML(stringlike.string)
|
||||
elsif stringlike.respond_to?(:body)
|
||||
Nokogiri::HTML(stringlike.body.to_s)
|
||||
else
|
||||
Nokogiri::HTML(stringlike.to_s)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
|
||||
module Nokogiri
|
||||
module CSS
|
||||
class XPathVisitor
|
||||
|
||||
def visit_pseudo_class_text(node)
|
||||
"@type='text'"
|
||||
end
|
||||
|
||||
def visit_pseudo_class_password(node)
|
||||
"@type='password'"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
@ -169,8 +169,8 @@ module Webrat
|
||||
def page_dom
|
||||
return @response.dom if @response.respond_to?(:dom)
|
||||
|
||||
dom = Nokogiri::HTML(@response_body)
|
||||
|
||||
dom = Webrat.nokogiri_document(@response_body)
|
||||
|
||||
@response.meta_class.send(:define_method, :dom) do
|
||||
dom
|
||||
end
|
||||
@ -179,7 +179,7 @@ module Webrat
|
||||
end
|
||||
|
||||
def scoped_dom
|
||||
Nokogiri::HTML(@scope.dom.search(@selector).first.to_html)
|
||||
Webrat.nokogiri_document(@scope.dom.search(@selector).first.to_html)
|
||||
end
|
||||
|
||||
def locate_field(field_locator, *field_types)
|
||||
|
@ -1,15 +0,0 @@
|
||||
module Nokogiri
|
||||
module CSS
|
||||
class XPathVisitor
|
||||
|
||||
def visit_pseudo_class_text(node)
|
||||
"@type='text'"
|
||||
end
|
||||
|
||||
def visit_pseudo_class_password(node)
|
||||
"@type='password'"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
@ -3,12 +3,12 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
|
||||
module Webrat
|
||||
describe CheckboxField do
|
||||
it "should say it is checked if it is" do
|
||||
checkbox = CheckboxField.new(nil, (Nokogiri::HTML("<input type='checkbox' checked='checked'>")/'input').first)
|
||||
checkbox = CheckboxField.new(nil, (Webrat.nokogiri_document("<input type='checkbox' checked='checked'>")/'input').first)
|
||||
checkbox.should be_checked
|
||||
end
|
||||
|
||||
it "should say it is not checked if it is not" do
|
||||
checkbox = CheckboxField.new(nil, (Nokogiri::HTML("<input type='checkbox'>")/'input').first)
|
||||
checkbox = CheckboxField.new(nil, (Webrat.nokogiri_document("<input type='checkbox'>")/'input').first)
|
||||
checkbox.should_not be_checked
|
||||
end
|
||||
end
|
||||
|
Loading…
Reference in New Issue
Block a user