forked from MithrilJS/mithril.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmithril.trust.js
85 lines (70 loc) · 2.39 KB
/
mithril.trust.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
describe("m.trust()", function () {
"use strict"
it("exists", function () {
expect(m.trust).to.be.a("function")
})
it("returns an instance of String", function () {
expect(m.trust("foo")).to.be.an.instanceof(String)
})
it("does not modify the string", function () {
expect(m.trust("foo").valueOf()).to.equal("foo")
})
it("is not identical to the string", function () {
expect(m.trust("foo")).to.not.equal("foo")
})
// 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"))
expect(root.childNodes[0].innerHTML).to.equal("a&b")
})
it("works with mixed trusted content in div", function () {
var root = document.createElement("div")
m.render(root, [m.trust("<p>1</p><p>2</p>"), m("i", "foo")])
expect(root.childNodes[2].tagName).to.equal("I")
})
it("works with mixed trusted content in text nodes", function () {
var root = document.createElement("div")
m.render(root, [
m.trust("<p>1</p>123<p>2</p>"),
m("i", "foo")
])
expect(root.childNodes[3].tagName).to.equal("I")
})
// TODO: m.trust's contents are having their tags stripped.
xit("works with mixed trusted content in td", function () {
var root = document.createElement("table")
root.appendChild(root = document.createElement("tr"))
m.render(root, [
m.trust("<td>1</td><td>2</td>"),
m("td", "foo")
])
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>")
})
// https://github.com/lhorie/mithril.js/issues/1045
it("correctly injects script tags and executes them", function () {
var HTMLString =
"<script>document.getElementById('root').innerText='After'</script>"
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")
})
})
})