diff --git a/lib/eval-script.js b/lib/eval-script.js index fd13afa..73e0493 100644 --- a/lib/eval-script.js +++ b/lib/eval-script.js @@ -12,6 +12,7 @@ module.exports = function(el) { } script.type = "text/javascript" + script.id = el.id; /* istanbul ignore if */ if (src !== "") { @@ -33,7 +34,7 @@ module.exports = function(el) { // execute parent.appendChild(script) // avoid pollution only in head or body tags - if (parent instanceof HTMLHeadElement || parent instanceof HTMLBodyElement) { + if ((parent instanceof HTMLHeadElement || parent instanceof HTMLBodyElement) && parent.contains(script)) { parent.removeChild(script) } diff --git a/tests/lib/eval-scripts.js b/tests/lib/eval-scripts.js index 64a66fe..6491a74 100644 --- a/tests/lib/eval-scripts.js +++ b/tests/lib/eval-scripts.js @@ -21,3 +21,21 @@ tape("test evalScript method", function(t) { t.end() }) + +tape("evalScript should not throw an error if the script removed itself", function(t) { + var script = document.createElement("script") + script.id = "myScript"; + script.innerHTML = "const script = document.querySelector('#myScript');" + + "script.parentNode.removeChild(script);"; + + try { + evalScript(script); + + t.pass("Missing script tested successfully"); + } catch (e) { + console.error(e); + t.fail("Attempted to remove missing script"); + } + + t.end(); +})