From 9f5c40c744ac5db19706ab9476e7a83acda0b15c Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Fri, 19 Dec 2008 18:50:08 -0600 Subject: [PATCH] Ensure Enumerable#grep can handle strings with RegExp metacharacters. [#257 state:resolved] --- CHANGELOG | 2 ++ src/lang/enumerable.js | 2 +- test/unit/enumerable_test.js | 8 ++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 555c80d..066785e 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* Ensure Enumerable#grep can handle strings with RegExp metacharacters. (Marton Kiss-Albert, kangax) + * Switch to the "doScroll approach" for the dom:loaded custom event. (javier, Diego Perini, Nick Stakenburg, Andrew Dupont) * Optimize document.viewport.get(Dimensions|Width|Height). (Nick Stakenburg, Andrew Dupont) diff --git a/src/lang/enumerable.js b/src/lang/enumerable.js index b2155e0..138e897 100644 --- a/src/lang/enumerable.js +++ b/src/lang/enumerable.js @@ -75,7 +75,7 @@ var Enumerable = (function() { var results = []; if (Object.isString(filter)) - filter = new RegExp(filter); + filter = new RegExp(RegExp.escape(filter)); this.each(function(value, index) { if (filter.match(value)) diff --git a/test/unit/enumerable_test.js b/test/unit/enumerable_test.js index fdc7c01..3e0d00b 100644 --- a/test/unit/enumerable_test.js +++ b/test/unit/enumerable_test.js @@ -136,6 +136,14 @@ new Test.Unit.Runner({ this.assertEnumEqual($('grepHeader', 'grepCell'), $('grepTable', 'grepTBody', 'grepRow', 'grepHeader', 'grepCell').grep(new Selector('.cell'))); + + // troublesome characters + this.assertEnumEqual(['?a', 'c?'], ['?a','b','c?'].grep('?')); + this.assertEnumEqual(['*a', 'c*'], ['*a','b','c*'].grep('*')); + this.assertEnumEqual(['+a', 'c+'], ['+a','b','c+'].grep('+')); + this.assertEnumEqual(['{1}a', 'c{1}'], ['{1}a','b','c{1}'].grep('{1}')); + this.assertEnumEqual(['(a', 'c('], ['(a','b','c('].grep('(')); + this.assertEnumEqual(['|a', 'c|'], ['|a','b','c|'].grep('|')); }, testInclude: function() {