/
highlight.spec.js
356 lines (304 loc) · 10.3 KB
/
highlight.spec.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
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
'use strict';
const should = require('chai').should(); // eslint-disable-line
const hljs = require('highlight.js');
const Entities = require('html-entities').XmlEntities;
const entities = new Entities();
const validator = require('html-tag-validator');
const testJson = {
foo: 1,
bar: 2
};
const testString = JSON.stringify(testJson, null, ' ');
const start = '<figure class="highlight plain"><table><tr>';
const end = '</tr></table></figure>';
const gutterStart = '<td class="gutter"><pre>';
const gutterEnd = '</pre></td>';
const codeStart = '<td class="code"><pre>';
const codeEnd = '</pre></td>';
function gutter(start, end) {
let result = gutterStart;
for (let i = start; i <= end; i++) {
result += `<span class="line">${i}</span><br>`;
}
result += gutterEnd;
return result;
}
function code(str, lang) {
let data;
if (lang) {
data = hljs.highlight(lang.toLowerCase(), str);
} else if (lang === null) {
data = {value: str};
} else {
data = {value: entities.encode(str)};
}
const lines = data.value.split('\n');
return lines.reduce((prev, current) => {
return `${prev}<span class="line">${current}</span><br>`;
}, codeStart) + codeEnd;
}
function assertResult(result, ...args) {
result.should.eql(start + args.join('') + end);
}
function validateHtmlAsync(str, done) {
validator(str, (err, ast) => {
if (err) {
done(err);
} else {
done();
}
});
}
describe('highlight', () => {
const highlight = require('../lib/highlight');
it('default', done => {
const result = highlight(testString);
assertResult(result, gutter(1, 4), code(testString));
validateHtmlAsync(result, done);
});
it('str must be a string', () => {
highlight.should.throw('str must be a string!');
});
it('gutter: false', done => {
const result = highlight(testString, {gutter: false});
assertResult(result, code(testString));
validateHtmlAsync(result, done);
});
it('gutter: true, but "wrap: false" (conflict)', done => {
const result = highlight(testString, {gutter: true, wrap: false});
assertResult(result, gutter(1, 4), code(testString));
validateHtmlAsync(result, done);
});
it('wrap: false (without hljs, without lang)', done => {
const result = highlight(testString, {gutter: false, wrap: false});
result.should.eql([
'<pre><code class="highlight plain">',
entities.encode(testString),
'</code></pre>'
].join(''));
validateHtmlAsync(result, done);
});
it('wrap: false (with hljs, without lang)', done => {
const result = highlight(testString, {gutter: false, wrap: false, hljs: true});
result.should.eql([
'<pre><code class="hljs plain">',
entities.encode(testString),
'</code></pre>'
].join(''));
validateHtmlAsync(result, done);
});
it('wrap: false (without hljs, with lang)', done => {
const result = highlight(testString, {gutter: false, wrap: false, lang: 'json'});
hljs.configure({classPrefix: ''});
result.should.eql([
'<pre><code class="highlight json">',
hljs.highlight('json', testString).value,
'</code></pre>'
].join(''));
validateHtmlAsync(result, done);
});
it('wrap: false (with hljs, with lang)', done => {
const result = highlight(testString, {gutter: false, wrap: false, hljs: true, lang: 'json'});
hljs.configure({classPrefix: 'hljs-'});
result.should.eql([
'<pre><code class="hljs json">',
hljs.highlight('json', testString).value,
'</code></pre>'
].join(''));
validateHtmlAsync(result, done);
});
it('firstLine', done => {
const result = highlight(testString, {firstLine: 3});
assertResult(result, gutter(3, 6), code(testString));
validateHtmlAsync(result, done);
});
it('lang = json', done => {
const result = highlight(testString, {lang: 'json'});
result.should.eql([
'<figure class="highlight json"><table><tr>',
gutter(1, 4),
code(testString, 'json'),
end
].join(''));
validateHtmlAsync(result, done);
});
it('auto detect', done => {
const result = highlight(testString, {autoDetect: true});
result.should.eql([
'<figure class="highlight json"><table><tr>',
gutter(1, 4),
code(testString, 'json'),
end
].join(''));
validateHtmlAsync(result, done);
});
it('don\'t highlight if language not found', done => {
const result = highlight('test', {lang: 'jrowiejrowi'});
assertResult(result, gutter(1, 1), code('test'));
validateHtmlAsync(result, done);
});
// it('don\'t highlight if parse failed'); missing-unit-test
it('caption', done => {
const result = highlight(testString, {
caption: 'hello world'
});
result.should.eql([
'<figure class="highlight plain"><figcaption>hello world</figcaption><table><tr>',
gutter(1, 4),
code(testString),
end
].join(''));
validateHtmlAsync(result, done);
});
it('tab', done => {
const str = [
'function fib(i){',
'\tif (i <= 1) return i;',
'\treturn fib(i - 1) + fib(i - 2);',
'}'
].join('\n');
const result = highlight(str, {tab: ' ', lang: 'js'});
result.should.eql([
'<figure class="highlight js"><table><tr>',
gutter(1, 4),
code(str.replace(/\t/g, ' '), 'js'),
end
].join(''));
validateHtmlAsync(result, done);
});
it('escape html entity', done => {
const str = [
'deploy:',
' type: git',
' repo: <repository url>',
' branch: [branch]',
' message: [message]'
].join('\n');
const result = highlight(str);
result.should.include('<repository url>');
validateHtmlAsync(result, done);
});
it('highlight sublanguages', done => {
const str = '<node><?php echo "foo"; ?></node>';
const result = highlight(str, { autoDetect: true });
result.should.eql([
'<figure class="highlight php-template"><table><tr>',
gutter(1, 1),
code('<span class="xml"><span class="tag"><<span class="name">node</span>></span></span><span class="php"><span class="meta"><?php</span> <span class="keyword">echo</span> <span class="string">"foo"</span>; <span class="meta">?></span></span><span class="xml"><span class="tag"></<span class="name">node</span>></span></span>', null),
end
].join(''));
validateHtmlAsync(result, done);
});
it('parse multi-line strings correctly', done => {
const str = [
'var string = `',
' Multi',
' line',
' string',
'`'
].join('\n');
const result = highlight(str, {lang: 'js'});
result.should.eql([
'<figure class="highlight js"><table><tr>',
gutter(1, 5),
code('<span class="keyword">var</span> string = <span class="string">`</span>\n<span class="string"> Multi</span>\n<span class="string"> line</span>\n<span class="string"> string</span>\n<span class="string">`</span>', null),
end
].join(''));
validateHtmlAsync(result, done);
});
it('parse multi-line strings including empty line', done => {
const str = [
'var string = `',
' Multi',
'',
' string',
'`'
].join('\n');
const result = highlight(str, {lang: 'js'});
result.should.eql([
'<figure class="highlight js"><table><tr>',
gutter(1, 5),
code('<span class="keyword">var</span> string = <span class="string">`</span>\n<span class="string"> Multi</span>\n<span class="string"></span>\n<span class="string"> string</span>\n<span class="string">`</span>', null),
end
].join(''));
validateHtmlAsync(result, done);
});
it('auto detect of multi-line statement', done => {
const str = [
'"use strict";',
'var string = `',
' Multi',
'',
' string',
'`'
].join('\n');
const result = highlight(str, {autoDetect: true});
result.should.eql([
'<figure class="highlight javascript"><table><tr>',
gutter(1, 6),
code('<span class="meta">"use strict"</span>;\n<span class="keyword">var</span> string = <span class="string">`</span>\n<span class="string"> Multi</span>\n<span class="string"></span>\n<span class="string"> string</span>\n<span class="string">`</span>', null),
end
].join(''));
validateHtmlAsync(result, done);
});
it('gives the highlight class to marked lines', done => {
const str = [
'roses are red',
'violets are blue',
'sugar is sweet',
'and so are you'
].join('\n');
const result = highlight(str, {mark: [1, 3, 5]});
result.should.include('class="line marked">roses');
result.should.include('class="line">violets');
result.should.include('class="line marked">sugar');
result.should.include('class="line">and');
validateHtmlAsync(result, done);
});
it('hljs compatibility - with lines', done => {
const str = [
'function (a) {',
' if (a > 3)',
' return true;',
' return false;',
'}'
].join('\n');
const result = highlight(str, {hljs: true, lang: 'javascript' });
result.should.include(gutterStart);
result.should.include(codeStart);
result.should.include('code class="hljs javascript"');
result.should.include('class="hljs-function"');
result.should.include(gutter(1, 5));
validateHtmlAsync(result, done);
});
it('hljs compatibility - no lines', done => {
const str = [
'function (a) {',
' if (a > 3)',
' return true;',
' return false;',
'}'
].join('\n');
const result = highlight(str, {hljs: true, gutter: false, wrap: false, lang: 'javascript'});
result.should.not.include(gutterStart);
result.should.not.include(codeStart);
result.should.include('code class="hljs javascript"');
result.should.include('class="hljs-function"');
validateHtmlAsync(result, done);
});
it('hljs compatibility - wrap is true', done => {
const str = [
'function (a) {',
' if (a > 3)',
' return true;',
' return false;',
'}'
].join('\n');
const result = highlight(str, {hljs: true, gutter: false, wrap: true, lang: 'javascript'});
result.should.not.include(gutterStart);
result.should.include(codeStart);
result.should.include('code class="hljs javascript"');
result.should.include('class="hljs-function"');
validateHtmlAsync(result, done);
});
});