123456789_123456789_123456789_123456789_123456789_

Class: Capybara::Selector::XPathBuilder Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/capybara/selector/builders/xpath_builder.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(expression) ⇒ XPathBuilder

[ GitHub ]

  
# File 'lib/capybara/selector/builders/xpath_builder.rb', line 9

def initialize(expression)
  @expression = expression || ''
end

Instance Attribute Details

#expression (readonly)

[ GitHub ]

  
# File 'lib/capybara/selector/builders/xpath_builder.rb', line 13

attr_reader :expression

Instance Method Details

#add_attribute_conditions(**conditions)

[ GitHub ]

  
# File 'lib/capybara/selector/builders/xpath_builder.rb', line 15

def add_attribute_conditions(**conditions)
  @expression = conditions.inject(expression) do |xp, (name, value)|
    conditions = name == :class ? class_conditions(value) : attribute_conditions(name => value)
    return xp if conditions.nil?

    if xp.is_a? XPath::Expression
      xp[conditions]
    else
      "(#{xp})[#{conditions}]"
    end
  end
end

#attribute_conditions(attributes) (private)

[ GitHub ]

  
# File 'lib/capybara/selector/builders/xpath_builder.rb', line 30

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

#class_conditions(classes) (private)

[ GitHub ]

  
# File 'lib/capybara/selector/builders/xpath_builder.rb', line 47

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

#regexp_to_xpath_conditions(regexp) (private)

[ GitHub ]

  
# File 'lib/capybara/selector/builders/xpath_builder.rb', line 62

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