diff --git a/notebook/static/base/js/utils.js b/notebook/static/base/js/utils.js index 66c1db918..46911233b 100644 --- a/notebook/static/base/js/utils.js +++ b/notebook/static/base/js/utils.js @@ -445,11 +445,11 @@ define([ // carriage return characters function fixCarriageReturn(txt) { txt = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline - while (txt.search(/\r/g) > -1) { - var base = txt.match(/^.*\r+/m)[0].replace(/\r/, ''); - var insert = txt.match(/\r+.*$/m)[0].replace(/\r/, ''); + while (txt.search(/\r[^$]/g) > -1) { + var base = txt.match(/^(.*)\r+/m)[1]; + var insert = txt.match(/\r+(.*)$/m)[1]; insert = insert + base.slice(insert.length, base.length); - txt = txt.replace(/\r+.*$/m, '\r').replace(/^.*\r+/m, insert); + txt = txt.replace(/\r+.*$/m, '\r').replace(/^.*\r/m, insert); } return txt; } diff --git a/notebook/tests/base/utils.js b/notebook/tests/base/utils.js index 0038e58cd..4d6428a68 100644 --- a/notebook/tests/base/utils.js +++ b/notebook/tests/base/utils.js @@ -48,7 +48,33 @@ casper.notebook_test(function () { that.test.assertEquals(result, testcase.result, "Overwriting characters processed"); }); + var input = [ + 'hasrn\r\n', + 'hasn\n', + '\n', + 'abcdef\r', + 'hello\n', + 'ab3\r', + 'x2\r\r', + '1\r', + ].join(''); + + var output = [ + 'hasrn\n', + 'hasn\n', + '\n', + 'hellof\n', + '123\r' + ].join(''); + + var result = this.evaluate(function (input) { + return IPython.utils.fixCarriageReturn(input); + }, input); + + this.test.assertEquals(result, output, "IPython.utils.fixCarriageReturns works"); + // Test load_extensions + this.thenEvaluate(function() { define('nbextensions/a', [], function() { window.a = true; }); define('nbextensions/c', [], function() { window.c = true; });