Detect if the document is XML or HTML using the Content-Type when in Rails mode

This commit is contained in:
Bryan Helmkamp 2008-12-02 01:03:38 -05:00
parent a205b6fa44
commit 5cf23cbda3
5 changed files with 70 additions and 2 deletions

View File

@ -279,13 +279,25 @@ module Webrat
def page_dom #:nodoc: def page_dom #:nodoc:
return @response.dom if @response.respond_to?(:dom) return @response.dom if @response.respond_to?(:dom)
dom = Webrat::XML.document(@response_body)
if @session.xml_content_type?
dom = Webrat::XML.xml_document(@response_body)
else
dom = Webrat::XML.html_document(@response_body)
end
Webrat.define_dom_method(@response, dom) Webrat.define_dom_method(@response, dom)
return dom return dom
end end
def scoped_dom #:nodoc: def scoped_dom #:nodoc:
Webrat::XML.document("<html>" + Webrat::XML.to_html(Webrat::XML.css_search(@scope.dom, @selector).first) + "</html>") source = Webrat::XML.to_html(Webrat::XML.css_search(@scope.dom, @selector).first)
if @session.xml_content_type?
Webrat::XML.xml_document(source)
else
Webrat::XML.html_document(source)
end
end end
def locate_field(field_locator, *field_types) #:nodoc: def locate_field(field_locator, *field_types) #:nodoc:

View File

@ -204,6 +204,10 @@ module Webrat
page_scope.dom page_scope.dom
end end
def xml_content_type?
false
end
def_delegators :current_scope, :fill_in, :fills_in def_delegators :current_scope, :fill_in, :fills_in
def_delegators :current_scope, :set_hidden_field def_delegators :current_scope, :set_hidden_field
def_delegators :current_scope, :submit_form def_delegators :current_scope, :submit_form

View File

@ -13,6 +13,22 @@ module Webrat #:nodoc:
end end
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) def self.to_html(element)
if Webrat.configuration.parse_with_nokogiri? if Webrat.configuration.parse_with_nokogiri?
element.to_html element.to_html

View File

@ -18,6 +18,38 @@ module Webrat
end end
end end
def self.html_nokogiri_document(stringlike) #:nodoc:
return stringlike.dom if stringlike.respond_to?(:dom)
if Nokogiri::HTML::Document === stringlike
stringlike
elsif Nokogiri::XML::NodeSet === stringlike
stringlike
elsif StringIO === stringlike
Nokogiri::HTML(stringlike.string)
elsif stringlike.respond_to?(:body)
Nokogiri::HTML(stringlike.body.to_s)
else
Nokogiri::HTML(stringlike.to_s)
end
end
def self.xml_nokogiri_document(stringlike) #:nodoc:
return stringlike.dom if stringlike.respond_to?(:dom)
if Nokogiri::HTML::Document === stringlike
stringlike
elsif Nokogiri::XML::NodeSet === stringlike
stringlike
elsif StringIO === stringlike
Nokogiri::XML(stringlike.string)
elsif stringlike.respond_to?(:body)
Nokogiri::XML(stringlike.body.to_s)
else
Nokogiri::XML(stringlike.to_s)
end
end
def self.define_dom_method(object, dom) #:nodoc: def self.define_dom_method(object, dom) #:nodoc:
object.meta_class.send(:define_method, :dom) do object.meta_class.send(:define_method, :dom) do
dom dom

View File

@ -36,6 +36,10 @@ module Webrat
response.code.to_i response.code.to_i
end end
def xml_content_type?
response.headers["Content-Type"].to_s =~ /xml/
end
protected protected
def integration_session def integration_session