rubocop-rspec 2.16.0 → 2.18.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,146 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module RuboCop
4
- module Cop
5
- module RSpec
6
- # Helps parsing css selector.
7
- module CssSelector
8
- COMMON_OPTIONS = %w[
9
- above below left_of right_of near count minimum maximum between text
10
- id class style visible obscured exact exact_text normalize_ws match
11
- wait filter_set focused
12
- ].freeze
13
- SPECIFIC_OPTIONS = {
14
- 'button' => (
15
- COMMON_OPTIONS + %w[disabled name value title type]
16
- ).freeze,
17
- 'link' => (
18
- COMMON_OPTIONS + %w[href alt title download]
19
- ).freeze,
20
- 'table' => (
21
- COMMON_OPTIONS + %w[
22
- caption with_cols cols with_rows rows
23
- ]
24
- ).freeze,
25
- 'select' => (
26
- COMMON_OPTIONS + %w[
27
- disabled name placeholder options enabled_options
28
- disabled_options selected with_selected multiple with_options
29
- ]
30
- ).freeze,
31
- 'field' => (
32
- COMMON_OPTIONS + %w[
33
- checked unchecked disabled valid name placeholder
34
- validation_message readonly with type multiple
35
- ]
36
- ).freeze
37
- }.freeze
38
- SPECIFIC_PSEUDO_CLASSES = %w[
39
- not() disabled enabled checked unchecked
40
- ].freeze
41
-
42
- module_function
43
-
44
- # @param element [String]
45
- # @param attribute [String]
46
- # @return [Boolean]
47
- # @example
48
- # specific_pesudo_classes?('button', 'name') # => true
49
- # specific_pesudo_classes?('link', 'invalid') # => false
50
- def specific_options?(element, attribute)
51
- SPECIFIC_OPTIONS.fetch(element, []).include?(attribute)
52
- end
53
-
54
- # @param pseudo_class [String]
55
- # @return [Boolean]
56
- # @example
57
- # specific_pesudo_classes?('disabled') # => true
58
- # specific_pesudo_classes?('first-of-type') # => false
59
- def specific_pesudo_classes?(pseudo_class)
60
- SPECIFIC_PSEUDO_CLASSES.include?(pseudo_class)
61
- end
62
-
63
- # @param selector [String]
64
- # @return [Boolean]
65
- # @example
66
- # id?('#some-id') # => true
67
- # id?('.some-class') # => false
68
- def id?(selector)
69
- selector.start_with?('#')
70
- end
71
-
72
- # @param selector [String]
73
- # @return [Boolean]
74
- # @example
75
- # attribute?('[attribute]') # => true
76
- # attribute?('attribute') # => false
77
- def attribute?(selector)
78
- selector.start_with?('[')
79
- end
80
-
81
- # @param selector [String]
82
- # @return [Array<String>]
83
- # @example
84
- # attributes('a[foo-bar_baz]') # => {"foo-bar_baz=>true}
85
- # attributes('button[foo][bar]') # => {"foo"=>true, "bar"=>true}
86
- # attributes('table[foo=bar]') # => {"foo"=>"'bar'"}
87
- def attributes(selector)
88
- selector.scan(/\[(.*?)\]/).flatten.to_h do |attr|
89
- key, value = attr.split('=')
90
- [key, normalize_value(value)]
91
- end
92
- end
93
-
94
- # @param selector [String]
95
- # @return [Boolean]
96
- # @example
97
- # common_attributes?('a[focused]') # => true
98
- # common_attributes?('button[focused][visible]') # => true
99
- # common_attributes?('table[id=some-id]') # => true
100
- # common_attributes?('h1[invalid]') # => false
101
- def common_attributes?(selector)
102
- attributes(selector).keys.difference(COMMON_OPTIONS).none?
103
- end
104
-
105
- # @param selector [String]
106
- # @return [Array<String>]
107
- # @example
108
- # pseudo_classes('button:not([disabled])') # => ['not()']
109
- # pseudo_classes('a:enabled:not([valid])') # => ['enabled', 'not()']
110
- def pseudo_classes(selector)
111
- # Attributes must be excluded or else the colon in the `href`s URL
112
- # will also be picked up as pseudo classes.
113
- # "a:not([href='http://example.com']):enabled" => "a:not():enabled"
114
- ignored_attribute = selector.gsub(/\[.*?\]/, '')
115
- # "a:not():enabled" => ["not()", "enabled"]
116
- ignored_attribute.scan(/:([^:]*)/).flatten
117
- end
118
-
119
- # @param selector [String]
120
- # @return [Boolean]
121
- # @example
122
- # multiple_selectors?('a.cls b#id') # => true
123
- # multiple_selectors?('a.cls') # => false
124
- def multiple_selectors?(selector)
125
- selector.match?(/[ >,+~]/)
126
- end
127
-
128
- # @param value [String]
129
- # @return [Boolean, String]
130
- # @example
131
- # normalize_value('true') # => true
132
- # normalize_value('false') # => false
133
- # normalize_value(nil) # => false
134
- # normalize_value("foo") # => "'foo'"
135
- def normalize_value(value)
136
- case value
137
- when 'true' then true
138
- when 'false' then false
139
- when nil then true
140
- else "'#{value}'"
141
- end
142
- end
143
- end
144
- end
145
- end
146
- end