123456789_123456789_123456789_123456789_123456789_

Class: Capybara::Selector::CSSBuilder Private

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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(expression) ⇒ CSSBuilder

[ GitHub ]

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

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

Instance Attribute Details

#expression (readonly)

[ GitHub ]

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

attr_reader :expression

Instance Method Details

#add_attribute_conditions(**attributes)

[ GitHub ]

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

def add_attribute_conditions(**attributes)
  @expression = attributes.inject(expression) do |css, (name, value)|
    conditions = if name == :class
      class_conditions(value)
    elsif value.is_a? Regexp
      regexp_conditions(name, value)
    else
      [attribute_conditions(name => value)]
    end

    ::Capybara::Selector::CSS.split(css).map do |sel|
      next sel if conditions.empty?

      conditions.map { |cond| sel + cond }.join(', ')
    end.join(', ')
  end
end

#attribute_conditions(attributes) (private)

[ GitHub ]

  
# File 'lib/capybara/selector/builders/css_builder.rb', line 43

def attribute_conditions(attributes)
  attributes.map do |attribute, value|
    case value
    when XPath::Expression
      raise ArgumentError, "XPath expressions are not supported for the :#{attribute} filter with CSS based selectors"
    when Regexp
      Selector::RegexpDisassembler.new(value).substrings.map do |str|
        "[#{attribute}*='#{str}'#{' i' if value.casefold?}]"
      end.join
    when true
      "[#{attribute}]"
    when false
      ':not([attribute])'
    else
      if attribute == :id
        "##{::Capybara::Selector::CSS.escape(value)}"
      else
        "[#{attribute}='#{value}']"
      end
    end
  end.join
end

#class_conditions(classes) (private)

[ GitHub ]

  
# File 'lib/capybara/selector/builders/css_builder.rb', line 66

def class_conditions(classes)
  case classes
  when XPath::Expression
    raise ArgumentError, 'XPath expressions are not supported for the :class filter with CSS based selectors'
  when Regexp
    Selector::RegexpDisassembler.new(classes).alternated_substrings.map do |strs|
      strs.map do |str|
        "[class*='#{str}'#{' i' if classes.casefold?}]"
      end.join
    end
  else
    cls = Array(classes).reject { |c| c.is_a? Regexp }.group_by { |cl| cl.match?(/^!(?!!!)/) }
    [(cls[false].to_a.map { |cl| ".#{Capybara::Selector::CSS.escape(cl.sub(/^!!/, ''))}" } +
    cls[true].to_a.map { |cl| ":not(.#{Capybara::Selector::CSS.escape(cl.slice(1..))})" }).join]
  end
end

#regexp_conditions(name, value) (private)

[ GitHub ]

  
# File 'lib/capybara/selector/builders/css_builder.rb', line 35

def regexp_conditions(name, value)
  Selector::RegexpDisassembler.new(value).alternated_substrings.map do |strs|
    strs.map do |str|
      "[#{name}*='#{str}'#{' i' if value.casefold?}]"
    end.join
  end
end