Skip to content

Commit

Permalink
Use nil as regexp disassembler separator rather than empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
twalpole committed Nov 5, 2018
1 parent be89e62 commit 671f871
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions lib/capybara/selector/regexp_disassembler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def substrings
private

def process(alternation:)
strs = extract_strings(Regexp::Parser.parse(@regexp), [''], alternation: alternation)
strs = extract_strings(Regexp::Parser.parse(@regexp), alternation: alternation)
strs = collapse(combine(strs).map(&:flatten))
strs.each { |str| str.map!(&:upcase) } if @regexp.casefold?
strs
Expand Down Expand Up @@ -63,14 +63,14 @@ def combine(strs)

def collapse(strs)
strs.map do |substrings|
substrings.slice_before(&:empty?).map(&:join).reject(&:empty?).uniq
substrings.slice_before(&:nil?).map(&:join).reject(&:empty?).uniq
end
end

def extract_strings(expression, strings, alternation: false)
def extract_strings(expression, strings = [], alternation: false)
expression.each do |exp|
if optional?(exp)
strings.push('')
strings.push(nil)
next
end

Expand All @@ -80,7 +80,7 @@ def extract_strings(expression, strings, alternation: false)
end

if %i[meta set].include?(exp.type)
strings.push('')
strings.push(nil)
next
end

Expand All @@ -91,22 +91,22 @@ def extract_strings(expression, strings, alternation: false)
when :escape
strings.push(exp.char * min_repeat(exp))
else
strings.push('')
strings.push(nil)
end
else
min_repeat(exp).times { extract_strings(exp, strings, alternation: alternation) }
end
strings.push('') unless fixed_repeat?(exp)
strings.push(nil) unless fixed_repeat?(exp)
end
strings
end

def alternative_strings(expression)
alternatives = expression.alternatives.map { |sub_exp| extract_strings(sub_exp, [], alternation: true) }
if alternatives.all? { |alt| alt.any? { |a| !a.empty? } }
alternatives = expression.alternatives.map { |sub_exp| extract_strings(sub_exp, alternation: true) }
if alternatives.all?(&:any?)
Set.new(alternatives)
else
strings.push('')
nil
end
end
end
Expand Down

0 comments on commit 671f871

Please sign in to comment.