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:
Bryan Helmkamp 2008-11-28 02:02:17 -05:00
parent 18e65bfa44
commit 87211f260d
9 changed files with 91 additions and 47 deletions

View File

@ -11,7 +11,7 @@ module Webrat
end
def text
str = Webrat::XML.inner_text(@element)
str = Webrat::XML.all_inner_text(@element)
str.gsub!("\n","")
str.strip!
str.squeeze!(" ")

View File

@ -44,7 +44,7 @@ module Webrat
end
def text
Webrat::XML.inner_text(@element)
Webrat::XML.all_inner_text(@element)
end
protected

View File

@ -4,7 +4,7 @@ module Webrat
module Locators
def field_by_xpath(xpath)
element = dom.at(xpath)
element = Webrat::XML.xpath_search(dom, xpath).first
forms.detect_mapped do |form|
form.field_by_element(element)

View File

@ -7,7 +7,12 @@ module Webrat
end
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)
case @content

View File

@ -289,7 +289,10 @@ module Webrat
end
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
def locate_field(field_locator, *field_types) #:nodoc:

View File

@ -5,8 +5,8 @@ module Webrat #:nodoc:
if Webrat.configuration.parse_with_nokogiri?
Webrat.nokogiri_document(stringlike)
else
Webrat::XML.hpricot_document(stringlike)
# Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html)
# Webrat::XML.hpricot_document(stringlike)
Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html)
end
end
@ -26,39 +26,71 @@ module Webrat #:nodoc:
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)
element.inner_html
if Webrat.configuration.parse_with_nokogiri?
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
def self.inner_text(element)
element.inner_text
if Webrat.configuration.parse_with_nokogiri?
element.inner_text
else
if defined?(Hpricot::Doc) && element.is_a?(Hpricot::Doc)
element.inner_text
else
element.text
end
end
end
def self.attribute(element, attribute_name)
# case element
# when Nokogiri::XML::Element, Hash
return element[attribute_name] if element.is_a?(Hash)
if Webrat.configuration.parse_with_nokogiri?
element[attribute_name]
# else
# element.attributes[attribute_name]
# end
else
element.attributes[attribute_name]
end
end
def self.xpath_search(element, *searches)
searches.flatten.map do |search|
# REXML::XPath.match(element, search)
element.xpath(search)
if Webrat.configuration.parse_with_nokogiri?
element.xpath(search)
else
REXML::XPath.match(element, search)
end
end.flatten.compact
end
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))
# element.css(*searches)
else
searches.map do |search|
element.search(search)
end.flatten.compact
end
# else
# searches.map do |search|
# element.search(search)
# end.flatten.compact
# end
end
def self.css_to_xpath(*selectors)

View File

@ -120,13 +120,13 @@ describe "field_labeled" do
describe "finding a field with it's label containing newlines" do
using_this_html <<-HTML
<html>
<form>
<label for="element_42">
A label with
<a>a link on it's own line</a>
</label>
<input type="text" id="element_42">
</form>
<form>
<label for="element_42">
A label with
<a>a link on it's own line</a>
</label>
<input type="text" id="element_42">
</form>
</html>
HTML

View File

@ -40,14 +40,14 @@ describe "within" do
it "should submit forms within a scope" do
with_html <<-HTML
<html>
<form id="form1" action="/form1">
<label>Email: <input type="text" name="email" />
<input type="submit" value="Add" />
</form>
<form id="form2" action="/form2">
<label>Email: <input type="text" name="email" />
<input type="submit" value="Add" />
</form>
<form id="form1" action="/form1">
<label>Email: <input type="text" name="email" />
<input type="submit" value="Add" />
</form>
<form id="form2" action="/form2">
<label>Email: <input type="text" name="email" />
<input type="submit" value="Add" />
</form>
</html>
HTML

View File

@ -12,6 +12,10 @@ require File.expand_path(File.dirname(__FILE__) + "/fakes/test_session")
require "merb-core"
Webrat.configure do |config|
config.parse_with_nokogiri = false
end
Spec::Runner.configure do |config|
include Webrat::Methods