Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ import { slice } from './util';
* existing DOM tree rooted at `replaceNode`
*/
export function render(vnode, parentDom, replaceNode) {
// https://github.com/preactjs/preact/issues/3794
if (parentDom === document) {
parentDom = document.documentElement;
}

if (options._root) options._root(vnode, parentDom);

// We abuse the `replaceNode` parameter in `hydrate()` to signal if we are in
Expand Down
2 changes: 2 additions & 0 deletions test/_util/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ export function clearOptions() {
* @param {HTMLElement} scratch
*/
export function teardown(scratch) {
if (!document.contains(scratch)) return;

if (
scratch &&
('__k' in scratch || '_children' in scratch) &&
Expand Down
20 changes: 19 additions & 1 deletion test/browser/render.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { setupRerender } from 'preact/test-utils';
import { createElement, render, Component, options } from 'preact';
import { createElement, render, Component, options, Fragment } from 'preact';
import {
setupScratch,
teardown,
Expand Down Expand Up @@ -1884,4 +1884,22 @@ describe('render()', () => {
);
}
});

it('should work with document', () => {
document.textContent = '';
const App = () => (
<Fragment>
<head>
<title>Test</title>
</head>
<body>
<p>Test</p>
</body>
</Fragment>
);
render(<App />, document);
expect(document.documentElement.innerHTML).to.equal(
'<head><title>Test</title></head><body><p>Test</p></body>\n'
);
});
});