Browse files

Fix failing string test in Chrome.

  • Loading branch information...
1 parent 8c9ead4 commit 810f0f609df2499034391165da94065afce7c91a @savetheclocktower savetheclocktower committed Feb 5, 2014
Showing with 11 additions and 4 deletions.
  1. +9 −2 src/prototype/lang/string.js
  2. +2 −2 test/unit/string_test.js
View
11 src/prototype/lang/string.js
@@ -38,6 +38,13 @@ Object.extend(String.prototype, (function() {
var template = new Template(replacement);
return function(match) { return template.evaluate(match) };
}
+
+ // In some versions of Chrome, an empty RegExp has "(?:)" as a `source`
+ // property instead of an empty string.
+ function isNonEmptyRegExp(regexp) {
+ return regexp.source && regexp.source !== '(?:)';
+ }
+
/**
* String#gsub(pattern, replacement) -> String
@@ -93,8 +100,8 @@ Object.extend(String.prototype, (function() {
if (Object.isString(pattern))
pattern = RegExp.escape(pattern);
-
- if (!(pattern.length || pattern.source)) {
+
+ if (!(pattern.length || isNonEmptyRegExp(pattern))) {
replacement = replacement('');
return replacement + source.split('').join(replacement) + replacement;
}
View
4 test/unit/string_test.js
@@ -46,9 +46,9 @@ new Test.Unit.Runner({
'ウィメンズ2007\nクルーズコレクション'.gsub('\n','<br/>'));
this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
- source.gsub('', 'bar'));
+ source.gsub('', 'bar'), 'empty string');
this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
- source.gsub(new RegExp(''), 'bar'));
+ source.gsub(new RegExp(''), 'bar'), 'empty regexp');
},
testGsubWithReplacementTemplateString: function() {

0 comments on commit 810f0f6

Please sign in to comment.