From 67d6ad57c3719301e082d468c28ce4bd44fef4b4 Mon Sep 17 00:00:00 2001 From: Daniel Imms Date: Thu, 26 Jul 2018 12:23:34 -0700 Subject: [PATCH] Add tests --- src/SelectionManager.test.ts | 36 ++++++++++++++++++++++++++++++++++++ src/SelectionManager.ts | 6 ++++-- src/SelectionModel.test.ts | 5 +++++ src/SelectionModel.ts | 2 +- 4 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/SelectionManager.test.ts b/src/SelectionManager.test.ts index 4ae0c08f..20e1ca60 100644 --- a/src/SelectionManager.test.ts +++ b/src/SelectionManager.test.ts @@ -183,6 +183,42 @@ describe('SelectionManager', () => { selectionManager.selectWordAt([15, 0]); assert.equal(selectionManager.selectionText, 'ij"'); }); + it('should expand upwards or downards for wrapped lines', () => { + buffer.lines.set(0, stringToRow(' foo')); + buffer.lines.set(1, stringToRow('bar ')); + (buffer.lines.get(1)).isWrapped = true; + selectionManager.selectWordAt([1, 1]); + assert.equal(selectionManager.selectionText, 'foobar'); + selectionManager.model.clearSelection(); + selectionManager.selectWordAt([78, 0]); + assert.equal(selectionManager.selectionText, 'foobar'); + }); + it('should expand both upwards and downwards for word wrapped over many lines', () => { + const expectedText = 'fooaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccbar'; + buffer.lines.set(0, stringToRow(' foo')); + buffer.lines.set(1, stringToRow('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa')); + buffer.lines.set(2, stringToRow('bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb')); + buffer.lines.set(3, stringToRow('cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc')); + buffer.lines.set(4, stringToRow('bar ')); + (buffer.lines.get(1)).isWrapped = true; + (buffer.lines.get(2)).isWrapped = true; + (buffer.lines.get(3)).isWrapped = true; + (buffer.lines.get(4)).isWrapped = true; + selectionManager.selectWordAt([78, 0]); + assert.equal(selectionManager.selectionText, expectedText); + selectionManager.model.clearSelection(); + selectionManager.selectWordAt([40, 1]); + assert.equal(selectionManager.selectionText, expectedText); + selectionManager.model.clearSelection(); + selectionManager.selectWordAt([40, 2]); + assert.equal(selectionManager.selectionText, expectedText); + selectionManager.model.clearSelection(); + selectionManager.selectWordAt([40, 3]); + assert.equal(selectionManager.selectionText, expectedText); + selectionManager.model.clearSelection(); + selectionManager.selectWordAt([1, 4]); + assert.equal(selectionManager.selectionText, expectedText); + }); describe('emoji', () => { it('should treat a single emoji as a word when wrapped in spaces', () => { buffer.lines.set(0, stringToRow(' ⚽ a')); // The a is here to prevent the space being trimmed in selectionText diff --git a/src/SelectionManager.ts b/src/SelectionManager.ts index 3fcc600a..3731eb9a 100644 --- a/src/SelectionManager.ts +++ b/src/SelectionManager.ts @@ -451,8 +451,10 @@ export class SelectionManager extends EventEmitter implements ISelectionManager * Removes the listeners that are registered when mousedown is triggered. */ private _removeMouseDownListeners(): void { - this._terminal.element.ownerDocument.removeEventListener('mousemove', this._mouseMoveListener); - this._terminal.element.ownerDocument.removeEventListener('mouseup', this._mouseUpListener); + if (this._terminal.element.ownerDocument) { + this._terminal.element.ownerDocument.removeEventListener('mousemove', this._mouseMoveListener); + this._terminal.element.ownerDocument.removeEventListener('mouseup', this._mouseUpListener); + } clearInterval(this._dragScrollIntervalTimer); this._dragScrollIntervalTimer = null; } diff --git a/src/SelectionModel.test.ts b/src/SelectionModel.test.ts index 85486bab..59b2ce75 100644 --- a/src/SelectionModel.test.ts +++ b/src/SelectionModel.test.ts @@ -128,6 +128,11 @@ describe('SelectionManager', () => { model.selectionEnd = [3, 2]; assert.deepEqual(model.finalSelectionEnd, [4, 2]); }); + it('should return the end on a different row when start + length overflows onto a following row', () => { + model.selectionStart = [78, 2]; + model.selectionStartLength = 4; + assert.deepEqual(model.finalSelectionEnd, [2, 3]); + }); it('should return selection end if selection end is after selection start + length', () => { model.selectionStart = [2, 2]; model.selectionStartLength = 2; diff --git a/src/SelectionModel.ts b/src/SelectionModel.ts index b8e770b7..f87667f2 100644 --- a/src/SelectionModel.ts +++ b/src/SelectionModel.ts @@ -76,7 +76,7 @@ export class SelectionModel { return null; } - // Use the selection start if the end doesn't exist or they're reversed + // Use the selection start + length if the end doesn't exist or they're reversed if (!this.selectionEnd || this.areSelectionValuesReversed()) { const startPlusLength = this.selectionStart[0] + this.selectionStartLength; if (startPlusLength > this._terminal.cols) {