-
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix an incorrect autocorrect for Capybara/RedundantWithinFind
when escape required css selector
#137
base: main
Are you sure you want to change the base?
Fix an incorrect autocorrect for Capybara/RedundantWithinFind
when escape required css selector
#137
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,11 +53,15 @@ def msg(node) | |
end | ||
|
||
def replaced(node) | ||
replaced = node.arguments.map(&:source).join(', ') | ||
if node.method?(:find_by_id) | ||
replaced.sub(/\A(["'])/, '\1#') | ||
node.first_argument.source.match(/\A(['"])(.*)['"]\z/) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This won’t work with any other form of string, like triple-quoted, those weird There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we use something like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Doesn’t it? |
||
quote = ::Regexp.last_match(1) | ||
css = ::Regexp.last_match(2) | ||
return ["#{quote}##{CssSelector.css_escape(css)}#{quote}", | ||
*node.arguments[1..].map(&:source)].join(', ') | ||
end | ||
else | ||
replaced | ||
node.arguments.map(&:source).join(', ') | ||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,4 +143,73 @@ | |
expect(described_class.multiple_selectors?('a.cls\>b')).to be false | ||
end | ||
end | ||
|
||
describe 'CssSelector.css_escape' do | ||
context 'when selector is a single hyphen' do | ||
let(:selector) { '-' } | ||
|
||
it 'escapes the hyphen character' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? Does it have a special meaning as a CSS selector? |
||
expect(described_class.css_escape(selector)).to eq('\\-') | ||
end | ||
end | ||
|
||
context 'when selector contains NULL character' do | ||
let(:selector) { "abc\0def" } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this realistic? |
||
|
||
it 'replaces NULL character with U+FFFD' do | ||
expect(described_class.css_escape(selector)).to eq('abc�def') | ||
end | ||
end | ||
|
||
context 'when selector contains control characters' do | ||
let(:selector) { "abc\x01\x1F\x7Fdef" } | ||
|
||
it 'escapes control characters as hexadecimal with a trailing space' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to handle this? |
||
expect(described_class.css_escape(selector)) | ||
.to eq('abc\\1 \\1f \\7f def') | ||
end | ||
end | ||
|
||
context 'when selector starts with a digit' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this possible? Why would anyone do this? |
||
let(:selector) { '1abc' } | ||
|
||
it 'escapes the starting digit as hexadecimal with a trailing space' do | ||
expect(described_class.css_escape(selector)).to eq('\\31 abc') | ||
end | ||
end | ||
|
||
context 'when selector starts with a hyphen followed by a digit' do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This seems to e an invalid CSS selector case, why should we handle it? |
||
let(:selector) { '-1abc' } | ||
|
||
it 'escapes the digit following a hyphen as hexadecimal with a ' \ | ||
'trailing space' do | ||
expect(described_class.css_escape(selector)).to eq('-\\31 abc') | ||
end | ||
end | ||
|
||
context 'when selector contains alphanumeric characters, hyphen, or ' \ | ||
'underscore' do | ||
let(:selector) { 'a-Z_0-9' } | ||
|
||
it 'does not escape alphanumeric characters, hyphen, or underscore' do | ||
expect(described_class.css_escape(selector)).to eq('a-Z_0-9') | ||
end | ||
end | ||
|
||
context 'when selector contains special characters needing escape' do | ||
let(:selector) { 'a!b@c#d$' } | ||
|
||
it 'escapes special characters' do | ||
expect(described_class.css_escape(selector)).to eq('a\\!b\\@c\\#d\\$') | ||
end | ||
end | ||
|
||
context 'when selector contains mixed cases' do | ||
let(:selector) { "ab\x00\x7F" } | ||
|
||
it 'handles mixed cases appropriately' do | ||
expect(described_class.css_escape(selector)).to eq('ab�\\7f ') | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you feel to only add handling for the period in this PR, and leaving the rest for later?