Module: RuboCop::Cop::Capybara::CssSelector Private

Defined in:
lib/rubocop/cop/capybara/mixin/css_selector.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Helps parsing css selector.

Class Method Summary collapse

Class Method Details

.attribute?(selector) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Examples:

attribute?('[attribute]') # => true
attribute?('attribute') # => false

Parameters:

  • selector (String)

Returns:

  • (Boolean)


48
49
50
# File 'lib/rubocop/cop/capybara/mixin/css_selector.rb', line 48

def attribute?(selector)
  selector.start_with?('[')
end

.attributes(selector) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Examples:

attributes('a[foo-bar_baz]') # => {"foo-bar_baz=>nil}
attributes('button[foo][bar=baz]') # => {"foo"=>nil, "bar"=>"'baz'"}
attributes('table[foo=bar]') # => {"foo"=>"'bar'"}
attributes('[foo="bar[baz][qux]"]') # => {"foo"=>"'bar[baz][qux]'"}

Parameters:

  • selector (String)

Returns:

  • (Array<String>)


59
60
61
# File 'lib/rubocop/cop/capybara/mixin/css_selector.rb', line 59

def attributes(selector)
  CssAttributesParser.new(selector).parse
end

.classes(selector) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Examples:

classes('#some-id') # => []
classes('.some-cls') # => ['some-cls']
classes('#some-id.some-cls') # => ['some-cls']
classes('#some-id.cls1.cls2') # => ['cls1', 'cls2']

Parameters:

  • selector (String)

Returns:

  • (Array<String>)


39
40
41
# File 'lib/rubocop/cop/capybara/mixin/css_selector.rb', line 39

def classes(selector)
  selector.scan(/\.([\w-]*)/).flatten
end

.id(selector) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Examples:

id('#some-id') # => some-id
id('.some-cls') # => nil
id('#some-id.cls') # => some-id

Parameters:

  • selector (String)

Returns:

  • (String)


17
18
19
20
21
# File 'lib/rubocop/cop/capybara/mixin/css_selector.rb', line 17

def id(selector)
  return unless id?(selector)

  selector.delete('#').gsub(selector.scan(/[^\\]([>,+~.].*)/).join, '')
end

.id?(selector) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Examples:

id?('#some-id') # => true
id?('.some-cls') # => false

Parameters:

  • selector (String)

Returns:

  • (Boolean)


28
29
30
# File 'lib/rubocop/cop/capybara/mixin/css_selector.rb', line 28

def id?(selector)
  selector.start_with?('#')
end

.multiple_selectors?(selector) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Examples:

multiple_selectors?('a.cls b#id') # => true
multiple_selectors?('a.cls') # => false

Parameters:

  • selector (String)

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/rubocop/cop/capybara/mixin/css_selector.rb', line 82

def multiple_selectors?(selector)
  normalize = selector.gsub(/(\\[>,+~]|\(.*\))/, '')
  normalize.match?(/[ >,+~]/)
end

.pseudo_classes(selector) ⇒ Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Examples:

pseudo_classes('button:not([disabled])') # => ['not()']
pseudo_classes('a:enabled:not([valid])') # => ['enabled', 'not()']

Parameters:

  • selector (String)

Returns:

  • (Array<String>)


68
69
70
71
72
73
74
75
# File 'lib/rubocop/cop/capybara/mixin/css_selector.rb', line 68

def pseudo_classes(selector)
  # Attributes must be excluded or else the colon in the `href`s URL
  # will also be picked up as pseudo classes.
  # "a:not([href='http://example.com']):enabled" => "a:not():enabled"
  ignored_attribute = selector.gsub(/\[.*?\]/, '')
  # "a:not():enabled" => ["not()", "enabled"]
  ignored_attribute.scan(/:([^:]*)/).flatten
end