Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for issue 6887 #6893

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/renderers/dom/client/ReactBrowserEventEmitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,13 @@ var ReactBrowserEventEmitter = Object.assign({}, ReactEventEmitterMixin, {
);
ReactBrowserEventEmitter.ReactEventListener = ReactEventListener;
},

/**
* @param {boolean} hasEventPageXY
*/
injectHasEventPageXY: function(val) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding this method (which will end up in prod builds for no gain), let’s extract the if block contents into something like:

testEventPageXY: function() {
  if (document.createEvent) {
    hasEventPageXY = evt !== null && 'pageX' in evt;
  } else {
    hasEventPageXY = false;
  }
}

Then call this method both from ensureScrollValueMonitoring when if (hasEventPageXY === undefined) and from tests after stubbing out document.createElement.

hasEventPageXY = val;
},
},

/**
Expand Down Expand Up @@ -359,8 +366,12 @@ var ReactBrowserEventEmitter = Object.assign({}, ReactEventEmitterMixin, {
*/
ensureScrollValueMonitoring: function() {
if (hasEventPageXY === undefined) {
hasEventPageXY =
document.createEvent && 'pageX' in document.createEvent('MouseEvent');
if (document.createEvent) {
var evt = document.createEvent('MouseEvent');
hasEventPageXY = evt !== null && 'pageX' in evt;
Copy link
Collaborator

@gaearon gaearon Aug 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would need to be != null instead of !== null. If we’re protecting, let’s protect fully.

You will also need to add a note like

// Protect against document.createEvent() returning null
// Some popup blocker extensions appear to do this:
// https://github.com/facebook/react/issues/6887

} else {
hasEventPageXY = false;
}
}
if (!hasEventPageXY && !isMonitoringScrollValue) {
var refresh = ViewportMetrics.refreshScrollValues;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ describe('ReactBrowserEventEmitter', function() {
expect(LISTENER.mock.calls.length).toBe(1);
});

it('should not crash ensureScrollValueMonitoring when createEvent returns null', function() {
var originalCreateEvent = document.createEvent;
var mockedCreateEvent = jest.fn().mockImplementation(function() {
return null;
});
try {
document.createEvent = mockedCreateEvent;
ReactBrowserEventEmitter.injection.injectHasEventPageXY(undefined);
ReactBrowserEventEmitter.ensureScrollValueMonitoring();
} finally {
document.createEvent = originalCreateEvent;
}

expect(mockedCreateEvent.mock.calls.length).toBe(1);
});

it(
'should not invoke handlers if ReactBrowserEventEmitter is disabled',
function() {
Expand Down