diff --git a/lib/webrat/core/matchers/have_xpath.rb b/lib/webrat/core/matchers/have_xpath.rb index e06bc418..b3eb96fe 100644 --- a/lib/webrat/core/matchers/have_xpath.rb +++ b/lib/webrat/core/matchers/have_xpath.rb @@ -1,3 +1,6 @@ +require "webrat/core/nokogiri" +require "webrat/core/rexml" + module Webrat module Matchers @@ -16,9 +19,13 @@ def matches?(stringlike) end def matches_rexml?(stringlike) - @query = query + if REXML::Node === stringlike || Array === stringlike + @query = query.map { |q| q.gsub(%r'//', './') } + else + @query = query + end - @document = rexml_document(stringlike) + @document = Webrat.rexml_document(stringlike) matched = @query.map do |q| if @document.is_a?(Array) @@ -42,28 +49,6 @@ def matches_nokogiri?(stringlike) matched = @document.xpath(*@query) matched.any? && (!@block || @block.call(matched)) 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, Array - @query = query.map { |q| q.gsub(%r'//', './') } - 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 def query [@expected].flatten.compact diff --git a/lib/webrat/core/rexml.rb b/lib/webrat/core/rexml.rb new file mode 100644 index 00000000..83f9dd19 --- /dev/null +++ b/lib/webrat/core/rexml.rb @@ -0,0 +1,24 @@ +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 \ No newline at end of file diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index adf1d6a0..98336ee9 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -1,4 +1,3 @@ -require "nokogiri" require "webrat/core/form" require "webrat/core/locators"