Check that container is a valid DOM element

This commit is contained in:
Ben Alpert
2013-06-25 10:54:28 -07:00
parent a2bc7387e4
commit f6c4d2d161
3 changed files with 23 additions and 1 deletions
+4
View File
@@ -511,6 +511,10 @@ var ReactComponent = {
container,
transaction,
shouldReuseMarkup) {
invariant(
container && container.nodeType === 1,
'mountComponentIntoNode(...): Target container is not a DOM element.'
);
var renderStart = Date.now();
var markup = this.mountComponent(rootID, transaction);
ReactMount.totalInstantiationTime += (Date.now() - renderStart);
+1 -1
View File
@@ -35,7 +35,7 @@ var containersByReactRootID = {};
* @return {?*} DOM element that may have the reactRoot ID, or null.
*/
function getReactRootElementInContainer(container) {
return container.firstChild;
return container && container.firstChild;
}
/**
+18
View File
@@ -31,6 +31,24 @@ describe('ReactComponent', function() {
reactComponentExpect = require('reactComponentExpect');
});
it('should throw on invalid render targets', function() {
var container = document.createElement('div');
// jQuery objects are basically arrays; people often pass them in by mistake
expect(function() {
React.renderComponent(<div></div>, [container]);
}).toThrow(
'Invariant Violation: mountComponentIntoNode(...): Target container is ' +
'not a DOM element.'
);
expect(function() {
React.renderComponent(<div></div>, null);
}).toThrow(
'Invariant Violation: mountComponentIntoNode(...): Target container is ' +
'not a DOM element.'
);
});
it('should throw when supplying a ref outside of render method', function() {
var instance = <div ref="badDiv" />;
expect(function() {