webrat/lib/webrat/core/xml.rb

115 lines
2.8 KiB
Ruby
Raw Normal View History

2008-11-30 06:38:55 +00:00
require "webrat/core/xml/nokogiri"
require "webrat/core/xml/hpricot"
require "webrat/core/xml/rexml"
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-30 06:38:55 +00:00
Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
end
end
def self.html_document(stringlike) #:nodoc:
if Webrat.configuration.parse_with_nokogiri?
Webrat.html_nokogiri_document(stringlike)
else
Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
end
end
def self.xml_document(stringlike) #:nodoc:
if Webrat.configuration.parse_with_nokogiri?
Webrat.xml_nokogiri_document(stringlike)
else
Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
end
end
def self.to_html(element)
if Webrat.configuration.parse_with_nokogiri?
element.to_html
else
element.to_s
end
end
def self.inner_html(element)
if Webrat.configuration.parse_with_nokogiri?
element.inner_html
else
element.text
end
end
def self.all_inner_text(element)
if Webrat.configuration.parse_with_nokogiri?
element.inner_text
else
Hpricot(element.to_s).children.first.inner_text
end
end
def self.inner_text(element)
if Webrat.configuration.parse_with_nokogiri?
element.inner_text
else
if defined?(Hpricot::Doc) && element.is_a?(Hpricot::Doc)
element.inner_text
else
element.text
end
end
end
2008-11-29 05:49:00 +00:00
def self.xpath_to(element)
if Webrat.configuration.parse_with_nokogiri?
element.path
else
element.xpath
end
end
def self.attribute(element, attribute_name)
return element[attribute_name] if element.is_a?(Hash)
if Webrat.configuration.parse_with_nokogiri?
element[attribute_name]
else
element.attributes[attribute_name]
end
end
def self.xpath_at(*args)
xpath_search(*args).first
end
def self.css_at(*args)
css_search(*args).first
end
2008-11-27 05:40:18 +00:00
def self.xpath_search(element, *searches)
searches.flatten.map do |search|
if Webrat.configuration.parse_with_nokogiri?
element.xpath(search)
else
REXML::XPath.match(element, search)
end
2008-11-27 05:40:18 +00:00
end.flatten.compact
end
2008-11-25 00:59:27 +00:00
def self.css_search(element, *searches) #:nodoc:
2008-11-29 06:45:03 +00:00
xpath_search(element, css_to_xpath(*searches))
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