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 incorrect calculation of onMouseEnter/Leave component path #11164

Merged
merged 4 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ var EnterLeaveEventPlugin;
var React;
var ReactDOM;
var ReactDOMComponentTree;
var ReactTestUtils;

describe('EnterLeaveEventPlugin', () => {
beforeEach(() => {
jest.resetModules();

React = require('react');
ReactDOM = require('react-dom');
ReactTestUtils = require('react-dom/test-utils');
// TODO: can we express this test with only public API?
ReactDOMComponentTree = require('ReactDOMComponentTree');
EnterLeaveEventPlugin = require('EnterLeaveEventPlugin');
Expand Down Expand Up @@ -58,4 +60,38 @@ describe('EnterLeaveEventPlugin', () => {
expect(enter.target).toBe(div);
expect(enter.relatedTarget).toBe(iframe.contentWindow);
});

// Regression test for https://github.com/facebook/react/issues/10906.
it('should find the common parent after updates', () => {
let parentEnterCalls = 0;
let childEnterCalls = 0;
let parent = null;
class Parent extends React.Component {
render() {
return (
<div
onMouseEnter={() => parentEnterCalls++}
ref={node => (parent = node)}>
{this.props.showChild &&
<div onMouseEnter={() => childEnterCalls++} />}
</div>
);
}
}

const div = document.createElement('div');
ReactDOM.render(<Parent />, div);
// The issue only reproduced on insertion during the first update.
ReactDOM.render(<Parent showChild={true} />, div);

// Enter from parent into the child.
ReactTestUtils.simulateNativeEventOnNode('topMouseOut', parent, {
target: parent,
relatedTarget: parent.firstChild,
});

// Entering a child should fire on the child, not on the parent.
expect(childEnterCalls).toBe(1);
expect(parentEnterCalls).toBe(0);
});
});
32 changes: 26 additions & 6 deletions src/renderers/shared/shared/ReactTreeTraversal.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,38 @@ function traverseTwoPhase(inst, fn, arg) {
* "entered" or "left" that element.
*/
function traverseEnterLeave(from, to, fn, argFrom, argTo) {
var common = from && to ? getLowestCommonAncestor(from, to) : null;
var pathFrom = [];
while (from && from !== common) {
const common = from && to ? getLowestCommonAncestor(from, to) : null;
const pathFrom = [];
while (true) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

We try to avoid while (true) since (at least in older versions) it has been known to deopt.

I appreciate the rewrite for the temp variable and easy of read. But arguably this just make the condition seem a lot more complicated than it really is.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Didn't it deopt when you have continues rather than break? I don't remember. @trueadm

if (!from) {
break;
}
if (from === common) {
break;
}
const alternate = from.alternate;
if (alternate !== null && alternate === common) {
break;
}
pathFrom.push(from);
from = getParent(from);
}
var pathTo = [];
while (to && to !== common) {
const pathTo = [];
while (true) {
if (!to) {
break;
}
if (to === common) {
break;
}
const alternate = to.alternate;
if (alternate !== null && alternate === common) {
break;
}
pathTo.push(to);
to = getParent(to);
}
var i;
let i;
for (i = 0; i < pathFrom.length; i++) {
fn(pathFrom[i], 'bubbled', argFrom);
}
Expand Down