provide way to specify class starting with !

This commit is contained in:
Thomas Walpole 2018-10-16 14:13:20 -07:00
parent 09690bbfd3
commit f35e2771a6
3 changed files with 10 additions and 5 deletions

View File

@ -37,8 +37,8 @@ module Capybara
when Regexp
attribute_conditions(class: classes)
else
cls = Array(classes).group_by { |cl| cl.start_with? '!' }
(cls[false].to_a.map { |cl| ".#{Capybara::Selector::CSS.escape(cl)}" } +
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[true].to_a.map { |cl| ":not(.#{Capybara::Selector::CSS.escape(cl.slice(1..-1))})" }).join
end
end

View File

@ -30,10 +30,10 @@ module Capybara
attribute_conditions(class: classes)
else
Array(classes).map do |klass|
if klass.start_with?('!')
if klass.start_with?('!') && !klass.start_with?('!!!')
!XPath.attr(:class).contains_word(klass.slice(1..-1))
else
XPath.attr(:class).contains_word(klass)
XPath.attr(:class).contains_word(klass.sub(/^!!/,''))
end
end.reduce(:&)
end

View File

@ -17,7 +17,7 @@ RSpec.describe Capybara do
<p>Yes it is</p>
</div>
<p class="bb cc">Some Content</p>
<p class="bb dd"></p>
<p class="bb dd !mine"></p>
</div>
<div id="#special">
</div>
@ -253,6 +253,11 @@ 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
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
it "works with 'special' characters" do
expect(string.find(:custom_css_selector, 'input', class: '.special')[:id]).to eq 'file'
expect(string.find(:custom_css_selector, 'input', class: '2checkbox')[:id]).to eq '2checkbox'