From e9bdaef0afc407ea3220ad39d69d88731d8d06fa Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Mon, 23 Feb 2009 20:13:41 -0600 Subject: [PATCH] String#gsub should escape RegExp metacharacters when the first argument is a string. [#469 state:resolved] (michael, kangax) --- CHANGELOG | 2 ++ src/lang/string.js | 3 +++ test/unit/string_test.js | 17 ++++++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/CHANGELOG b/CHANGELOG index 3f84b1c..e011e29 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -1,3 +1,5 @@ +* String#gsub should escape RegExp metacharacters when the first argument is a string. [#469 state:resolved] (michael, kangax) + * Fix order of replacement in String#unescapeHTML [#544 state:resolved] (SWeini, kangax) * Fix issue where a Selector query rooted on a node that had not been attached to the document failed in IE. [#464 state:resolved] (jddalton, kangax, Douglas Fraser, Andrew Dupont) diff --git a/src/lang/string.js b/src/lang/string.js index 47f4ba6..89c89e9 100644 --- a/src/lang/string.js +++ b/src/lang/string.js @@ -38,6 +38,9 @@ Object.extend(String.prototype, (function() { function gsub(pattern, replacement) { var result = '', source = this, match; replacement = prepareReplacement(replacement); + + if (Object.isString(pattern)) + pattern = RegExp.escape(pattern); if (!(pattern.length || pattern.source)) { replacement = replacement(''); diff --git a/test/unit/string_test.js b/test/unit/string_test.js index a0b016c..c6ced57 100644 --- a/test/unit/string_test.js +++ b/test/unit/string_test.js @@ -64,6 +64,19 @@ new Test.Unit.Runner({ source.gsub(/(.)(o+)/, '#{3}')); }, + testGsubWithTroublesomeCharacters: function() { + this.assertEqual('ab', 'a|b'.gsub('|', '')); + //'ab'.gsub('', ''); // freeze + this.assertEqual('ab', 'ab(?:)'.gsub('(?:)', '')); + this.assertEqual('ab', 'ab()'.gsub('()', '')); + this.assertEqual('ab', 'ab'.gsub('^', '')); + this.assertEqual('ab', 'a?b'.gsub('?', '')) + this.assertEqual('ab', 'a+b'.gsub('+', '')); + this.assertEqual('ab', 'a*b'.gsub('*', '')); + this.assertEqual('ab', 'a{1}b'.gsub('{1}', '')); + this.assertEqual('ab', 'a.b'.gsub('.', '')); + }, + testSubWithReplacementFunction: function() { var source = 'foo boo boz'; @@ -303,7 +316,7 @@ new Test.Unit.Runner({ }, testTemplateEvaluationWithIndexing: function() { - var source = '#{0} = #{[0]} - #{1} = #{[1]} - #{[2][0]} - #{[2].name} - #{first[0]} - #{[first][0]} - #{[\\]]} - #{first[\\]]}'; + var source = '#{0} = #{[0]} - #{1} = #{[1]} - #{[2][0]} - #{[2].name} - #{first[0]} - #{[first][0]} - #{[\]]} - #{first[\]]}'; var subject = [ 'zero', 'one', [ 'two-zero' ] ]; subject[2].name = 'two-zero-name'; subject.first = subject[2]; @@ -316,6 +329,8 @@ new Test.Unit.Runner({ this.assertEqual('two-zero', new Template('#{first[0]}').evaluate(subject)); this.assertEqual('\\', new Template('#{[\\]]}').evaluate(subject)); this.assertEqual('first\\', new Template('#{first[\\]]}').evaluate(subject)); + this.assertEqual('\\', new Template('#{[\]]}').evaluate(subject)); + this.assertEqual('first\\', new Template('#{first[\]]}').evaluate(subject)); this.assertEqual('empty - empty2', new Template('#{[]} - #{m[]}').evaluate({ '': 'empty', m: {'': 'empty2'}})); this.assertEqual('zero = zero - one = one - two-zero - two-zero-name - two-zero - two-zero - \\ - first\\', new Template(source).evaluate(subject)); },