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-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-30 06:38:55 +00:00
|
|
|
Webrat.rexml_document(Webrat.hpricot_document(stringlike).to_html)
|
2008-12-02 06:03:38 +00:00
|
|
|
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)
|
2008-11-11 05:28:15 +00:00
|
|
|
end
|
|
|
|
end
|
2008-11-27 05:52:17 +00:00
|
|
|
|
2008-11-28 07:02:17 +00:00
|
|
|
def self.to_html(element)
|
|
|
|
if Webrat.configuration.parse_with_nokogiri?
|
|
|
|
element.to_html
|
|
|
|
else
|
|
|
|
element.to_s
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-11-28 05:12:21 +00:00
|
|
|
def self.inner_html(element)
|
2008-11-28 07:02:17 +00:00
|
|
|
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
|
2008-11-28 05:12:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.inner_text(element)
|
2008-11-28 07:02:17 +00:00
|
|
|
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
|
2008-11-28 05:12:21 +00:00
|
|
|
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
|
|
|
|
|
2008-11-27 05:52:17 +00:00
|
|
|
def self.attribute(element, attribute_name)
|
2008-11-28 07:02:17 +00:00
|
|
|
return element[attribute_name] if element.is_a?(Hash)
|
|
|
|
|
|
|
|
if Webrat.configuration.parse_with_nokogiri?
|
2008-11-28 05:12:21 +00:00
|
|
|
element[attribute_name]
|
2008-11-28 07:02:17 +00:00
|
|
|
else
|
|
|
|
element.attributes[attribute_name]
|
|
|
|
end
|
2008-11-29 06:08:58 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.xpath_at(*args)
|
|
|
|
xpath_search(*args).first
|
|
|
|
end
|
2008-11-11 05:28:15 +00:00
|
|
|
|
2008-11-30 20:24:50 +00:00
|
|
|
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|
|
2008-11-28 07:02:17 +00:00
|
|
|
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))
|
2008-11-11 05:28:15 +00:00
|
|
|
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
|