diff --git a/src/core/ReactInstanceHandles.js b/src/core/ReactInstanceHandles.js index 525ecc7dac..a262af8943 100644 --- a/src/core/ReactInstanceHandles.js +++ b/src/core/ReactInstanceHandles.js @@ -58,7 +58,7 @@ function getReactRootIDString(index) { * @return {boolean} True if the character is a separator or end of the ID. * @private */ -function isMarker(id, index) { +function isBoundary(id, index) { return id.charAt(index) === SEPARATOR || index === id.length; } @@ -75,6 +75,21 @@ function isValidID(id) { ); } +/** + * Checks if the first ID is an ancestor of or equal to the second ID. + * + * @param {string} ancestorID + * @param {string} descendantID + * @return {boolean} True if `ancestorID` is an ancestor of `descendantID`. + * @internal + */ +function isAncestorIDOf(ancestorID, descendantID) { + return ( + descendantID.indexOf(ancestorID) === 0 && + isBoundary(descendantID, ancestorID.length) + ); +} + /** * Gets the parent ID of the supplied React DOM ID, `id`. * @@ -102,12 +117,8 @@ function getNextDescendantID(ancestorID, destinationID) { ancestorID, destinationID ); - var longestCommonID = getFirstCommonAncestorID( - ancestorID, - destinationID - ); invariant( - longestCommonID === ancestorID, + isAncestorIDOf(ancestorID, destinationID), 'getNextDescendantID(...): React has made an invalid assumption about ' + 'the DOM hierarchy. Expected `%s` to be an ancestor of `%s`.', ancestorID, @@ -120,7 +131,7 @@ function getNextDescendantID(ancestorID, destinationID) { // another separator or we reach the end of `destinationID`. var start = ancestorID.length + SEPARATOR_LENGTH; for (var i = start; i < destinationID.length; i++) { - if (isMarker(destinationID, i)) { + if (isBoundary(destinationID, i)) { break; } } @@ -146,7 +157,7 @@ function getFirstCommonAncestorID(oneID, twoID) { var lastCommonMarkerIndex = 0; // Use `<=` to traverse until the "EOL" of the shorter string. for (var i = 0; i <= minLength; i++) { - if (isMarker(oneID, i) && isMarker(twoID, i)) { + if (isBoundary(oneID, i) && isBoundary(twoID, i)) { lastCommonMarkerIndex = i; } else if (oneID.charAt(i) !== twoID.charAt(i)) { break; @@ -182,10 +193,9 @@ function traverseParentPath(start, stop, cb, arg, skipFirst, skipLast) { 'traverseParentPath(...): Cannot traverse from and to the same ID, `%s`.', start ); - var ancestorID = getFirstCommonAncestorID(start, stop); - var traverseUp = ancestorID === stop; + var traverseUp = isAncestorIDOf(stop, start); invariant( - traverseUp || ancestorID === start, + traverseUp || isAncestorIDOf(start, stop), 'traverseParentPath(%s, %s, ...): Cannot traverse from two IDs that do ' + 'not have a parent path.', start, @@ -277,7 +287,7 @@ var ReactInstanceHandles = { while (child) { if (id === child.id) { return child; - } else if (id.indexOf(child.id) === 0) { + } else if (isAncestorIDOf(child.id, 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 05154dfeca..4c7b227381 100644 --- a/src/core/__tests__/ReactInstanceHandles-test.js +++ b/src/core/__tests__/ReactInstanceHandles-test.js @@ -84,6 +84,24 @@ describe('ReactInstanceHandles', function() { }); }); + describe('findComponentRoot', function() { + it('should find the correct node with prefix sibling IDs', function() { + var parentNode = document.createElement('div'); + var childNodeA = document.createElement('div'); + var childNodeB = document.createElement('div'); + parentNode.appendChild(childNodeA); + parentNode.appendChild(childNodeB); + + parentNode.id = '.react[0]'; + childNodeA.id = '.react[0].0'; + childNodeB.id = '.react[0].0:1'; + + expect( + ReactInstanceHandles.findComponentRoot(parentNode, childNodeB.id) + ).toBe(childNodeB); + }); + }); + describe('getReactRootIDFromNodeID', function() { it('should support strings', function() { var test = '.reactRoot[s_0_1][0]..[1]';