From 84c1dc628357b3bb703f6bccd4c0624a1059a420 Mon Sep 17 00:00:00 2001 From: Niklas von Hertzen Date: Sun, 2 Mar 2014 16:03:22 +0200 Subject: [PATCH] Remove script tags from cloned DOM --- src/core.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/core.js b/src/core.js index 6d6e35b..91033ba 100644 --- a/src/core.js +++ b/src/core.js @@ -86,10 +86,25 @@ function createWindowClone(ownerDocument, width, height, options) { documentClone.write(""); documentClone.close(); - documentClone.replaceChild(documentClone.adoptNode(documentElement), documentClone.documentElement); + documentClone.replaceChild(removeScriptNodes(documentClone.adoptNode(documentElement)), documentClone.documentElement); if (options.type === "view") { container.contentWindow.scrollTo(window.scrollX, window.scrollY); } resolve(container); }); } + +function removeScriptNodes(parent) { + [].slice.call(parent.childNodes, 0).filter(isElementNode).forEach(function(node) { + if (node.tagName === "SCRIPT") { + parent.removeChild(node); + } else { + removeScriptNodes(node); + } + }); + return parent; +} + +function isElementNode(node) { + return node.nodeType === Node.ELEMENT_NODE; +}