Sha256: e6984df18b6b06b0a64989667043c9e8cbd0d56efcde74cc5ffb8a19017f112b

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module RuboCop
  module Cop
    module Capybara
      # Css selector parser.
      # @api private
      class CssAttributesParser
        def initialize(selector)
          @selector = selector
          @state = :initial
          @temp = ''
          @results = {}
          @bracket_count = 0
        end

        # @return [Array<String>]
        def parse # rubocop:disable Metrics/MethodLength
          @selector.chars do |char|
            if char == '['
              on_bracket_start
            elsif char == ']'
              on_bracket_end
            elsif @state == :inside_attr
              @temp += char
            end
          end
          @results
        end

        private

        def on_bracket_start
          @bracket_count += 1
          if @state == :initial
            @state = :inside_attr
          else
            @temp += '['
          end
        end

        def on_bracket_end
          @bracket_count -= 1
          if @bracket_count.zero?
            @state = :initial
            key, value = @temp.split('=')
            @results[key] = normalize_value(value)
            @temp.clear
          else
            @temp += ']'
          end
        end

        # @param value [String]
        # @return [Boolean, String]
        # @example
        #   normalize_value('true') # => true
        #   normalize_value('false') # => false
        #   normalize_value(nil) # => nil
        #   normalize_value("foo") # => "'foo'"
        def normalize_value(value)
          case value
          when 'true' then true
          when 'false' then false
          when nil then nil
          else "'#{value.gsub(/"|'/, '')}'"
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
mlh-rubocop-config-1.0.3 vendor/bundle/ruby/3.2.0/gems/rubocop-capybara-2.20.0/lib/rubocop/cop/capybara/mixin/css_attributes_parser.rb
rubocop-capybara-2.20.0 lib/rubocop/cop/capybara/mixin/css_attributes_parser.rb