Dropping support for Hpricot and REXML

This commit is contained in:
Bryan Helmkamp 2009-08-19 22:43:23 -04:00
parent 997ff97405
commit 11f30d1d2e
31 changed files with 156 additions and 355 deletions

View File

@ -1,5 +1,12 @@
== Git == Git
REMOVED: Support for Hpricot + REXML as an alternative to Nokogiri.
Hpricot and REXML were difficult to work with, REXML is terribly slow,
and Nokogiri is recommended even by the author of Hpricot (_why). Now
that Nokogiri works on JRuby, Webrat is going to use it as it's sole
XML backend.
* Minor enhancements * Minor enhancements
* Update the Merb support to be based directly on Rack (Simon Rozet) * Update the Merb support to be based directly on Rack (Simon Rozet)

View File

@ -1,10 +1,10 @@
require "rack"
require "nokogiri"
module Webrat module Webrat
# The common base class for all exceptions raised by Webrat. # The common base class for all exceptions raised by Webrat.
class WebratError < StandardError class WebratError < StandardError
end end
end end
require "rack"
require "nokogiri"
require "webrat/core/xml/nokogiri"
require "webrat/core" require "webrat/core"

View File

@ -1,6 +1,5 @@
require "webrat/core/configuration" require "webrat/core/configuration"
require "webrat/core/xml" require "webrat/core/xml"
require "webrat/core/xml/nokogiri"
require "webrat/core/logging" require "webrat/core/logging"
require "webrat/core/elements/form" require "webrat/core/elements/form"
require "webrat/core/scope" require "webrat/core/scope"

View File

@ -16,13 +16,10 @@ module Webrat
# Webrat can be configured using the Webrat.configure method. For example: # Webrat can be configured using the Webrat.configure method. For example:
# #
# Webrat.configure do |config| # Webrat.configure do |config|
# config.parse_with_nokogiri = false # config.mode = :sinatra
# end # end
class Configuration class Configuration
# Should XHTML be parsed with Nokogiri? Defaults to true, except on JRuby. When false, Hpricot and REXML are used
attr_writer :parse_with_nokogiri
# Webrat's mode, set automatically when requiring webrat/rails, webrat/merb, etc. # Webrat's mode, set automatically when requiring webrat/rails, webrat/merb, etc.
attr_reader :mode # :nodoc: attr_reader :mode # :nodoc:
@ -63,7 +60,6 @@ module Webrat
def initialize # :nodoc: def initialize # :nodoc:
self.open_error_files = true self.open_error_files = true
self.parse_with_nokogiri = true
self.application_environment = :test self.application_environment = :test
self.application_port = 3001 self.application_port = 3001
self.application_address = 'localhost' self.application_address = 'localhost'
@ -74,10 +70,6 @@ module Webrat
self.selenium_browser_startup_timeout = 5 self.selenium_browser_startup_timeout = 5
end end
def parse_with_nokogiri? #:nodoc:
@parse_with_nokogiri ? true : false
end
def open_error_files? #:nodoc: def open_error_files? #:nodoc:
@open_error_files ? true : false @open_error_files ? true : false
end end

View File

@ -4,7 +4,7 @@ module Webrat
class Area < Element #:nodoc: class Area < Element #:nodoc:
def self.xpath_search def self.xpath_search
".//area" [".//area"]
end end
def click(method = nil, options = {}) def click(method = nil, options = {})
@ -14,7 +14,7 @@ module Webrat
protected protected
def href def href
Webrat::XML.attribute(@element, "href") @element["href"]
end end
def absolute_href def absolute_href

View File

@ -3,14 +3,14 @@ module Webrat
class Element # :nodoc: class Element # :nodoc:
def self.load_all(session, dom) def self.load_all(session, dom)
Webrat::XML.xpath_search(dom, xpath_search).map do |element| dom.xpath(*xpath_search).map do |element|
load(session, element) load(session, element)
end end
end end
def self.load(session, element) def self.load(session, element)
return nil if element.nil? return nil if element.nil?
session.elements[Webrat::XML.xpath_to(element)] ||= self.new(session, element) session.elements[element.path] ||= self.new(session, element)
end end
attr_reader :element attr_reader :element
@ -21,7 +21,7 @@ module Webrat
end end
def path def path
Webrat::XML.xpath_to(@element) @element.path
end end
def inspect def inspect

View File

@ -32,7 +32,7 @@ module Webrat
def self.load(session, element) def self.load(session, element)
return nil if element.nil? return nil if element.nil?
session.elements[Webrat::XML.xpath_to(element)] ||= field_class(element).new(session, element) session.elements[element.path] ||= field_class(element).new(session, element)
end end
def self.field_class(element) def self.field_class(element)
@ -41,7 +41,7 @@ module Webrat
when "select" then SelectField when "select" then SelectField
when "textarea" then TextareaField when "textarea" then TextareaField
else else
case Webrat::XML.attribute(element, "type") case element["type"]
when "checkbox" then CheckboxField when "checkbox" then CheckboxField
when "hidden" then HiddenField when "hidden" then HiddenField
when "radio" then RadioField when "radio" then RadioField
@ -67,11 +67,11 @@ module Webrat
end end
def id def id
Webrat::XML.attribute(@element, "id") @element["id"]
end end
def disabled? def disabled?
@element.attributes.has_key?("disabled") && Webrat::XML.attribute(@element, "disabled") != 'false' @element.attributes.has_key?("disabled") && @element["disabled"] != 'false'
end end
def raise_error_if_disabled def raise_error_if_disabled
@ -128,7 +128,7 @@ module Webrat
end end
def name def name
Webrat::XML.attribute(@element, "name") @element["name"]
end end
def escaped_value def escaped_value
@ -155,14 +155,14 @@ module Webrat
end end
unless id.blank? unless id.blank?
@label_elements += Webrat::XML.xpath_search(form.element, ".//label[@for = '#{id}']") @label_elements += form.element.xpath(".//label[@for = '#{id}']")
end end
@label_elements @label_elements
end end
def default_value def default_value
Webrat::XML.attribute(@element, "value") @element["value"]
end end
def replace_param_value(params, oval, nval) def replace_param_value(params, oval, nval)
@ -199,7 +199,7 @@ module Webrat
def click def click
raise_error_if_disabled raise_error_if_disabled
set(Webrat::XML.attribute(@element, "value")) unless Webrat::XML.attribute(@element, "name").blank? set(@element["value"]) unless @element["name"].blank?
form.submit form.submit
end end
@ -246,11 +246,11 @@ module Webrat
def check def check
raise_error_if_disabled raise_error_if_disabled
set(Webrat::XML.attribute(@element, "value") || "on") set(@element["value"] || "on")
end end
def checked? def checked?
Webrat::XML.attribute(@element, "checked") == "checked" @element["checked"] == "checked"
end end
def uncheck def uncheck
@ -261,8 +261,8 @@ module Webrat
protected protected
def default_value def default_value
if Webrat::XML.attribute(@element, "checked") == "checked" if @element["checked"] == "checked"
Webrat::XML.attribute(@element, "value") || "on" @element["value"] || "on"
else else
nil nil
end end
@ -295,11 +295,11 @@ module Webrat
option.set(nil) option.set(nil)
end end
set(Webrat::XML.attribute(@element, "value") || "on") set(@element["value"] || "on")
end end
def checked? def checked?
Webrat::XML.attribute(@element, "checked") == "checked" @element["checked"] == "checked"
end end
protected protected
@ -309,8 +309,8 @@ module Webrat
end end
def default_value def default_value
if Webrat::XML.attribute(@element, "checked") == "checked" if @element["checked"] == "checked"
Webrat::XML.attribute(@element, "value") || "on" @element["value"] || "on"
else else
nil nil
end end
@ -327,7 +327,7 @@ module Webrat
protected protected
def default_value def default_value
Webrat::XML.inner_html(@element) @element.inner_html
end end
end end
@ -378,14 +378,14 @@ module Webrat
class ResetField < Field #:nodoc: class ResetField < Field #:nodoc:
def self.xpath_search def self.xpath_search
".//input[@type = 'reset']" [".//input[@type = 'reset']"]
end end
end end
class SelectField < Field #:nodoc: class SelectField < Field #:nodoc:
def self.xpath_search def self.xpath_search
".//select" [".//select"]
end end
def options def options
@ -395,12 +395,12 @@ module Webrat
protected protected
def default_value def default_value
selected_options = Webrat::XML.xpath_search(@element, ".//option[@selected = 'selected']") selected_options = @element.xpath(".//option[@selected = 'selected']")
selected_options = Webrat::XML.xpath_search(@element, ".//option[position() = 1]") if selected_options.empty? selected_options = @element.xpath(".//option[position() = 1]") if selected_options.empty?
selected_options.map do |option| selected_options.map do |option|
return "" if option.nil? return "" if option.nil?
Webrat::XML.attribute(option, "value") || Webrat::XML.inner_html(option) option["value"] || option.inner_html
end.uniq end.uniq
end end

View File

@ -9,7 +9,7 @@ module Webrat
attr_reader :element attr_reader :element
def self.xpath_search def self.xpath_search
".//form" [".//form"]
end end
def fields def fields
@ -27,7 +27,7 @@ module Webrat
protected protected
def dom def dom
Webrat::XML.xpath_at(@session.dom, path) @session.dom.xpath(path).first
end end
def fields_by_type(field_types) def fields_by_type(field_types)
@ -50,11 +50,11 @@ module Webrat
end end
def form_method def form_method
Webrat::XML.attribute(@element, "method").blank? ? :get : Webrat::XML.attribute(@element, "method").downcase @element["method"].blank? ? :get : @element["method"].downcase
end end
def form_action def form_action
Webrat::XML.attribute(@element, "action").blank? ? @session.current_url : Webrat::XML.attribute(@element, "action") @element["action"].blank? ? @session.current_url : @element["action"]
end end
def merge(all_params, new_param) def merge(all_params, new_param)

View File

@ -6,11 +6,11 @@ module Webrat
attr_reader :element attr_reader :element
def self.xpath_search def self.xpath_search
".//label" [".//label"]
end end
def for_id def for_id
Webrat::XML.attribute(@element, "for") @element["for"]
end end
def field def field
@ -21,9 +21,9 @@ module Webrat
def field_element def field_element
if for_id.blank? if for_id.blank?
Webrat::XML.xpath_at(@element, *Field.xpath_search_excluding_hidden) @element.xpath(*Field.xpath_search_excluding_hidden).first
else else
Webrat::XML.css_search(@session.current_dom, "#" + for_id).first @session.current_dom.css("#" + for_id).first
end end
end end

View File

@ -7,7 +7,7 @@ module Webrat
class Link < Element #:nodoc: class Link < Element #:nodoc:
def self.xpath_search def self.xpath_search
".//a[@href]" [".//a[@href]"]
end end
def click(options = {}) def click(options = {})
@ -26,7 +26,7 @@ module Webrat
protected protected
def id def id
Webrat::XML.attribute(@element, "id") @element["id"]
end end
def data def data
@ -34,11 +34,11 @@ module Webrat
end end
def title def title
Webrat::XML.attribute(@element, "title") @element["title"]
end end
def href def href
Webrat::XML.attribute(@element, "href") @element["href"]
end end
def absolute_href def absolute_href
@ -58,7 +58,7 @@ module Webrat
end end
def onclick def onclick
Webrat::XML.attribute(@element, "onclick") @element["onclick"]
end end
def http_method def http_method

View File

@ -4,7 +4,7 @@ module Webrat
class SelectOption < Element #:nodoc: class SelectOption < Element #:nodoc:
def self.xpath_search def self.xpath_search
".//option" [".//option"]
end end
def choose def choose
@ -28,7 +28,7 @@ module Webrat
end end
def value def value
Webrat::XML.attribute(@element, "value") || Webrat::XML.inner_html(@element) @element["value"] || @element.inner_html
end end
end end

View File

@ -13,7 +13,7 @@ module Webrat
module Locators module Locators
def field_by_xpath(xpath) def field_by_xpath(xpath)
Field.load(@session, Webrat::XML.xpath_at(dom, xpath)) Field.load(@session, dom.xpath(xpath).first)
end end
end end

View File

@ -11,8 +11,8 @@ module Webrat
def area_element def area_element
area_elements.detect do |area_element| area_elements.detect do |area_element|
Webrat::XML.attribute(area_element, "title") =~ matcher || area_element["title"] =~ matcher ||
Webrat::XML.attribute(area_element, "id") =~ matcher area_element["id"] =~ matcher
end end
end end
@ -21,7 +21,7 @@ module Webrat
end end
def area_elements def area_elements
Webrat::XML.xpath_search(@dom, Area.xpath_search) @dom.xpath(*Area.xpath_search)
end end
def error_message def error_message

View File

@ -20,24 +20,24 @@ module Webrat
end end
def matches_id?(element) def matches_id?(element)
(@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") =~ @value) || (@value.is_a?(Regexp) && element["id"] =~ @value) ||
(!@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") == @value.to_s) (!@value.is_a?(Regexp) && element["id"] == @value.to_s)
end end
def matches_value?(element) def matches_value?(element)
Webrat::XML.attribute(element, "value") =~ /^\W*#{Regexp.escape(@value.to_s)}/i element["value"] =~ /^\W*#{Regexp.escape(@value.to_s)}/i
end end
def matches_html?(element) def matches_html?(element)
Webrat::XML.inner_html(element) =~ /#{Regexp.escape(@value.to_s)}/i element.inner_html =~ /#{Regexp.escape(@value.to_s)}/i
end end
def matches_alt?(element) def matches_alt?(element)
Webrat::XML.attribute(element, "alt") =~ /^\W*#{Regexp.escape(@value.to_s)}/i element["alt"] =~ /^\W*#{Regexp.escape(@value.to_s)}/i
end end
def button_elements def button_elements
Webrat::XML.xpath_search(@dom, *ButtonField.xpath_search) @dom.xpath(*ButtonField.xpath_search)
end end
def error_message def error_message

View File

@ -12,15 +12,15 @@ module Webrat
def field_element def field_element
field_elements.detect do |field_element| field_elements.detect do |field_element|
if @value.is_a?(Regexp) if @value.is_a?(Regexp)
Webrat::XML.attribute(field_element, "id") =~ @value field_element["id"] =~ @value
else else
Webrat::XML.attribute(field_element, "id") == @value.to_s field_element["id"] == @value.to_s
end end
end end
end end
def field_elements def field_elements
Webrat::XML.xpath_search(@dom, *Field.xpath_search) @dom.xpath(*Field.xpath_search)
end end
def error_message def error_message

View File

@ -25,7 +25,7 @@ module Webrat
end end
def label_elements def label_elements
Webrat::XML.xpath_search(@dom, Label.xpath_search) @dom.xpath(*Label.xpath_search)
end end
def error_message def error_message
@ -33,7 +33,7 @@ module Webrat
end end
def text(element) def text(element)
str = Webrat::XML.all_inner_text(element) str = element.inner_text
str.gsub!("\n","") str.gsub!("\n","")
str.strip! str.strip!
str.squeeze!(" ") str.squeeze!(" ")

View File

@ -11,19 +11,19 @@ module Webrat
def field_element def field_element
field_elements.detect do |field_element| field_elements.detect do |field_element|
Webrat::XML.attribute(field_element, "name") == @value.to_s field_element["name"] == @value.to_s
end end
end end
def field_elements def field_elements
Webrat::XML.xpath_search(@dom, *xpath_searches) @dom.xpath(*xpath_searches)
end end
def xpath_searches def xpath_searches
if @field_types.any? if @field_types.any?
@field_types.map { |field_type| field_type.xpath_search }.flatten @field_types.map { |field_type| field_type.xpath_search }.flatten
else else
Array(Field.xpath_search) Field.xpath_search
end end
end end

View File

@ -10,7 +10,7 @@ module Webrat
end end
def form_element def form_element
Webrat::XML.css_at(@dom, "#" + @value) @dom.css("#" + @value).first
end end
end end

View File

@ -17,11 +17,11 @@ module Webrat
end end
def label_elements def label_elements
Webrat::XML.xpath_search(@dom, Label.xpath_search) @dom.xpath(*Label.xpath_search)
end end
def text(label_element) def text(label_element)
str = Webrat::XML.all_inner_text(label_element) str = label_element.inner_text
str.gsub!("\n","") str.gsub!("\n","")
str.strip! str.strip!
str.squeeze!(" ") str.squeeze!(" ")

View File

@ -10,7 +10,7 @@ module Webrat
end end
def link_element def link_element
matching_links.min { |a, b| Webrat::XML.all_inner_text(a).length <=> Webrat::XML.all_inner_text(b).length } matching_links.min { |a, b| a.inner_text.length <=> b.inner_text.length }
end end
def matching_links def matching_links
@ -27,21 +27,21 @@ module Webrat
matcher = /#{Regexp.escape(@value.to_s)}/i matcher = /#{Regexp.escape(@value.to_s)}/i
end end
replace_nbsp(Webrat::XML.all_inner_text(link)) =~ matcher || replace_nbsp(link.inner_text) =~ matcher ||
replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher || replace_nbsp_ref(link.inner_html) =~ matcher ||
Webrat::XML.attribute(link, "title")=~ matcher link["title"] =~ matcher
end end
def matches_id?(link) def matches_id?(link)
if @value.is_a?(Regexp) if @value.is_a?(Regexp)
(Webrat::XML.attribute(link, "id") =~ @value) ? true : false link["id"] =~ @value ? true : false
else else
(Webrat::XML.attribute(link, "id") == @value) ? true : false link["id"] == @value ? true : false
end end
end end
def link_elements def link_elements
Webrat::XML.xpath_search(@dom, *Link.xpath_search) @dom.xpath(*Link.xpath_search)
end end
def replace_nbsp(str) def replace_nbsp(str)

View File

@ -19,17 +19,17 @@ module Webrat
field.options.detect do |o| field.options.detect do |o|
if @option_text.is_a?(Regexp) if @option_text.is_a?(Regexp)
Webrat::XML.inner_html(o.element) =~ @option_text o.element.inner_html =~ @option_text
else else
Webrat::XML.inner_html(o.element) == @option_text.to_s o.element.inner_html == @option_text.to_s
end end
end end
else else
option_element = option_elements.detect do |o| option_element = option_elements.detect do |o|
if @option_text.is_a?(Regexp) if @option_text.is_a?(Regexp)
Webrat::XML.inner_html(o) =~ @option_text o.inner_html =~ @option_text
else else
Webrat::XML.inner_html(o) == @option_text.to_s o.inner_html == @option_text.to_s
end end
end end
@ -38,7 +38,7 @@ module Webrat
end end
def option_elements def option_elements
Webrat::XML.xpath_search(@dom, *SelectOption.xpath_search) @dom.xpath(*SelectOption.xpath_search)
end end
def error_message def error_message

View File

@ -7,13 +7,8 @@ module Webrat
end end
def matches?(stringlike) def matches?(stringlike)
if Webrat.configuration.parse_with_nokogiri? @document = Webrat::XML.document(stringlike)
@document = Webrat.nokogiri_document(stringlike) @element = @document.inner_text
else
@document = Webrat.hpricot_document(stringlike)
end
@element = Webrat::XML.inner_text(@document)
case @content case @content
when String when String

View File

@ -1,5 +1,4 @@
require "webrat/core/xml/nokogiri" require "webrat/core/xml"
require "webrat/core/xml/rexml"
module Webrat module Webrat
module Matchers module Matchers
@ -23,31 +22,7 @@ module Webrat
end end
def matches(stringlike) def matches(stringlike)
if Webrat.configuration.parse_with_nokogiri? nokogiri_matches(stringlike)
nokogiri_matches(stringlike)
else
rexml_matches(stringlike)
end
end
def rexml_matches(stringlike)
if REXML::Node === stringlike || Array === stringlike
@query = query.map { |q| q.gsub(%r'^//', './/') }
else
@query = query
end
add_options_conditions_to(@query)
@document = Webrat.rexml_document(stringlike)
@query.map do |q|
if @document.is_a?(Array)
@document.map { |d| REXML::XPath.match(d, q) }
else
REXML::XPath.match(@document, q)
end
end.flatten.compact
end end
def nokogiri_matches(stringlike) def nokogiri_matches(stringlike)

View File

@ -312,12 +312,12 @@ module Webrat
dom = Webrat::XML.html_document(@response_body) dom = Webrat::XML.html_document(@response_body)
end end
Webrat.define_dom_method(@response, dom) Webrat::XML.define_dom_method(@response, dom)
return dom return dom
end end
def scoped_dom def scoped_dom
Webrat::XML.css_at(@scope.dom, @selector) @scope.dom.css(@selector).first
end end
def locate_field(field_locator, *field_types) #:nodoc: def locate_field(field_locator, *field_types) #:nodoc:

View File

@ -1,115 +1,78 @@
require "webrat/core/xml/nokogiri" require "webrat/core_extensions/meta_class"
require "webrat/core/xml/hpricot"
require "webrat/core/xml/rexml"
module Webrat #:nodoc: module Webrat #:nodoc:
module XML #:nodoc: module XML #:nodoc:
def self.document(stringlike) #:nodoc: def self.document(stringlike) #:nodoc:
if Webrat.configuration.parse_with_nokogiri? return stringlike.dom if stringlike.respond_to?(:dom)
Webrat.nokogiri_document(stringlike)
if Nokogiri::HTML::Document === stringlike
stringlike
elsif Nokogiri::XML::NodeSet === stringlike
stringlike
elsif StringIO === stringlike
Nokogiri::HTML(stringlike.string)
elsif stringlike.respond_to?(:body)
Nokogiri::HTML(stringlike.body.to_s)
else else
Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html) Nokogiri::HTML(stringlike.to_s)
end end
end end
def self.html_document(stringlike) #:nodoc: def self.html_document(stringlike) #:nodoc:
if Webrat.configuration.parse_with_nokogiri? return stringlike.dom if stringlike.respond_to?(:dom)
Webrat.html_nokogiri_document(stringlike)
if Nokogiri::HTML::Document === stringlike
stringlike
elsif Nokogiri::XML::NodeSet === stringlike
stringlike
elsif StringIO === stringlike
Nokogiri::HTML(stringlike.string)
elsif stringlike.respond_to?(:body)
Nokogiri::HTML(stringlike.body.to_s)
else else
Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html) Nokogiri::HTML(stringlike.to_s)
end end
end end
def self.xml_document(stringlike) #:nodoc: def self.xml_document(stringlike) #:nodoc:
if Webrat.configuration.parse_with_nokogiri? return stringlike.dom if stringlike.respond_to?(:dom)
Webrat.xml_nokogiri_document(stringlike)
if Nokogiri::HTML::Document === stringlike
stringlike
elsif Nokogiri::XML::NodeSet === stringlike
stringlike
elsif StringIO === stringlike
Nokogiri::XML(stringlike.string)
elsif stringlike.respond_to?(:body)
Nokogiri::XML(stringlike.body.to_s)
else else
Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html) Nokogiri::XML(stringlike.to_s)
end end
end end
def self.to_html(element) def self.define_dom_method(object, dom) #:nodoc:
if Webrat.configuration.parse_with_nokogiri? object.meta_class.send(:define_method, :dom) do
element.to_html dom
else
element.to_s
end end
end end
def self.inner_html(element)
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)
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.xpath_to(element)
if Webrat.configuration.parse_with_nokogiri?
element.path
else
element.xpath
end
end
def self.attribute(element, attribute_name)
return element[attribute_name] if element.is_a?(Hash)
if Webrat.configuration.parse_with_nokogiri?
element[attribute_name]
else
element.attributes[attribute_name]
end
end
def self.xpath_at(*args)
xpath_search(*args).first
end
def self.css_at(*args)
css_search(*args).first
end
def self.xpath_search(element, *searches)
searches.flatten.map do |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:
xpath_search(element, css_to_xpath(*searches))
end
def self.css_to_xpath(*selectors)
selectors.map do |rule|
Nokogiri::CSS.xpath_for(rule, :prefix => ".//")
end.flatten.uniq
end
end end
end end
module Nokogiri #:nodoc:
module CSS #:nodoc:
class XPathVisitor #:nodoc:
def visit_pseudo_class_text(node) #:nodoc:
"@type='text'"
end
def visit_pseudo_class_password(node) #:nodoc:
"@type='password'"
end
end
end
end

View File

@ -1,19 +0,0 @@
module Webrat
def self.hpricot_document(stringlike)
return stringlike.dom if stringlike.respond_to?(:dom)
if Hpricot::Doc === stringlike
stringlike
elsif Hpricot::Elements === stringlike
stringlike
elsif StringIO === stringlike
Hpricot(stringlike.string)
elsif stringlike.respond_to?(:body)
Hpricot(stringlike.body.to_s)
else
Hpricot(stringlike.to_s)
end
end
end

View File

@ -1,76 +0,0 @@
require "webrat/core_extensions/meta_class"
module Webrat
def self.nokogiri_document(stringlike) #:nodoc:
return stringlike.dom if stringlike.respond_to?(:dom)
if Nokogiri::HTML::Document === stringlike
stringlike
elsif Nokogiri::XML::NodeSet === stringlike
stringlike
elsif StringIO === stringlike
Nokogiri::HTML(stringlike.string)
elsif stringlike.respond_to?(:body)
Nokogiri::HTML(stringlike.body.to_s)
else
Nokogiri::HTML(stringlike.to_s)
end
end
def self.html_nokogiri_document(stringlike) #:nodoc:
return stringlike.dom if stringlike.respond_to?(:dom)
if Nokogiri::HTML::Document === stringlike
stringlike
elsif Nokogiri::XML::NodeSet === stringlike
stringlike
elsif StringIO === stringlike
Nokogiri::HTML(stringlike.string)
elsif stringlike.respond_to?(:body)
Nokogiri::HTML(stringlike.body.to_s)
else
Nokogiri::HTML(stringlike.to_s)
end
end
def self.xml_nokogiri_document(stringlike) #:nodoc:
return stringlike.dom if stringlike.respond_to?(:dom)
if Nokogiri::HTML::Document === stringlike
stringlike
elsif Nokogiri::XML::NodeSet === stringlike
stringlike
elsif StringIO === stringlike
Nokogiri::XML(stringlike.string)
elsif stringlike.respond_to?(:body)
Nokogiri::XML(stringlike.body.to_s)
else
Nokogiri::XML(stringlike.to_s)
end
end
def self.define_dom_method(object, dom) #:nodoc:
object.meta_class.send(:define_method, :dom) do
dom
end
end
end
module Nokogiri #:nodoc:
module CSS #:nodoc:
class XPathVisitor #:nodoc:
def visit_pseudo_class_text(node) #:nodoc:
"@type='text'"
end
def visit_pseudo_class_password(node) #:nodoc:
"@type='password'"
end
end
end
end

View File

@ -1,24 +0,0 @@
module Webrat
def self.rexml_document(stringlike)
stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)
case stringlike
when REXML::Document
stringlike.root
when REXML::Node, Array
stringlike
else
begin
REXML::Document.new(stringlike.to_s).root
rescue REXML::ParseException => e
if e.message.include?("second root element")
REXML::Document.new("<fake-root-element>#{stringlike}</fake-root-element>").root
else
raise e
end
end
end
end
end

View File

@ -2,12 +2,6 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper")
describe Webrat::Configuration do describe Webrat::Configuration do
Spec::Matchers.define :parse_with_nokogiri do
match do |config|
config.parse_with_nokogiri?
end
end
Spec::Matchers.define :open_error_files do Spec::Matchers.define :open_error_files do
match do |config| match do |config|
config.open_error_files? config.open_error_files?
@ -18,11 +12,6 @@ describe Webrat::Configuration do
Webrat.configuration.should respond_to(:mode) Webrat.configuration.should respond_to(:mode)
end end
it "should use Nokogiri as the parser by default" do
config = Webrat::Configuration.new
config.should parse_with_nokogiri
end
it "should open error files by default" do it "should open error files by default" do
config = Webrat::Configuration.new config = Webrat::Configuration.new
config.should open_error_files config.should open_error_files

View File

@ -10,7 +10,7 @@ module Webrat
</html> </html>
HTML HTML
element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first element = Webrat::XML.document(html).css("input").first
checkbox = CheckboxField.new(nil, element) checkbox = CheckboxField.new(nil, element)
checkbox.inspect.should =~ /#<Webrat::CheckboxField @element=<input type=['"]checkbox['"] checked(=['"]checked['"])?\/?>>/ checkbox.inspect.should =~ /#<Webrat::CheckboxField @element=<input type=['"]checkbox['"] checked(=['"]checked['"])?\/?>>/
end end
@ -25,7 +25,7 @@ module Webrat
</html> </html>
HTML HTML
element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first element = Webrat::XML.document(html).css("input").first
checkbox = CheckboxField.new(nil, element) checkbox = CheckboxField.new(nil, element)
checkbox.should be_checked checkbox.should be_checked
end end
@ -37,7 +37,7 @@ module Webrat
</html> </html>
HTML HTML
element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first element = Webrat::XML.document(html).css("input").first
checkbox = CheckboxField.new(nil, element) checkbox = CheckboxField.new(nil, element)
checkbox.should_not be_checked checkbox.should_not be_checked
end end
@ -51,7 +51,7 @@ module Webrat
</html> </html>
HTML HTML
element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first element = Webrat::XML.document(html).css("input").first
radio_button = RadioField.new(nil, element) radio_button = RadioField.new(nil, element)
radio_button.should be_checked radio_button.should be_checked
end end
@ -61,7 +61,7 @@ module Webrat
<html><input type='radio' /></html> <html><input type='radio' /></html>
HTML HTML
element = Webrat::XML.css_search(Webrat::XML.document(html), "input").first element = Webrat::XML.document(html).css("input").first
radio_button = RadioField.new(nil, element) radio_button = RadioField.new(nil, element)
radio_button.should_not be_checked radio_button.should_not be_checked
end end
@ -77,7 +77,7 @@ module Webrat
</html> </html>
HTML HTML
element = Webrat::XML.css_search(Webrat::XML.document(html), 'input').first element = Webrat::XML.document(html).css('input').first
text_field = TextField.new(nil, element) text_field = TextField.new(nil, element)
text_field.to_param.should == { 'email' => 'user@example.com' } text_field.to_param.should == { 'email' => 'user@example.com' }
end end

View File

@ -1,6 +1,6 @@
require File.expand_path(File.dirname(__FILE__) + "/../spec_helper") require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
if defined?(Nokogiri::XML) && Webrat.configuration.parse_with_nokogiri? # if defined?(Nokogiri::XML) && Webrat.configuration.parse_with_nokogiri?
describe "Nokogiri Extension" do describe "Nokogiri Extension" do
include Webrat::Matchers include Webrat::Matchers
@ -74,4 +74,4 @@ if defined?(Nokogiri::XML) && Webrat.configuration.parse_with_nokogiri?
end end
end end
end end
end # end