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/logging"
|
||||||
require "webrat/core/flunk"
|
require "webrat/core/flunk"
|
||||||
require "webrat/core/form"
|
require "webrat/core/form"
|
||||||
|
|
|
@ -3,15 +3,15 @@ module Webrat
|
||||||
|
|
||||||
class HasContent
|
class HasContent
|
||||||
def initialize(content)
|
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
|
begin
|
||||||
require "nokogiri"
|
require "nokogiri"
|
||||||
require "webrat/nokogiri"
|
require "webrat/core/nokogiri"
|
||||||
rescue LoadError => e
|
rescue LoadError => e
|
||||||
if require "rexml/document"
|
require "rexml/document"
|
||||||
require "webrat/vendor/nokogiri/css"
|
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
|
||||||
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@content = content
|
@content = content
|
||||||
|
@ -38,7 +38,7 @@ module Webrat
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches_nokogiri?(stringlike)
|
def matches_nokogiri?(stringlike)
|
||||||
@document = nokogiri_document(stringlike)
|
@document = Webrat.nokogiri_document(stringlike)
|
||||||
@element = @document.inner_text
|
@element = @document.inner_text
|
||||||
|
|
||||||
case @content
|
case @content
|
||||||
|
@ -70,20 +70,6 @@ module Webrat
|
||||||
end
|
end
|
||||||
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
|
# ==== Returns
|
||||||
# String:: The failure message.
|
# String:: The failure message.
|
||||||
def failure_message
|
def failure_message
|
||||||
|
|
|
@ -3,15 +3,16 @@ module Webrat
|
||||||
|
|
||||||
class HaveXpath
|
class HaveXpath
|
||||||
def initialize(expected, &block)
|
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
|
# Require nokogiri and fall back on rexml
|
||||||
begin
|
begin
|
||||||
require "nokogiri"
|
require "nokogiri"
|
||||||
require "webrat/nokogiri"
|
require "webrat/core/nokogiri"
|
||||||
rescue LoadError => e
|
rescue LoadError => e
|
||||||
if require "rexml/document"
|
require "rexml/document"
|
||||||
require "webrat/vendor/nokogiri/css"
|
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
|
||||||
warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"")
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
@expected = expected
|
@expected = expected
|
||||||
|
@ -36,7 +37,7 @@ module Webrat
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches_nokogiri?(stringlike)
|
def matches_nokogiri?(stringlike)
|
||||||
@document = nokogiri_document(stringlike)
|
@document = Webrat.nokogiri_document(stringlike)
|
||||||
@document.xpath(*query).any?
|
@document.xpath(*query).any?
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -61,20 +62,6 @@ module Webrat
|
||||||
end
|
end
|
||||||
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
|
def query
|
||||||
[@expected].flatten.compact
|
[@expected].flatten.compact
|
||||||
end
|
end
|
||||||
|
|
|
@ -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
|
def page_dom
|
||||||
return @response.dom if @response.respond_to?(: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
|
@response.meta_class.send(:define_method, :dom) do
|
||||||
dom
|
dom
|
||||||
end
|
end
|
||||||
|
@ -179,7 +179,7 @@ module Webrat
|
||||||
end
|
end
|
||||||
|
|
||||||
def scoped_dom
|
def scoped_dom
|
||||||
Nokogiri::HTML(@scope.dom.search(@selector).first.to_html)
|
Webrat.nokogiri_document(@scope.dom.search(@selector).first.to_html)
|
||||||
end
|
end
|
||||||
|
|
||||||
def locate_field(field_locator, *field_types)
|
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
|
module Webrat
|
||||||
describe CheckboxField do
|
describe CheckboxField do
|
||||||
it "should say it is checked if it is" 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
|
checkbox.should be_checked
|
||||||
end
|
end
|
||||||
|
|
||||||
it "should say it is not checked if it is not" do
|
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
|
checkbox.should_not be_checked
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue