-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.lua
346 lines (311 loc) · 10.4 KB
/
test.lua
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
-- To run:
-- busted test.lua
-- You can install busted with Luarocks
-- Check if we are running in the Lua versions we want to:
print("LUA = ".._VERSION)
print("JIT = "..(jit and jit.version or 'no'))
describe("SAX", function()
local sax = require 'lullaby.sax'
local yield = coroutine.yield
it("Should print DOM", function()
local stream = sax.from_coro(function()
yield(sax.StartEvent('a', {}))
for i=1,3 do
yield(sax.StartEvent('b', {{'x', 'xv'}}))
yield(sax.TextEvent(tostring(i)))
yield(sax.EndEvent('b'))
end
yield(sax.EndEvent('a'))
end)
local dom = sax.to_dom(stream)
assert.are_same(
--expected
{nodetype='TAG', tagname='a', attrs={}, children={
{nodetype='TAG', tagname='b', attrs={{'x', 'xv'}}, children={
{nodetype='TEXT', text='1'},
}},
{nodetype='TAG', tagname='b', attrs={{'x', 'xv'}}, children={
{nodetype='TEXT', text='2'},
}},
{nodetype='TAG', tagname='b', attrs={{'x', 'xv'}}, children={
{nodetype='TEXT', text='3'},
}}
}},
--result:
dom
)
end)
end)
describe("Lullaby", function()
local H = require 'lullaby'
local function dotest(pretty, body)
return H._printToString(pretty, function() H.usingHtml(body) end)
end
local function assert_html(expected, body)
assert.are_same(expected, dotest(false, body))
end
local function assert_error(errmsg, body)
assert.has_error(function() dotest(false, body) end, errmsg)
end
it("Text escaping", function()
assert_html([[ asd ; & < > " ' foo]],
function(_ENV) Text(" asd ; & < > \" \' foo") end)
end)
it("Attribute escaping", function()
assert_html([[<div id="x" title=" asd ; & < > " ' foo"></div>]],
function(_ENV) DIV{title=" asd ; & < > \" \' foo", id="x"} end)
end)
describe("Normal elements", function()
it("nil content", function()
assert_html([[<span></span>]],
function(_ENV) SPAN{} end)
end)
it("text content", function()
assert_html([[<span>Hello</span>]],
function(_ENV) SPAN{"Hello"} end)
end)
it("html content", function()
assert_html([[<span><strong></strong></span>]],
function(_ENV) SPAN{function() STRONG{} end} end)
end)
end)
describe("Void elements", function()
it("nil content", function()
assert_html([[<br>]],
function(_ENV) BR{} end)
end)
it("text content", function()
assert_error("Void element br should not have contents",
function(_ENV) BR{"Hello"} end)
end)
it("html content", function()
assert_error("Void element br should not have contents",
function(_ENV) BR{function() end} end)
end)
end)
describe("Raw elements", function()
it("does not escape", function()
assert_html([[<script>window.x=(1<2) && 3 > 4;</script>]],
function(_ENV) SCRIPT{Raw"window.x=(1<2) && 3 > 4;"} end)
end)
it("should be raw", function()
assert_error("script expects raw content",
function(_ENV) SCRIPT{"x"} end)
end)
it("forbids close tag", function()
assert_error("Close tag in raw content for script element",
function(_ENV) SCRIPT{Raw"pattern = /</"} end)
end)
end)
it("Should forbid unwrapped child tags", function()
assert_error("Missing function wrapper in element body",
function(_ENV) SPAN{STRONG{}} end)
end)
it("Should expect a table argument", function()
assert_error("Element constructor should receive a table parameter",
function(_ENV) SPAN("hello") end)
end)
it("Should expect a table argument", function()
assert_error("Multiple element body parameters",
function(_ENV) SPAN{"hello", "world"} end)
end)
describe("Attributes", function()
it("should be case insensitive", function()
assert_html([[<div ID="asd"></div>]],
function(_ENV) DIV{ ID="asd" } end)
end)
it("should detect unknown attributes", function()
assert_error("Unknown attribute \"blablabla\"",
function(_ENV) DIV{ blablabla="foo" } end)
end)
it("should detect misplaced attributes", function()
assert_error("Attribute \"disabled\" not allowed on tag \"div\"",
function(_ENV) DIV{ disabled=true } end)
end)
describe("Types", function()
it("text", function()
assert_html([[<div id="asd"></div>]],
function(_ENV) DIV{ id="asd" } end)
assert_error("Attribute \"id\" received a boolean; expected string",
function(_ENV) DIV{ id=true } end)
end)
it("enum", function()
assert_html([[<form method="POST"></form>]],
function(_ENV) FORM{ method="POST" } end)
assert_error("Attribute \"method\" does not allow value \"asd\"",
function(_ENV) FORM{ method="asd" } end)
end)
it("bool", function()
assert_html([[<button disabled></button>]],
function(_ENV) BUTTON{ disabled=true } end)
assert_html([[<button></button>]],
function(_ENV) BUTTON{ disabled=false } end)
assert_error("Attribute \"disabled\" received a string; expected boolean",
function(_ENV) BUTTON{ disabled="true" } end)
end)
it("URL", function()
assert_html([[<a href="http://www.example.com/">x</a>]],
function(_ENV) A{ href=AbsUrl{'http', 'www.example.com'}, "x"} end)
assert_error("Attribute \"href\" received a string; expected Url",
function(_ENV) A{ href="www.example.com" } end)
end)
it("Raw", function()
assert_html([[<div onclick="alert('hi')"></div>]],
function(_ENV) DIV{ onclick=Raw"alert('hi')" } end)
assert_error("Attribute \"onclick\" received a string; expected Raw",
function(_ENV) DIV{ onclick="alert('hi')" } end)
end)
end)
end)
describe("Urls", function()
local function assert_url(href, url)
assert_html('<a href="'..href..'"></a>', function(_ENV) A{ href=url } end)
end
it("Everything", function()
assert_url("http://www.example.com/a/b.html?t=10m&x=y#x1",
H.AbsUrl{'http', 'www.example.com', {'a','b.html'}, params={t='10m',x='y'}, hash="x1"})
end)
it("URL escaping", function()
assert_url("http://www.example.com/?%3C%3E=%23&%3F%25=%20#%2E%2E",
H.AbsUrl{'http', 'www.example.com', nil, params={['?%']=' ', ['<>']='#'}, hash=".."})
end)
it("no scheme", function()
assert_url("//www.example.com/",
H.AbsUrl{nil, 'www.example.com'})
end)
it("asolute path", function()
assert_url("/x/y/z.html",
H.AbsUrl{nil, nil, {'x', 'y', 'z.html'}})
end)
it("relative", function()
assert_url("foo?x=y#z",
H.RelUrl{{'foo'}, params={x="y"}, hash="z"})
end)
it("just query", function()
assert_url("?t=10m",
H.RelUrl{nil, params={t='10m'}})
end)
it("just hash", function()
assert_url("#Chen",
H.RelUrl{nil, hash="Chen"})
end)
it("nonstring query params", function()
assert_url("?n=42",
H.RelUrl{nil, params={n=42}})
end)
it("null query params", function()
assert_url("?bar&foo",
H.RelUrl{nil, params={foo=H.Nil, bar=H.Nil}})
end)
local function url_error(err, ctor, args)
assert_error(err, function(_ENV) A{href=ctor(args)} end)
end
describe("Errors", function()
it("bad scheme", function()
url_error("Unrecognized scheme \"zzzz\"", H.AbsUrl, {'zzzz', 'www.example.com'})
end)
it("missing host", function()
url_error("Host must be present if scheme is present", H.AbsUrl, {'http', nil})
end)
it("slash in hostname", function()
url_error("Bad characters in host \"www.foo.com/bar\"", H.AbsUrl, {'http', 'www.foo.com/bar'})
end)
it("empty relative url", function()
url_error("Empty relative Url", H.RelUrl, {})
end)
end)
end)
it('data-X attributes', function()
assert_html([[<div data-1000="qwe" data-X-Y="zxc" data-foo="asd"></div>]],
function(_ENV)
DIV{ ['data-foo']="asd", ['data-X-Y']="zxc", ['data-1000']="qwe" }
end)
end)
describe('Document Constructor', function()
it("no spaces", function()
assert.are_equal(
[[<!DOCTYPE html>
<html><head><title>T</title></head><body></body></html>]],
dotest(false, function(_ENV)
local doc = Document({
title="T",
})
doc()
end)
)
end)
it("pretty printing", function()
assert.are_equal(
[[<!DOCTYPE html>
<html
><head
><meta charset="utf-8"
><title
>Hello</title
><link href="./foo.css" rel="stylesheet"
><script src="./foo.js"
></script
></head
><body
><!-- This is a comment --><span onclick="alert("oi")"
>as<d</span
><span>OI</span><img SRC="http://www.pudim.com.br/" alt="Pudim"
><div class="FOO" fb:foo="bar"
><button disabled
>Click me</button
><pre contenteditable="true" style="background-color:green"
>
XXX</pre
><ol
><li
>1</li
><li
><strong
>2</strong
></li
><li
>3</li
></ol
></div
></body
></html
>]],
dotest(true, function(_ENV)
local doc = Document{
encoding="utf-8",
title="Hello",
head=function()
LINK{rel='stylesheet', href=Raw'./foo.css'}
SCRIPT{src=Raw'./foo.js'}
end,
body=function()
RawHtml("<!-- This is a comment -->")
SPAN{ onclick=Raw'alert("oi")', 'as<d'}
RawHtml("<span>OI</span>")
IMG{ SRC=AbsUrl{'http', 'www.pudim.com.br'}, alt="Pudim" }
DIV{ class="FOO", ["fb:foo"]=Raw"bar", function()
BUTTON{disabled=true, function()
Text("Click me")
end}
PRE{ style=Raw'background-color:green', contenteditable='true',
'XXX'}
OL{function()
for i = 1,3 do
LI{function()
if i == 2 then
STRONG{tostring(i)}
else
Text(tostring(i))
end
end}
end
end}
end}
end,
}
doc()
end)
)
end)
end)
end)