dragenter event incorrectly fires on Text nodes (should be Element nodes only)
Categories
(Core :: DOM: Copy & Paste and Drag & Drop, defect, P2)
Tracking
()
| Tracking | Status | |
|---|---|---|
| firefox92 | --- | fixed |
People
(Reporter: u587334, Assigned: saschanaz)
References
Details
Attachments
(2 files)
User Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36
Steps to reproduce:
I was writing a drag event handler on a <table>.
I needed to obtain the parent <tr> element while dragging over various nested elements, such as a <div> containing text inside <td>.
I was having trouble getting consistent results between browsers with Node.nodeName. So I decided to use Element.closest().
Here's a quick HTML example of my markup nesting (it's not all that complicated):
<tr>
<td>
Test 123<div>Test 123</div>
</td>
</tr>
Actual results:
It seemed to work, but I sometimes got this error: Uncaught TypeError: event.target.closest is not a function
Upon investigation, it only happens if event.target has a nodeType of TEXT_NODE. This appears to be a "Text Interface" according to MDN: https://developer.mozilla.org/en-US/docs/Web/API/Text.
It would seem that Element.closest() can only handle type ELEMENT_NODE.
Expected results:
The text node itself has a parentElement property that Element.closest() should be able to use. This bug does not occur in Chrome.
Maybe this is related: https://github.com/whatwg/dom/issues/161
Comment 2•5 years ago
|
||
The Bugbug bot thinks this bug should belong to the 'Core::DOM: Core & HTML' component, and is moving the bug to that component. Please revert this change in case you think the bot is wrong.
This appears to be a workaround that makes my code work as it should (adapted from whatwg issue):
if (!Text.prototype.closest) Text.prototype.closest = function(s) {return this.parentNode.closest(s); }
Existing polyfills don't seem to solve this particular issue because they still only work with Element.
| Assignee | ||
Comment 4•5 years ago
|
||
This bug does not occur in Chrome.
Chrome also limits closest() to Element so it should be same. Could you share the full page for us to debug the issue? Possibly https://codepen.io or such?
The text node itself has a
parentElementproperty thatElement.closest()should be able to use.
Sounds like a valid argument to me. Anne, do you recall why closest() is limited to Elements?
Here's a test case: https://codepen.io/bryc/pen/MWpWzYv
To reproduce the issue:
- Click and hold on "Test" text.
- Drag to the right towards "123" text, careful not to cross over the dotted red line.
(Don't let go just yet)
Result: Firefox should give the error, Chrome will log "a1" to the console.
- Continue dragging from the black "123" text, over the dotted red line, down to the red "123" text below it.
Result: Firefox will log "a1", and then give a second error. Chrome will log a second "a1".
Hope that helps highlight the issue.
https://developer.mozilla.org/en-US/docs/Web/API/Node/contains
This method works somewhat similarly, it checks if a node is contained in another node. Might be something to contrast against, because it could often be used together and be assumed to work similarly. Firefox doesn't have this issue here.
| Assignee | ||
Comment 7•5 years ago
|
||
Hmm, it seems Firefox emits dragenter with text nodes while Chrome emits with the parent element. The spec says the target must be "Immediate user selection or the body element" where the former is also an element, so the target should always be an element.
Anne, could you double check also for this one?
Comment 8•5 years ago
|
||
Agreed that the specification says that dragenter events must only fire on elements.
| Assignee | ||
Updated•5 years ago
|
| Assignee | ||
Comment 11•5 years ago
|
||
Updated•5 years ago
|
| Assignee | ||
Comment 12•5 years ago
|
||
Because EventDispatcher::Dispatch can run script (but not marked as such, which is bug 1539884).
Depends on D121006
Updated•5 years ago
|
Comment 14•5 years ago
|
||
Comment 15•5 years ago
|
||
| bugherder | ||
https://hg.mozilla.org/mozilla-central/rev/763aa9ea82f0
https://hg.mozilla.org/mozilla-central/rev/1db9d4f36b1e
Description
•