Fix evalScripts() #186

Merged
BehindTheMath merged 2 commits from fix/eval-script into master 2018-11-25 15:21:08 -05:00
2 changed files with 20 additions and 1 deletions

View File

@@ -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)
}

View File

@@ -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();
})