-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro-html-template.tests.coffee
More file actions
219 lines (168 loc) · 7.17 KB
/
micro-html-template.tests.coffee
File metadata and controls
219 lines (168 loc) · 7.17 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
assert = require('chai').assert
compile = require('../').compile
version = require('../package.json').version
runtime = require('secure-filters')
{filters, render} = require('../dist/micro-html-template-runtime.min.js')
filters.upper = (val) -> val.toUpperCase()
filters.wrap = (val, prefix, suffix) -> prefix + val + suffix
env =
id : ">>id<<"
style : ">>\"'wow'\"<<"
uri : "foo=bar&baz=baf"
html : "<a href='#'>"
script : "foop'\"barp //]]></script>"
evaluate = (template, env) ->
render(compile(template), env)
describe "version #{version}", ->
describe "with no macros or macros in unsafe contexts", ->
tpl = [
"<h1 id='123'>hi there</h1>"
"<div>hello {not_a_macro}</div>"
"<h1 {{macro_in_attr_name}}='123'>hi there</h1>"
"<style>{{macro_in_style_tag}}</style>"
"<script>var x = '{{macro_in_script_tag}}'</script>"
"<h1 style='{{macro_in_style_attr_val}}'>hi there</h1>"
"<h1 onclick='{{macro_in_onstar_attr_val}}'>hi there</h1>"
]
it "should be the same as the input", ->
assert.equal(evaluate(t, env), t) for t in tpl
describe "with macro delimiter escaped", ->
tpl = "<div>hello {{{{not_a_macro}}</div>"
ret = "<div>hello {{not_a_macro}}</div>"
it "should properly emit the escaped delimiter", ->
assert.equal(evaluate(tpl, env), ret)
describe "with macro in attribute value", ->
tpl = "<h1 id='{{id}}'>hi there</h1>"
ret1 = "<h1 id='#{runtime.html(env.id)}'>hi there</h1>"
ret2 = "<h1 id=''>hi there</h1>"
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
it "should expand undefined to ''", ->
assert.equal(evaluate(tpl, {}), ret2)
describe "with macro in uri type attribute value", ->
tpl = "<script src='https://example.com?x={{uri}}'></script>"
ret1 = "<script src='https://example.com?x=#{runtime.uri(env.uri)}'></script>"
ret2 = "<script src='https://example.com?x='></script>"
it "should be expanded and uri escaped", ->
assert.equal(evaluate(tpl, env), ret1)
it "should expand undefined to ''", ->
assert.equal(evaluate(tpl, {}), ret2)
describe "with macro in div tag", ->
tpl = "<div>{{html}}</div>"
ret1 = "<div>#{runtime.html(env.html)}</div>"
ret2 = "<div></div>"
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
it "should expand undefined to ''", ->
assert.equal(evaluate(tpl, {}), ret2)
describe "with multiple tags", ->
tpl = """
<script src='https://example.com?x={{uri}}'></script>
<div>{{html}}</div>
"""
ret1 = """
<script src='https://example.com?x=#{runtime.uri(env.uri)}'></script>
<div>#{runtime.html(env.html)}</div>
"""
ret2 = """
<script src='https://example.com?x='></script>
<div></div>
"""
it "should be each expanded and escaped properly", ->
assert.equal(evaluate(tpl, env), ret1)
it "should expand undefined to ''", ->
assert.equal(evaluate(tpl, {}), ret2)
describe "with nested tags", ->
tpl = """
<div>
{{html}}
<script src='https://example.com?x={{uri}}'></script>
</div>
"""
ret1 = """
<div>
#{runtime.html(env.html)}
<script src='https://example.com?x=#{runtime.uri(env.uri)}'></script>
</div>
"""
# Whitespace below is significant:
ret2 = """
<div>
<script src='https://example.com?x='></script>
</div>
"""
it "should be expanded and escaped properly", ->
assert.equal(evaluate(tpl, env), ret1)
it "should expand undefined to ''", ->
assert.equal(evaluate(tpl, {}), ret2)
describe "with HTML comments", ->
tpl = """
<!-- This is a comment. -->
<script src='https://example.com?x={{uri}}'></script>
"""
# Whitespace below is significant:
ret = """
<script src='https://example.com?x=#{runtime.uri(env.uri)}'></script>
"""
it "should remove the comment and render the rest", ->
assert.equal(evaluate(tpl, env), ret)
describe "with boolean attribute", ->
tpl = "<h1 id='{{id}}' data-foo>hi there</h1>"
ret1 = "<h1 id='#{runtime.html(env.id)}' data-foo=''>hi there</h1>"
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
describe "with attribute value containing single quotes", ->
tpl = "<img onload=\"console.log('foop')\" src=\"#\">"
ret1 = "<img onload='console.log('foop')' src='#'>"
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
describe "with macro containing string literals and numbers", ->
tpl = '<div>{{"foop\\n"}} {{1956}} {{-1.5}} {{1.2e4}}</div>'
ret1 = '<div>foop\n 1956 -1.5 12000</div>'
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
describe "with macro using custom filter", ->
tpl = '<div>{{"foop" | upper}}</div>'
ret1 = '<div>FOOP</div>'
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
describe "with macro using a method invocation filter without parens", ->
tpl = '<div>{{"foop" | .toUpperCase}}</div>'
ret1 = '<div>FOOP</div>'
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
describe "with macro using a method invocation filter with parens", ->
tpl = '<div>{{"foop" | .toUpperCase()}}</div>'
ret1 = '<div>FOOP</div>'
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
describe "with macro using chained method invocation filters", ->
tpl = '<div>{{"foop" | .toUpperCase() | .substr(2)}}</div>'
ret1 = '<div>OP</div>'
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
describe "with macro using custom filter without initial val", ->
tpl = '<div>{{upper("foop")}}</div>'
ret1 = '<div>FOOP</div>'
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
describe "with macro using composition of custom filters", ->
tpl = '<div>{{"foop" | upper | wrap("[", "]")}}</div>'
ret1 = '<div>[FOOP]</div>'
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
describe "with macro using composition of custom filters without initial val", ->
tpl = '<div>{{upper("foop") | wrap("[", "]")}}</div>'
ret1 = '<div>[FOOP]</div>'
it "should be expanded and html escaped", ->
assert.equal(evaluate(tpl, env), ret1)
describe "with macro using the raw filter", ->
tpl = '<div>{{"[foop]" | raw}}</div>'
ret1 = '<div>[foop]</div>'
it "should be expanded and not escaped", ->
assert.equal(evaluate(tpl, env), ret1)
describe "with macro using the raw filter applied to html filter in a URI type attribute value", ->
tpl = "<img src='{{\"http://example.com?foo=<bar>\" | html | raw}}'>"
ret1 = '<img src=\'http://example.com?foo=<bar>\'>'
it "should be expanded and html escaped but not uri escaped", ->
assert.equal(evaluate(tpl, env), ret1)