Skip to content

Commit f50aa57

Browse files
authored
Merge pull request #1983 from minrk/fixCR
fix carriage return handling [backport]
2 parents ad5c1a2 + a77d5c3 commit f50aa57

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

notebook/static/base/js/utils.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,11 @@ define([
385385
// carriage return characters
386386
function fixCarriageReturn(txt) {
387387
txt = txt.replace(/\r+\n/gm, '\n'); // \r followed by \n --> newline
388-
while (txt.search(/\r/g) > -1) {
389-
var base = txt.match(/^.*\r+/m)[0].replace(/\r/, '');
390-
var insert = txt.match(/\r+.*$/m)[0].replace(/\r/, '');
388+
while (txt.search(/\r[^$]/g) > -1) {
389+
var base = txt.match(/^(.*)\r+/m)[1];
390+
var insert = txt.match(/\r+(.*)$/m)[1];
391391
insert = insert + base.slice(insert.length, base.length);
392-
txt = txt.replace(/\r+.*$/m, '\r').replace(/^.*\r+/m, insert);
392+
txt = txt.replace(/\r+.*$/m, '\r').replace(/^.*\r/m, insert);
393393
}
394394
return txt;
395395
}

notebook/tests/base/utils.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,31 @@ casper.notebook_test(function () {
2626

2727
this.test.assertEquals(result, output, "IPython.utils.fixConsole() handles [0m correctly");
2828

29+
var input = [
30+
'hasrn\r\n',
31+
'hasn\n',
32+
'\n',
33+
'abcdef\r',
34+
'hello\n',
35+
'ab3\r',
36+
'x2\r\r',
37+
'1\r',
38+
].join('');
39+
40+
var output = [
41+
'hasrn\n',
42+
'hasn\n',
43+
'\n',
44+
'hellof\n',
45+
'123\r'
46+
].join('');
47+
48+
var result = this.evaluate(function (input) {
49+
return IPython.utils.fixCarriageReturn(input);
50+
}, input);
51+
52+
this.test.assertEquals(result, output, "IPython.utils.fixCarriageReturns works");
53+
2954
this.thenEvaluate(function() {
3055
define('nbextensions/a', [], function() { window.a = true; });
3156
define('nbextensions/c', [], function() { window.c = true; });

0 commit comments

Comments
 (0)