Merge pull request #923 from iamjohnlong/safari-createContextualFragment-fix
Moves createContextualFragment into try/catch function
This commit is contained in:
commit
e32f37c523
3 changed files with 24 additions and 10 deletions
12
mithril.js
12
mithril.js
|
|
@ -626,6 +626,13 @@ var m = (function app(window, undefined) {
|
||||||
else if (cached.children.tag) unload(cached.children);
|
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) {
|
function injectHTML(parentElement, index, data) {
|
||||||
var nextSibling = parentElement.childNodes[index];
|
var nextSibling = parentElement.childNodes[index];
|
||||||
if (nextSibling) {
|
if (nextSibling) {
|
||||||
|
|
@ -639,10 +646,7 @@ var m = (function app(window, undefined) {
|
||||||
else nextSibling.insertAdjacentHTML("beforebegin", data);
|
else nextSibling.insertAdjacentHTML("beforebegin", data);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
if (window.Range && window.Range.prototype.createContextualFragment) {
|
appendTextFragment(parentElement, data);
|
||||||
parentElement.appendChild($document.createRange().createContextualFragment(data));
|
|
||||||
}
|
|
||||||
else parentElement.insertAdjacentHTML("beforeend", data);
|
|
||||||
}
|
}
|
||||||
var nodes = [];
|
var nodes = [];
|
||||||
while (parentElement.childNodes[index] !== nextSibling) {
|
while (parentElement.childNodes[index] !== nextSibling) {
|
||||||
|
|
|
||||||
|
|
@ -20,19 +20,20 @@ describe("m.trust()", function () {
|
||||||
// FIXME: implement document.createRange().createContextualFragment() in the
|
// FIXME: implement document.createRange().createContextualFragment() in the
|
||||||
// mock window for these tests
|
// mock window for these tests
|
||||||
dom(function () {
|
dom(function () {
|
||||||
xit("isn't escaped in m.render()", function () {
|
|
||||||
|
it("isn't escaped in m.render()", function () {
|
||||||
var root = document.createElement("div")
|
var root = document.createElement("div")
|
||||||
m.render(root, m("div", "a", m.trust("&"), "b"))
|
m.render(root, m("div", "a", m.trust("&"), "b"))
|
||||||
expect(root.childNodes[0].innerHTML).to.equal("a&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")
|
var root = document.createElement("div")
|
||||||
m.render(root, [m.trust("<p>1</p><p>2</p>"), m("i", "foo")])
|
m.render(root, [m.trust("<p>1</p><p>2</p>"), m("i", "foo")])
|
||||||
expect(root.childNodes[2].tagName).to.equal("I")
|
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")
|
var root = document.createElement("div")
|
||||||
m.render(root, [
|
m.render(root, [
|
||||||
m.trust("<p>1</p>123<p>2</p>"),
|
m.trust("<p>1</p>123<p>2</p>"),
|
||||||
|
|
@ -41,9 +42,7 @@ describe("m.trust()", function () {
|
||||||
expect(root.childNodes[3].tagName).to.equal("I")
|
expect(root.childNodes[3].tagName).to.equal("I")
|
||||||
})
|
})
|
||||||
|
|
||||||
// FIXME: this is a bug (trusted string's contents rendered as just
|
it("works with mixed trusted content in td", function () {
|
||||||
// textual contents)
|
|
||||||
xit("works with mixed trusted content in td", function () {
|
|
||||||
var root = document.createElement("table")
|
var root = document.createElement("table")
|
||||||
root.appendChild(root = document.createElement("tr"))
|
root.appendChild(root = document.createElement("tr"))
|
||||||
|
|
||||||
|
|
@ -54,5 +53,16 @@ describe("m.trust()", function () {
|
||||||
|
|
||||||
expect(root.childNodes[2].tagName).to.equal("TD")
|
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("<div><p>&copy;</p><p>©</p>©</div>")
|
||||||
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
||||||
0
tests/trust-test.html
Normal file
0
tests/trust-test.html
Normal file
Loading…
Add table
Add a link
Reference in a new issue