Sha256: 5afd308e83acb41b004208e9a8406db7cebb169e96d1adb8c572d623e138eee0

Contents?: true

Size: 1.53 KB

Versions: 2

Compression:

Stored size: 1.53 KB

Contents

# frozen_string_literal: true

require 'xpath'

module Capybara
  class Selector
    # @api private
    class XPathBuilder
      class << self
        def attribute_conditions(attributes)
          attributes.map do |attribute, value|
            case value
            when XPath::Expression
              XPath.attr(attribute)[value]
            when Regexp
              XPath.attr(attribute)[regexp_to_xpath_conditions(value)]
            when true
              XPath.attr(attribute)
            when false, nil
              !XPath.attr(attribute)
            else
              XPath.attr(attribute) == value.to_s
            end
          end.reduce(:&)
        end

        def class_conditions(classes)
          case classes
          when XPath::Expression, Regexp
            attribute_conditions(class: classes)
          else
            Array(classes).map do |klass|
              if klass.start_with?('!') && !klass.start_with?('!!!')
                !XPath.attr(:class).contains_word(klass.slice(1..-1))
              else
                XPath.attr(:class).contains_word(klass.sub(/^!!/, ''))
              end
            end.reduce(:&)
          end
        end

      private

        def regexp_to_xpath_conditions(regexp)
          condition = XPath.current
          condition = condition.uppercase if regexp.casefold?
          Selector::RegexpDisassembler.new(regexp).alternated_substrings.map do |strs|
            strs.map { |str| condition.contains(str) }.reduce(:&)
          end.reduce(:|)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
capybara-3.11.1 lib/capybara/selector/builders/xpath_builder.rb
capybara-3.11.0 lib/capybara/selector/builders/xpath_builder.rb