Skip to content

Commit

Permalink
Merge pull request #324 from canjs/contains
Browse files Browse the repository at this point in the history
Render in environments when there is no body element
  • Loading branch information
matthewp authored Dec 28, 2018
2 parents 9edd069 + c08dc9a commit 3c0d3d5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
3 changes: 2 additions & 1 deletion can-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,8 @@ var Component = Construct.extend(
nodeLists.update(nodeList, getChildNodes(el));

if(viewModel && viewModel.connectedCallback) {
componentInPage = DOCUMENT().body.contains(el);
var body = DOCUMENT().body;
componentInPage = body && body.contains(el);

if(componentInPage) {
disconnectedCallback = viewModel.connectedCallback(el);
Expand Down
26 changes: 26 additions & 0 deletions test/component-instantiation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var QUnit = require("steal-qunit");
var SimpleMap = require("can-simple-map");
var stache = require("can-stache");
var value = require("can-value");
var globals = require("can-globals");

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

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

QUnit.equal(count, 1, "only updated once");
});

QUnit.test("Can render in a document with no body", function() {
var doc = document.implementation.createHTMLDocument("Testing");
globals.setKeyValue("document", doc);

doc.documentElement.removeChild(doc.body);

var ComponentConstructor = Component.extend({
tag: "with-no-body",
view: "test",
ViewModel: {
connectedCallback: function() {}
}
});

try {
new ComponentConstructor();
QUnit.ok(true, "rendered without throwing");
} catch(e){
QUnit.ok(false, "threw" + e.toString());
}
finally {
globals.setKeyValue("document", document);
}
});

0 comments on commit 3c0d3d5

Please sign in to comment.