From dbd9d99bcd01849b737f608a8862c4300cbae217 Mon Sep 17 00:00:00 2001 From: CommitSyncScript Date: Thu, 27 Jun 2013 16:30:03 -0700 Subject: [PATCH] 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`. --- src/core/ReactInstanceHandles.js | 6 +++--- .../__tests__/ReactInstanceHandles-test.js | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/core/ReactInstanceHandles.js b/src/core/ReactInstanceHandles.js index de0b2777e2..af508207e3 100644 --- a/src/core/ReactInstanceHandles.js +++ b/src/core/ReactInstanceHandles.js @@ -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; diff --git a/src/core/__tests__/ReactInstanceHandles-test.js b/src/core/__tests__/ReactInstanceHandles-test.js index 34531223fa..4daef67300 100644 --- a/src/core/__tests__/ReactInstanceHandles-test.js +++ b/src/core/__tests__/ReactInstanceHandles-test.js @@ -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() {