From c01f44a32cd2f6f798d8a89c9eba888a5607a912 Mon Sep 17 00:00:00 2001 From: Bryan Helmkamp Date: Fri, 7 Nov 2008 02:48:48 -0500 Subject: [PATCH] Refactoring nokogiri usage --- lib/webrat/core.rb | 1 + lib/webrat/core/matchers/have_content.rb | 28 +++++-------------- lib/webrat/core/matchers/have_xpath.rb | 27 +++++-------------- lib/webrat/core/nokogiri.rb | 34 ++++++++++++++++++++++++ lib/webrat/core/scope.rb | 6 ++--- lib/webrat/nokogiri.rb | 15 ----------- spec/webrat/core/field_spec.rb | 4 +-- 7 files changed, 54 insertions(+), 61 deletions(-) create mode 100644 lib/webrat/core/nokogiri.rb delete mode 100644 lib/webrat/nokogiri.rb diff --git a/lib/webrat/core.rb b/lib/webrat/core.rb index 04e1d48..cce4704 100644 --- a/lib/webrat/core.rb +++ b/lib/webrat/core.rb @@ -1,3 +1,4 @@ +require "webrat/core/nokogiri" require "webrat/core/logging" require "webrat/core/flunk" require "webrat/core/form" diff --git a/lib/webrat/core/matchers/have_content.rb b/lib/webrat/core/matchers/have_content.rb index a979742..895d33b 100644 --- a/lib/webrat/core/matchers/have_content.rb +++ b/lib/webrat/core/matchers/have_content.rb @@ -3,15 +3,15 @@ module Webrat class HasContent 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 require "nokogiri" - require "webrat/nokogiri" + require "webrat/core/nokogiri" rescue LoadError => e - if require "rexml/document" - require "webrat/vendor/nokogiri/css" - warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"") - end + require "rexml/document" + warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"") end @content = content @@ -38,7 +38,7 @@ module Webrat end def matches_nokogiri?(stringlike) - @document = nokogiri_document(stringlike) + @document = Webrat.nokogiri_document(stringlike) @element = @document.inner_text case @content @@ -70,20 +70,6 @@ module Webrat 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 # String:: The failure message. def failure_message diff --git a/lib/webrat/core/matchers/have_xpath.rb b/lib/webrat/core/matchers/have_xpath.rb index 58c4506..8fd23d8 100644 --- a/lib/webrat/core/matchers/have_xpath.rb +++ b/lib/webrat/core/matchers/have_xpath.rb @@ -3,15 +3,16 @@ module Webrat class HaveXpath 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 begin require "nokogiri" - require "webrat/nokogiri" + require "webrat/core/nokogiri" rescue LoadError => e - if require "rexml/document" - require "webrat/vendor/nokogiri/css" - warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"") - end + require "rexml/document" + warn("Standard REXML library is slow. Please consider installing nokogiri.\nUse \"sudo gem install nokogiri\"") end @expected = expected @@ -36,7 +37,7 @@ module Webrat end def matches_nokogiri?(stringlike) - @document = nokogiri_document(stringlike) + @document = Webrat.nokogiri_document(stringlike) @document.xpath(*query).any? end @@ -61,20 +62,6 @@ module Webrat 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 [@expected].flatten.compact end diff --git a/lib/webrat/core/nokogiri.rb b/lib/webrat/core/nokogiri.rb new file mode 100644 index 0000000..30bd858 --- /dev/null +++ b/lib/webrat/core/nokogiri.rb @@ -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 \ No newline at end of file diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 366289b..4fb7736 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -169,8 +169,8 @@ module Webrat def page_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 dom end @@ -179,7 +179,7 @@ module Webrat end def scoped_dom - Nokogiri::HTML(@scope.dom.search(@selector).first.to_html) + Webrat.nokogiri_document(@scope.dom.search(@selector).first.to_html) end def locate_field(field_locator, *field_types) diff --git a/lib/webrat/nokogiri.rb b/lib/webrat/nokogiri.rb deleted file mode 100644 index b0aa7c5..0000000 --- a/lib/webrat/nokogiri.rb +++ /dev/null @@ -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 \ No newline at end of file diff --git a/spec/webrat/core/field_spec.rb b/spec/webrat/core/field_spec.rb index 609ea35..84172d4 100644 --- a/spec/webrat/core/field_spec.rb +++ b/spec/webrat/core/field_spec.rb @@ -3,12 +3,12 @@ require File.expand_path(File.dirname(__FILE__) + "/../../spec_helper") module Webrat describe CheckboxField do it "should say it is checked if it is" do - checkbox = CheckboxField.new(nil, (Nokogiri::HTML("")/'input').first) + checkbox = CheckboxField.new(nil, (Webrat.nokogiri_document("")/'input').first) checkbox.should be_checked end it "should say it is not checked if it is not" do - checkbox = CheckboxField.new(nil, (Nokogiri::HTML("")/'input').first) + checkbox = CheckboxField.new(nil, (Webrat.nokogiri_document("")/'input').first) checkbox.should_not be_checked end end