Sha256: 33fc9ad5aff17a5104605983e1c780bc7785964bbb11d5d827ca665a40b3cf6a

Contents?: true

Size: 1.58 KB

Versions: 4

Compression:

Stored size: 1.58 KB

Contents

# frozen_string_literal: true

require 'xpath'

module Capybara
  class Selector
    # @api private
    class CSSBuilder
      class << self
        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

        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
            attribute_conditions(class: classes)
          else
            cls = Array(classes).group_by { |cl| cl.start_with?('!') && !cl.start_with?('!!!') }
            (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..-1))})" }).join
          end
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
capybara-3.11.1 lib/capybara/selector/builders/css_builder.rb
capybara-3.11.0 lib/capybara/selector/builders/css_builder.rb
capybara-3.10.1 lib/capybara/selector/builders/css_builder.rb
capybara-3.10.0 lib/capybara/selector/builders/css_builder.rb