From 1673579d32493ad922b9743708a4a49acf9b8c86 Mon Sep 17 00:00:00 2001 From: Ian Henderson Date: Tue, 17 May 2016 11:05:59 -0700 Subject: [PATCH 1/3] Add replaceScriptNodes function --- mithril.js | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/mithril.js b/mithril.js index d7054596..9478add4 100644 --- a/mithril.js +++ b/mithril.js @@ -1176,9 +1176,41 @@ $document.createRange().createContextualFragment(data)) } catch (e) { parentElement.insertAdjacentHTML("beforeend", data) + replaceScriptNodes(parentElement) } } + // Replace script tags inside given DOM element with executable ones. + // Will also check children recursively and replace any found script + // tags in same manner. + function replaceScriptNodes(node) { + if (node.tagName === "SCRIPT") { + node.parentNode.replaceChild(buildExecutableNode(node), node) + } else { + var children = node.childNodes + if (children && children.length) { + for (var i = 0; i < children.length; i++) { + replaceScriptNodes(children[i]) + } + } + } + + return node + } + + // Replace script element with one whose contents are executable. + function buildExecutableNode(node){ + var scriptEl = document.createElement("script") + var attrs = node.attributes + + for (var i = 0; i < attrs.length; i++) { + scriptEl.setAttribute(attrs[i].name, attrs[i].value) + } + + scriptEl.text = node.innerHTML + return scriptEl + } + function injectHTML(parentElement, index, data) { var nextSibling = parentElement.childNodes[index] if (nextSibling) { From e03f194e32275d422e515ccf7c45921fa9c5ae78 Mon Sep 17 00:00:00 2001 From: Ian Henderson Date: Wed, 18 May 2016 12:28:01 -0700 Subject: [PATCH 2/3] Add test for replaceScriptNodes --- test/mithril.trust.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/test/mithril.trust.js b/test/mithril.trust.js index 17727a0d..0acd94fa 100644 --- a/test/mithril.trust.js +++ b/test/mithril.trust.js @@ -65,5 +65,21 @@ describe("m.trust()", function () { expect(root.innerHTML) .to.equal("

&copy;

©

©
") }) + + // https://github.com/lhorie/mithril.js/issues/1045 + it("correctly injects script tags and executes them", function () { + var HTMLString = + "" + var root = document.createElement("div") + var child = document.createElement("div") + root.id = "root" + root.innerText = "Before" + root.appendChild(child) + document.body.appendChild(root) + + m.render(child, m.trust(HTMLString)) + + expect(root.innerText).to.equal("After") + }) }) }) From 51e553f9802592a9f20be252a68de20ca2e4f90b Mon Sep 17 00:00:00 2001 From: AlonsoSoto Date: Fri, 20 May 2016 14:36:20 -0500 Subject: [PATCH 3/3] Update mithril.request.md --- docs/mithril.request.md | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/mithril.request.md b/docs/mithril.request.md index e2e7b3af..96defe1d 100644 --- a/docs/mithril.request.md +++ b/docs/mithril.request.md @@ -272,6 +272,7 @@ data.append("file", file) m.request({ method: "POST", url: "/upload", + data: data, serialize: function(data) {return data} }) ```