]> git.proxmox.com Git - mirror_xterm.js.git/blobdiff - src/SelectionManager.ts
Make SelectionManager more resilient
[mirror_xterm.js.git] / src / SelectionManager.ts
index 31ee8c2c8f677e6b34ee8ba2bade1680b43abd3b..8f1d0e2084a2426dddd56b14e07f9c4c610fcf76 100644 (file)
@@ -7,7 +7,7 @@ import * as Browser from './utils/Browser';
 import { CharMeasure } from './utils/CharMeasure';
 import { CircularList } from './utils/CircularList';
 import { EventEmitter } from './EventEmitter';
-import { ITerminal } from './Interfaces';
+import { ITerminal, ICircularList } from './Interfaces';
 import { SelectionModel } from './SelectionModel';
 import { translateBufferLineToString } from './utils/BufferLine';
 
@@ -98,7 +98,7 @@ export class SelectionManager extends EventEmitter {
 
   constructor(
     private _terminal: ITerminal,
-    private _buffer: CircularList<any>,
+    private _buffer: ICircularList<[number, string, number][]>,
     private _rowContainer: HTMLElement,
     private _charMeasure: CharMeasure
   ) {
@@ -147,7 +147,7 @@ export class SelectionManager extends EventEmitter {
    * switched in or out.
    * @param buffer The active buffer.
    */
-  public setBuffer(buffer: CircularList<any>): void {
+  public setBuffer(buffer: ICircularList<[number, string, number][]>): void {
     this._buffer = buffer;
     this.clearSelection();
   }
@@ -186,7 +186,7 @@ export class SelectionManager extends EventEmitter {
     for (let i = start[1] + 1; i <= end[1] - 1; i++) {
       const bufferLine = this._buffer.get(i);
       const lineText = translateBufferLineToString(bufferLine, true);
-      if (bufferLine.isWrapped) {
+      if ((<any>bufferLine).isWrapped) {
         result[result.length - 1] += lineText;
       } else {
         result.push(lineText);
@@ -197,7 +197,7 @@ export class SelectionManager extends EventEmitter {
     if (start[1] !== end[1]) {
       const bufferLine = this._buffer.get(end[1]);
       const lineText = translateBufferLineToString(bufferLine, true, 0, end[0]);
-      if (bufferLine.isWrapped) {
+      if ((<any>bufferLine).isWrapped) {
         result[result.length - 1] += lineText;
       } else {
         result.push(lineText);
@@ -277,11 +277,15 @@ export class SelectionManager extends EventEmitter {
    */
   private _getMouseBufferCoords(event: MouseEvent): [number, number] {
     const coords = Mouse.getCoords(event, this._rowContainer, this._charMeasure, this._terminal.cols, this._terminal.rows, true);
+    if (!coords) {
+      return null;
+    }
+
     // Convert to 0-based
     coords[0]--;
     coords[1]--;
     // Convert viewport coords to buffer coords
-    coords[1] += this._terminal.ydisp;
+    coords[1] += this._terminal.buffer.ydisp;
     return coords;
   }
 
@@ -377,15 +381,25 @@ export class SelectionManager extends EventEmitter {
     this._model.selectionStartLength = 0;
     this._model.isSelectAllActive = false;
     this._activeSelectionMode = SelectionMode.NORMAL;
+
+    // Initialize the new selection
     this._model.selectionStart = this._getMouseBufferCoords(event);
-    if (this._model.selectionStart) {
-      this._model.selectionEnd = null;
-      // If the mouse is over the second half of a wide character, adjust the
-      // selection to cover the whole character
-      const char = this._buffer.get(this._model.selectionStart[1])[this._model.selectionStart[0]];
-      if (char[LINE_DATA_WIDTH_INDEX] === 0) {
-        this._model.selectionStart[0]++;
-      }
+    if (!this._model.selectionStart) {
+      return;
+    }
+    this._model.selectionEnd = null;
+
+    // Ensure the line exists
+    const line = this._buffer.get(this._model.selectionStart[1]);
+    if (!line) {
+      return;
+    }
+
+    // If the mouse is over the second half of a wide character, adjust the
+    // selection to cover the whole character
+    const char = line[this._model.selectionStart[0]];
+    if (char[LINE_DATA_WIDTH_INDEX] === 0) {
+      this._model.selectionStart[0]++;
     }
   }
 
@@ -426,6 +440,10 @@ export class SelectionManager extends EventEmitter {
 
     // Set the initial selection end based on the mouse coordinates
     this._model.selectionEnd = this._getMouseBufferCoords(event);
+    if (!this._model.selectionEnd) {
+      this.refresh(true);
+      return;
+    }
 
     // Select the entire line if line select mode is active.
     if (this._activeSelectionMode === SelectionMode.LINE) {
@@ -476,9 +494,9 @@ export class SelectionManager extends EventEmitter {
       this._terminal.scrollDisp(this._dragScrollAmount, false);
       // Re-evaluate selection
       if (this._dragScrollAmount > 0) {
-        this._model.selectionEnd = [this._terminal.cols - 1, this._terminal.ydisp + this._terminal.rows];
+        this._model.selectionEnd = [this._terminal.cols - 1, this._terminal.buffer.ydisp + this._terminal.rows];
       } else {
-        this._model.selectionEnd = [0, this._terminal.ydisp];
+        this._model.selectionEnd = [0, this._terminal.buffer.ydisp];
       }
       this.refresh();
     }
@@ -522,6 +540,10 @@ export class SelectionManager extends EventEmitter {
    */
   private _getWordAt(coords: [number, number]): IWordPosition {
     const bufferLine = this._buffer.get(coords[1]);
+    if (!bufferLine) {
+      return null;
+    }
+
     const line = translateBufferLineToString(bufferLine, false);
 
     // Get actual index, taking into consideration wide characters
@@ -590,8 +612,10 @@ export class SelectionManager extends EventEmitter {
    */
   protected _selectWordAt(coords: [number, number]): void {
     const wordPosition = this._getWordAt(coords);
-    this._model.selectionStart = [wordPosition.start, coords[1]];
-    this._model.selectionStartLength = wordPosition.length;
+    if (wordPosition) {
+      this._model.selectionStart = [wordPosition.start, coords[1]];
+      this._model.selectionStartLength = wordPosition.length;
+    }
   }
 
   /**
@@ -600,7 +624,9 @@ export class SelectionManager extends EventEmitter {
    */
   private _selectToWordAt(coords: [number, number]): void {
     const wordPosition = this._getWordAt(coords);
-    this._model.selectionEnd = [this._model.areSelectionValuesReversed() ? wordPosition.start : (wordPosition.start + wordPosition.length), coords[1]];
+    if (wordPosition) {
+      this._model.selectionEnd = [this._model.areSelectionValuesReversed() ? wordPosition.start : (wordPosition.start + wordPosition.length), coords[1]];
+    }
   }
 
   /**