Skip to content

Commit

Permalink
Ignore negative text requirements when extracting strings from regexps
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Jul 25, 2019
1 parent 7e2d19b commit f1139a5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/capybara/selector/regexp_disassembler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ def initialize(exp)
def extract_strings(process_alternatives)
strings = []
each do |exp|
next if exp.ignore?

next strings.push(nil) if exp.optional? && !process_alternatives

next strings.push(exp.alternative_strings) if exp.alternation? && process_alternatives
Expand Down Expand Up @@ -159,6 +161,11 @@ def alternative_strings
alts.all?(&:any?) ? Set.new(alts) : nil
end

def ignore?
[Regexp::Expression::Assertion::NegativeLookahead,
Regexp::Expression::Assertion::NegativeLookbehind].any? { |klass| @exp.is_a? klass }
end

private

def indeterminate?
Expand Down
10 changes: 10 additions & 0 deletions spec/regexp_dissassembler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,16 @@
)
end

it 'ignores negative lookaheads' do
verify_strings(
/^(?!.*\bContributing Editor\b).*$/ => %w[],
/abc(?!.*def).*/ => %w[abc],
/(?!.*def)abc/ => %w[abc],
/abc(?!.*def.*).*ghi/ => %w[abc ghi],
/abc(?!.*bcd)def/ => %w[abcdef]
)
end

it 'handles anchors' do
verify_strings(
/^abc/ => %w[abc],
Expand Down

0 comments on commit f1139a5

Please sign in to comment.