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

Version Path
watir-7.1.0 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-7.0.0 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-7.0.0.beta5 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-7.0.0.beta4 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-7.0.0.beta3 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-7.0.0.beta2 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-7.0.0.beta1 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.19.1 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.19.0 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.18.0 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.17.0 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.16.5 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.16.4 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.16.3 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.16.2 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.16.1 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.16.0 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.15.1 lib/watir/locators/element/selector_builder/regexp_disassembler.rb
watir-6.15.0 lib/watir/locators/element/selector_builder/regexp_disassembler.rb