Skip to content

Commit 3c0d3d5

Browse files
authored
Merge pull request #324 from canjs/contains
Render in environments when there is no body element
2 parents 9edd069 + c08dc9a commit 3c0d3d5

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

can-component.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,8 @@ var Component = Construct.extend(
601601
nodeLists.update(nodeList, getChildNodes(el));
602602

603603
if(viewModel && viewModel.connectedCallback) {
604-
componentInPage = DOCUMENT().body.contains(el);
604+
var body = DOCUMENT().body;
605+
componentInPage = body && body.contains(el);
605606

606607
if(componentInPage) {
607608
disconnectedCallback = viewModel.connectedCallback(el);

test/component-instantiation-test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var QUnit = require("steal-qunit");
44
var SimpleMap = require("can-simple-map");
55
var stache = require("can-stache");
66
var value = require("can-value");
7+
var globals = require("can-globals");
78

89
QUnit.module("can-component instantiation");
910

@@ -331,3 +332,28 @@ QUnit.test("component instantiation is not observable", function(){
331332

332333
QUnit.equal(count, 1, "only updated once");
333334
});
335+
336+
QUnit.test("Can render in a document with no body", function() {
337+
var doc = document.implementation.createHTMLDocument("Testing");
338+
globals.setKeyValue("document", doc);
339+
340+
doc.documentElement.removeChild(doc.body);
341+
342+
var ComponentConstructor = Component.extend({
343+
tag: "with-no-body",
344+
view: "test",
345+
ViewModel: {
346+
connectedCallback: function() {}
347+
}
348+
});
349+
350+
try {
351+
new ComponentConstructor();
352+
QUnit.ok(true, "rendered without throwing");
353+
} catch(e){
354+
QUnit.ok(false, "threw" + e.toString());
355+
}
356+
finally {
357+
globals.setKeyValue("document", document);
358+
}
359+
});

0 commit comments

Comments
 (0)