diff --git a/index.js b/index.js index 7066712..d2598b9 100644 --- a/index.js +++ b/index.js @@ -15,8 +15,12 @@ if (window && window.MutationObserver) { eachAttr(mutations[i], turnon, turnoff) continue } - eachMutation(mutations[i].removedNodes, turnoff) - eachMutation(mutations[i].addedNodes, turnon) + eachMutation(mutations[i].removedNodes, function (index, el) { + if (!document.documentElement.contains(el)) turnoff(index, el) + }) + eachMutation(mutations[i].addedNodes, function (index, el) { + if (document.documentElement.contains(el)) turnon(index, el) + }) } }) if (document.body) { diff --git a/test.js b/test.js index 12c104e..9f9a8a2 100644 --- a/test.js +++ b/test.js @@ -8,13 +8,13 @@ test('onload/onunload', function (t) { el.textContent = 'test' onload(el, function () { t.ok(true, 'onload called') + document.body.removeChild(el) }, function () { t.ok(true, 'onunload called') document.body.innerHTML = '' t.end() }) document.body.appendChild(el) - document.body.removeChild(el) }) test('assign key attr', function (t) { @@ -66,13 +66,13 @@ test('nested', function (t) { var e3 = document.createElement('div') onload(e3, function () { t.ok(true, 'onload called') + e2.removeChild(e3) }, function () { t.ok(true, 'onunload called') document.body.innerHTML = '' t.end() }) e2.appendChild(e3) - e2.removeChild(e3) }) test('complex', function (t) {