mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
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:
committed by
Paul O’Shannessy
parent
c032743b93
commit
dbd9d99bcd
@@ -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() {
|
||||
|
||||
Reference in New Issue
Block a user