Skip to content

Commit

Permalink
Merge pull request #659 from jdanyow/paste
Browse files Browse the repository at this point in the history
Normalize line endings on paste
  • Loading branch information
parisk authored May 11, 2017
2 parents 2221f70 + 0532e5c commit 6dad013
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/handlers/Clipboard.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,14 @@ describe('evaluateCopiedTextProcessing', function () {
assert.equal(processedText.indexOf(nonBreakingSpace), -1);
});
});

describe('evaluatePastedTextProcessing', function () {
it('should replace carriage return + line feed with line feed on windows', function () {
const pastedText = 'foo\r\nbar\r\n',
processedText = Clipboard.prepareTextForTerminal(pastedText, false),
windowsProcessedText = Clipboard.prepareTextForTerminal(pastedText, true);

assert.equal(processedText, 'foo\r\nbar\r\n');
assert.equal(windowsProcessedText, 'foo\nbar\n');
});
});
12 changes: 12 additions & 0 deletions src/handlers/Clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ export function prepareTextForClipboard(text: string): string {
return processedText;
}

/**
* Prepares text to be pasted into the terminal by normalizing the line endings
* @param text The pasted text that needs processing before inserting into the terminal
*/
export function prepareTextForTerminal(text: string, isMSWindows: boolean): string {
if (isMSWindows) {
return text.replace(/\r?\n/g, '\n');
}
return text;
}

/**
* Binds copy functionality to the given terminal.
* @param {ClipboardEvent} ev The original copy event to be handled
Expand Down Expand Up @@ -68,6 +79,7 @@ export function pasteHandler(ev: ClipboardEvent, term: ITerminal) {
let text: string;

let dispatchPaste = function(text) {
text = prepareTextForTerminal(text, term.browser.isMSWindows);
term.handler(text);
term.textarea.value = '';
return term.cancel(ev);
Expand Down

0 comments on commit 6dad013

Please sign in to comment.