From fa1cd89aa9feae2e5dfb5aa669ed6964dbf8db48 Mon Sep 17 00:00:00 2001 From: Paris Date: Fri, 10 Jun 2016 16:17:14 +0300 Subject: [PATCH 1/3] Fix copying of non-breaking spaces --- src/xterm.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/xterm.js b/src/xterm.js index a806a03..5984e7d 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -551,13 +551,22 @@ /** - * Bind copy event. Stript trailing whitespaces from selection. + * Bind copy event. */ Terminal.bindCopy = function(term) { on(term.element, 'copy', function(ev) { - var selectedText = window.getSelection().toString(), + var space = String.fromCharCode(32), + nonBreakingSpace = String.fromCharCode(160), + allNonBreakingSpaces = new RegExp(nonBreakingSpace, 'g'), + selectedText = window.getSelection().toString(), copiedText = selectedText.split('\n').map(function (element) { - return element.replace(/\s+$/g, ''); + /** + * Strip all trailing white spaces and convert all non-breaking spaces to regular + * spaces. + */ + var line = element.replace(/\s+$/g, '').replace(allNonBreakingSpaces, space); + + return line; }).join('\n'); ev.clipboardData.setData('text/plain', copiedText); ev.preventDefault(); From 00f4232ecca1270131ce822a1576ee2e539c22d8 Mon Sep 17 00:00:00 2001 From: Paris Date: Fri, 10 Jun 2016 16:27:58 +0300 Subject: [PATCH 2/3] Export copied text processing to static method --- src/xterm.js | 42 ++++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 14 deletions(-) diff --git a/src/xterm.js b/src/xterm.js index 5984e7d..ce0a246 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -549,26 +549,40 @@ }, true); }; + /** + * Prepares text copied from terminal selection, to be saved in the clipboard by: + * 1. stripping all trailing white spaces + * 2. converting all non-breaking spaces to regular spaces + * @param {string} text The copied text that needs processing for storing in clipboard + * @static + */ + Terminal.prepareCopiedTextForClipboard = function (text) { + var space = String.fromCharCode(32), + nonBreakingSpace = String.fromCharCode(160), + allNonBreakingSpaces = new RegExp(nonBreakingSpace, 'g'), + processedText = text.split('\n').map(function (line) { + /** + * Strip all trailing white spaces and convert all non-breaking spaces to regular + * spaces. + */ + var processedLine = line.replace(/\s+$/g, '').replace(allNonBreakingSpaces, space); + + return processedLine; + }).join('\n'); + + return processedText; + }; /** - * Bind copy event. + * Binds copy functionality to the given terminal. + * @static */ Terminal.bindCopy = function(term) { on(term.element, 'copy', function(ev) { - var space = String.fromCharCode(32), - nonBreakingSpace = String.fromCharCode(160), - allNonBreakingSpaces = new RegExp(nonBreakingSpace, 'g'), - selectedText = window.getSelection().toString(), - copiedText = selectedText.split('\n').map(function (element) { - /** - * Strip all trailing white spaces and convert all non-breaking spaces to regular - * spaces. - */ - var line = element.replace(/\s+$/g, '').replace(allNonBreakingSpaces, space); + var copiedText = window.getSelection().toString(), + text = Terminal.prepareCopiedTextForClipboard(copiedText); - return line; - }).join('\n'); - ev.clipboardData.setData('text/plain', copiedText); + ev.clipboardData.setData('text/plain', text); ev.preventDefault(); }); }; From fed92ac5c8a312b31b061184fe1f1470e740825b Mon Sep 17 00:00:00 2001 From: Paris Date: Fri, 10 Jun 2016 16:35:56 +0300 Subject: [PATCH 3/3] Implement tests --- src/xterm.js | 1 + test/test.js | 14 ++++++++++++++ 2 files changed, 15 insertions(+) diff --git a/src/xterm.js b/src/xterm.js index ce0a246..273fdca 100644 --- a/src/xterm.js +++ b/src/xterm.js @@ -554,6 +554,7 @@ * 1. stripping all trailing white spaces * 2. converting all non-breaking spaces to regular spaces * @param {string} text The copied text that needs processing for storing in clipboard + * @returns {string} * @static */ Terminal.prepareCopiedTextForClipboard = function (text) { diff --git a/test/test.js b/test/test.js index e603d65..ffbc285 100644 --- a/test/test.js +++ b/test/test.js @@ -54,4 +54,18 @@ describe('xterm.js', function() { assert.equal(xterm.evaluateKeyEscapeSequence({ ctrlKey: true, keyCode: 39 }).key, '\x1b[5C'); // CSI 5 C }); }); + + describe('evaluateCopiedTextProcessing', function () { + it('should strip trailing whitespaces and replace nbsps with spaces', function () { + var nonBreakingSpace = String.fromCharCode(160), + copiedText = 'echo' + nonBreakingSpace + 'hello' + nonBreakingSpace, + processedText = Terminal.prepareCopiedTextForClipboard(copiedText); + + // No trailing spaces + assert.equal(processedText.match(/\s+$/), null); + + // No non-breaking space + assert.equal(processedText.indexOf(nonBreakingSpace), -1); + }); + }); });