Skip to content

Commit

Permalink
String#gsub should escape RegExp metacharacters when the first argume…
Browse files Browse the repository at this point in the history
…nt is a string. [#469 state:resolved] (michael, kangax)
  • Loading branch information
savetheclocktower committed Feb 24, 2009
1 parent e3845ba commit e9bdaef
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions 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)
Expand Down
3 changes: 3 additions & 0 deletions src/lang/string.js
Expand Up @@ -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('');
Expand Down
17 changes: 16 additions & 1 deletion test/unit/string_test.js
Expand Up @@ -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';

Expand Down Expand Up @@ -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];
Expand All @@ -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));
},
Expand Down

0 comments on commit e9bdaef

Please sign in to comment.