-
Notifications
You must be signed in to change notification settings - Fork 5
/
core.clj
397 lines (335 loc) · 13 KB
/
core.clj
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
(ns tinsel.test.core
(:use tinsel.core
clojure.test)
;; These two are just for testing, see test-ns-membership.
(:require [clojure.string :as str]
[clojure.zip :as zip]
[hiccup.core :as hiccup]
[hiccup.page :as hpage])
(:use clojure.walk))
;; The most basic test: one with no transformations.
(deftemplate no-transforms-template [[:h1 "Hi!"]] [])
(deftest test-no-transform-template
(is (= "<h1>Hi!</h1>"
(no-transforms-template))))
;; Slightly more advanced: a transformation, but not based on arguments.
(deftemplate simple-transform-template [[:title "Title"] [:h1 "Hi!"]]
[arg-map]
(fn [zip-loc] (= (first (zip/node zip-loc)) "title"))
(fn [node] [:title "Cool Title"]))
(deftest test-simple-transform-template
(is (= "<title>Cool Title</title><h1>Hi!</h1>"
(simple-transform-template {}))))
;; A transformation, but based on template args.
(deftemplate argument-transform-template [[:title "Title"] [:h1 "Hi!"]]
[arg-map]
#(= (first (zip/node %)) "title")
(fn [node] [:title '(:title arg-map)]))
(deftest test-argument-transform-template
(is (= "<title>Cool Title</title><h1>Hi!</h1>"
(argument-transform-template {:title "Cool Title"}))))
;; Two transformations.
(deftemplate multiple-transform-template [[:title "Title"] [:h1 "Hi!"]]
[arg-map]
#(= (first (zip/node %)) "title")
(fn [node] [:title '(:title arg-map)])
#(= (first (zip/node %)) "h1")
(fn [node] [:h1 '(:header arg-map)]))
(deftest test-multiple-transform-template
(is (= "<title>Cool Title</title><h1>A Header</h1>"
(multiple-transform-template {:title "Cool Title"
:header "A Header"}))))
;;
;; Test Selectors
;;
;; Select multiple nodes by tag.
(deftemplate select-tag-template
[[:body#some-id [:h1] [:h1]]]
[arg-map]
(tag= :h1)
(fn [node] [:h1 '(:heading arg-map)]))
(deftest test-select-tag-template
(is (= "<body id=\"some-id\"><h1>HEADING</h1><h1>HEADING</h1></body>"
(select-tag-template {:heading "HEADING"}))))
;; Select by presence of attribute.
(deftemplate select-attr-present-template
[[:body [:ul {:some-attr "someval"} [:li "An item"]]]]
[added-li]
(has-attr? :some-attr)
(append-content [:li added-li]))
(deftest test-select-attr-present-template
(is (= "<body><ul some-attr=\"someval\"><li>An item</li><li>Another item</li></ul></body>"
(select-attr-present-template "Another item"))))
;; Select by equality of attribute.
(deftemplate select-attr-equal-template
[[:body [:ul {:some-attr "someval"} [:li "An item"]]]]
[added-li]
(attr= :some-attr "someval")
(append-content [:li added-li]))
(deftest test-select-attr-equal-template
(is (= "<body><ul some-attr=\"someval\"><li>An item</li><li>Another item</li></ul></body>"
(select-attr-equal-template "Another item"))))
;; Select by id.
(deftemplate select-id-template
[[:body#short-id [:h1 {:id "long-id"} "A heading."]]]
[arg-map]
(id= :long-id)
(fn [node] [:h1 {:id "long-id"} '(:heading arg-map)]))
(deftest test-select-id-template
(is (= "<body id=\"short-id\"><h1 id=\"long-id\">Some cool heading.</h1></body>"
(select-id-template {:heading "Some cool heading."}))))
;; Select by class.
(deftemplate select-class-template
[[:body [:ul [:li.a] [:li.a.b] [:li.a.b.c]]]]
[a b c]
(has-class? :a) (append-content a)
(has-class? :b) (append-content b)
(has-class? :c) (append-content c))
(deftest test-select-class-template
(is (= "<body><ul><li class=\"a\">A</li><li class=\"a b\">AB</li><li class=\"a b c\">ABC</li></ul></body>"
(select-class-template "A" "B" "C"))))
;; Select by nth child.
(deftemplate select-nth-child-template
[[:body [:ul [:li] [:li] [:li#here] [:li]]]]
[third-list-item]
(nth-child? 3)
(set-content third-list-item))
(deftemplate select-1st-child-template
[[:body [:ul [:li] [:li]]]] ;; Want to ensure root isn't selected.
[added-attrs]
(nth-child? 1)
(set-attrs added-attrs))
(deftest test-select-nth-child-template
(is (= "<body><ul><li></li><li></li><li id=\"here\">HERE</li><li></li></ul></body>"
(select-nth-child-template "HERE")))
(is (= "<body><ul first-child=\"true\"><li first-child=\"true\"></li><li></li></ul></body>"
(select-1st-child-template {:first-child "true"}))))
;; Select by nth last child.
(deftemplate select-nth-last-child-template
[[:body [:ul [:li] [:li] [:li#here] [:li]]]]
[third-list-item]
(nth-last-child? 2)
(set-content third-list-item))
(deftemplate select-last-child-template
[[:body [:ul [:li] [:li]]]] ;; Want to ensure root isn't selected.
[added-attrs]
(nth-last-child? 1)
(set-attrs added-attrs))
(deftest test-select-nth-last-child-template
(is (= "<body><ul><li></li><li></li><li id=\"here\">HERE</li><li></li></ul></body>"
(select-nth-last-child-template "HERE")))
(is (= "<body><ul last-child=\"true\"><li></li><li last-child=\"true\"></li></ul></body>"
(select-last-child-template {:last-child "true"}))))
;;
;; Test Selector Combinators
;;
;; Test both every-pred and some-fn combinators.
(deftemplate select-every-template
[[:body [:div.a] [:div.b] [:span.a] [:span.b]]]
[a b]
(every-selector (tag= :div)
(has-class? :a))
(append-content [:p (str "Div.a: " a)])
(some-selector (tag= :span)
(has-class? :b))
(append-content [:p (str "Span or .b: " b)]))
(deftest test-select-every-template
(is (= "<body><div class=\"a\"><p>Div.a: A</p></div><div class=\"b\"><p>Span or .b: B</p></div><span class=\"a\"><p>Span or .b: B</p></span><span class=\"b\"><p>Span or .b: B</p></span></body>"
(select-every-template "A" "B"))))
;; Test select combinator.
(deftemplate select-path-template
[[:body [:div.a [:div.b [:span.c [:span.d "Bullseye"]]
[:span#fakeout [:span.d]]]]]]
[new-content]
(select (has-class? :a)
(has-class? :b)
(every-selector (has-class? :c)
(tag= :span))
(has-class? :d))
(set-content new-content))
(deftest test-select-path-template
(is (= "<body><div class=\"a\"><div class=\"b\"><span class=\"c\"><span class=\"d\">Wow, found it!</span></span><span id=\"fakeout\"><span class=\"d\"></span></span></div></div></body>"
(select-path-template "Wow, found it!"))))
;; Test or-ancestor combinator.
(deftemplate select-or-ancestor-template
[[:body [:div.a [:div.b [:span.c [:span.d "Bullseye"]]
[:span#fakeout [:span.d]]]]]]
[new-content]
(select (or-ancestor (tag= :div))
(or-ancestor (has-class? :c))
(has-class? :d))
(set-content new-content))
(deftest test-select-or-ancestor-template
(is (= "<body><div class=\"a\"><div class=\"b\"><span class=\"c\"><span class=\"d\">Wow, found it!</span></span><span id=\"fakeout\"><span class=\"d\"></span></span></div></div></body>"
(select-or-ancestor-template "Wow, found it!"))))
;;
;; Test Transformers
;;
;; Set content
(deftemplate set-content-template1
[[:body [:h1#replace-me "Replace me!"]]]
[arg-map]
(id= :replace-me)
(set-content (:replacement arg-map)))
(deftemplate set-content-template2
[[:body [:h1#replace-me "Replace me!"]]]
[arg-map]
(id= :replace-me)
(set-content [:em (:replacement arg-map)]))
;; Make sure we can replace the root node's content.
(deftemplate set-content-template3
[[:html [:h1 "I'm going to be replaced!"]]]
[arg-map]
(tag= :html)
(set-content [:h2 (:replacement arg-map)]))
(deftest test-set-content-template
(is (= "<body><h1 id=\"replace-me\">You have been replaced.</h1></body>"
(set-content-template1 {:replacement "You have been replaced."})))
(is (= "<body><h1 id=\"replace-me\"><em>You have been replaced.</em></h1></body>"
(set-content-template2 {:replacement "You have been replaced."})))
(is (= "<html><h2>I'm the replacement.</h2></html>"
(set-content-template3 {:replacement "I'm the replacement."}))))
;; Append content
(deftemplate append-content-template1
[[:body [:ul#add-to-me
[:li "Add something after me!"]]]]
[arg-map]
(id= :add-to-me)
(append-content (:addition arg-map)))
(deftemplate append-content-template2
[[:body [:ul#add-to-me
[:li "Add something after me!"]]]]
[addition]
(id= :add-to-me)
(append-content addition))
;; Make sure we can append content to an empty root node.
(deftemplate append-content-template3
[[:html#doc]]
[addition]
(id= :doc)
(append-content addition))
(deftest test-append-content-template
(is (= "<body><ul id=\"add-to-me\"><li>Add something after me!</li><li>Ohai!</li></ul></body>"
(append-content-template1 {:addition [:li "Ohai!"]})))
(is (= "<body><ul id=\"add-to-me\"><li>Add something after me!</li><li>Ohai!</li></ul></body>"
(append-content-template2 [:li "Ohai!"])))
(is (= "<html id=\"doc\"><h1>The content.</h1></html>"
(append-content-template3 [:h1 "The content."]))))
;; Prepend content
(deftemplate prepend-content-template1
[[:body [:ul#add-to-me
[:li "Add something before me!"]]]]
[arg-map]
(id= :add-to-me)
(prepend-content (:addition arg-map)))
(deftemplate prepend-content-template2
[[:body [:ul#add-to-me
[:li "Add something before me!"]]]]
[addition]
(id= :add-to-me)
(prepend-content addition))
;; Make sure we can prepend content to an empty root node.
(deftemplate prepend-content-template3
[[:html#doc]]
[addition]
(tag= :html)
(prepend-content addition))
(deftest test-prepend-content-template
(is (= "<body><ul id=\"add-to-me\"><li>Ohai!</li><li>Add something before me!</li></ul></body>"
(prepend-content-template1 {:addition [:li "Ohai!"]})))
(is (= "<body><ul id=\"add-to-me\"><li>Ohai!</li><li>Add something before me!</li></ul></body>"
(prepend-content-template2 [:li "Ohai!"])))
(is (= "<html id=\"doc\"><h1>The content.</h1></html>"
(prepend-content-template3 [:h1 "The content."]))))
;; Set Attributes
(deftemplate set-attribute-template1
[[:body [:img]]]
[arg-map]
(tag= :img)
(set-attrs {:src (:url arg-map)}))
(deftemplate set-attribute-template2
[[:body [:img]]]
[arg-url]
(tag= :img)
(set-attrs {:src arg-url}))
(deftemplate set-attribute-template3
[[:body [:a "Some link text"]]]
[arg-url]
(tag= :a)
(set-attrs {:href arg-url}))
(deftemplate set-attribute-template4 ;; Testing map arg in a symbol...
[[:body [:img]]]
[arg-map]
(tag= :img)
(set-attrs arg-map))
(deftest test-set-attribute-template
(is (= "<body><img src=\"http://example.com/img.jpg\" /></body>"
(set-attribute-template1 {:url "http://example.com/img.jpg"})))
(is (= "<body><img src=\"http://example.com/img.jpg\" /></body>"
(set-attribute-template2 "http://example.com/img.jpg")))
(is (= "<body><a href=\"http://fark.com\">Some link text</a></body>"
(set-attribute-template3 "http://fark.com")))
(is (= "<body><img src=\"http://example.com/img.jpg\" /></body>"
(set-attribute-template4 {:src "http://example.com/img.jpg"}))))
;;
;; Transformer Combinators
;;
(deftemplate accumulate-template1
[[:body [:div]]]
[arg-map]
(tag= :div)
(accumulate
(set-content [:p (:middle arg-map)])
(prepend-content [:p (:first arg-map)])
(append-content [:p (:last arg-map)])
(set-attrs {:id "paras"})))
(deftest test-accumulate-template
(is (= "<body><div id=\"paras\"><p>I'm first!</p><p>I'm the middle!</p><p>I'm last!</p></div></body>"
(accumulate-template1 {:first "I'm first!"
:middle "I'm the middle!"
:last "I'm last!"}))))
;;
;; Miscellaneous
;;
;; A slightly larger template, check output quality against hiccup.
(deftemplate medium-template
[[:html
[:head
[:title "Literal String"]]
[:body
[:div.example]
[:ul.times-table]]]]
[text]
(tag= :div)
(set-content text)
(tag= :ul)
(set-content (for [n (range 1 13)]
[:li n " * 9 = " (* n 9)])))
(deftest test-medium-template
(is (= (medium-template "Some text")
(let [text "Some text"]
(hiccup/html
[:html
[:head
[:title "Literal String"]]
[:body
[:div.example text]
[:ul.times-table
(for [n (range 1 13)]
[:li n " * 9 = " (* n 9)])]]])))))
;; Test for hiccup page-helpers to work.
(deftemplate hiccup-page-helpers
[[:html [:head (hpage/include-js "some-js.js")]]]
[css-filename]
(tag= :head)
(append-content (hpage/include-css css-filename)))
(deftest test-hiccup-page-helpers
(is (= "<html><head><script src=\"some-js.js\" type=\"text/javascript\"></script><link href=\"some-css.css\" rel=\"stylesheet\" type=\"text/css\" /></head></html>"
(hiccup-page-helpers "some-css.css"))))
;; Make sure we can use a string as a hiccup form, so we can set doctype.
(deftemplate string-as-form-template
["<!DOCTYPE html>" [:html ["body"]] "<!--asdf-->"]
[])
(deftest test-string-as-form
(is (= "<!DOCTYPE html><html><body></body></html><!--asdf-->"
(string-as-form-template))))