lib/capybara/selector.rb in capybara-2.9.1 vs lib/capybara/selector.rb in capybara-2.9.2

- old
+ new

@@ -427,24 +427,28 @@ # @locator Match id or text contents # @filter [Element, String] :for The element or id of the element associated with the label # Capybara.add_selector(:label) do label "label" - xpath do |locator| + xpath(:for) do |locator, options| xpath = XPath.descendant(:label) xpath = xpath[XPath.string.n.is(locator.to_s) | XPath.attr(:id).equals(locator.to_s)] unless locator.nil? + if options.has_key?(:for) && !options[:for].is_a?(Capybara::Node::Element) + xpath = xpath[XPath.attr(:for).equals(options[:for].to_s).or((~XPath.attr(:for)).and(XPath.descendant()[XPath.attr(:id).equals(options[:for].to_s)]))] + end xpath end filter(:for) do |node, field_or_value| if field_or_value.is_a? Capybara::Node::Element - if field_or_value[:id] && (field_or_value[:id] == node[:for]) - true + if node[:for] + field_or_value[:id] == node[:for] else field_or_value.find_xpath('./ancestor::label[1]').include? node.base end else - node[:for] == field_or_value.to_s + #Non element values were handled through the expression filter + true end end describe do |options| desc = String.new