Use REXML and Hpricot together when Nokogiri is not available
This gets us the ability to use XPath consistently, as well as Hpricot's forgiving parser, when using JRuby
This commit is contained in:
parent
18e65bfa44
commit
87211f260d
@ -11,7 +11,7 @@ module Webrat
|
|||||||
end
|
end
|
||||||
|
|
||||||
def text
|
def text
|
||||||
str = Webrat::XML.inner_text(@element)
|
str = Webrat::XML.all_inner_text(@element)
|
||||||
str.gsub!("\n","")
|
str.gsub!("\n","")
|
||||||
str.strip!
|
str.strip!
|
||||||
str.squeeze!(" ")
|
str.squeeze!(" ")
|
||||||
|
@ -44,7 +44,7 @@ module Webrat
|
|||||||
end
|
end
|
||||||
|
|
||||||
def text
|
def text
|
||||||
Webrat::XML.inner_text(@element)
|
Webrat::XML.all_inner_text(@element)
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
@ -4,7 +4,7 @@ module Webrat
|
|||||||
module Locators
|
module Locators
|
||||||
|
|
||||||
def field_by_xpath(xpath)
|
def field_by_xpath(xpath)
|
||||||
element = dom.at(xpath)
|
element = Webrat::XML.xpath_search(dom, xpath).first
|
||||||
|
|
||||||
forms.detect_mapped do |form|
|
forms.detect_mapped do |form|
|
||||||
form.field_by_element(element)
|
form.field_by_element(element)
|
||||||
|
@ -7,7 +7,12 @@ module Webrat
|
|||||||
end
|
end
|
||||||
|
|
||||||
def matches?(stringlike)
|
def matches?(stringlike)
|
||||||
@document = Webrat::XML.document(stringlike)
|
if Webrat.configuration.parse_with_nokogiri?
|
||||||
|
@document = Webrat.nokogiri_document(stringlike)
|
||||||
|
else
|
||||||
|
@document = Webrat::XML.hpricot_document(stringlike)
|
||||||
|
end
|
||||||
|
|
||||||
@element = Webrat::XML.inner_text(@document)
|
@element = Webrat::XML.inner_text(@document)
|
||||||
|
|
||||||
case @content
|
case @content
|
||||||
|
@ -289,7 +289,10 @@ module Webrat
|
|||||||
end
|
end
|
||||||
|
|
||||||
def scoped_dom #:nodoc:
|
def scoped_dom #:nodoc:
|
||||||
Webrat::XML.document(Webrat::XML.css_search(@scope.dom, @selector).first.to_html)
|
# if @selector == "#form2"
|
||||||
|
# require "rubygems"; require "ruby-debug"; Debugger.start; debugger
|
||||||
|
# end
|
||||||
|
Webrat::XML.document("<html>" + Webrat::XML.to_html(Webrat::XML.css_search(@scope.dom, @selector).first) + "</html>")
|
||||||
end
|
end
|
||||||
|
|
||||||
def locate_field(field_locator, *field_types) #:nodoc:
|
def locate_field(field_locator, *field_types) #:nodoc:
|
||||||
|
@ -5,8 +5,8 @@ module Webrat #:nodoc:
|
|||||||
if Webrat.configuration.parse_with_nokogiri?
|
if Webrat.configuration.parse_with_nokogiri?
|
||||||
Webrat.nokogiri_document(stringlike)
|
Webrat.nokogiri_document(stringlike)
|
||||||
else
|
else
|
||||||
Webrat::XML.hpricot_document(stringlike)
|
# Webrat::XML.hpricot_document(stringlike)
|
||||||
# Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html)
|
Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -26,39 +26,71 @@ module Webrat #:nodoc:
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.to_html(element)
|
||||||
|
if Webrat.configuration.parse_with_nokogiri?
|
||||||
|
element.to_html
|
||||||
|
else
|
||||||
|
element.to_s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def self.inner_html(element)
|
def self.inner_html(element)
|
||||||
|
if Webrat.configuration.parse_with_nokogiri?
|
||||||
element.inner_html
|
element.inner_html
|
||||||
|
else
|
||||||
|
element.text
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def self.all_inner_text(element)
|
||||||
|
if Webrat.configuration.parse_with_nokogiri?
|
||||||
|
element.inner_text
|
||||||
|
else
|
||||||
|
Hpricot(element.to_s).children.first.inner_text
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.inner_text(element)
|
def self.inner_text(element)
|
||||||
|
if Webrat.configuration.parse_with_nokogiri?
|
||||||
element.inner_text
|
element.inner_text
|
||||||
|
else
|
||||||
|
if defined?(Hpricot::Doc) && element.is_a?(Hpricot::Doc)
|
||||||
|
element.inner_text
|
||||||
|
else
|
||||||
|
element.text
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.attribute(element, attribute_name)
|
def self.attribute(element, attribute_name)
|
||||||
# case element
|
return element[attribute_name] if element.is_a?(Hash)
|
||||||
# when Nokogiri::XML::Element, Hash
|
|
||||||
|
if Webrat.configuration.parse_with_nokogiri?
|
||||||
element[attribute_name]
|
element[attribute_name]
|
||||||
# else
|
else
|
||||||
# element.attributes[attribute_name]
|
element.attributes[attribute_name]
|
||||||
# end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.xpath_search(element, *searches)
|
def self.xpath_search(element, *searches)
|
||||||
searches.flatten.map do |search|
|
searches.flatten.map do |search|
|
||||||
# REXML::XPath.match(element, search)
|
if Webrat.configuration.parse_with_nokogiri?
|
||||||
element.xpath(search)
|
element.xpath(search)
|
||||||
|
else
|
||||||
|
REXML::XPath.match(element, search)
|
||||||
|
end
|
||||||
end.flatten.compact
|
end.flatten.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.css_search(element, *searches) #:nodoc:
|
def self.css_search(element, *searches) #:nodoc:
|
||||||
if Webrat.configuration.parse_with_nokogiri?
|
# if Webrat.configuration.parse_with_nokogiri?
|
||||||
xpath_search(element, css_to_xpath(*searches))
|
xpath_search(element, css_to_xpath(*searches))
|
||||||
# element.css(*searches)
|
# element.css(*searches)
|
||||||
else
|
# else
|
||||||
searches.map do |search|
|
# searches.map do |search|
|
||||||
element.search(search)
|
# element.search(search)
|
||||||
end.flatten.compact
|
# end.flatten.compact
|
||||||
end
|
# end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.css_to_xpath(*selectors)
|
def self.css_to_xpath(*selectors)
|
||||||
|
@ -12,6 +12,10 @@ require File.expand_path(File.dirname(__FILE__) + "/fakes/test_session")
|
|||||||
|
|
||||||
require "merb-core"
|
require "merb-core"
|
||||||
|
|
||||||
|
Webrat.configure do |config|
|
||||||
|
config.parse_with_nokogiri = false
|
||||||
|
end
|
||||||
|
|
||||||
Spec::Runner.configure do |config|
|
Spec::Runner.configure do |config|
|
||||||
include Webrat::Methods
|
include Webrat::Methods
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user