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

@ -38,7 +38,7 @@ module Capybara
attribute_conditions(class: classes)
else
cls = Array(classes).group_by { |cl| cl.start_with?('!') && !cl.start_with?('!!!') }
(cls[false].to_a.map { |cl| ".#{Capybara::Selector::CSS.escape(cl.sub(/^!!/,''))}" } +
(cls[false].to_a.map { |cl| ".#{Capybara::Selector::CSS.escape(cl.sub(/^!!/, ''))}" } +
cls[true].to_a.map { |cl| ":not(.#{Capybara::Selector::CSS.escape(cl.slice(1..-1))})" }).join
end
end

View File

@ -33,7 +33,7 @@ module Capybara
if klass.start_with?('!') && !klass.start_with?('!!!')
!XPath.attr(:class).contains_word(klass.slice(1..-1))
else
XPath.attr(:class).contains_word(klass.sub(/^!!/,''))
XPath.attr(:class).contains_word(klass.sub(/^!!/, ''))
end
end.reduce(:&)
end

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