Skip to content

fix(autocomplete example): regex metacharacter escape #4921

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

Merged
merged 5 commits into from
Jun 8, 2017

Conversation

girtsernstsons
Copy link
Contributor

autocomplete material example fails if regex metacharacters (ie *) are provided. Fixed by adding escapeSpecChars function

autocomplete material example fails if regex metacharacter (ie *) is provided. Fixed by adding escapeSpecChars function
@googlebot googlebot added the cla: yes PR author has agreed to Google's Contributor License Agreement label Jun 1, 2017
@girtsernstsons
Copy link
Contributor Author

girtsernstsons commented Jun 1, 2017

Plunker example

filterStates(val: string) {
return val ? this.states.filter(s => new RegExp(`^${val}`, 'gi').test(s))
let escVal: string = this.escapeSpecChars(val);
return val ? this.states.filter(s => new RegExp(`^${escVal}`, 'gi').test(s))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a cleaner solution would be to just not use a regex at all and instead just use indexOf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint. Decided to use includes instead of indexOf

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related #4440 (comment)

@@ -73,7 +73,7 @@ export class AutocompleteOverviewExample {
}

filterStates(val: string) {
return val ? this.states.filter(s => new RegExp(`^${val}`, 'gi').test(s))
return val ? this.states.filter(s => s.toLowerCase().includes(val.toLowerCase()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

includes is not supported in IE11 (which we still support). We try to avoid requiring polyfills where possible

@willshowell
Copy link
Contributor

@jelbourn Per your suggestion to switch to indexOf, I believe the regex filter was put in place for a11y reasons

#3373, #4440

@jelbourn
Copy link
Member

jelbourn commented Jun 5, 2017

@willshowell good point; it should be specifically indexOf(...) === 0

@jelbourn jelbourn changed the title fix(autocomplete): regex metacharacter escape fix(autocomplete example): regex metacharacter escape Jun 6, 2017
@girtsernstsons
Copy link
Contributor Author

@jelbourn @willshowell how about adding a toggle "Search from beginning" in the example which would switch between indexOf===0 and indexOf>-1? IMHO it would provide a broader use case to developers.

@jelbourn
Copy link
Member

jelbourn commented Jun 7, 2017

It's just an example- I think the indexOf === 0 is sufficient

@girtsernstsons
Copy link
Contributor Author

@jelbourn ok, fixed with indexOf === 0

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@jelbourn jelbourn added pr: lgtm action: merge The PR is ready for merge by the caretaker labels Jun 7, 2017
@andrewseguin andrewseguin merged commit aafa6b0 into angular:master Jun 8, 2017
@girtsernstsons girtsernstsons deleted the autocomplete-regex-esc branch June 8, 2017 19:34
@rafaelss95
Copy link
Contributor

rafaelss95 commented Jun 9, 2017

I know it's just an example, but with this change it behaves in a different way:

Before:

When "a" is typed it shows "Alabama", "Alaska", "Arizona", "Arkansas"

Now:

When "a" is typed it shows simply nothing, because indexOf is case sensitive.

Also, I think it should matches the whole string... example:

If I type "k" it should match Arkansas, for example.

So, to make it work in the cases described above, I'd suggest to change it to:

return val ? this.states.filter(s => s.toLowerCase().indexOf(val.toLowerCase()) !== -1)

PLUNKER

@jelbourn

@girtsernstsons
Copy link
Contributor Author

@rafaelss95
Fix already handles case sensitivity in the same way you've proposed - aafa6b0.
The reason, why indexOf(...) === 0 is more preferable over indexOf(...) !== -1, can be found here #3373

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 6, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
action: merge The PR is ready for merge by the caretaker cla: yes PR author has agreed to Google's Contributor License Agreement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants