diff --git a/lib/webrat/core/locators/field_labeled_locator.rb b/lib/webrat/core/locators/field_labeled_locator.rb index adaa2c2..7288931 100644 --- a/lib/webrat/core/locators/field_labeled_locator.rb +++ b/lib/webrat/core/locators/field_labeled_locator.rb @@ -1,10 +1,10 @@ require "webrat/core_extensions/detect_mapped" -require "webrat/core/locators/locator" +require "webrat/core/locators/label_locator" module Webrat module Locators - class FieldLabeledLocator < Locator # :nodoc: + class FieldLabeledLocator < LabelLocator # :nodoc: def locate matching_labels.any? && matching_labels.detect_mapped { |label| label.field } @@ -18,28 +18,10 @@ module Webrat end end - def matching_label_elements - label_elements.select do |label_element| - text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i - end - end - - def label_elements - Webrat::XML.xpath_search(@dom, Label.xpath_search) - end - def error_message "Could not find field labeled #{@value.inspect}" end - def text(element) - str = Webrat::XML.all_inner_text(element) - str.gsub!("\n","") - str.strip! - str.squeeze!(" ") - str - end - end # Locates a form field based on a label element in the HTML source. diff --git a/lib/webrat/core/locators/label_locator.rb b/lib/webrat/core/locators/label_locator.rb index 633fa05..5fc3a34 100644 --- a/lib/webrat/core/locators/label_locator.rb +++ b/lib/webrat/core/locators/label_locator.rb @@ -7,11 +7,11 @@ module Webrat class LabelLocator < Locator # :nodoc: def locate - Label.load(@session, label_element) + Label.load(@session, matching_label_elements.first) end - def label_element - label_elements.detect do |label_element| + def matching_label_elements + label_elements.select do |label_element| text(label_element) =~ /^\W*#{Regexp.escape(@value.to_s)}\b/i end end @@ -21,13 +21,22 @@ module Webrat end def text(label_element) - str = Webrat::XML.all_inner_text(label_element) + str = Webrat::XML.all_inner_text(without_fields(label_element)) str.gsub!("\n","") str.strip! str.squeeze!(" ") str end + protected + + def without_fields(label_element) + result = Webrat::XML.deep_copy(label_element) + Webrat::XML.xpath_search(result, *Field.xpath_search).each do |child| + child.remove + end + result + end end end diff --git a/lib/webrat/core/xml.rb b/lib/webrat/core/xml.rb index 86dd2c8..f4b21e6 100644 --- a/lib/webrat/core/xml.rb +++ b/lib/webrat/core/xml.rb @@ -111,5 +111,12 @@ module Webrat #:nodoc: end.flatten.uniq end + def self.deep_copy(element) + if Webrat.configuration.parse_with_nokogiri? + element.dup + else + element.deep_clone + end + end end end \ No newline at end of file diff --git a/spec/public/locators/field_labeled_spec.rb b/spec/public/locators/field_labeled_spec.rb index 7a102df..656f581 100644 --- a/spec/public/locators/field_labeled_spec.rb +++ b/spec/public/locators/field_labeled_spec.rb @@ -9,6 +9,20 @@ describe "field_labeled" do end end + def using_html_with_this_body body + using_this_html <<-HTML + + +
+