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
4 changes: 3 additions & 1 deletion can-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,9 @@ var Component = Construct.extend(
});
} else {
var removalDisposal = domMutate.onNodeRemoval(el, function () {
if (!el.ownerDocument.documentElement.contains(el)) {
var doc = el.ownerDocument;
var rootNode = doc.contains ? doc : doc.documentElement;
if (!rootNode.contains(el)) {
removalDisposal();
callTeardownFunctions();
}
Expand Down
27 changes: 27 additions & 0 deletions test/component-in-stache-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var QUnit = require("steal-qunit");
var viewModel = require("can-view-model");
var canDev = require("can-log/dev/dev");
var testHelpers = require("can-test-helpers");
var domMutate = require("can-dom-mutate");
var globals = require("can-globals");

QUnit.module("can-component can be rendered by can-stache");

Expand Down Expand Up @@ -117,3 +119,28 @@ QUnit.test("Component can be removed from the page", 3, function(){
prop.set(4);
QUnit.equal(frag.firstChild.getElementsByTagName("to-be-removed")[0].innerHTML, "4");
});

QUnit.test("Cleans up itself on the documentElement removal", function() {
Component.extend({
tag: "ssr-cleanup",
view: "hello world",
ViewModel: {}
});

var doc = document.implementation.createHTMLDocument("Test");
var realDoc = globals.getKeyValue("document");
globals.setKeyValue("document", doc);

var frag = stache("<ssr-cleanup />")({});
doc.body.appendChild(frag);

domMutate.onNodeRemoval(doc.body.firstChild, function() {
globals.setKeyValue("document", realDoc);
QUnit.ok(true, "Called back without throwing");
start();
})

stop();

doc.removeChild(doc.documentElement);
});