diff --git a/lib/webrat/core/matchers/have_content.rb b/lib/webrat/core/matchers/have_content.rb
index 24ded0a..a979742 100644
--- a/lib/webrat/core/matchers/have_content.rb
+++ b/lib/webrat/core/matchers/have_content.rb
@@ -3,12 +3,31 @@ module Webrat
class HasContent
def initialize(content)
+ # Require nokogiri and fall back on rexml
+ begin
+ require "nokogiri"
+ require "webrat/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
+ end
+
@content = content
end
-
- def matches?(element)
- element = element.body.to_s if element.respond_to?(:body)
- @element = element
+
+ def matches?(stringlike)
+ if defined?(Nokogiri::XML)
+ matches_nokogiri?(stringlike)
+ else
+ matches_rexml?(stringlike)
+ end
+ end
+
+ def matches_rexml?(stringlike)
+ @document = rexml_document(stringlike)
+ @element = @document.inner_text
case @content
when String
@@ -18,16 +37,63 @@ module Webrat
end
end
+ def matches_nokogiri?(stringlike)
+ @document = nokogiri_document(stringlike)
+ @element = @document.inner_text
+
+ case @content
+ when String
+ @element.include?(@content)
+ when Regexp
+ @element.match(@content)
+ end
+ end
+
+ def rexml_document(stringlike)
+ stringlike = stringlike.body.to_s if stringlike.respond_to?(:body)
+
+ case stringlike
+ when REXML::Document
+ stringlike.root
+ when REXML::Node
+ stringlike
+ when StringIO, String
+ begin
+ REXML::Document.new(stringlike.to_s).root
+ rescue REXML::ParseException => e
+ if e.message.include?("second root element")
+ REXML::Document.new("