From 0b9cbd1f8219660fab3783858cc5bc63cccec678 Mon Sep 17 00:00:00 2001 From: John Long Date: Tue, 5 Jan 2016 12:20:40 -0700 Subject: [PATCH 1/4] Moves createContextualFragment into try/catch function --- mithril.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/mithril.js b/mithril.js index 2e1153bb..f91a8848 100644 --- a/mithril.js +++ b/mithril.js @@ -622,6 +622,13 @@ var m = (function app(window, undefined) { else if (cached.children.tag) unload(cached.children); } } + function appendTextFragment(parentElement, data) { + try { + parentElement.appendChild($document.createRange().createContextualFragment(data)); + } catch (e) { + parentElement.insertAdjacentHTML("beforeend", data); + } + } function injectHTML(parentElement, index, data) { var nextSibling = parentElement.childNodes[index]; if (nextSibling) { @@ -635,10 +642,7 @@ var m = (function app(window, undefined) { else nextSibling.insertAdjacentHTML("beforebegin", data); } else { - if (window.Range && window.Range.prototype.createContextualFragment) { - parentElement.appendChild($document.createRange().createContextualFragment(data)); - } - else parentElement.insertAdjacentHTML("beforeend", data); + appendTextFragment(parentElement, data); } var nodes = []; while (parentElement.childNodes[index] !== nextSibling) { From 2cc898877dcc9aaabccbd876cea918b28d25357e Mon Sep 17 00:00:00 2001 From: John Long Date: Tue, 5 Jan 2016 14:25:52 -0700 Subject: [PATCH 2/4] Possibly fixes trust tests --- test/mithril.trust.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/mithril.trust.js b/test/mithril.trust.js index e8679f29..d16bf11b 100644 --- a/test/mithril.trust.js +++ b/test/mithril.trust.js @@ -20,19 +20,19 @@ describe("m.trust()", function () { // FIXME: implement document.createRange().createContextualFragment() in the // mock window for these tests dom(function () { - xit("isn't escaped in m.render()", function () { + it("isn't escaped in m.render()", function () { var root = document.createElement("div") m.render(root, m("div", "a", m.trust("&"), "b")) expect(root.childNodes[0].innerHTML).to.equal("a&b") }) - xit("works with mixed trusted content in div", function () { + it("works with mixed trusted content in div", function () { var root = document.createElement("div") m.render(root, [m.trust("

1

2

"), m("i", "foo")]) expect(root.childNodes[2].tagName).to.equal("I") }) - xit("works with mixed trusted content in text nodes", function () { + it("works with mixed trusted content in text nodes", function () { var root = document.createElement("div") m.render(root, [ m.trust("

1

123

2

"), @@ -43,7 +43,7 @@ describe("m.trust()", function () { // FIXME: this is a bug (trusted string's contents rendered as just // textual contents) - xit("works with mixed trusted content in td", function () { + it("works with mixed trusted content in td", function () { var root = document.createElement("table") root.appendChild(root = document.createElement("tr")) From aad2fc3e33e43118cd6593e2f51f346ed3dbd3c7 Mon Sep 17 00:00:00 2001 From: John Long Date: Tue, 5 Jan 2016 15:03:52 -0700 Subject: [PATCH 3/4] Adds test --- test/mithril.trust.js | 12 ++++++++++++ tests/trust-test.html | 0 2 files changed, 12 insertions(+) create mode 100644 tests/trust-test.html diff --git a/test/mithril.trust.js b/test/mithril.trust.js index d16bf11b..b28316e6 100644 --- a/test/mithril.trust.js +++ b/test/mithril.trust.js @@ -20,6 +20,7 @@ describe("m.trust()", function () { // FIXME: implement document.createRange().createContextualFragment() in the // mock window for these tests dom(function () { + it("isn't escaped in m.render()", function () { var root = document.createElement("div") m.render(root, m("div", "a", m.trust("&"), "b")) @@ -54,5 +55,16 @@ describe("m.trust()", function () { expect(root.childNodes[2].tagName).to.equal("TD") }) + + it("works with trusted content in div", function () { + var root = document.createElement("div") + m.render(root, m('div', [ + m('p', '©'), + m('p', m.trust('©')), + m.trust('©'), + ])) + expect(root.innerHTML).to.equal("

©

©

©
") + }) + }) }) diff --git a/tests/trust-test.html b/tests/trust-test.html new file mode 100644 index 00000000..e69de29b From 1eada0de80cb1c8b88dea3eb510b7c6671cb2971 Mon Sep 17 00:00:00 2001 From: John Long Date: Fri, 8 Jan 2016 08:59:48 -0700 Subject: [PATCH 4/4] Removes FIXME comment --- test/mithril.trust.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/mithril.trust.js b/test/mithril.trust.js index b28316e6..c64adb4c 100644 --- a/test/mithril.trust.js +++ b/test/mithril.trust.js @@ -42,8 +42,6 @@ describe("m.trust()", function () { expect(root.childNodes[3].tagName).to.equal("I") }) - // FIXME: this is a bug (trusted string's contents rendered as just - // textual contents) it("works with mixed trusted content in td", function () { var root = document.createElement("table") root.appendChild(root = document.createElement("tr"))