Skip to content

Commit 810f0f6

Browse files
Fix failing string test in Chrome.
1 parent 8c9ead4 commit 810f0f6

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/prototype/lang/string.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ Object.extend(String.prototype, (function() {
3838
var template = new Template(replacement);
3939
return function(match) { return template.evaluate(match) };
4040
}
41+
42+
// In some versions of Chrome, an empty RegExp has "(?:)" as a `source`
43+
// property instead of an empty string.
44+
function isNonEmptyRegExp(regexp) {
45+
return regexp.source && regexp.source !== '(?:)';
46+
}
47+
4148

4249
/**
4350
* String#gsub(pattern, replacement) -> String
@@ -93,8 +100,8 @@ Object.extend(String.prototype, (function() {
93100

94101
if (Object.isString(pattern))
95102
pattern = RegExp.escape(pattern);
96-
97-
if (!(pattern.length || pattern.source)) {
103+
104+
if (!(pattern.length || isNonEmptyRegExp(pattern))) {
98105
replacement = replacement('');
99106
return replacement + source.split('').join(replacement) + replacement;
100107
}

test/unit/string_test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ new Test.Unit.Runner({
4646
'ウィメンズ2007\nクルーズコレクション'.gsub('\n','<br/>'));
4747

4848
this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
49-
source.gsub('', 'bar'));
49+
source.gsub('', 'bar'), 'empty string');
5050
this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
51-
source.gsub(new RegExp(''), 'bar'));
51+
source.gsub(new RegExp(''), 'bar'), 'empty regexp');
5252
},
5353

5454
testGsubWithReplacementTemplateString: function() {

0 commit comments

Comments
 (0)