]> git.ipfire.org Git - thirdparty/bootstrap.git/commitdiff
typeahead should escape regexp special chars
authorJacob Thornton <jacobthornton@gmail.com>
Mon, 19 Mar 2012 22:33:28 +0000 (15:33 -0700)
committerJacob Thornton <jacobthornton@gmail.com>
Mon, 19 Mar 2012 22:33:28 +0000 (15:33 -0700)
js/bootstrap-typeahead.js
js/tests/unit/bootstrap-typeahead.js

index dc2f88221861d17460e895b00566ab3cf079a0ba..804e60dc446cbb65ccd00798625c739c4e0bc2ea 100644 (file)
     }
 
   , highlighter: function (item) {
-      return item.replace(new RegExp('(' + this.query + ')', 'ig'), function ($1, match) {
+      var query = this.query.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
+      return item.replace(new RegExp('(' + query + ')', 'ig'), function ($1, match) {
         return '<strong>' + match + '</strong>'
       })
     }
index 96ea7c45fce472b41d3d684f2fc94cf3ace47064..25d313c85c6898b7bad4599f405e40363f09d693 100644 (file)
@@ -52,6 +52,22 @@ $(function () {
         typeahead.$menu.remove()
       })
 
+      test("should not explode when regex chars are entered", function () {
+        var $input = $('<input />').typeahead({
+              source: ['aa', 'ab', 'ac', 'mdo*', 'fat+']
+            })
+          , typeahead = $input.data('typeahead')
+
+        $input.val('+')
+        typeahead.lookup()
+
+        ok(typeahead.$menu.is(":visible"), 'typeahead is visible')
+        equals(typeahead.$menu.find('li').length, 1, 'has 1 item in menu')
+        equals(typeahead.$menu.find('.active').length, 1, 'one item is active')
+
+        typeahead.$menu.remove()
+      })
+
       test("should hide menu when query entered", function () {
         stop()
         var $input = $('<input />').typeahead({