Avoid String#sub with empty pattern to make endless loop [#534 state:resolved]
Signed-off-by: Sam Stephenson <sam@37signals.com>
This commit is contained in:
parent
9bb3d7517d
commit
ceb7b72621
|
@ -39,6 +39,11 @@ Object.extend(String.prototype, (function() {
|
|||
var result = '', source = this, match;
|
||||
replacement = prepareReplacement(replacement);
|
||||
|
||||
if (!(pattern.length || pattern.source)) {
|
||||
replacement = replacement('');
|
||||
return replacement + source.split('').join(replacement) + replacement;
|
||||
}
|
||||
|
||||
while (source.length > 0) {
|
||||
if (match = source.match(pattern)) {
|
||||
result += source.slice(0, match.index);
|
||||
|
|
|
@ -44,6 +44,11 @@ new Test.Unit.Runner({
|
|||
'ウィメンズ2007\nクルーズコレクション'.gsub(/\n/,'<br/>'));
|
||||
this.assertEqual('ウィメンズ2007<br/>クルーズコレクション',
|
||||
'ウィメンズ2007\nクルーズコレクション'.gsub('\n','<br/>'));
|
||||
|
||||
this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
|
||||
source.gsub('', 'bar'));
|
||||
this.assertEqual('barfbarobarobar barbbarobarobar barbbarobarzbar',
|
||||
source.gsub(new RegExp(''), 'bar'));
|
||||
},
|
||||
|
||||
testGsubWithReplacementTemplateString: function() {
|
||||
|
|
Loading…
Reference in New Issue