Fix evalScripts() (#186)
* Set the id of the inserted <script>. * Check if the <script> still exists before trying to remove it.
This commit was merged in pull request #186.
This commit is contained in:
@@ -12,6 +12,7 @@ module.exports = function(el) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
script.type = "text/javascript"
|
script.type = "text/javascript"
|
||||||
|
script.id = el.id;
|
||||||
|
|
||||||
/* istanbul ignore if */
|
/* istanbul ignore if */
|
||||||
if (src !== "") {
|
if (src !== "") {
|
||||||
@@ -33,7 +34,7 @@ module.exports = function(el) {
|
|||||||
// execute
|
// execute
|
||||||
parent.appendChild(script)
|
parent.appendChild(script)
|
||||||
// avoid pollution only in head or body tags
|
// 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)
|
parent.removeChild(script)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,3 +21,21 @@ tape("test evalScript method", function(t) {
|
|||||||
|
|
||||||
t.end()
|
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();
|
||||||
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user