Fix findComponentRoot w/ Unidentified Nodes

The current `ReactInstanceHandles` has a bug where `findComponentRoot` barfs if it comes across a node that was not identified by React (via `ReactID`). This fixes that.

This was always a bug, but it became more apparent once we switched to `data-reactid` because arbitrary `document.createElement`'d nodes are much more likely to have an `id` than they are to have a `data-reactid`.
This commit is contained in:
CommitSyncScript
2013-06-27 16:44:08 -07:00
committed by Paul O’Shannessy
parent c032743b93
commit dbd9d99bcd
2 changed files with 22 additions and 3 deletions
+3 -3
View File
@@ -286,10 +286,10 @@ var ReactInstanceHandles = {
findComponentRoot: function(ancestorNode, id) {
var child = ancestorNode.firstChild;
while (child) {
var childId = ReactID.getID(child);
if (id === childId) {
var childID = ReactID.getID(child);
if (id === childID) {
return child;
} else if (isAncestorIDOf(childId, id)) {
} else if (childID && isAncestorIDOf(childID, id)) {
return ReactInstanceHandles.findComponentRoot(child, id);
}
child = child.nextSibling;
@@ -102,6 +102,25 @@ describe('ReactInstanceHandles', function() {
)
).toBe(childNodeB);
});
it('should work around unidentified nodes', function() {
var parentNode = document.createElement('div');
var childNodeA = document.createElement('div');
var childNodeB = document.createElement('div');
parentNode.appendChild(childNodeA);
parentNode.appendChild(childNodeB);
ReactID.setID(parentNode, '.react[0]');
// No ID on `childNodeA`.
ReactID.setID(childNodeB, '.react[0].0:1');
expect(
ReactInstanceHandles.findComponentRoot(
parentNode,
ReactID.getID(childNodeB)
)
).toBe(childNodeB);
});
});
describe('getReactRootIDFromNodeID', function() {