@samitouri / QOS-React-2 / commits / dbd9d99bcd
Fork 0

The library for web and native user interfaces.

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`.

CommitSyncScript committed Jun 27, 2013 at 16:30 UTC dbd9d99bcd01849b737f608a8862c4300cbae217
2 files changed +22 -3
src/core/ReactInstanceHandles.js
+3 -3
@@ -286,10 +286,10 @@ var ReactInstanceHandles = {
286 findComponentRoot: function(ancestorNode, id) {
287 var child = ancestorNode.firstChild;
288 while (child) {
289 - var childId = ReactID.getID(child);
290 - if (id === childId) {
289 + var childID = ReactID.getID(child);
290 + if (id === childID) {
291 return child;
292 - } else if (isAncestorIDOf(childId, id)) {
292 + } else if (childID && isAncestorIDOf(childID, id)) {
293 return ReactInstanceHandles.findComponentRoot(child, id);
294 }
295 child = child.nextSibling;
src/core/__tests__/ReactInstanceHandles-test.js
+19
@@ -102,6 +102,25 @@ describe('ReactInstanceHandles', function() {
102 )
103 ).toBe(childNodeB);
104 });
105 +
106 + it('should work around unidentified nodes', function() {
107 + var parentNode = document.createElement('div');
108 + var childNodeA = document.createElement('div');
109 + var childNodeB = document.createElement('div');
110 + parentNode.appendChild(childNodeA);
111 + parentNode.appendChild(childNodeB);
112 +
113 + ReactID.setID(parentNode, '.react[0]');
114 + // No ID on `childNodeA`.
115 + ReactID.setID(childNodeB, '.react[0].0:1');
116 +
117 + expect(
118 + ReactInstanceHandles.findComponentRoot(
119 + parentNode,
120 + ReactID.getID(childNodeB)
121 + )
122 + ).toBe(childNodeB);
123 + });
124 });
125
126 describe('getReactRootIDFromNodeID', function() {