forked from helpers/handlebars-helpers
-
Notifications
You must be signed in to change notification settings - Fork 5
/
html.js
249 lines (229 loc) · 9.19 KB
/
html.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
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
const assert = require('assert');
const hbs = require('handlebars').create();
const helpers = require('..');
helpers.html({ handlebars: hbs });
const locals = {data: [{aaa: 'AAA', bbb: 'BBB'}, {aaa: 'CCC', bbb: 'DDD'}]};
let actual;
describe('html', function() {
describe('attr', function() {
it('should strip html from a string.', function() {
const actual = hbs.compile('<div{{{attr class=foo}}}></div>')({foo: 'btn'});
assert.equal(actual, '<div class="btn"></div>');
assert.equal(hbs.compile('{{attr}}')(), '');
});
});
describe('css', function() {
it('should return an empty string when no context is passed:', function() {
assert.equal(hbs.compile('{{{css}}}')(), '');
});
it('should use a path passed as a string', function() {
const actual = hbs.compile('{{{css "abc.css"}}}')();
assert.equal(actual, '<link type="text/css" rel="stylesheet" href="abc.css">');
});
it('should use options.assets', function() {
const actual = hbs.compile('{{{css "abc.css"}}}')({options: {assets: 'foo'}});
assert.equal(actual, '<link type="text/css" rel="stylesheet" href="foo/abc.css">');
});
it('should ensure that options.assets is a string', function() {
const actual = hbs.compile('{{{css "abc.css"}}}')({options: {assets: null}});
assert.equal(actual, '<link type="text/css" rel="stylesheet" href="abc.css">');
});
it('should not use options.assets when passing in an absolute url', function() {
const actual = hbs.compile('{{{css "https://abc.com/bar.css"}}}')({options: {assets: 'foo'}});
assert.equal(actual, '<link type="text/css" rel="stylesheet" href="https://abc.com/bar.css">');
});
it('should use the `href` attribute on the hash', function() {
actual = hbs.compile('{{{css href=""}}}')();
assert.equal(actual, '');
actual = hbs.compile('{{{css href="abc.css"}}}')();
assert.equal(actual, '<link type="text/css" rel="stylesheet" href="abc.css">');
});
it('should create multiple tags from an array passed on the context:', function() {
const ctx = {styles: ['a.css', 'bcss', 'c.css'] };
assert.equal(hbs.compile('{{{css styles}}}')(ctx), [
'<link type="text/css" rel="stylesheet" href="a.css">',
'<link type="text/css" rel="stylesheet" href="bcss">',
'<link type="text/css" rel="stylesheet" href="c.css">'
].join('\n'));
});
it('should create a less tag (TODO: only works with array format)', function() {
const ctx = {styles: ['a.less'] };
assert.equal(hbs.compile('{{{css styles}}}')(ctx), '<link type="text/css" rel="stylesheet/less" href="a.less">');
});
});
describe('ul', function() {
it('should should return an unordered list', function() {
const fn = hbs.compile('{{#ul data class="names"}}{{aaa}} {{bbb}}{{/ul}}');
assert.equal(fn(locals), '<ul class="names"><li>AAA BBB</li>\n<li>CCC DDD</li></ul>');
});
});
describe('ol', function() {
it('should should return an ordered list', function() {
const fn = hbs.compile('{{#ol data class="names"}}{{aaa}} {{bbb}}{{/ol}}');
assert.equal(fn(locals), '<ol class="names"><li>AAA BBB</li>\n<li>CCC DDD</li></ol>');
});
});
describe('thumbnailImage', function() {
describe('{{{thumbnailImage context}}}', function() {
it('should return figure with link and caption', function() {
const context = {
data: {
id: 'id',
alt: 'Picture of a placeholder',
thumbnail: 'http://placehold.it/200x200/0eafff/ffffff.png',
size: {
width: 200,
height: 200
},
full: 'http://placehold.it/600x400/0eafff/ffffff.png',
caption: 'My new caption!'
}
};
const fn = hbs.compile('{{{thumbnailImage data}}}');
const comparison = [
'<figure id="image-id">',
'<a href="http://placehold.it/600x400/0eafff/ffffff.png" rel="thumbnail">',
'<img alt="Picture of a placeholder" src="http://placehold.it/200x200/0eafff/ffffff.png" width="200" height="200">',
'</a>',
'<figcaption>My new caption!</figcaption>',
'</figure>'
].join('\n');
assert.equal(fn(context), comparison);
});
it('should return figure with extra class "test"', function() {
const source = '{{{thumbnailImage data}}}';
const context = {
data: {
id: 'id',
alt: 'Picture of a placeholder',
thumbnail: 'http://placehold.it/200x200/0eafff/ffffff.png',
size: {
width: 200,
height: 200
},
classes: {
figure: ['test']
},
full: 'http://placehold.it/600x400/0eafff/ffffff.png',
caption: 'My new caption!'
}
};
const fn = hbs.compile(source);
const comparison = [
'<figure id="image-id" class="test">',
'<a href="http://placehold.it/600x400/0eafff/ffffff.png" rel="thumbnail">',
'<img alt="Picture of a placeholder" src="http://placehold.it/200x200/0eafff/ffffff.png" width="200" height="200">',
'</a>',
'<figcaption>My new caption!</figcaption>',
'</figure>'
].join('\n');
assert.equal(fn(context), comparison);
});
it('should return figure with image that has class "test"', function() {
const source = '{{{thumbnailImage data}}}';
const context = {
data: {
id: 'id',
alt: 'Picture of a placeholder',
thumbnail: 'http://placehold.it/200x200/0eafff/ffffff.png',
size: {
width: 200,
height: 200
},
full: 'http://placehold.it/600x400/0eafff/ffffff.png',
classes: {
image: ['test']
},
caption: 'My new caption!'
}
};
const fn = hbs.compile(source);
const comparison = [
'<figure id="image-id">',
'<a href="http://placehold.it/600x400/0eafff/ffffff.png" rel="thumbnail">',
'<img alt="Picture of a placeholder" src="http://placehold.it/200x200/0eafff/ffffff.png" width="200" height="200" class="test">',
'</a>',
'<figcaption>My new caption!</figcaption>',
'</figure>'
].join('\n');
assert.equal(fn(context), comparison);
});
it('should return figure with link that has class "test"', function() {
const source = '{{{thumbnailImage data}}}';
const context = {
data: {
id: 'id',
alt: 'Picture of a placeholder',
thumbnail: 'http://placehold.it/200x200/0eafff/ffffff.png',
size: {
width: 200,
height: 200
},
full: 'http://placehold.it/600x400/0eafff/ffffff.png',
classes: {
link: ['test']
},
caption: 'My new caption!'
}
};
const fn = hbs.compile(source);
const comparison = [
'<figure id="image-id">',
'<a href="http://placehold.it/600x400/0eafff/ffffff.png" rel="thumbnail" class="test">',
'<img alt="Picture of a placeholder" src="http://placehold.it/200x200/0eafff/ffffff.png" width="200" height="200">',
'</a>',
'<figcaption>My new caption!</figcaption>',
'</figure>'
].join('\n');
assert.equal(fn(context), comparison);
});
it('should return figure without link', function() {
const source = '{{{thumbnailImage data}}}';
const context = {
data: {
id: 'id',
alt: 'Picture of a placeholder',
thumbnail: 'http://placehold.it/200x200/0eafff/ffffff.png',
size: {
width: 200,
height: 200
},
caption: 'My new caption!'
}
};
const fn = hbs.compile(source);
const comparison = [
'<figure id="image-id">',
'<img alt="Picture of a placeholder" src="http://placehold.it/200x200/0eafff/ffffff.png" width="200" height="200">',
'<figcaption>My new caption!</figcaption>',
'</figure>'
].join('\n');
assert.equal(fn(context), comparison);
});
it('should return figure without caption', function() {
const source = '{{{thumbnailImage data}}}';
const context = {
data: {
id: 'id',
alt: 'Picture of a placeholder',
thumbnail: 'http://placehold.it/200x200/0eafff/ffffff.png',
size: {
width: 200,
height: 200
},
full: 'http://placehold.it/600x400/0eafff/ffffff.png'
}
};
const fn = hbs.compile(source);
const comparison = [
'<figure id="image-id">',
'<a href="http://placehold.it/600x400/0eafff/ffffff.png" rel="thumbnail">',
'<img alt="Picture of a placeholder" src="http://placehold.it/200x200/0eafff/ffffff.png" width="200" height="200">',
'</a>',
'</figure>'
].join('\n');
assert.equal(fn(context), comparison);
});
});
});
});