2008-11-25 00:59:27 +00:00
|
|
|
module Webrat #:nodoc:
|
|
|
|
module XML #:nodoc:
|
2008-11-11 05:28:15 +00:00
|
|
|
|
2008-11-25 00:59:27 +00:00
|
|
|
def self.document(stringlike) #:nodoc:
|
2008-11-23 19:59:07 +00:00
|
|
|
if Webrat.configuration.parse_with_nokogiri?
|
2008-11-11 05:28:15 +00:00
|
|
|
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-11 05:28:15 +00:00
|
|
|
|
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)
|
2008-11-11 05:28:15 +00:00
|
|
|
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:
|
2008-11-23 19:59:07 +00:00
|
|
|
if Webrat.configuration.parse_with_nokogiri?
|
2008-11-27 05:40:18 +00:00
|
|
|
xpath_search(element, css_to_xpath(*searches))
|
|
|
|
# element.css(*searches)
|
2008-11-11 05:28:15 +00:00
|
|
|
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
|
|
|
|
|
2008-11-11 05:28:15 +00:00
|
|
|
end
|
|
|
|
end
|