From 246633512e9fd8980a6b4d7afa17eb26e55ba5c0 Mon Sep 17 00:00:00 2001 From: Alex Dima Date: Thu, 25 Feb 2016 11:39:42 +0100 Subject: [PATCH] Fixes #2802 --- .../editor/browser/controller/mouseTarget.ts | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/src/vs/editor/browser/controller/mouseTarget.ts b/src/vs/editor/browser/controller/mouseTarget.ts index 37e2f5065e156..f008144210a2a 100644 --- a/src/vs/editor/browser/controller/mouseTarget.ts +++ b/src/vs/editor/browser/controller/mouseTarget.ts @@ -362,34 +362,49 @@ export class MouseTargetFactory { } private _actualDoHitTestWithCaretRangeFromPoint(hitx:number, hity:number): IHitTestResult { - var resultPosition: IPosition = null; - var resultHitTarget: Element = null; - var range:Range = (document).caretRangeFromPoint(hitx, hity); + let range:Range = (document).caretRangeFromPoint(hitx, hity); - var container = range ? range.startContainer : null; - var parent1 = container ? container.parentNode : null; - var parent2 = parent1 ? parent1.parentNode : null; - var parent3 = parent2 ? parent2.parentNode : null; + if (!range || !range.startContainer) { + return { + position: null, + hitTarget: null + }; + } - var parent2ClassName = parent2 && parent2.nodeType === parent2.ELEMENT_NODE ? (parent2).className : ''; - var parent3ClassName = parent3 && parent3.nodeType === parent3.ELEMENT_NODE ? (parent3).className : ''; + // Chrome always hits a TEXT_NODE, while Edge sometimes hits a token span + let startContainer = range.startContainer; - if (parent3ClassName === ClassNames.VIEW_LINE) { - resultPosition = this.viewHelper.getPositionFromDOMInfo(range.startContainer.parentNode, range.startOffset); - } else if (parent2ClassName === ClassNames.VIEW_LINE) { - resultPosition = this.viewHelper.getPositionFromDOMInfo(range.startContainer, range.startOffset); - } else { - // Looks like we've hit something foreign - resultHitTarget = parent1; - } + if (startContainer.nodeType === startContainer.TEXT_NODE) { + // startContainer is expected to be the token text + let parent1 = startContainer.parentNode; // expected to be the token span + let parent2 = parent1 ? parent1.parentNode : null; // expected to be the view line container span + let parent3 = parent2 ? parent2.parentNode : null; // expected to be the view line div + let parent3ClassName = parent3 && parent3.nodeType === parent3.ELEMENT_NODE ? (parent3).className : null; - // WebKit now shows warning in console for calling Range.detach(), because it is a no-op - // per DOM (http://dom.spec.whatwg.org/#dom-range-detach), therefore not calling .detach() anymore. + if (parent3ClassName === ClassNames.VIEW_LINE) { + return { + position: this.viewHelper.getPositionFromDOMInfo(parent1, range.startOffset), + hitTarget: null + }; + } + } else if (startContainer.nodeType === startContainer.ELEMENT_NODE) { + // startContainer is expected to be the token span + let parent1 = startContainer.parentNode; // expected to be the view line container span + let parent2 = parent1 ? parent1.parentNode : null; // expected to be the view line div + let parent2ClassName = parent2 && parent2.nodeType === parent2.ELEMENT_NODE ? (parent2).className : null; + + if (parent2ClassName === ClassNames.VIEW_LINE) { + return { + position: this.viewHelper.getPositionFromDOMInfo(startContainer, (startContainer).textContent.length), + hitTarget: null + }; + } + } return { - position: resultPosition, - hitTarget: resultHitTarget + position: null, + hitTarget: range.startContainer.parentNode }; }