Skip to content

Commit

Permalink
Fix negated class selector option - Issue #2103
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Sep 26, 2018
1 parent 09eedb6 commit 6f71931
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
7 changes: 7 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Version 3.8.2
Release date: unreleased

### Fixed

* Fixed negated class selector option - Issue #2103

# Version 3.8.1
Release date: 2018-09-22

Expand Down
4 changes: 2 additions & 2 deletions lib/capybara/queries/selector_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def css_from_classes

classes = Array(options[:class]).group_by { |cl| cl.start_with? '!' }
(classes[false].to_a.map { |cl| ".#{Capybara::Selector::CSS.escape(cl)}" } +
classes[true].to_a.map { |cl| ":not(.#{Capybara::Selector::CSS.escape(cl.slice(1))})" }).join
classes[true].to_a.map { |cl| ":not(.#{Capybara::Selector::CSS.escape(cl.slice(1..-1))})" }).join
end

def css_from_id
Expand All @@ -266,7 +266,7 @@ def xpath_from_classes

Array(options[:class]).map do |klass|
if klass.start_with?('!')
!XPath.attr(:class).contains_word(klass.slice(1))
!XPath.attr(:class).contains_word(klass.slice(1..-1))
else
XPath.attr(:class).contains_word(klass)
end
Expand Down
32 changes: 16 additions & 16 deletions spec/selector_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<title>selectors</title>
</head>
<body>
<div class="a" id="page">
<div class="b" id="content">
<h1 class="a">Totally awesome</h1>
<div class="aa" id="page">
<div class="bb" id="content">
<h1 class="aa">Totally awesome</h1>
<p>Yes it is</p>
</div>
<p class="b c">Some Content</p>
<p class="b d"></p>
<p class="bb cc">Some Content</p>
<p class="bb dd"></p>
</div>
<div id="#special">
</div>
Expand Down Expand Up @@ -84,22 +84,22 @@

describe 'modify_selector' do
it 'allows modifying a selector' do
el = string.find(:custom_selector, 'a')
el = string.find(:custom_selector, 'aa')
expect(el.tag_name).to eq 'div'
Capybara.modify_selector :custom_selector do
css { |css_class| "h1.#{css_class}" }
end
el = string.find(:custom_selector, 'a')
el = string.find(:custom_selector, 'aa')
expect(el.tag_name).to eq 'h1'
end

it "doesn't change existing filters" do
Capybara.modify_selector :custom_selector do
css { |css_class| "p.#{css_class}" }
end
expect(string).to have_selector(:custom_selector, 'b', count: 1)
expect(string).to have_selector(:custom_selector, 'b', not_empty: false, count: 1)
expect(string).to have_selector(:custom_selector, 'b', not_empty: :all, count: 2)
expect(string).to have_selector(:custom_selector, 'bb', count: 1)
expect(string).to have_selector(:custom_selector, 'bb', not_empty: false, count: 1)
expect(string).to have_selector(:custom_selector, 'bb', not_empty: :all, count: 2)
end
end

Expand Down Expand Up @@ -151,15 +151,15 @@

context 'with :class option' do
it 'works with compound css selectors' do
expect(string.all(:custom_css_selector, 'div, h1', class: 'a').size).to eq 2
expect(string.all(:custom_css_selector, 'h1, div', class: 'a').size).to eq 2
expect(string.all(:custom_css_selector, 'div, h1', class: 'aa').size).to eq 2
expect(string.all(:custom_css_selector, 'h1, div', class: 'aa').size).to eq 2
end

it 'handles negated classes' do
expect(string.all(:custom_css_selector, 'div, p', class: ['b', '!c']).size).to eq 2
expect(string.all(:custom_css_selector, 'div, p', class: ['!c', '!d', 'b']).size).to eq 1
expect(string.all(:custom_xpath_selector, XPath.descendant(:div, :p), class: ['b', '!c']).size).to eq 2
expect(string.all(:custom_xpath_selector, XPath.descendant(:div, :p), class: ['!c', '!d', 'b']).size).to eq 1
expect(string.all(:custom_css_selector, 'div, p', class: ['bb', '!cc']).size).to eq 2
expect(string.all(:custom_css_selector, 'div, p', class: ['!cc', '!dd', 'bb']).size).to eq 1
expect(string.all(:custom_xpath_selector, XPath.descendant(:div, :p), class: ['bb', '!cc']).size).to eq 2
expect(string.all(:custom_xpath_selector, XPath.descendant(:div, :p), class: ['!cc', '!dd', 'bb']).size).to eq 1
end

it "works with 'special' characters" do
Expand Down

0 comments on commit 6f71931

Please sign in to comment.