diff --git a/lib/webrat/core/label.rb b/lib/webrat/core/label.rb index e2e08c0..7131bd0 100644 --- a/lib/webrat/core/label.rb +++ b/lib/webrat/core/label.rb @@ -11,7 +11,7 @@ module Webrat end def text - str = Webrat::XML.inner_text(@element) + str = Webrat::XML.all_inner_text(@element) str.gsub!("\n","") str.strip! str.squeeze!(" ") diff --git a/lib/webrat/core/link.rb b/lib/webrat/core/link.rb index 272f7d9..81653bc 100644 --- a/lib/webrat/core/link.rb +++ b/lib/webrat/core/link.rb @@ -44,7 +44,7 @@ module Webrat end def text - Webrat::XML.inner_text(@element) + Webrat::XML.all_inner_text(@element) end protected diff --git a/lib/webrat/core/locators.rb b/lib/webrat/core/locators.rb index 3e4b96a..ea8361c 100644 --- a/lib/webrat/core/locators.rb +++ b/lib/webrat/core/locators.rb @@ -4,7 +4,7 @@ module Webrat module Locators def field_by_xpath(xpath) - element = dom.at(xpath) + element = Webrat::XML.xpath_search(dom, xpath).first forms.detect_mapped do |form| form.field_by_element(element) diff --git a/lib/webrat/core/matchers/have_content.rb b/lib/webrat/core/matchers/have_content.rb index 2c4f43e..99a4f69 100644 --- a/lib/webrat/core/matchers/have_content.rb +++ b/lib/webrat/core/matchers/have_content.rb @@ -7,7 +7,12 @@ module Webrat end def matches?(stringlike) - @document = Webrat::XML.document(stringlike) + if Webrat.configuration.parse_with_nokogiri? + @document = Webrat.nokogiri_document(stringlike) + else + @document = Webrat::XML.hpricot_document(stringlike) + end + @element = Webrat::XML.inner_text(@document) case @content diff --git a/lib/webrat/core/scope.rb b/lib/webrat/core/scope.rb index 0758bcf..3a25d3d 100644 --- a/lib/webrat/core/scope.rb +++ b/lib/webrat/core/scope.rb @@ -289,7 +289,10 @@ module Webrat end def scoped_dom #:nodoc: - Webrat::XML.document(Webrat::XML.css_search(@scope.dom, @selector).first.to_html) + # if @selector == "#form2" + # require "rubygems"; require "ruby-debug"; Debugger.start; debugger + # end + Webrat::XML.document("" + Webrat::XML.to_html(Webrat::XML.css_search(@scope.dom, @selector).first) + "") end def locate_field(field_locator, *field_types) #:nodoc: diff --git a/lib/webrat/core/xml.rb b/lib/webrat/core/xml.rb index 8a0de4e..4572edc 100644 --- a/lib/webrat/core/xml.rb +++ b/lib/webrat/core/xml.rb @@ -5,8 +5,8 @@ module Webrat #:nodoc: if Webrat.configuration.parse_with_nokogiri? Webrat.nokogiri_document(stringlike) else - Webrat::XML.hpricot_document(stringlike) - # Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html) + # Webrat::XML.hpricot_document(stringlike) + Webrat.rexml_document(Webrat::XML.hpricot_document(stringlike).to_html) end end @@ -26,39 +26,71 @@ module Webrat #:nodoc: 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) - element.inner_html + 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) - element.inner_text + 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 def self.attribute(element, attribute_name) - # case element - # when Nokogiri::XML::Element, Hash + return element[attribute_name] if element.is_a?(Hash) + + if Webrat.configuration.parse_with_nokogiri? element[attribute_name] - # else - # element.attributes[attribute_name] - # end + else + element.attributes[attribute_name] + end end def self.xpath_search(element, *searches) searches.flatten.map do |search| - # REXML::XPath.match(element, search) - element.xpath(search) + if Webrat.configuration.parse_with_nokogiri? + element.xpath(search) + else + REXML::XPath.match(element, search) + end end.flatten.compact end def self.css_search(element, *searches) #:nodoc: - if Webrat.configuration.parse_with_nokogiri? + # if Webrat.configuration.parse_with_nokogiri? xpath_search(element, css_to_xpath(*searches)) # element.css(*searches) - else - searches.map do |search| - element.search(search) - end.flatten.compact - end + # else + # searches.map do |search| + # element.search(search) + # end.flatten.compact + # end end def self.css_to_xpath(*selectors) diff --git a/spec/api/locators/field_labeled_spec.rb b/spec/api/locators/field_labeled_spec.rb index 33066b3..82cdba0 100644 --- a/spec/api/locators/field_labeled_spec.rb +++ b/spec/api/locators/field_labeled_spec.rb @@ -50,12 +50,12 @@ describe "field_labeled" do HTML - + should_return_a Webrat::TextField, :for => "The Label" with_an_id_of "element_42", :for => "The Label" should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" end - + describe "finding a hidden field" do using_this_html <<-HTML @@ -65,12 +65,12 @@ describe "field_labeled" do HTML - + should_return_a Webrat::HiddenField, :for => "The Label" with_an_id_of "element_42", :for => "The Label" should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" end - + describe "finding a checkbox" do using_this_html <<-HTML @@ -80,12 +80,12 @@ describe "field_labeled" do HTML - + should_return_a Webrat::CheckboxField, :for => "The Label" with_an_id_of "element_42", :for => "The Label" should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" end - + describe "finding a radio button" do using_this_html <<-HTML @@ -95,13 +95,13 @@ describe "field_labeled" do HTML - + should_return_a Webrat::RadioField, :for => "The Label" with_an_id_of "element_42", :for => "The Label" should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" end - - + + describe "finding a text area" do using_this_html <<-HTML @@ -111,7 +111,7 @@ describe "field_labeled" do HTML - + should_return_a Webrat::TextareaField, :for => "The Label" with_an_id_of "element_42", :for => "The Label" should_raise_error_matching /Could not find .* "Other Label"/, :for => "Other Label" @@ -120,13 +120,13 @@ describe "field_labeled" do describe "finding a field with it's label containing newlines" do using_this_html <<-HTML -
- - -
+
+ + +
HTML diff --git a/spec/api/within_spec.rb b/spec/api/within_spec.rb index 0af74ed..7892970 100644 --- a/spec/api/within_spec.rb +++ b/spec/api/within_spec.rb @@ -40,14 +40,14 @@ describe "within" do it "should submit forms within a scope" do with_html <<-HTML -
-