pozil / sfdc-ui-lookup-lwc

Salesforce Lookup Component built with Lightning Web Components.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Special characters cause errors

skycafemix opened this issue · comments

I love this component, fantastic work. I discovered a problem that if a user types a symbol it gets interpreted as regular expression, throwing a component error from Lookup.setSearchResults:

 [	 [Invalid regular expression: /([)/: Unterminated character class]
 ?	 [Invalid regular expression: /(?)/: Invalid group]
 +	 [Invalid regular expression: /(+)/: Nothing to repeat]

I don't think business users can handle regexes so the key is to trap out error characters and those which would not be part of someone's name (since I am using this for a user lookup). I think it is reasonable.

FWIW I brushed up on javascript regexes here and made a patch. Sorry I do not have time to formally submit.. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
which suggested
function escapeRegExp(string) {
return string.replace(/[.*+-?^${}()|[]\]/g, '\$&'); // $& means the whole matched string
}

Here is my patch:
At setSearchResults line 56:

- const regex = new RegExp(`(${this._searchTerm})`, 'gi');
+ const trapreg = /[+\?^${}()|[\]\\]/g
+ var cleaned = this._searchTerm.replace(trapreg,'');
+ //const regex = new RegExp(`(${this._searchTerm})`, 'gi'); // your current code
+ const regex = new RegExp(`(${cleaned})`, 'gi');

This quick fix works for me.

Hi @skycafemix, thanks for reaching out with this bug and even better with a fix :)
I've implemented the fix and released it in v2.5.1.

Hi @pozil oh that is so quick! Thank you for your efforts.