jsdom icon indicating copy to clipboard operation
jsdom copied to clipboard

Implement `Document` `elementFromPoint()`

Open dsathyakumar opened this issue 9 years ago • 7 comments

TypeError: Object [object HTMLDocument] has no method 'elementFromPoint'

document.elementFromPoint is not implemented by jsdom. Raising it as an issue cos it was raised elsewhere https://github.com/inikulin/parse5/issues/50

dsathyakumar avatar Apr 01 '16 16:04 dsathyakumar

It's impossible to implement it in jsdom currently, because it requires layouting.

inikulin avatar Apr 01 '16 18:04 inikulin

It could at least be stubbed, though.

quantizor avatar Apr 01 '16 19:04 quantizor

In that case feature detection wouldn't work anymore - you can also easily stub it in the created callback.

Sebmaster avatar Apr 01 '16 19:04 Sebmaster

I'm working on a library (built with Node on server side) that conducts accessibility (A11y) audits on HTML, for which one of the utils has code to check if an element is offscreen, in scrollable area, outside the viewport and various such stuff.

document.elementFromPoint was an easy way to calculate it. The issue began when I started writing server side test cases for the utils, via JSDOM. It wasn't supporting it.

dsathyakumar avatar Apr 02 '16 04:04 dsathyakumar

Yeah that will realistically never work in JSDOM. No layout engine = no screen, no scrolling, etc.

For your needs I'd recommend something like phantom.

quantizor avatar Apr 02 '16 04:04 quantizor

It's interesting why some angular unit tests in my repo have been passing with jest 22. They test code which uses this method. Now with jest 27, I hit this issue.

gkarapeev avatar Mar 10 '22 18:03 gkarapeev

It is unfortunate today that using document.elementFromPoint causes an error given that document.elementFromPoint is an old and stable API.

I propose that document.elementFromPoint be added to jsdom, and that the function would just return null, as null is a valid return of the function. Making this change would mean that calls to document.elementFromPoint would no longer error

document.elementFromPoint(x: number, y: number): Element | null

Implementation:

document.elementFromPoint = function elementFromPoint() {
    return null;
  }

I am happy to raise a pull request @domenic if it is helpful

alexreardon avatar Mar 26 '23 20:03 alexreardon