trix icon indicating copy to clipboard operation
trix copied to clipboard

Uncaught `TypeError` Crashing Drag-and-Drop in Chrome 127+ & Firefox

Open codeCraft-Ritik opened this issue 1 month ago • 0 comments

Location

  • File: src/trix/models/point_mapper.js
  • Lines: 8–16
  • Invoked by: SelectionManager.setLocationRangeFromPointRange via Level2InputController.dragover and drop

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)
  }
  // ...
}
  1. Starting in Chrome 127 (July 2024), Chromium shipped the standard document.caretPositionFromPoint API (aligning with Firefox). Previously, Chromium only supported caretRangeFromPoint.
  2. As a consequence, all modern Chromium browsers now enter the if (document.caretPositionFromPoint) branch.
  3. According to the CSSOM View specification, document.caretPositionFromPoint(x, y) returns null when the pointer coordinates fall outside viewport bounds, on non-content regions, margins, or padding.
  4. Trix directly attempts object destructuring on the return value: const { offsetNode, offset } = document.caretPositionFromPoint(x, y).
  5. When a user drags a file or text over the edge, border, or padding of the editor, caretPositionFromPoint returns null, throwing an unhandled exception:
    TypeError: Cannot destructure property 'offsetNode' of 'document.caretPositionFromPoint(...)' as it is null.
    
    This immediately halts the dragover / drop event 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)
    }

codeCraft-Ritik avatar Oct 06 '26 04:10 codeCraft-Ritik