From 11f30d1d2e6b4cf49bf8990b80f47a6f85ea14ae Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Wed, 19 Aug 2009 22:43:23 -0400 Subject: [PATCH] Dropping support for Hpricot and REXML --- History.txt | 7 + lib/webrat.rb | 6 +- lib/webrat/core.rb | 1 - lib/webrat/core/configuration.rb | 10 +- lib/webrat/core/elements/area.rb | 4 +- lib/webrat/core/elements/element.rb | 6 +- lib/webrat/core/elements/field.rb | 44 +++--- lib/webrat/core/elements/form.rb | 8 +- lib/webrat/core/elements/label.rb | 8 +- lib/webrat/core/elements/link.rb | 10 +- lib/webrat/core/elements/select_option.rb | 4 +- lib/webrat/core/locators.rb | 2 +- lib/webrat/core/locators/area_locator.rb | 6 +- lib/webrat/core/locators/button_locator.rb | 12 +- .../core/locators/field_by_id_locator.rb | 6 +- .../core/locators/field_labeled_locator.rb | 4 +- .../core/locators/field_named_locator.rb | 6 +- lib/webrat/core/locators/form_locator.rb | 2 +- lib/webrat/core/locators/label_locator.rb | 4 +- lib/webrat/core/locators/link_locator.rb | 14 +- .../core/locators/select_option_locator.rb | 10 +- lib/webrat/core/matchers/have_content.rb | 9 +- lib/webrat/core/matchers/have_xpath.rb | 29 +--- lib/webrat/core/scope.rb | 4 +- lib/webrat/core/xml.rb | 145 +++++++----------- lib/webrat/core/xml/hpricot.rb | 19 --- lib/webrat/core/xml/nokogiri.rb | 76 --------- lib/webrat/core/xml/rexml.rb | 24 --- spec/private/core/configuration_spec.rb | 15 +- spec/private/core/field_spec.rb | 12 +- spec/private/nokogiri_spec.rb | 4 +- 31 files changed, 156 insertions(+), 355 deletions(-) delete mode 100644 lib/webrat/core/xml/hpricot.rb delete mode 100644 lib/webrat/core/xml/nokogiri.rb delete mode 100644 lib/webrat/core/xml/rexml.rb diff --git a/History.txt b/History.txt index 2da27d1..358c45a 100644 --- a/History.txt +++ b/History.txt @@ -1,5 +1,12 @@ == 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 * Update the Merb support to be based directly on Rack (Simon Rozet) diff --git a/lib/webrat.rb b/lib/webrat.rb index 93e1177..bd823de 100644 --- a/lib/webrat.rb +++ b/lib/webrat.rb @@ -1,10 +1,10 @@ +require "rack" +require "nokogiri" + module Webrat # The common base class for all exceptions raised by Webrat. class WebratError < StandardError end end -require "rack" -require "nokogiri" -require "webrat/core/xml/nokogiri" require "webrat/core" diff --git a/lib/webrat/core.rb b/lib/webrat/core.rb index 5d8a57e..5b89604 100644 --- a/lib/webrat/core.rb +++ b/lib/webrat/core.rb @@ -1,6 +1,5 @@ require "webrat/core/configuration" require "webrat/core/xml" -require "webrat/core/xml/nokogiri" require "webrat/core/logging" require "webrat/core/elements/form" require "webrat/core/scope" diff --git a/lib/webrat/core/configuration.rb b/lib/webrat/core/configuration.rb index c035246..f969373 100755 --- a/lib/webrat/core/configuration.rb +++ b/lib/webrat/core/configuration.rb @@ -16,13 +16,10 @@ module Webrat # Webrat can be configured using the Webrat.configure method. For example: # # Webrat.configure do |config| - # config.parse_with_nokogiri = false + # config.mode = :sinatra # end 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. attr_reader :mode # :nodoc: @@ -63,7 +60,6 @@ module Webrat def initialize # :nodoc: self.open_error_files = true - self.parse_with_nokogiri = true self.application_environment = :test self.application_port = 3001 self.application_address = 'localhost' @@ -74,10 +70,6 @@ module Webrat self.selenium_browser_startup_timeout = 5 end - def parse_with_nokogiri? #:nodoc: - @parse_with_nokogiri ? true : false - end - def open_error_files? #:nodoc: @open_error_files ? true : false end diff --git a/lib/webrat/core/elements/area.rb b/lib/webrat/core/elements/area.rb index e2f08bb..9832450 100644 --- a/lib/webrat/core/elements/area.rb +++ b/lib/webrat/core/elements/area.rb @@ -4,7 +4,7 @@ module Webrat class Area < Element #:nodoc: def self.xpath_search - ".//area" + [".//area"] end def click(method = nil, options = {}) @@ -14,7 +14,7 @@ module Webrat protected def href - Webrat::XML.attribute(@element, "href") + @element["href"] end def absolute_href diff --git a/lib/webrat/core/elements/element.rb b/lib/webrat/core/elements/element.rb index 59dd268..7c8c9ba 100644 --- a/lib/webrat/core/elements/element.rb +++ b/lib/webrat/core/elements/element.rb @@ -3,14 +3,14 @@ module Webrat class Element # :nodoc: 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) end end def self.load(session, element) 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 attr_reader :element @@ -21,7 +21,7 @@ module Webrat end def path - Webrat::XML.xpath_to(@element) + @element.path end def inspect diff --git a/lib/webrat/core/elements/field.rb b/lib/webrat/core/elements/field.rb index c2812f3..91c9243 100644 --- a/lib/webrat/core/elements/field.rb +++ b/lib/webrat/core/elements/field.rb @@ -32,7 +32,7 @@ module Webrat def self.load(session, element) 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 def self.field_class(element) @@ -41,7 +41,7 @@ module Webrat when "select" then SelectField when "textarea" then TextareaField else - case Webrat::XML.attribute(element, "type") + case element["type"] when "checkbox" then CheckboxField when "hidden" then HiddenField when "radio" then RadioField @@ -67,11 +67,11 @@ module Webrat end def id - Webrat::XML.attribute(@element, "id") + @element["id"] end def disabled? - @element.attributes.has_key?("disabled") && Webrat::XML.attribute(@element, "disabled") != 'false' + @element.attributes.has_key?("disabled") && @element["disabled"] != 'false' end def raise_error_if_disabled @@ -128,7 +128,7 @@ module Webrat end def name - Webrat::XML.attribute(@element, "name") + @element["name"] end def escaped_value @@ -155,14 +155,14 @@ module Webrat end unless id.blank? - @label_elements += Webrat::XML.xpath_search(form.element, ".//label[@for = '#{id}']") + @label_elements += form.element.xpath(".//label[@for = '#{id}']") end @label_elements end def default_value - Webrat::XML.attribute(@element, "value") + @element["value"] end def replace_param_value(params, oval, nval) @@ -199,7 +199,7 @@ module Webrat def click 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 end @@ -246,11 +246,11 @@ module Webrat def check raise_error_if_disabled - set(Webrat::XML.attribute(@element, "value") || "on") + set(@element["value"] || "on") end def checked? - Webrat::XML.attribute(@element, "checked") == "checked" + @element["checked"] == "checked" end def uncheck @@ -261,8 +261,8 @@ module Webrat protected def default_value - if Webrat::XML.attribute(@element, "checked") == "checked" - Webrat::XML.attribute(@element, "value") || "on" + if @element["checked"] == "checked" + @element["value"] || "on" else nil end @@ -295,11 +295,11 @@ module Webrat option.set(nil) end - set(Webrat::XML.attribute(@element, "value") || "on") + set(@element["value"] || "on") end def checked? - Webrat::XML.attribute(@element, "checked") == "checked" + @element["checked"] == "checked" end protected @@ -309,8 +309,8 @@ module Webrat end def default_value - if Webrat::XML.attribute(@element, "checked") == "checked" - Webrat::XML.attribute(@element, "value") || "on" + if @element["checked"] == "checked" + @element["value"] || "on" else nil end @@ -327,7 +327,7 @@ module Webrat protected def default_value - Webrat::XML.inner_html(@element) + @element.inner_html end end @@ -378,14 +378,14 @@ module Webrat class ResetField < Field #:nodoc: def self.xpath_search - ".//input[@type = 'reset']" + [".//input[@type = 'reset']"] end end class SelectField < Field #:nodoc: def self.xpath_search - ".//select" + [".//select"] end def options @@ -395,12 +395,12 @@ module Webrat protected def default_value - selected_options = Webrat::XML.xpath_search(@element, ".//option[@selected = 'selected']") - selected_options = Webrat::XML.xpath_search(@element, ".//option[position() = 1]") if selected_options.empty? + selected_options = @element.xpath(".//option[@selected = 'selected']") + selected_options = @element.xpath(".//option[position() = 1]") if selected_options.empty? selected_options.map do |option| return "" if option.nil? - Webrat::XML.attribute(option, "value") || Webrat::XML.inner_html(option) + option["value"] || option.inner_html end.uniq end diff --git a/lib/webrat/core/elements/form.rb b/lib/webrat/core/elements/form.rb index 801eeb8..21d710b 100644 --- a/lib/webrat/core/elements/form.rb +++ b/lib/webrat/core/elements/form.rb @@ -9,7 +9,7 @@ module Webrat attr_reader :element def self.xpath_search - ".//form" + [".//form"] end def fields @@ -27,7 +27,7 @@ module Webrat protected def dom - Webrat::XML.xpath_at(@session.dom, path) + @session.dom.xpath(path).first end def fields_by_type(field_types) @@ -50,11 +50,11 @@ module Webrat end def form_method - Webrat::XML.attribute(@element, "method").blank? ? :get : Webrat::XML.attribute(@element, "method").downcase + @element["method"].blank? ? :get : @element["method"].downcase end 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 def merge(all_params, new_param) diff --git a/lib/webrat/core/elements/label.rb b/lib/webrat/core/elements/label.rb index 8f9b952..9e97f83 100644 --- a/lib/webrat/core/elements/label.rb +++ b/lib/webrat/core/elements/label.rb @@ -6,11 +6,11 @@ module Webrat attr_reader :element def self.xpath_search - ".//label" + [".//label"] end def for_id - Webrat::XML.attribute(@element, "for") + @element["for"] end def field @@ -21,9 +21,9 @@ module Webrat def field_element if for_id.blank? - Webrat::XML.xpath_at(@element, *Field.xpath_search_excluding_hidden) + @element.xpath(*Field.xpath_search_excluding_hidden).first else - Webrat::XML.css_search(@session.current_dom, "#" + for_id).first + @session.current_dom.css("#" + for_id).first end end diff --git a/lib/webrat/core/elements/link.rb b/lib/webrat/core/elements/link.rb index 66395f5..5dfe504 100644 --- a/lib/webrat/core/elements/link.rb +++ b/lib/webrat/core/elements/link.rb @@ -7,7 +7,7 @@ module Webrat class Link < Element #:nodoc: def self.xpath_search - ".//a[@href]" + [".//a[@href]"] end def click(options = {}) @@ -26,7 +26,7 @@ module Webrat protected def id - Webrat::XML.attribute(@element, "id") + @element["id"] end def data @@ -34,11 +34,11 @@ module Webrat end def title - Webrat::XML.attribute(@element, "title") + @element["title"] end def href - Webrat::XML.attribute(@element, "href") + @element["href"] end def absolute_href @@ -58,7 +58,7 @@ module Webrat end def onclick - Webrat::XML.attribute(@element, "onclick") + @element["onclick"] end def http_method diff --git a/lib/webrat/core/elements/select_option.rb b/lib/webrat/core/elements/select_option.rb index 91f6830..c0763b4 100644 --- a/lib/webrat/core/elements/select_option.rb +++ b/lib/webrat/core/elements/select_option.rb @@ -4,7 +4,7 @@ module Webrat class SelectOption < Element #:nodoc: def self.xpath_search - ".//option" + [".//option"] end def choose @@ -28,7 +28,7 @@ module Webrat end def value - Webrat::XML.attribute(@element, "value") || Webrat::XML.inner_html(@element) + @element["value"] || @element.inner_html end end diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index 20f56d4..4609de5 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -13,7 +13,7 @@ module Webrat module Locators def field_by_xpath(xpath) - Field.load(@session, Webrat::XML.xpath_at(dom, xpath)) + Field.load(@session, dom.xpath(xpath).first) end end diff --git a/lib/webrat/core/locators/area_locator.rb b/lib/webrat/core/locators/area_locator.rb index 93900c4..c730eb7 100644 --- a/lib/webrat/core/locators/area_locator.rb +++ b/lib/webrat/core/locators/area_locator.rb @@ -11,8 +11,8 @@ module Webrat def area_element area_elements.detect do |area_element| - Webrat::XML.attribute(area_element, "title") =~ matcher || - Webrat::XML.attribute(area_element, "id") =~ matcher + area_element["title"] =~ matcher || + area_element["id"] =~ matcher end end @@ -21,7 +21,7 @@ module Webrat end def area_elements - Webrat::XML.xpath_search(@dom, Area.xpath_search) + @dom.xpath(*Area.xpath_search) end def error_message diff --git a/lib/webrat/core/locators/button_locator.rb b/lib/webrat/core/locators/button_locator.rb index e07e4d7..021898c 100644 --- a/lib/webrat/core/locators/button_locator.rb +++ b/lib/webrat/core/locators/button_locator.rb @@ -20,24 +20,24 @@ module Webrat end def matches_id?(element) - (@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") =~ @value) || - (!@value.is_a?(Regexp) && Webrat::XML.attribute(element, "id") == @value.to_s) + (@value.is_a?(Regexp) && element["id"] =~ @value) || + (!@value.is_a?(Regexp) && element["id"] == @value.to_s) end 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 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 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 def button_elements - Webrat::XML.xpath_search(@dom, *ButtonField.xpath_search) + @dom.xpath(*ButtonField.xpath_search) end def error_message diff --git a/lib/webrat/core/locators/field_by_id_locator.rb b/lib/webrat/core/locators/field_by_id_locator.rb index f7e5cd3..403d8a6 100644 --- a/lib/webrat/core/locators/field_by_id_locator.rb +++ b/lib/webrat/core/locators/field_by_id_locator.rb @@ -12,15 +12,15 @@ module Webrat def field_element field_elements.detect do |field_element| if @value.is_a?(Regexp) - Webrat::XML.attribute(field_element, "id") =~ @value + field_element["id"] =~ @value else - Webrat::XML.attribute(field_element, "id") == @value.to_s + field_element["id"] == @value.to_s end end end def field_elements - Webrat::XML.xpath_search(@dom, *Field.xpath_search) + @dom.xpath(*Field.xpath_search) end def error_message diff --git a/lib/webrat/core/locators/field_labeled_locator.rb b/lib/webrat/core/locators/field_labeled_locator.rb index 8071be2..791f968 100644 --- a/lib/webrat/core/locators/field_labeled_locator.rb +++ b/lib/webrat/core/locators/field_labeled_locator.rb @@ -25,7 +25,7 @@ module Webrat end def label_elements - Webrat::XML.xpath_search(@dom, Label.xpath_search) + @dom.xpath(*Label.xpath_search) end def error_message @@ -33,7 +33,7 @@ module Webrat end def text(element) - str = Webrat::XML.all_inner_text(element) + str = element.inner_text str.gsub!("\n","") str.strip! str.squeeze!(" ") diff --git a/lib/webrat/core/locators/field_named_locator.rb b/lib/webrat/core/locators/field_named_locator.rb index 37601aa..88cb4ea 100644 --- a/lib/webrat/core/locators/field_named_locator.rb +++ b/lib/webrat/core/locators/field_named_locator.rb @@ -11,19 +11,19 @@ module Webrat def 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 def field_elements - Webrat::XML.xpath_search(@dom, *xpath_searches) + @dom.xpath(*xpath_searches) end def xpath_searches if @field_types.any? @field_types.map { |field_type| field_type.xpath_search }.flatten else - Array(Field.xpath_search) + Field.xpath_search end end diff --git a/lib/webrat/core/locators/form_locator.rb b/lib/webrat/core/locators/form_locator.rb index e9858e9..43707d2 100644 --- a/lib/webrat/core/locators/form_locator.rb +++ b/lib/webrat/core/locators/form_locator.rb @@ -10,7 +10,7 @@ module Webrat end def form_element - Webrat::XML.css_at(@dom, "#" + @value) + @dom.css("#" + @value).first end end diff --git a/lib/webrat/core/locators/label_locator.rb b/lib/webrat/core/locators/label_locator.rb index 19a40e6..e7ed9f5 100644 --- a/lib/webrat/core/locators/label_locator.rb +++ b/lib/webrat/core/locators/label_locator.rb @@ -17,11 +17,11 @@ module Webrat end def label_elements - Webrat::XML.xpath_search(@dom, Label.xpath_search) + @dom.xpath(*Label.xpath_search) end def text(label_element) - str = Webrat::XML.all_inner_text(label_element) + str = label_element.inner_text str.gsub!("\n","") str.strip! str.squeeze!(" ") diff --git a/lib/webrat/core/locators/link_locator.rb b/lib/webrat/core/locators/link_locator.rb index a2e92a2..13dd9fd 100644 --- a/lib/webrat/core/locators/link_locator.rb +++ b/lib/webrat/core/locators/link_locator.rb @@ -10,7 +10,7 @@ module Webrat end 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 def matching_links @@ -27,21 +27,21 @@ module Webrat matcher = /#{Regexp.escape(@value.to_s)}/i end - replace_nbsp(Webrat::XML.all_inner_text(link)) =~ matcher || - replace_nbsp_ref(Webrat::XML.inner_html(link)) =~ matcher || - Webrat::XML.attribute(link, "title")=~ matcher + replace_nbsp(link.inner_text) =~ matcher || + replace_nbsp_ref(link.inner_html) =~ matcher || + link["title"] =~ matcher end def matches_id?(link) if @value.is_a?(Regexp) - (Webrat::XML.attribute(link, "id") =~ @value) ? true : false + link["id"] =~ @value ? true : false else - (Webrat::XML.attribute(link, "id") == @value) ? true : false + link["id"] == @value ? true : false end end def link_elements - Webrat::XML.xpath_search(@dom, *Link.xpath_search) + @dom.xpath(*Link.xpath_search) end def replace_nbsp(str) diff --git a/lib/webrat/core/locators/select_option_locator.rb b/lib/webrat/core/locators/select_option_locator.rb index 5605cb1..674ad1a 100644 --- a/lib/webrat/core/locators/select_option_locator.rb +++ b/lib/webrat/core/locators/select_option_locator.rb @@ -19,17 +19,17 @@ module Webrat field.options.detect do |o| if @option_text.is_a?(Regexp) - Webrat::XML.inner_html(o.element) =~ @option_text + o.element.inner_html =~ @option_text else - Webrat::XML.inner_html(o.element) == @option_text.to_s + o.element.inner_html == @option_text.to_s end end else option_element = option_elements.detect do |o| if @option_text.is_a?(Regexp) - Webrat::XML.inner_html(o) =~ @option_text + o.inner_html =~ @option_text else - Webrat::XML.inner_html(o) == @option_text.to_s + o.inner_html == @option_text.to_s end end @@ -38,7 +38,7 @@ module Webrat end def option_elements - Webrat::XML.xpath_search(@dom, *SelectOption.xpath_search) + @dom.xpath(*SelectOption.xpath_search) end def error_message diff --git a/lib/webrat/core/matchers/have_content.rb b/lib/webrat/core/matchers/have_content.rb index e31fd36..c3b6f33 100644 --- a/lib/webrat/core/matchers/have_content.rb +++ b/lib/webrat/core/matchers/have_content.rb @@ -7,13 +7,8 @@ module Webrat end def matches?(stringlike) - if Webrat.configuration.parse_with_nokogiri? - @document = Webrat.nokogiri_document(stringlike) - else - @document = Webrat.hpricot_document(stringlike) - end - - @element = Webrat::XML.inner_text(@document) + @document = Webrat::XML.document(stringlike) + @element = @document.inner_text case @content when String diff --git a/lib/webrat/core/matchers/have_xpath.rb b/lib/webrat/core/matchers/have_xpath.rb index efc9a8d..58b2fff 100644 --- a/lib/webrat/core/matchers/have_xpath.rb +++ b/lib/webrat/core/matchers/have_xpath.rb @@ -1,5 +1,4 @@ -require "webrat/core/xml/nokogiri" -require "webrat/core/xml/rexml" +require "webrat/core/xml" module Webrat module Matchers @@ -23,31 +22,7 @@ module Webrat end def matches(stringlike) - if Webrat.configuration.parse_with_nokogiri? - 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 + nokogiri_matches(stringlike) end def nokogiri_matches(stringlike) diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index cc52ac7..6c66b87 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -312,12 +312,12 @@ module Webrat dom = Webrat::XML.html_document(@response_body) end - Webrat.define_dom_method(@response, dom) + Webrat::XML.define_dom_method(@response, dom) return dom end def scoped_dom - Webrat::XML.css_at(@scope.dom, @selector) + @scope.dom.css(@selector).first end def locate_field(field_locator, *field_types) #:nodoc: diff --git a/lib/webrat/core/xml.rb b/lib/webrat/core/xml.rb index 9abe13b..463b764 100644 --- a/lib/webrat/core/xml.rb +++ b/lib/webrat/core/xml.rb @@ -1,115 +1,78 @@ -require "webrat/core/xml/nokogiri" -require "webrat/core/xml/hpricot" -require "webrat/core/xml/rexml" +require "webrat/core_extensions/meta_class" module Webrat #:nodoc: module XML #:nodoc: def self.document(stringlike) #:nodoc: - if Webrat.configuration.parse_with_nokogiri? - Webrat.nokogiri_document(stringlike) + 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 - Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html) + Nokogiri::HTML(stringlike.to_s) end end def self.html_document(stringlike) #:nodoc: - if Webrat.configuration.parse_with_nokogiri? - Webrat.html_nokogiri_document(stringlike) + 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 - Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html) + Nokogiri::HTML(stringlike.to_s) end end def self.xml_document(stringlike) #:nodoc: - if Webrat.configuration.parse_with_nokogiri? - Webrat.xml_nokogiri_document(stringlike) + 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 - Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html) + Nokogiri::XML(stringlike.to_s) end end - def self.to_html(element) - if Webrat.configuration.parse_with_nokogiri? - element.to_html - else - element.to_s + def self.define_dom_method(object, dom) #:nodoc: + object.meta_class.send(:define_method, :dom) do + dom 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 + +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 + diff --git a/lib/webrat/core/xml/hpricot.rb b/lib/webrat/core/xml/hpricot.rb deleted file mode 100644 index 8402e4a..0000000 --- a/lib/webrat/core/xml/hpricot.rb +++ /dev/null @@ -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 diff --git a/lib/webrat/core/xml/nokogiri.rb b/lib/webrat/core/xml/nokogiri.rb deleted file mode 100644 index a473916..0000000 --- a/lib/webrat/core/xml/nokogiri.rb +++ /dev/null @@ -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 diff --git a/lib/webrat/core/xml/rexml.rb b/lib/webrat/core/xml/rexml.rb deleted file mode 100644 index cfcd45e..0000000 --- a/lib/webrat/core/xml/rexml.rb +++ /dev/null @@ -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("#{stringlike}").root - else - raise e - end - end - end - end - -end diff --git a/spec/private/core/configuration_spec.rb b/spec/private/core/configuration_spec.rb index 5511ec3..7db3f51 100755 --- a/spec/private/core/configuration_spec.rb +++ b/spec/private/core/configuration_spec.rb @@ -1,28 +1,17 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") 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 match do |config| config.open_error_files? end end - + it "should have a mode" do Webrat.configuration.should respond_to(:mode) 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 config = Webrat::Configuration.new config.should open_error_files diff --git a/spec/private/core/field_spec.rb b/spec/private/core/field_spec.rb index 654f8c1..58174c1 100644 --- a/spec/private/core/field_spec.rb +++ b/spec/private/core/field_spec.rb @@ -10,7 +10,7 @@ module Webrat 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.inspect.should =~ /#>/ end @@ -25,7 +25,7 @@ module Webrat 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.should be_checked end @@ -37,7 +37,7 @@ module Webrat 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.should_not be_checked end @@ -51,7 +51,7 @@ module Webrat 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.should be_checked end @@ -61,7 +61,7 @@ module Webrat 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.should_not be_checked end @@ -77,7 +77,7 @@ module Webrat 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.to_param.should == { 'email' => 'user@example.com' } end diff --git a/spec/private/nokogiri_spec.rb b/spec/private/nokogiri_spec.rb index 0cdafb8..82f8cbc 100644 --- a/spec/private/nokogiri_spec.rb +++ b/spec/private/nokogiri_spec.rb @@ -1,6 +1,6 @@ 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 include Webrat::Matchers @@ -74,4 +74,4 @@ if defined?(Nokogiri::XML) && Webrat.configuration.parse_with_nokogiri? end end end -end +# end