diff --git a/lib/TextBox.js b/lib/TextBox.js index 0aca54c..ab9e048 100644 --- a/lib/TextBox.js +++ b/lib/TextBox.js @@ -1,6 +1,7 @@ import { assign, bind, + isUndefined, pick } from 'min-dash'; @@ -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); @@ -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); +} \ No newline at end of file diff --git a/test/DirectEditingSpec.js b/test/DirectEditingSpec.js index b17a2f9..fdfbb32 100644 --- a/test/DirectEditingSpec.js +++ b/test/DirectEditingSpec.js @@ -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

Bar
'); + })); + + + it('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

Bar
'); + })); + }); });