webrat/lib/webrat/core/xml.rb

52 lines
1.3 KiB
Ruby
Raw Normal View History

2008-11-25 00:59:27 +00:00
module Webrat #:nodoc:
module XML #:nodoc:
2008-11-25 00:59:27 +00:00
def self.document(stringlike) #:nodoc:
if Webrat.configuration.parse_with_nokogiri?
Webrat.nokogiri_document(stringlike)
else
2008-11-27 05:33:11 +00:00
Webrat::XML.hpricot_document(stringlike)
end
end
def self.hpricot_document(stringlike)
return stringlike.dom if stringlike.respond_to?(:dom)
2008-11-27 05:33:11 +00:00
if Hpricot::Doc === stringlike
stringlike
elsif Hpricot::Elements === stringlike
stringlike
elsif StringIO === stringlike
Hpricot(stringlike.string)
elsif stringlike.respond_to?(:body)
Hpricot(stringlike.body.to_s)
else
Hpricot(stringlike.to_s)
end
end
2008-11-27 05:40:18 +00:00
def self.xpath_search(element, *searches)
searches.flatten.map do |search|
element.xpath(search)
end.flatten.compact
end
2008-11-25 00:59:27 +00:00
def self.css_search(element, *searches) #:nodoc:
if Webrat.configuration.parse_with_nokogiri?
2008-11-27 05:40:18 +00:00
xpath_search(element, css_to_xpath(*searches))
# element.css(*searches)
else
searches.map do |search|
element.search(search)
end.flatten.compact
end
end
2008-11-27 05:40:18 +00:00
def self.css_to_xpath(*selectors)
selectors.map do |rule|
Nokogiri::CSS.xpath_for(rule, :prefix => ".//")
end.flatten.uniq
end
end
end