Skip to content

Commit

Permalink
Extracting some rexml code into a module
Browse files Browse the repository at this point in the history
  • Loading branch information
brynary committed Nov 10, 2008
1 parent 859a15b commit 59704da
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
33 changes: 9 additions & 24 deletions lib/webrat/core/matchers/have_xpath.rb
@@ -1,3 +1,6 @@
require "webrat/core/nokogiri"
require "webrat/core/rexml"

module Webrat
module Matchers

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

def query
[@expected].flatten.compact
Expand Down
24 changes: 24 additions & 0 deletions 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("<fake-root-element>#{stringlike}</fake-root-element>").root
else
raise e
end
end
end
end

end
1 change: 0 additions & 1 deletion lib/webrat/core/scope.rb
@@ -1,4 +1,3 @@
require "nokogiri"
require "webrat/core/form"
require "webrat/core/locators"

Expand Down

0 comments on commit 59704da

Please sign in to comment.