const LINE_DATA_CHAR_INDEX = 1;
const LINE_DATA_WIDTH_INDEX = 2;
+const NON_BREAKING_SPACE_CHAR = String.fromCharCode(160);
+const ALL_NON_BREAKING_SPACE_REGEX = new RegExp(NON_BREAKING_SPACE_CHAR, 'g');
+
export class SelectionManager extends EventEmitter {
protected _model: SelectionModel;
result.push(this._translateBufferLineToString(this._buffer.get(end[1]), true, 0, end[0]));
}
- return result.join('\n');
+ // Format string by replacing non-breaking space chars with regular spaces
+ // and joining the array into a multi-line string.
+ const formattedResult = result.map(line => {
+ return line.replace(ALL_NON_BREAKING_SPACE_REGEX, ' ');
+ }).join('\n');
+
+ return formattedResult;
}
/**
import * as Terminal from '../xterm';
import * as Clipboard from './Clipboard';
-
-describe('evaluateCopiedTextProcessing', function () {
- it('should replace non-breaking spaces with regular spaces', () => {
- const nbsp = String.fromCharCode(160);
- const result = Clipboard.prepareTextForClipboard(`foo${nbsp}bar\ntest${nbsp}${nbsp}`);
- assert.equal(result, 'foo bar\ntest ');
- });
-});
-
describe('evaluatePastedTextProcessing', function () {
it('should replace carriage return + line feed with line feed on windows', function () {
const pastedText = 'foo\r\nbar\r\n',
declare var window: IWindow;
-const SPACE_CHAR = String.fromCharCode(32);
-const NON_BREAKING_SPACE_CHAR = String.fromCharCode(160);
-const ALL_NON_BREAKING_SPACE_REGEX = new RegExp(NON_BREAKING_SPACE_CHAR, 'g');
-
-/**
- * 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
- * @returns {string}
- */
-export function prepareTextForClipboard(text: string): string {
- // TODO: Pass an unjoined string array into this function so not splitting is needed
- return text.split('\n').map(line => {
- return line.replace(ALL_NON_BREAKING_SPACE_REGEX, SPACE_CHAR);
- }).join('\n');
-}
-
/**
* 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
* @param {ClipboardEvent} ev The original copy event to be handled
*/
export function copyHandler(ev: ClipboardEvent, term: ITerminal, selectionManager: ISelectionManager) {
- // We cast `window` to `any` type, because TypeScript has not declared the `clipboardData`
- // property that we use below for Internet Explorer.
- let text = prepareTextForClipboard(selectionManager.selectionText);
-
if (term.browser.isMSIE) {
- window.clipboardData.setData('Text', text);
+ window.clipboardData.setData('Text', selectionManager.selectionText);
} else {
- ev.clipboardData.setData('text/plain', text);
+ ev.clipboardData.setData('text/plain', selectionManager.selectionText);
}
- ev.preventDefault(); // Prevent or the original text will be copied.
+ // Prevent or the original text will be copied.
+ ev.preventDefault();
}
/**
textarea.style.zIndex = '1000';
// Get textarea ready to copy from the context menu
- textarea.value = prepareTextForClipboard(selectionManager.selectionText);
+ textarea.value = selectionManager.selectionText;
textarea.focus();
textarea.select();
import { CompositionHelper } from './CompositionHelper';
import { EventEmitter } from './EventEmitter';
import { Viewport } from './Viewport';
-import { rightClickHandler, pasteHandler, copyHandler, prepareTextForClipboard } from './handlers/Clipboard';
+import { rightClickHandler, pasteHandler, copyHandler } from './handlers/Clipboard';
import { CircularList } from './utils/CircularList';
import { C0 } from './EscapeSequences';
import { InputHandler } from './InputHandler';
* behavior outside of xterm.js.
*/
Terminal.prototype.getSelection = function() {
- // TODO: Should prepareTextForClipboard logic be moved to SelectionManager?
- return prepareTextForClipboard(this.selectionManager.selectionText);
+ return this.selectionManager.selectionText;
}
/**