Skip to content

Commit

Permalink
useDropZone: Ensure drag event targets HTMLElement (#34272)
Browse files Browse the repository at this point in the history
Attempts to fix a TypeError that has been logged in the wild but is yet
to be reproduced. In production builds, it manifests as `e.dataset is
undefined`, pointing to the useDropZone hook.

* Don't cast FocusTarget instance to HTMLElement prior to calling
  isElementInZone.

* Perform type validations inside isElementInZone, then cast to
  HTMLElement.
  • Loading branch information
mcsf authored Aug 25, 2021
1 parent 8a88997 commit ac41a59
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions packages/compose/src/hooks/use-drop-zone/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,24 @@ export default function useDropZone( {
/**
* Checks if an element is in the drop zone.
*
* @param {HTMLElement|null} elementToCheck
* @param {EventTarget|null} targetToCheck
*
* @return {boolean} True if in drop zone, false if not.
*/
function isElementInZone( elementToCheck ) {
function isElementInZone( targetToCheck ) {
const { defaultView } = ownerDocument;
if (
! elementToCheck ||
! element.contains( elementToCheck )
! targetToCheck ||
! defaultView ||
! ( targetToCheck instanceof defaultView.HTMLElement ) ||
! element.contains( targetToCheck )
) {
return false;
}

/** @type {HTMLElement|null} */
let elementToCheck = targetToCheck;

do {
if ( elementToCheck.dataset.isDropZone ) {
return elementToCheck === element;
Expand Down Expand Up @@ -155,11 +161,7 @@ export default function useDropZone( {
// leaving the drop zone, which means the `relatedTarget`
// (element that has been entered) should be outside the drop
// zone.
if (
isElementInZone(
/** @type {HTMLElement|null} */ ( event.relatedTarget )
)
) {
if ( isElementInZone( event.relatedTarget ) ) {
return;
}

Expand Down

0 comments on commit ac41a59

Please sign in to comment.