DRY selector expressions setting

This commit is contained in:
Thomas Walpole 2018-10-16 15:27:56 -07:00
parent 3326b2ede4
commit b03c934166
4 changed files with 14 additions and 15 deletions

View File

@ -227,12 +227,7 @@ module Capybara
# @return [#call] The block that will be called to generate the XPath expression
#
def xpath(*allowed_filters, &block)
if block
@format, @expression = :xpath, block
allowed_filters = parameter_names(block) if allowed_filters.empty?
allowed_filters.flatten.each { |ef| expression_filters[ef] = Filters::IdentityExpressionFilter.new(ef) }
end
format == :xpath ? @expression : nil
expression(:xpath, allowed_filters, &block)
end
##
@ -250,12 +245,7 @@ module Capybara
# @return [#call] The block that will be called to generate the CSS selector
#
def css(*allowed_filters, &block)
if block
@format, @expression = :css, block
allowed_filters = parameter_names(block) if allowed_filters.empty?
allowed_filters.flatten.each { |ef| expression_filters[ef] = Filters::IdentityExpressionFilter.new(ef) }
end
format == :css ? @expression : nil
expression(:css, allowed_filters, &block)
end
##
@ -465,6 +455,15 @@ module Capybara
def parameter_names(block)
block.parameters.select { |(type, _name)| %i[key keyreq].include? type }.map { |(_type, name)| name }
end
def expression(type, allowed_filters, &block)
if block
@format, @expression = type, block
allowed_filters = parameter_names(block) if allowed_filters.empty?
allowed_filters.flatten.each { |ef| expression_filters[ef] = Filters::IdentityExpressionFilter.new(ef) }
end
format == type ? @expression : nil
end
end
end

View File

@ -253,7 +253,7 @@ RSpec.describe Capybara do
expect(string.all(:custom_xpath_selector, XPath.descendant(:div, :p), class: ['!cc', '!dd', 'bb']).size).to eq 1
end
it 'handles classes starting with ! by requiring negated negated first', :focus_ do
it 'handles classes starting with ! by requiring negated negated first' do
expect(string.all(:custom_css_selector, 'div, p', class: ['!!!mine']).size).to eq 1
expect(string.all(:custom_xpath_selector, XPath.descendant(:div, :p), class: ['!!!mine']).size).to eq 1
end