Skip to content

Commit

Permalink
Merge pull request facebook#6364 from p-jackson/issue-5700
Browse files Browse the repository at this point in the history
Don't wrap drag events in IE/Edge in dev builds
  • Loading branch information
jimfb committed Apr 7, 2016
2 parents 7b47e3e + 4687a03 commit 2e8f28c
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/shared/utils/ReactErrorUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

'use strict';

var SyntheticDragEvent = require('SyntheticDragEvent');

var caughtError = null;

/**
Expand Down Expand Up @@ -65,6 +67,11 @@ if (__DEV__) {
typeof document.createEvent === 'function') {
var fakeNode = document.createElement('react');
ReactErrorUtils.invokeGuardedCallback = function(name, func, a, b) {
if (!canWrapEvent(a)) {
invokeGuardedCallback(name, func, a, b);
return;
}

var boundFunc = func.bind(null, a, b);
var evtType = `react-${name}`;
fakeNode.addEventListener(evtType, boundFunc, false);
Expand All @@ -74,6 +81,41 @@ if (__DEV__) {
fakeNode.removeEventListener(evtType, boundFunc, false);
};
}

var cacheCanWrapEvent = null;

/**
* IE and Edge don't allow access to the DataTransfer.dropEffect property when
* it's wrapped in another event. This function detects whether we're in an
* environment that behaves this way.
*
* @param {*} ev Event that is being tested
*/
function canWrapEvent(ev) {
if (!(ev instanceof SyntheticDragEvent)) {
return true;
} else if (cacheCanWrapEvent !== null) {
return cacheCanWrapEvent;
}

var canAccessDropEffect = false;
function handleWrappedEvent() {
try {
ev.dataTransfer.dropEffect; // eslint-disable-line no-unused-expressions
canAccessDropEffect = true;
} catch (e) {}
}

var wrappedEventName = 'react-wrappeddragevent';
var wrappedEvent = document.createEvent('Event');
wrappedEvent.initEvent(wrappedEventName, false, false);
fakeNode.addEventListener(wrappedEventName, handleWrappedEvent, false);
fakeNode.dispatchEvent(wrappedEvent);
fakeNode.removeEventListener(wrappedEventName, handleWrappedEvent, false);

cacheCanWrapEvent = canAccessDropEffect;
return canAccessDropEffect;
}
}

module.exports = ReactErrorUtils;

0 comments on commit 2e8f28c

Please sign in to comment.