-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix an invalid attributes parse when name with multiple
[]
partially fixed: #78
- Loading branch information
Showing
7 changed files
with
128 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
spec/rubocop/cop/capybara/mixin/css_attributes_parser_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Capybara::CssAttributesParser do | ||
describe 'CssSelector.new.parse' do | ||
it 'returns attributes hash when specify attributes' do | ||
expect(described_class.new('a[foo-bar_baz]').parse).to eq( | ||
'foo-bar_baz' => nil | ||
) | ||
expect(described_class.new('table[foo=bar]').parse).to eq( | ||
'foo' => "'bar'" | ||
) | ||
end | ||
|
||
it 'returns attributes hash when specify multiple attributes' do | ||
expect(described_class.new('button[foo][bar=baz]').parse).to eq( | ||
'foo' => nil, 'bar' => "'baz'" | ||
) | ||
end | ||
|
||
it 'returns attributes hash when specify nested attributes' do | ||
expect(described_class.new('[foo="bar[baz]"]').parse).to eq( | ||
'foo' => "'bar[baz]'" | ||
) | ||
end | ||
|
||
it 'returns attributes hash when specify nested and include ' \ | ||
'multiple bracket' do | ||
expect(described_class.new('[foo="bar[baz][qux]"]').parse).to eq( | ||
'foo' => "'bar[baz][qux]'" | ||
) | ||
end | ||
|
||
it 'returns empty hash when specify not include attributes' do | ||
expect(described_class.new('h1.cls#id').parse).to eq({}) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters