trix
trix copied to clipboard
Uncaught `TypeError` Crashing Drag-and-Drop in Chrome 127+ & Firefox
Location
- File: src/trix/models/point_mapper.js
- Lines: 8–16
- Invoked by:
SelectionManager.setLocationRangeFromPointRangeviaLevel2InputController.dragoveranddrop
Root Cause
In PointMapper.createDOMRangeFromPoint:
createDOMRangeFromPoint({ x, y }) {
let domRange
if (document.caretPositionFromPoint) {
const { offsetNode, offset } = document.caretPositionFromPoint(x, y)
domRange = document.createRange()
domRange.setStart(offsetNode, offset)
return domRange
} else if (document.caretRangeFromPoint) {
return document.caretRangeFromPoint(x, y)
}
// ...
}
- Starting in Chrome 127 (July 2024), Chromium shipped the standard
document.caretPositionFromPointAPI (aligning with Firefox). Previously, Chromium only supportedcaretRangeFromPoint. - As a consequence, all modern Chromium browsers now enter the
if (document.caretPositionFromPoint)branch. - According to the CSSOM View specification,
document.caretPositionFromPoint(x, y)returnsnullwhen the pointer coordinates fall outside viewport bounds, on non-content regions, margins, or padding. - Trix directly attempts object destructuring on the return value:
const { offsetNode, offset } = document.caretPositionFromPoint(x, y). - When a user drags a file or text over the edge, border, or padding of the editor,
caretPositionFromPointreturnsnull, throwing an unhandled exception:
This immediately halts theTypeError: Cannot destructure property 'offsetNode' of 'document.caretPositionFromPoint(...)' as it is null.dragover/dropevent processing loop.
Reproduction
const mapper = new Trix.PointMapper()
// Coordinates outside content boundary / in margins (e.g., negative or border coordinate)
mapper.createDOMRangeFromPoint({ x: -10, y: -10 })
// Uncaught TypeError: Cannot destructure property 'offsetNode' of 'document.caretPositionFromPoint(...)' as it is null.
Proposed Patch
In src/trix/models/point_mapper.js:
if (document.caretPositionFromPoint) {
- const { offsetNode, offset } = document.caretPositionFromPoint(x, y)
- domRange = document.createRange()
- domRange.setStart(offsetNode, offset)
- return domRange
+ const position = document.caretPositionFromPoint(x, y)
+ if (position) {
+ domRange = document.createRange()
+ domRange.setStart(position.offsetNode, position.offset)
+ return domRange
+ }
} else if (document.caretRangeFromPoint) {
return document.caretRangeFromPoint(x, y)
}