diff --git a/src/utils/Mouse.ts b/src/utils/Mouse.ts index ce852ace..a9efdf93 100644 --- a/src/utils/Mouse.ts +++ b/src/utils/Mouse.ts @@ -13,7 +13,7 @@ import { CharMeasure } from './CharMeasure'; * @param charMeasure The char measure object used to determine character sizes. */ export function getCoords(event: MouseEvent, rowContainer: HTMLElement, charMeasure: CharMeasure): [number, number] { - // ignore browsers without pageX for now + // Ignore browsers that don't support MouseEvent.pageX if (event.pageX == null) { return null; } @@ -22,15 +22,15 @@ export function getCoords(event: MouseEvent, rowContainer: HTMLElement, charMeas let y = event.pageY; let el = rowContainer; - // should probably check offsetParent - // but this is more portable + // Converts the coordinates from being relative to the document to being + // relative to the terminal. while (el && el !== self.document.documentElement) { x -= el.offsetLeft; y -= el.offsetTop; el = 'offsetParent' in el ? el.offsetParent : el.parentElement; } - // convert to cols/rows + // Convert to cols/rows x = Math.ceil(x / charMeasure.width); y = Math.ceil(y / charMeasure.height); @@ -52,14 +52,11 @@ export function getRawByteCoords(event: MouseEvent, rowContainer: HTMLElement, c let x = coords[0]; let y = coords[1]; - // be sure to avoid sending bad positions to the program - if (x < 0) x = 0; - if (x > colCount) x = colCount; - if (y < 0) y = 0; - if (y > rowCount) y = rowCount; + // Ensure coordinates are within the terminal viewport. + x = Math.min(Math.max(x, 0), colCount); + y = Math.min(Math.max(y, 0), rowCount); - // xterm sends raw bytes and - // starts at 32 (SP) for each. + // xterm sends raw bytes and starts at 32 (SP) for each. x += 32; y += 32;