linkedom icon indicating copy to clipboard operation
linkedom copied to clipboard

Event `target` not reflected correctly for `defaultView`

Open jed opened this issue 1 year ago • 6 comments

Running this (in nodejs v20.18.1):

import { parseHTML } from "linkedom";
const { document, window } = parseHTML();

for (let name of ["document", "window"]) {
  window[name].addEventListener("test", (e) =>
    console.assert(e.target === window[name], `${name} preserves target`)
  );
  window[name].dispatchEvent(new window.Event("test"));
}

logs this:

Assertion failed: window preserves target

It seems that the target set for events dispatched from defaultView ends up being globalThis, not defaultView.

jed avatar Feb 07 '25 00:02 jed

will have a look but just FYI it's not part of this project to always be specs compliant ... it's really a case by case choice but, most importantly, I really have little time to spend over this project these days so MRs might land faster than me.

WebReflection avatar Feb 07 '25 07:02 WebReflection

to me it's less of a spec thing, target is the most important property of Event, and without it there's no way to tell from the event the node from which it was dispatched. i'll take a look and see if i can implement it, but the code around it is pretty abstract (proxies returned from getters, et al).

jed avatar Feb 07 '25 14:02 jed

proxies returned from getters, et al

that's howDOMStrings or CSS in DOM works ... you should not be afraid by those because not on this path of the equation ;-)

WebReflection avatar Feb 07 '25 18:02 WebReflection

i guess i'm not sure how this works... seems like you're setting the event target to a new EventTarget instead of the window, but I don't understand why.

jed avatar Feb 07 '25 22:02 jed

The window is just a proxy for globalThis so maybe the callbacks shoukd be bound to window.get(this) instead? Can’t remember why I have to redefine the event target but I’ll have a look … hardly before Monday as I have relatives visiting and my 3yo birthday this WE

WebReflection avatar Feb 08 '25 08:02 WebReflection

awesome. linkedom is such a great project, thank you for your work on it!

jed avatar Feb 08 '25 20:02 jed