Sha256: 542e3926287870828e1862a9d986186cd93302612b5a81c9afcc62dabd1f2b0a
Contents?: true
Size: 1.77 KB
Versions: 19
Compression:
Stored size: 1.77 KB
Contents
# Source: https://github.com/teamcapybara/capybara/blob/xpath_regexp/lib/capybara/selector/regexp_disassembler.rb require 'regexp_parser' module Watir module Locators class Element class SelectorBuilder class RegexpDisassembler def initialize(regexp) @regexp = regexp @regexp_source = regexp.source end def substrings @substrings ||= begin strs = extract_strings(Regexp::Parser.parse(@regexp), [+'']) strs.map!(&:downcase) if @regexp.casefold? strs.reject(&:empty?).uniq end end private def min_repeat(exp) exp.quantifier&.min || 1 end def fixed_repeat?(exp) min_repeat(exp) == (exp.quantifier&.max || 1) end def optional?(exp) min_repeat(exp).zero? end def extract_strings(expression, strings) expression.each do |exp| if optional?(exp) strings.push(+'') next end if %i[meta set].include?(exp.type) strings.push(+'') next end if exp.terminal? case exp.type when :literal strings.last << (exp.text * min_repeat(exp)) when :escape strings.last << (exp.char * min_repeat(exp)) else strings.push(+'') end else min_repeat(exp).times { extract_strings(exp, strings) } end strings.push(+'') unless fixed_repeat?(exp) end strings end end end end end end
Version data entries
19 entries across 19 versions & 1 rubygems