Extracting some rexml code into a module

This commit is contained in:
Bryan Helmkamp 2008-11-09 20:49:55 -05:00
parent 859a15b8d8
commit 59704da270
3 changed files with 33 additions and 25 deletions

View File

@ -1,3 +1,6 @@
require "webrat/core/nokogiri"
require "webrat/core/rexml"
module Webrat module Webrat
module Matchers module Matchers
@ -16,9 +19,13 @@ module Webrat
end end
def matches_rexml?(stringlike) def matches_rexml?(stringlike)
if REXML::Node === stringlike || Array === stringlike
@query = query.map { |q| q.gsub(%r'//', './') }
else
@query = query @query = query
end
@document = rexml_document(stringlike) @document = Webrat.rexml_document(stringlike)
matched = @query.map do |q| matched = @query.map do |q|
if @document.is_a?(Array) if @document.is_a?(Array)
@ -43,28 +50,6 @@ module Webrat
matched.any? && (!@block || @block.call(matched)) matched.any? && (!@block || @block.call(matched))
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, 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("<fake-root-element>#{stringlike}</fake-root-element>").root
else
raise e
end
end
end
end
def query def query
[@expected].flatten.compact [@expected].flatten.compact
end end

24
lib/webrat/core/rexml.rb Normal file
View File

@ -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("<fake-root-element>#{stringlike}</fake-root-element>").root
else
raise e
end
end
end
end
end

View File

@ -1,4 +1,3 @@
require "nokogiri"
require "webrat/core/form" require "webrat/core/form"
require "webrat/core/locators" require "webrat/core/locators"