DRY selector expressions setting
This commit is contained in:
parent
3326b2ede4
commit
b03c934166
|
@ -38,7 +38,7 @@ module Capybara
|
||||||
attribute_conditions(class: classes)
|
attribute_conditions(class: classes)
|
||||||
else
|
else
|
||||||
cls = Array(classes).group_by { |cl| cl.start_with?('!') && !cl.start_with?('!!!') }
|
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
|
cls[true].to_a.map { |cl| ":not(.#{Capybara::Selector::CSS.escape(cl.slice(1..-1))})" }).join
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -33,7 +33,7 @@ module Capybara
|
||||||
if klass.start_with?('!') && !klass.start_with?('!!!')
|
if klass.start_with?('!') && !klass.start_with?('!!!')
|
||||||
!XPath.attr(:class).contains_word(klass.slice(1..-1))
|
!XPath.attr(:class).contains_word(klass.slice(1..-1))
|
||||||
else
|
else
|
||||||
XPath.attr(:class).contains_word(klass.sub(/^!!/,''))
|
XPath.attr(:class).contains_word(klass.sub(/^!!/, ''))
|
||||||
end
|
end
|
||||||
end.reduce(:&)
|
end.reduce(:&)
|
||||||
end
|
end
|
||||||
|
|
|
@ -227,12 +227,7 @@ module Capybara
|
||||||
# @return [#call] The block that will be called to generate the XPath expression
|
# @return [#call] The block that will be called to generate the XPath expression
|
||||||
#
|
#
|
||||||
def xpath(*allowed_filters, &block)
|
def xpath(*allowed_filters, &block)
|
||||||
if block
|
expression(:xpath, allowed_filters, &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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -250,12 +245,7 @@ module Capybara
|
||||||
# @return [#call] The block that will be called to generate the CSS selector
|
# @return [#call] The block that will be called to generate the CSS selector
|
||||||
#
|
#
|
||||||
def css(*allowed_filters, &block)
|
def css(*allowed_filters, &block)
|
||||||
if block
|
expression(:css, allowed_filters, &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
|
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -465,6 +455,15 @@ module Capybara
|
||||||
def parameter_names(block)
|
def parameter_names(block)
|
||||||
block.parameters.select { |(type, _name)| %i[key keyreq].include? type }.map { |(_type, name)| name }
|
block.parameters.select { |(type, _name)| %i[key keyreq].include? type }.map { |(_type, name)| name }
|
||||||
end
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -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
|
expect(string.all(:custom_xpath_selector, XPath.descendant(:div, :p), class: ['!cc', '!dd', 'bb']).size).to eq 1
|
||||||
end
|
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_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
|
expect(string.all(:custom_xpath_selector, XPath.descendant(:div, :p), class: ['!!!mine']).size).to eq 1
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue