From a4123a069ef05b6fdbb065d060baee47a61ec3ec Mon Sep 17 00:00:00 2001 From: Ben Newman Date: Mon, 8 Jul 2013 11:48:44 -0700 Subject: [PATCH] Continue over ID-less children in ReactMount.findComponentRoot. This fixes our perf test by coping with edge cases like the injection of `` between `` and `` nodes, which occurs automatically in some browsers when we set `.innerHTML`. Introducing more search branches would be risky if not for my previous commit that made `findComponentRoot` breadth-first instead of depth-first. --- src/core/ReactInstanceHandles.js | 7 +++++++ src/core/__tests__/ReactInstanceHandles-test.js | 13 +++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/core/ReactInstanceHandles.js b/src/core/ReactInstanceHandles.js index df3d2e8f5b..a4ddec7592 100644 --- a/src/core/ReactInstanceHandles.js +++ b/src/core/ReactInstanceHandles.js @@ -303,6 +303,13 @@ var ReactInstanceHandles = { firstChildren.push(child.firstChild); break; } + } else { + // If this child had no ID, then there's a chance that it was + // injected automatically by the browser, as when a `
` + // element sprouts an extra `` child as a side effect of + // `.innerHTML` parsing. Optimistically continue down this + // branch, but not before examining the other siblings. + firstChildren.push(child.firstChild); } child = child.nextSibling; } diff --git a/src/core/__tests__/ReactInstanceHandles-test.js b/src/core/__tests__/ReactInstanceHandles-test.js index 110612c1c8..b5161547c7 100644 --- a/src/core/__tests__/ReactInstanceHandles-test.js +++ b/src/core/__tests__/ReactInstanceHandles-test.js @@ -133,15 +133,20 @@ describe('ReactInstanceHandles', function() { // No ID on `childNodeA`, it was "rendered by the browser". ReactID.setID(childNodeB, '.react[0].1:0'); + expect(ReactInstanceHandles.findComponentRoot( + parentNode, + ReactID.getID(childNodeB) + )).toBe(childNodeB); + expect(function() { ReactInstanceHandles.findComponentRoot( parentNode, - ReactID.getID(childNodeB) + ReactID.getID(childNodeB) + ":junk" ); }).toThrow( - 'Invariant Violation: findComponentRoot(..., .react[0].1:0): Unable ' + - 'to find element. This probably means the DOM was unexpectedly ' + - 'mutated (e.g. by the browser).' + 'Invariant Violation: findComponentRoot(..., .react[0].1:0:junk): ' + + 'Unable to find element. This probably means the DOM was ' + + 'unexpectedly mutated (e.g. by the browser).' ); }); });