mbenford / ngTagsInput

Tags input directive for AngularJS
http://mbenford.github.io/ngTagsInput
MIT License
1.64k stars 541 forks source link

RegExp stack trace if entering a tag with invalid regexp metacharacters #124

Closed dennisjlee closed 10 years ago

dennisjlee commented 10 years ago

Steps

  1. Set up an ng-tags-input with autocomplete (e.g. the "Simple tags input with autocomplete" example on the demo page http://mbenford.github.io/ngTagsInput/demos.html)
  2. Type a tag name containing "**" or other characters that constitute an invalid regexp

This will produce a JS error like below:

SyntaxError: Invalid regular expression: /a**b/: Nothing to repeat
    at new RegExp (native)
    at o (http://cdnjs.cloudflare.com/ajax/libs/ng-tags-input/2.0.0/ng-tags-input.min.js:1:5988)
    at Scope.b.highlight (http://cdnjs.cloudflare.com/ajax/libs/ng-tags-input/2.0.0/ng-tags-input.min.js:1:6233)
    at http://code.angularjs.org/1.2.15/angular.js:10348:21
    at Object.getStringValue (http://code.angularjs.org/1.2.15/angular.js:17668:41)
    at Scope.$digest (http://code.angularjs.org/1.2.15/angular.js:12023:40)
    at Scope.$apply (http://code.angularjs.org/1.2.15/angular.js:12279:24)
    at done (http://code.angularjs.org/1.2.15/angular.js:7991:45)
    at completeRequest (http://code.angularjs.org/1.2.15/angular.js:8196:7)
    at XMLHttpRequest.xhr.onreadystatechange (http://code.angularjs.org/1.2.15/angular.js:8137:11) 

This is due to the code below in the autoComplete directive - user-generated text should be escaped before being passed to RegExp.

 if (scope.options.highlightMatchedText) {
    markdown = function(item, text) {
        var expression = new RegExp(text, 'gi');
        return item.replace(expression, '**$&**');
   };
 }
mbenford commented 10 years ago

Hi @dennisjlee. Thanks for reporting that issue! :thumbsup:

dennisjlee commented 10 years ago

Awesome, thanks for the quick fix!

On Sun, Apr 13, 2014 at 8:19 PM, Michael Benford notifications@github.comwrote:

Closed #124 https://github.com/mbenford/ngTagsInput/issues/124 via e3c695fhttps://github.com/mbenford/ngTagsInput/commit/e3c695f26f96ab642a4a1f1129638e763b84b231 .

Reply to this email directly or view it on GitHubhttps://github.com/mbenford/ngTagsInput/issues/124 .