Skip to content

Commit

Permalink
fix: preserve Windows newline characters
Browse files Browse the repository at this point in the history
  • Loading branch information
philippfromme committed May 20, 2021
1 parent 4c925d7 commit ba088af
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/TextBox.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
assign,
bind,
isUndefined,
pick
} from 'min-dash';

Expand Down Expand Up @@ -212,6 +213,7 @@ TextBox.prototype.handlePaste = function(e) {
};

TextBox.prototype.insertText = function(text) {
text = normalizeEndOfLineSequences(text);

// insertText command not supported by Internet Explorer
var success = document.execCommand('insertText', false, text);
Expand Down Expand Up @@ -452,3 +454,13 @@ TextBox.prototype.setSelection = function(container, offset) {
selection.removeAllRanges();
selection.addRange(range);
};

// helpers //////////

function normalizeEndOfLineSequences(string, endOfLineSequence) {
if (isUndefined(endOfLineSequence)) {
endOfLineSequence = '\n';
}

return string.replace(/\r\n|\r|\n/g, endOfLineSequence);
}
44 changes: 44 additions & 0 deletions test/DirectEditingSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,50 @@ describe('diagram-js-direct-editing', function() {
expect(directEditing._textbox.content.innerHTML).to.eql('FOO<video src=1 onerror=alert(\'hueh\')>');
}));


it('should preserve line breaks (LF)', inject(function(canvas, directEditing) {

// given
var shape = {
id: 's1',
x: 20, y: 10, width: 60, height: 50,
label: 'FOO'
};
canvas.addShape(shape);

directEditing.activate(shape);

var textBox = directEditing._textbox;

// when
textBox.insertText('Foo\n\nBar');

// then
expect(directEditing._textbox.content.innerHTML).to.eql('FOOFoo<div><br></div><div>Bar</div>');
}));


it.only('should preserve line breaks (CRLF)', inject(function(canvas, directEditing) {

// given
var shape = {
id: 's1',
x: 20, y: 10, width: 60, height: 50,
label: 'FOO'
};
canvas.addShape(shape);

directEditing.activate(shape);

var textBox = directEditing._textbox;

// when
textBox.insertText('Foo\r\n\r\nBar');

// then
expect(directEditing._textbox.content.innerHTML).to.eql('FOOFoo<div><br></div><div>Bar</div>');
}));

});

});
Expand Down

0 comments on commit ba088af

Please sign in to comment.