|
1 | | -import { adapter } from '../adapter'; |
2 | | -import isIframeWindow from './utils/is-window-iframe'; |
3 | | -import { AxisValuesData } from '../utils/values/axis-values'; |
4 | | -import { SharedWindow } from '../types'; |
| 1 | +import { AxisValuesData } from '../core/utils/values/axis-values'; |
5 | 2 | // @ts-ignore |
6 | | -import { Promise, utils } from '../../client/driver/deps/hammerhead'; |
7 | | -// @ts-ignore |
8 | | -import * as domUtils from '../../client/core/utils/dom'; |
| 3 | +import { Promise, utils } from '../driver/deps/hammerhead'; |
| 4 | +import * as domUtils from '../core/utils/dom'; |
| 5 | +import * as positionUtils from '../core/utils/position'; |
| 6 | +import getElementExceptUI from './utils/get-element-except-ui'; |
9 | 7 |
|
10 | | -function ensureImageMap<E> (imgElement: E, areaElement: E): Promise<E> { |
| 8 | +function ensureImageMap (imgElement: Element, areaElement: Element): Promise<HTMLElement> { |
11 | 9 | return Promise.resolve(domUtils.closest(areaElement, 'map')) |
12 | 10 | .then((mapElement: HTMLMapElement) => { |
13 | 11 | return mapElement && mapElement.name === domUtils.getImgMapName(imgElement) ? areaElement : imgElement; |
14 | 12 | }); |
15 | 13 | } |
16 | 14 |
|
17 | | -function findElementOrNonEmptyChildFromPoint<E> (point: AxisValuesData<number>, element?: E): Promise<E | null> { |
18 | | - return Promise.resolve(adapter.position.getElementFromPoint(point)) |
| 15 | +function findElementOrNonEmptyChildFromPoint (point: AxisValuesData<number>, element?: HTMLElement): Promise<HTMLElement | null> { |
| 16 | + return Promise.resolve(positionUtils.getElementFromPoint(point)) |
19 | 17 | .then((topElement: HTMLElement) => { |
20 | 18 | return Promise.resolve(domUtils.containsElement(element, topElement)) |
21 | 19 | .then((containsEl: HTMLElement) => containsEl && domUtils.getNodeText(topElement)) |
22 | 20 | .then((isNonEmptyChild: HTMLElement) => isNonEmptyChild || topElement && domUtils.isNodeEqual(topElement, element) ? topElement : null); |
23 | 21 | }); |
24 | 22 | } |
25 | 23 |
|
26 | | -function correctTopElementByExpectedElement<E> (topElement: E, expectedElement?: E): Promise<E> | E { |
| 24 | +function correctTopElementByExpectedElement (topElement: Element, expectedElement?: HTMLElement): Promise<HTMLElement> | HTMLElement { |
27 | 25 | if (!expectedElement || !topElement || domUtils.isNodeEqual(topElement, expectedElement)) |
28 | 26 | return topElement; |
29 | 27 |
|
@@ -56,24 +54,24 @@ function correctTopElementByExpectedElement<E> (topElement: E, expectedElement?: |
56 | 54 | if (!shouldSearchForMultilineLink) |
57 | 55 | return topElement; |
58 | 56 |
|
59 | | - return Promise.resolve(adapter.position.getClientDimensions(expectedElement)) |
| 57 | + return Promise.resolve(positionUtils.getClientDimensions(expectedElement)) |
60 | 58 | .then((linkRect: any) => findElementOrNonEmptyChildFromPoint({ x: linkRect.right - 1, y: linkRect.top + 1 }, expectedElement) |
61 | 59 | .then((el: any) => el || findElementOrNonEmptyChildFromPoint({ x: linkRect.left + 1, y: linkRect.bottom - 1 }, expectedElement)) |
62 | 60 | .then((el: any) => el || topElement)); |
63 | 61 | }); |
64 | 62 | } |
65 | 63 |
|
66 | | -export default function getElementFromPoint<E, W extends SharedWindow> (point: AxisValuesData<number>, win: W, expectedEl?: E): Promise<E> { |
67 | | - return adapter.getElementExceptUI(point) |
68 | | - .then((topElement: E) => { |
| 64 | +export default function getElementFromPoint (point: AxisValuesData<number>, win?: Window, expectedEl?: HTMLElement): Promise<HTMLElement> { |
| 65 | + return getElementExceptUI(point) |
| 66 | + .then((topElement: HTMLElement) => { |
69 | 67 | // NOTE: when trying to get an element by elementFromPoint in iframe and the target |
70 | 68 | // element is under any of shadow-ui elements, you will get null (only in IE). |
71 | 69 | // In this case, you should hide a top window's shadow-ui root to obtain an element. |
72 | 70 | let resChain = Promise.resolve(topElement); |
73 | 71 |
|
74 | | - if (!topElement && isIframeWindow(win) && point.x > 0 && point.y > 0) |
75 | | - resChain = resChain.then(() => adapter.getElementExceptUI(point, true)); |
| 72 | + if (!topElement && utils.dom.isIframeWindow(win || window) && point.x > 0 && point.y > 0) |
| 73 | + resChain = resChain.then(() => getElementExceptUI(point, true)); |
76 | 74 |
|
77 | | - return resChain.then((element: E) => correctTopElementByExpectedElement(element, expectedEl)); |
| 75 | + return resChain.then((element: HTMLElement) => correctTopElementByExpectedElement(element, expectedEl)); |
78 | 76 | }); |
79 | 77 | } |
0 commit comments