Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix evalScripts() #186

Merged
merged 2 commits into from
Nov 25, 2018
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
3 changes: 2 additions & 1 deletion lib/eval-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = function(el) {
}

script.type = "text/javascript"
script.id = el.id;

/* istanbul ignore if */
if (src !== "") {
Expand All @@ -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)
}

Expand Down
18 changes: 18 additions & 0 deletions tests/lib/eval-scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})