forked from twitter/twemoji
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
556 lines (553 loc) · 19 KB
/
test.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
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
/*! Copyright Twitter Inc. and other contributors. Licensed under MIT *//*
https://github.com/twitter/twemoji/blob/gh-pages/LICENSE
*/
var base = twemoji.base;
wru.test([{
name: 'string parsing',
test: function () {
// without variant
wru.assert(
'default parsing works',
twemoji.parse('I \u2764 emoji!') ===
'I <img class="emoji" draggable="false" alt="\u2764" src="' + base + '36x36/2764.png"> emoji!'
);
// with "as image" variant
wru.assert(
'default \uFE0F variant parsing works',
twemoji.parse('I \u2764\uFE0F emoji!') ===
'I <img class="emoji" draggable="false" alt="\u2764\uFE0F" src="' + base + '36x36/2764.png"> emoji!'
);
// with "as text" variant
wru.assert(
'default \uFE0E variant parsing works',
twemoji.parse('I \u2764\uFE0E emoji!') ===
'I \u2764\uFE0E emoji!'
);
}
},{
name: 'string parsing + size',
test: function () {
wru.assert(
'number is squared',
twemoji.parse('I \u2764 emoji!', {size: 72}) ===
'I <img class="emoji" draggable="false" alt="\u2764" src="' + base + '72x72/2764.png"> emoji!'
);
wru.assert(
'string is preserved',
twemoji.parse('I \u2764 emoji!', {size: 'any-size'}) ===
'I <img class="emoji" draggable="false" alt="\u2764" src="' + base + 'any-size/2764.png"> emoji!'
);
}
},{
name: 'string parsing + callback',
test: function () {
var result = false;
twemoji.parse('I \u2764 emoji!', function (icon, options, variant) {
result = icon === '2764' && options.size === '36x36' && !variant;
});
wru.assert('works OK without variant', result);
result = false;
twemoji.parse('I \u2764\uFE0F emoji!', function (icon, options, variant) {
result = icon === '2764' && options.size === '36x36' && variant === '\uFE0F';
});
wru.assert('works OK with variant', result);
result = true;
twemoji.parse('I \u2764\uFE0E emoji!', function (icon, options, variant) {
result = false;
});
wru.assert('not invoked when \uFE0E is matched', result);
}
},{
name: 'string parsing + callback returning `falsy`',
test: function () {
wru.assert(
'does not add an image',
'I \u2764\uFE0F emoji!' ===
twemoji.parse('I \u2764\uFE0F emoji!', function () {})
);
}
},{
name: 'string parsing + callback + size',
test: function () {
wru.assert(
'size is overwritten',
'I <img class="emoji" draggable="false" alt="\u2764" src="72x72/2764.png"> emoji!' ===
twemoji.parse(
'I \u2764 emoji!',
{
base: '',
size: 72
}
)
);
}
},{
name: 'twemoji.replace(str, callback)',
test: function () {
var result = false;
var str = twemoji.replace('I \u2764\uFE0E emoji!', function (match, emoji, variant) {
result = match === '\u2764\uFE0E' &&
emoji === '\u2764\uFE0E' &&
variant === '\uFE0E';
return '<3';
});
wru.assert('all exepected values are passed through', result);
wru.assert('returned value is the expected', str === 'I <3 emoji!');
}
},{
name: 'twemoji.test(str)',
test: function () {
wru.assert(
twemoji.test('I \u2764 emoji!') &&
twemoji.test('I \u2764\uFE0F emoji!') &&
twemoji.test('I \u2764\uFE0E emoji!') &&
!twemoji.test('nope')
);
}
},{
name: 'DOM parsing',
test: function () {
var img,
// without variant
div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764 emoji!'));
twemoji.parse(div);
wru.assert('default parsing works creating 3 nodes', div.childNodes.length === 3);
wru.assert('first child is the expected one', div.removeChild(div.firstChild).nodeValue === 'I ');
img = div.removeChild(div.firstChild);
wru.assert('second child is the image', img.nodeName === 'IMG');
wru.assert('img attributes are OK',
img.className === 'emoji' &&
img.getAttribute('draggable') === 'false' &&
img.src === base + '36x36/2764.png' &&
img.alt === '\u2764' &&
img.onerror === twemoji.onerror
);
wru.assert('last child is the expected one', div.removeChild(div.firstChild).nodeValue === ' emoji!');
// with "as image" variant
div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764\uFE0F emoji!'));
twemoji.parse(div);
wru.assert('default parsing created 3 nodes', div.childNodes.length === 3);
wru.assert('first child is the expected one', div.removeChild(div.firstChild).nodeValue === 'I ');
img = div.removeChild(div.firstChild);
wru.assert('second child is the image', img.nodeName === 'IMG');
wru.assert('img attributes are OK',
img.className === 'emoji' &&
img.getAttribute('draggable') === 'false' &&
img.src === base + '36x36/2764.png' &&
img.alt === '\u2764\uFE0F' &&
img.onerror === twemoji.onerror
);
wru.assert('last child is the expected one', div.removeChild(div.firstChild).nodeValue === ' emoji!');
// with "as text" variant
div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764\uFE0E emoji!'));
twemoji.parse(div);
wru.assert('default parsing did NOT create 3 nodes anyway', div.childNodes.length === 1);
}
},{
name: 'DOM parsing + size',
test: function () {
var img,
div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764 emoji!'));
twemoji.parse(div, {size: 16});
wru.assert('default parsing works creating 3 nodes', div.childNodes.length === 3);
wru.assert('first child is the expected one', div.removeChild(div.firstChild).nodeValue === 'I ');
img = div.removeChild(div.firstChild);
wru.assert('second child is the image', img.nodeName === 'IMG');
wru.assert('img attributes are OK',
img.className === 'emoji' &&
img.getAttribute('draggable') === 'false' &&
img.src === base + '16x16/2764.png' &&
img.alt === '\u2764' &&
img.onerror === twemoji.onerror
);
wru.assert('last child is the expected one', div.removeChild(div.firstChild).nodeValue === ' emoji!');
}
},{
name: 'DOM parsing + callback',
test: function () {
var result = false,
div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764 emoji!'));
twemoji.parse(div, function (icon, options, variant) {
result = icon === '2764' && options.size === '36x36' && !variant;
});
wru.assert('works OK without variant', result);
result = false;
div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764\uFE0F emoji!'));
twemoji.parse(div, function (icon, options, variant) {
result = icon === '2764' && options.size === '36x36' && variant === '\uFE0F';
});
wru.assert('works OK with variant', result);
result = true;
div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764\uFE0E emoji!'));
twemoji.parse(div, function (icon, options, variant) {
result = false;
});
wru.assert('not invoked when \uFE0E is matched', result);
}
},{
name: 'DOM parsing + callback returning `falsy`',
test: function () {
var div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764 emoji!'));
twemoji.parse(div, function () {});
wru.assert(div.innerHTML === 'I \u2764 emoji!');
}
},{
name: 'DOM parsing + callback + size',
test: function () {
var result = false,
div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764 emoji!'));
twemoji.parse(div, {
size: 16,
callback: function (icon, options, variant) {
result = icon === '2764' && options.size === '16x16' && !variant;
}
});
wru.assert('works OK without variant', result);
result = false;
div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764\uFE0F emoji!'));
twemoji.parse(div, {
size: 72,
callback: function (icon, options, variant) {
result = icon === '2764' && options.size === '72x72' && !!variant;
}
});
wru.assert('works OK with variant', result);
}
},{
name: 'nested nodes',
test: function () {
var str = '<img class="emoji" draggable="false" alt="\u2764" src="https://twemoji.maxcdn.com/36x36/2764.png">',
div = document.createElement('div'),
p,
img;
div.innerHTML = '<p>I \u2764 emoji<strong>!</strong></p><p>I \u2764 them too</p>';
p = div.getElementsByTagName('p');
twemoji.parse(div);
wru.assert('preserved structure', p.length === 2);
img = div.getElementsByTagName('img');
wru.assert('correct amount of images found', img.length === 2);
wru.assert('images are in the right place',
img[0].parentNode === p[0] &&
img[1].parentNode === p[1]
);
}
},{
name: 'only nodes are affected',
test: function () {
var div = document.createElement('div');
var innerHTML = '<script>/*\u2764*/</script><style>/*\u2764*/</style><hr class="\u2764">';
div.innerHTML = innerHTML;
twemoji.parse(div);
wru.assert(!/<img/i.test(div.innerHTML));
}
},{
name: 'DOM parsing multiple per node',
test: function () {
var div = document.createElement('div');
div.innerHTML = 'I \u2764\ufe0f emoji, you should \u2764 emoji too!';
twemoji.parse(div);
wru.assert('default parsing works creating 5 nodes', div.childNodes.length === 5);
wru.assert('first child is the expected one', div.removeChild(div.firstChild).nodeValue === 'I ');
wru.assert('second child is the expected one', div.removeChild(div.firstChild).alt === '\u2764\ufe0f');
wru.assert('third child is the expected one', div.removeChild(div.firstChild).nodeValue === ' emoji, you should ');
wru.assert('fourth child is the expected one', div.removeChild(div.firstChild).alt === '\u2764');
wru.assert('fifth child is the expected one', div.removeChild(div.firstChild).nodeValue === ' emoji too!');
}
},{
name: 'DOM parsing does not create XSS',
test: function () {
var div = document.createElement('div'), text, html;
div.innerHTML = 'I \u2764\ufe0f emoji, you shuold <3 them too!';
text = div.childNodes[0].nodeValue.slice('I \u2764\ufe0f'.length);
html = div.innerHTML.replace('\u2764\ufe0f', '');
twemoji.parse(div);
wru.assert('third child is the expected one', div.childNodes[2].nodeValue === text);
wru.assert('html unaltered', div.innerHTML.replace(/<img[^>]+?>/i, '') === html);
}
},{
name: 'string parsing + className',
test: function () {
var className = 'img-' + Math.random();
var img = 'I <img class="' + className + '" draggable="false" alt="\u2764" src="36x36/2764.png"> emoji!';
wru.assert(
'className is overwritten',
img ===
twemoji.parse(
'I \u2764 emoji!',
{
className: className,
base: ''
}
)
);
}
},{
name: 'DOM parsing + className',
test: function () {
var className = 'img-' + Math.random();
var img,
div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764 emoji!'));
twemoji.parse(div, {className: className});
wru.assert(
'className is overwritten',
div.getElementsByTagName('img')[0].className === className
);
}
},{
name: 'string parsing + attributes callback',
test: function () {
wru.assert(
'custom attributes are inserted',
'I <img class="emoji" draggable="false" alt="\u2764" src="' + base + '36x36/2764.png" title="Emoji: \u2764" data-test="We all <3 emoji"> emoji!' ===
twemoji.parse(
'I \u2764 emoji!',
{
attributes: function(icon) {
return {
title: 'Emoji: ' + icon,
'data-test': 'We all <3 emoji'
};
}
}
)
);
}
},{
name: 'string parsing + attributes callback content properly encoded',
test: function () {
wru.assert(
'custom attributes are inserted',
'I <img class="emoji" draggable="false" alt="\u2764" src="' + base + '36x36/2764.png" title="&amp;lt;script&amp;gt;alert("yo")&amp;lt;/script&amp;gt;"> emoji!' ===
twemoji.parse(
'I \u2764 emoji!',
{
attributes: function(icon) {
return {
title: '&lt;script&gt;alert("yo")&lt;/script&gt;'
};
}
}
)
);
}
},{
name: 'string parsing + attributes callback "on" attributes are omitted',
test: function () {
wru.assert(
'custom attributes are inserted',
'I <img class="emoji" draggable="false" alt="❤" src="' + base + '36x36/2764.png" title="test"> emoji!' ===
twemoji.parse(
'I \u2764 emoji!',
{
attributes: function(icon) {
return {
title: 'test',
onsomething: 'whoops!',
onclick: 'nope',
onmousedown: 'nada'
};
}
}
)
);
}
},{
name: 'DOM parsing + attributes callback',
test: function () {
var img,
// without variant
div = document.createElement('div');
div.appendChild(document.createTextNode('I \u2764 emoji!'));
twemoji.parse(
div, {
attributes: function(icon) {
return {
title: 'Emoji: ' + icon,
'data-test': 'We all <3 emoji',
onclick: 'nope',
onmousedown: 'nada'
};
}
}
);
wru.assert('default parsing works creating 3 nodes', div.childNodes.length === 3);
wru.assert('first child is the expected one', div.removeChild(div.firstChild).nodeValue === 'I ');
img = div.removeChild(div.firstChild);
wru.assert('second child is the image', img.nodeName === 'IMG');
wru.assert('img attributes are OK',
img.className === 'emoji' &&
img.getAttribute('draggable') === 'false' &&
img.src === base + '36x36/2764.png' &&
img.alt === '\u2764' &&
img.onerror === twemoji.onerror &&
img.getAttribute('title') === 'Emoji: \u2764' &&
img.getAttribute('data-test') === 'We all <3 emoji'
);
wru.assert('img on attributes are omitted',
img.onclick === null &&
img.onmousedown === null
);
}
},{
name: 'folder option',
test: function () {
var img = 'I <img class="emoji" draggable="false" alt="\u2764" src="svg/2764.svg"> emoji!';
wru.assert(
'folder is accepted',
img ===
twemoji.parse(
'I \u2764 emoji!',
{
folder: 'svg',
ext: '.svg',
base: ''
}
)
);
wru.assert(
'folder overwrites size',
img ===
twemoji.parse(
'I \u2764 emoji!',
{
size: 72,
folder: 'svg',
ext: '.svg',
base: ''
}
)
);
}
},{
name: 'non standard OSX variant',
test: function () {
var div = document.createElement('div');
div.innerHTML = '5\ufe0f\u20e3';
twemoji.parse(div);
wru.assert('recognized as graphical',
div.firstChild.className === 'emoji' &&
div.firstChild.getAttribute('draggable') === 'false' &&
div.firstChild.getAttribute('alt') === "5️⃣" &&
div.firstChild.src === 'http://twemoji.maxcdn.com/36x36/35-20e3.png'
);
wru.assert('the length is preserved',
div.getElementsByTagName('img')[0].alt.length === 3);
}
},{
name: 'same but standard OSX without variant',
test: function () {
var div = document.createElement('div');
div.innerHTML = '5\u20e3';
twemoji.parse(div);
wru.assert('recognized as graphical',
div.firstChild.className === 'emoji' &&
div.firstChild.getAttribute('draggable') === 'false' &&
div.firstChild.getAttribute('alt') === "5⃣" &&
div.firstChild.src === 'http://twemoji.maxcdn.com/36x36/35-20e3.png'
);
wru.assert('the length is preserved',
div.getElementsByTagName('img')[0].alt.length === 2);
}
},{
name: 'multiple parsing using a callback',
test: function () {
wru.assert(
'FE0E is still ignored',
twemoji.parse('\u25c0 \u25c0\ufe0e \u25c0\ufe0f', {
callback: function(icon){ return 'icon'; }
}) ===
'<img class="emoji" draggable="false" alt="\u25c0" src="icon"> \u25c0\ufe0e <img class="emoji" draggable="false" alt="\u25c0\ufe0f" src="icon">'
);
}
},{
name: 'non standard variant within others',
test: function () {
var a = [
'normal',
'forced-as-text',
'forced-as-apple-graphic',
'forced-as-graphic'
];
wru.assert('normal forced-as-text forced-as-apple-graphic forced-as-graphic' ===
twemoji.replace('\u25c0 \u25c0\ufe0e 5\ufe0f\u20e3 \u25c0\ufe0f', function(match, icon, variant){
if (variant === '\uFE0E') return a[1];
if (variant === '\uFE0F') return a[3];
if (!variant) return a[
icon.length === 3 && icon.charAt(1) === '\uFE0F' ? 2 : 0
];
})
);
}
},{
name: 'invalid variants and chars',
test: function () {
var div = document.createElement('div');
var img;
div.innerHTML = twemoji.parse('"\u2b1c\uFE0F"');
img = div.getElementsByTagName('img')[0];
wru.assert('correct img.alt 1', img.alt === "\u2b1c\uFE0F");
wru.assert('correct img.src 1', img.src.slice(-8) === '2b1c.png');
// other variants should be ignored
div.innerHTML = twemoji.parse('"\u2b1c\uFE00"');
img = div.getElementsByTagName('img')[0];
wru.assert('correct img.alt 2', img.alt === "\u2b1c");
wru.assert('correct img.src 2', img.src.slice(-8) === '2b1c.png');
div.removeChild(img);
// the variant without meanings are still there
div.innerHTML === '"\uFE00"';
// when there is a trailing \uFE0E there should be no image
div.innerHTML = twemoji.parse('"\u2b1c\uFE0E"');
wru.assert('correct length', div.getElementsByTagName('img').length === 0);
wru.assert('expected html', div.innerHTML === '"\u2b1c\uFE0E"');
}
}, {
name: 'SVG Elements are ignored',
test: function () {
if (typeof SVGElement !== 'undefined') {
var innerHTML, div = document.createElement('div');
div.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="40" height="40">' +
'<switch>' +
'<circle cx="20" cy="20" r="18" stroke="grey" stroke-width="2" fill="#99FF66" />' +
'<foreignObject>' +
'<div>I \u2764 emoji!</div>' +
'</foreignObject>' +
'</switch>' +
'</svg>';
// grab the normalized one
innerHTML = div.innerHTML;
twemoji.parse(div);
wru.assert('nothing changed', innerHTML === div.innerHTML);
} else {
wru.assert('nothing to do here');
}
}
}, {
name: 'using a different onerror',
test: function () {
var Image = window.Image;
window.Image = function () {
var self = new Image;
setTimeout(function () {
window.Image = Image;
self.onerror();
}, 10);
return self;
};
var div = document.createElement('div');
div.innerHTML = '5\ufe0f\u20e3';
twemoji.parse(div, {onerror: wru.async(function () {
wru.assert('OK');
})});
}
}]);