Warn when rendering directly into document.body

This is in response to #3207 to address concerns regarding third-party
scripts and browser plugins potentially altering DOM nodes within
document.body, causing problems with reconciliation.

Closes #3211.
This commit is contained in:
Ben Anderson
2015-02-19 21:39:37 -05:00
committed by Ben Alpert
parent f77de57e41
commit 75a8bc96b1
2 changed files with 21 additions and 0 deletions
+8
View File
@@ -450,6 +450,14 @@ var ReactMount = {
)
);
warning(
container && container.tagName !== 'BODY',
'render(): Rendering components directly into document.body is ' +
'discouraged, since its children are often manipulated by third-party ' +
'scripts and browser extensions. This may lead to subtle reconciliation ' +
'issues. Try rendering into a container element created for your app.'
);
var prevComponent = instancesByReactRootID[getReactRootID(container)];
if (prevComponent) {
@@ -144,4 +144,17 @@ describe('ReactMount', function() {
ReactMount.render(<div />, container);
expect(console.warn.mock.calls.length).toBe(0);
});
it('should warn when mounting into document.body', function () {
var iFrame = document.createElement('iframe');
document.body.appendChild(iFrame);
spyOn(console, 'warn');
ReactMount.render(<div />, iFrame.contentDocument.body);
expect(console.warn.calls.length).toBe(1);
expect(console.warn.calls[0].args[0]).toContain(
'Rendering components directly into document.body is discouraged'
);
});
});