This repository was archived by the owner on May 29, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 212
/
Copy pathwebfont_test.js
983 lines (797 loc) · 29.6 KB
/
webfont_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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
/*jshint node:true*/
'use strict';
var fs = require('fs');
var path = require('path');
var grunt = require('grunt');
var parseXMLString = require('xml2js').parseString;
var wf = require('../tasks/util/util');
var crypto = require('crypto');
var Promise = require('promise');
function find(haystack, needle) {
return haystack.indexOf(needle) !== -1;
}
function findDuplicates(haystack, needles) {
var sorted_arr = haystack.sort();
var results = [];
for (var i = 0; i < haystack.length - 1; i++) {
if (sorted_arr[i + 1] === sorted_arr[i]) {
results.push(sorted_arr[i]);
}
}
return results;
}
exports.webfont = {
test1: function(test) {
// All out files should be created and should not be empty
'woff,ttf,eot'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/test1/icons.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/test1/icons.' + type).length, name + ' file not empty.');
});
'css,html'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/test1/icons.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/test1/icons.' + type).length, name + ' file not empty.');
});
var svgs = grunt.file.expand('test/src/**.*');
var css = grunt.file.read('test/tmp/test1/icons.css');
var html = grunt.file.read('test/tmp/test1/icons.html');
// CSS links to font files are correct
'woff,ttf,eot'.split(',').forEach(function(type) {
test.ok(
find(css, 'url("icons.' + type),
'File path ' + type + ' should be in CSS file.'
);
});
// Double EOT (for IE9 compat mode)
test.ok(
find(css, 'src:url("icons.eot");'),
'First EOT declaration.'
);
test.ok(
find(css, 'src:url("icons.eot?#iefix") format("embedded-opentype"),'),
'Second EOT declaration.'
);
// Every SVG file should have corresponding entry in CSS and HTML files
svgs.forEach(function(file, index) {
var id = path.basename(file, '.svg');
test.ok(
find(css, '.icon_' + id + ':before'),
'Icon ' + id + ' should be in CSS file.'
);
test.ok(
find(css, 'content:"\\' + (wf.UNICODE_PUA_START + index).toString(16) + '"'),
'Character at index ' + index + ' has its codepoint in the CSS'
);
test.ok(
find(html, '<div class="icons__item" data-name="' + id + '"><i class="icon icon_' + id + '"></i> icon_' + id + '</div>'),
'Icon ' + id + ' should be in HTML file.'
);
});
test.done();
},
test2: function(test) {
var css = grunt.file.read('test/tmp/test2/myfont.css');
// Read hash
var hash = css.match(/url\("fonts\/myfont\.woff\?([0-9a-f]{32})"\)/);
hash = hash && hash[1];
test.ok(hash, 'Hash calculated.');
// All out files should be created and should not be empty
'woff,svg'.split(',').forEach(function(type) {
var name = type.toUpperCase(),
prefix = 'test/tmp/test2/fonts/myfont.';
test.ok(fs.existsSync(prefix + type), name + ' file created.');
test.ok(grunt.file.read(prefix + type).length, name + ' file not empty.');
});
'css,html'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/test2/myfont.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/test2/myfont.' + type).length, name + ' file not empty.');
});
// Excluded file types should not be created
'eot,ttf'.split(',').forEach(function(type) {
var name = type.toUpperCase(),
prefix = 'test/tmp/test2/fonts/myfont.';
test.ok(!fs.existsSync(prefix + type), name + ' file NOT created.');
});
var svgs = grunt.file.expand('test/src/**.*');
var html = grunt.file.read('test/tmp/test2/myfont.html');
// CSS links to font files are correct
'woff,svg'.split(',').forEach(function(type) {
test.ok(
find(css, 'url("fonts/myfont.' + type + '?' + hash),
'File path ' + type + ' should be in CSS file.'
);
});
// CSS links to excluded formats should not be included
'ttf,eot'.split(',').forEach(function(type) {
test.ok(
!find(css, 'fonts/myfont.' + type),
'File path ' + type + ' should be in CSS file.'
);
});
// Every SVG file should have corresponding entry in CSS and HTML files
svgs.forEach(function(file) {
var id = path.basename(file, '.svg');
test.ok(
find(css, '.icon-' + id + ':before'),
'Icon ' + id + ' should be in CSS file.'
);
test.ok(
find(html, '<div class="icons__item" data-name="' + id + '"><i class=" icon-' + id + '"></i> icon-' + id + '</div>'),
'Icon ' + id + ' should be in HTML file.'
);
});
test.done();
},
embed: function(test) {
// All out files should be created and should not be empty
'ttf,eot'.split(',').forEach(function(type) {
var name = type.toUpperCase(),
prefix = 'test/tmp/embed/icons.';
test.ok(fs.existsSync(prefix + type), name + ' file created.');
test.ok(grunt.file.read(prefix + type).length, name + ' file not empty.');
});
// WOFF should be deleted
'woff'.split(',').forEach(function(type) {
var name = type.toUpperCase(),
prefix = 'test/tmp/embed/icons.';
test.ok(!fs.existsSync(prefix + type), name + ' file NOT created.');
});
var css = grunt.file.read('test/tmp/embed/icons.css');
// Data:uri
var m = css.match(/data:application\/x-font-woff;charset=utf-8;base64,.*?format\("woff"\)/g);
test.equal(m && m.length, 1, 'WOFF (default) data:uri');
test.done();
},
embed_woff: function(test) {
// Excluded file types should not be created + WOFF should be deleted
'woff,ttf,eot'.split(',').forEach(function(type) {
var name = type.toUpperCase(),
prefix = 'test/tmp/embed_woff/icons.';
test.ok(!fs.existsSync(prefix + type), name + ' file NOT created.');
});
var css = grunt.file.read('test/tmp/embed_woff/icons.css');
var m;
// Data:uri
m = css.match(/data:application\/x-font-woff;charset=utf-8;base64,.*?format\("woff"\)/g);
test.equal(m && m.length, 1, 'Data:uri');
test.done();
},
embed_ttf: function(test) {
// Excluded file types should not be created + TTF should be deleted
'woff,ttf,eot'.split(',').forEach(function(type) {
var name = type.toUpperCase(),
prefix = 'test/tmp/embed_ttf/icons.';
test.ok(!fs.existsSync(prefix + type), name + ' file NOT created.');
});
var css = grunt.file.read('test/tmp/embed_ttf/icons.css');
var m;
// Data:uri
m = css.match(/data:application\/x-font-ttf;charset=utf-8;base64,.*?format\("truetype"\)/g);
test.equal(m && m.length, 1, 'TrueType data:uri');
test.done();
},
embed_ttf_woff: function(test) {
// Excluded file types should not be created + TTF should be deleted
'woff,ttf,eot'.split(',').forEach(function(type) {
var name = type.toUpperCase(),
prefix = 'test/tmp/embed_ttf_woff/icons.';
test.ok(!fs.existsSync(prefix + type), name + ' file NOT created.');
});
var css = grunt.file.read('test/tmp/embed_ttf_woff/icons.css');
var m;
// Data:uri
m = css.match(/data:application\/x-font-ttf;charset=utf-8;base64,.*?format\("truetype"\)/g);
test.equal(m && m.length, 1, 'TrueType data:uri');
m = css.match(/data:application\/x-font-woff;charset=utf-8;base64,.*?format\("woff"\)/g);
test.equal(m && m.length, 1, 'WOFF data:uri');
test.done();
},
one: function(test) {
// All out files should be created and should not be empty
'woff,ttf,eot'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/one/icons.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/one/icons.' + type).length, name + ' file not empty.');
});
'css,html'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/one/icons.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/one/icons.' + type).length, name + ' file not empty.');
});
var svgs = grunt.file.expand('test/src_one/**.*'),
css = grunt.file.read('test/tmp/one/icons.css');
// CSS links to font files are correct
'woff,ttf,eot'.split(',').forEach(function(type) {
test.ok(
find(css, 'icons.' + type),
'File path ' + type + ' should be in CSS file.'
);
});
// Every SVG file should have corresponding entry in CSS file
svgs.forEach(function(file) {
var id = path.basename(file, '.svg');
test.ok(
find(css, '.icon_' + id + ':before'),
'Icon ' + id + ' should be in CSS file.'
);
});
test.done();
},
template: function(test) {
var css = grunt.file.read('test/tmp/template/icons.css');
// There should be comment from custom template
test.ok(
find(css, 'Custom template'),
'Comment from custom template.'
);
test.done();
},
template_scss: function(test) {
var cssFilename = 'test/tmp/template_scss/_icons.scss';
test.ok(fs.existsSync(cssFilename), 'SCSS template: .scss file created.');
var css = grunt.file.read(cssFilename);
// There should be comment from custom template
test.ok(
find(css, 'Custom template'),
'SCSS template: comment from custom template.'
);
test.done();
},
template_sass: function(test) {
var cssFilename = 'test/tmp/template_sass/_icons.sass';
test.ok(fs.existsSync(cssFilename), 'SASS template: .sass file created (stylesheet extension derived from template name).');
var css = grunt.file.read(cssFilename);
// There should be comment from custom template
test.ok(
find(css, 'Custom template'),
'SASS template: comment from custom template.'
);
test.done();
},
enabled_template_variables: function(test) {
var cssFilename = 'test/tmp/enabled_template_variables/_icons.scss';
var css = grunt.file.read(cssFilename);
// There should be a variable declaration for scss preprocessor
test.ok(
find(css, '$icons-font-path : "../iamrelative/" !default;'),
'SCSS enable template variables: variable exists.'
);
// The variable declaration should be used
test.ok(
find(css, 'url($icons-font-path + "'),
'SCSS enable template variables: variable used.'
);
test.done();
},
html_template: function(test) {
var demo = grunt.file.read('test/tmp/html_template/icons.html');
// There should be comment from custom template
test.ok(
find(demo, 'Custom template'),
'Comment from custom template.'
);
test.done();
},
html_filename: function(test) {
var htmlfile = 'test/tmp/html_filename/index.html';
// There should be comment from custom template
test.ok(fs.existsSync(htmlfile), 'HTML demo file custom name created.');
test.done();
},
relative_path: function(test) {
var css = grunt.file.read('test/tmp/relative_path/icons.css');
// CSS links to font files are correct
'woff,ttf,eot'.split(',').forEach(function(type) {
test.ok(
find(css, 'url("../iamrelative/icons.' + type),
'File path ' + type + ' should be in CSS file.'
);
});
test.done();
},
sass: function(test) {
test.ok(fs.existsSync('test/tmp/sass/_icons.sass'), 'SASS file with underscore created.');
test.ok(!fs.existsSync('test/tmp/sass/icons.sass'), 'SASS file without underscore not created.');
test.ok(!fs.existsSync('test/tmp/sass/icons.css'), 'CSS file not created.');
var svgs = grunt.file.expand('test/src/**.*');
var sass = grunt.file.read('test/tmp/sass/_icons.sass');
// There should be comment from custom template
var m = sass.match(/\/\* *(.*?) *\*\//g);
test.ok(!m, 'No regular CSS comments.');
// There should be comment from custom template
m = sass.match(/^\/\//gm);
test.equal(m && m.length, 2, 'Single line comments.');
test.done();
},
less: function(test) {
test.ok(fs.existsSync('test/tmp/less/icons.less'), 'LESS file created.');
test.ok(!fs.existsSync('test/tmp/less/icons.css'), 'CSS file not created.');
var svgs = grunt.file.expand('test/src/**.*');
var less = grunt.file.read('test/tmp/less/icons.less');
// There should be comment from custom template
var m = less.match(/\/\* *(.*?) *\*\//g);
test.ok(!m, 'No regular CSS comments.');
// There should be comment from custom template
m = less.match(/^\/\//gm);
test.equal(m && m.length, 2, 'Single line comments.');
// Every SVG file should have two corresponding entries in CSS file
svgs.forEach(function(file) {
var id = path.basename(file, '.svg');
test.ok(
find(less, '.icon_' + id + ' {\n\t&:before'),
'LESS Mixin ' + id + ' should be in CSS file.'
);
});
test.done();
},
css_plus_scss: function(test) {
test.ok(fs.existsSync('test/tmp/scss/_icons.scss'), 'SCSS file with underscore created.');
test.ok(!fs.existsSync('test/tmp/scss/icons.scss'), 'SCSS file without underscore not created.');
test.ok(fs.existsSync('test/tmp/css/icons.css'), 'CSS file is created.');
test.done();
},
stylus_bem: function(test) {
test.ok(fs.existsSync('test/tmp/stylus_bem/icons.styl'), 'Stylus file created.');
test.ok(!fs.existsSync('test/tmp/stylus_bem/icons.css'), 'CSS file not created.');
var styl = grunt.file.read('test/tmp/stylus_bem/icons.styl');
// There should be comment from custom template
var m = styl.match(/\/\* *(.*?) *\*\//g);
test.ok(!m, 'No regular CSS comments.');
// There should be comment from custom template
m = styl.match(/^\/\//gm);
test.equal(m && m.length, 2, 'Single line comments.');
var stylus = require('stylus');
var s = stylus(styl);
s.render(function(err, css) {
if (err) {
console.log('Stylus compile error:');
console.log(err);
}
test.ok(!err, 'Stylus file compiled.');
test.done();
});
},
stylus_bootstrap: function(test) {
test.ok(fs.existsSync('test/tmp/stylus_bootstrap/icons.styl'), 'Stylus file created.');
test.ok(!fs.existsSync('test/tmp/stylus_bootstrap/icons.css'), 'CSS file not created.');
var styl = grunt.file.read('test/tmp/stylus_bootstrap/icons.styl');
var stylus = require('stylus');
var s = stylus(styl);
s.render(function(err, css) {
if (err) {
console.log('Stylus compile error:');
console.log(err);
}
test.ok(!err, 'Stylus file compiled.');
test.done();
});
},
spaces: function(test) {
var css = grunt.file.read('test/tmp/spaces/icons.css');
test.ok(
find(css, '.icon_ma-il-ru:before'),
'Spaces in class name should be replaced by hyphens.'
);
test.ok(
find(css, 'content:"\\' + wf.UNICODE_PUA_START.toString(16) + '";'),
'Right codepoint should exists.'
);
test.done();
},
disable_demo: function(test) {
test.ok(fs.existsSync('test/tmp/disable_demo/icons.css'), 'CSS file created.');
test.ok(!fs.existsSync('test/tmp/disable_demo/icons.html'), 'HTML file not created.');
test.done();
},
non_css_demo: function(test) {
test.ok(!fs.existsSync('test/tmp/non_css_demo/icons.css'), 'CSS file not created.');
test.ok(fs.existsSync('test/tmp/non_css_demo/icons.html'), 'HTML file created.');
var html = grunt.file.read('test/tmp/non_css_demo/icons.html');
test.ok(
find(html, '@font-face {'),
'Font-face declaration exists in HTML.'
);
test.ok(
find(html, '.icon {'),
'Base icon exists in HTML.'
);
test.ok(
!find(html, 'url("../iamrelative/icons-'),
'Relative paths should not be in HTML.'
);
test.ok(
!find(html, '&:before'),
'LESS mixins should not be in HTML.'
);
// Every SVG file should have corresponding entry in <style> block
var svgs = grunt.file.expand('test/src/**.*');
svgs.forEach(function(file) {
var id = path.basename(file, '.svg');
test.ok(
find(html, '.icon_' + id + ':before'),
'Icon ' + id + ' CSS should be in HTML file.'
);
});
test.done();
},
parent_source: function(test) {
var svgs = grunt.file.expand('test/src/**.*');
var css = grunt.file.read('test/tmp/parent_source/icons.css');
// Every SVG file should have corresponding entry in CSS file
svgs.forEach(function(file) {
var id = path.basename(file, '.svg');
test.ok(
find(css, '.icon_' + id + ':before'),
'Icon ' + id + ' should be in CSS file.'
);
});
test.done();
},
ligatures: function(test) {
var svgs = grunt.file.expand('test/ligatures_src/**.*');
var css = grunt.file.read('test/tmp/ligatures/icons.css');
// Every SVG file should have corresponding entry in CSS file
svgs.forEach(function(file) {
var name = path.basename(file, '.svg');
test.ok(
find(css, 'content:"'+name+'";'),
'Icon ' + name + ' should be in CSS file.'
);
});
test.done();
},
duplicate_names: function(test) {
var svgs = grunt.file.expand('test/src_duplicate_names/**/*.svg');
var css = grunt.file.read('test/tmp/duplicate_names/icons.css');
// Every SVG file should have corresponding entry in CSS file
svgs.forEach(function(file) {
var id = [path.basename(path.dirname(file)), path.basename(file, '.svg')].join('-');
test.ok(
find(css, '.icon_' + id + ':before'),
'Icon ' + id + ' should be in CSS file.'
);
});
test.done();
},
order: function(test) {
var svgs = grunt.file.expand('test/src/**.*');
var css = grunt.file.read('test/tmp/order/icons.css');
// Font-face src rules should be in right order
test.ok(
find(css, 'src:url("icons.svg#icons") format("svg"),\n\t\turl("icons.woff") format("woff");'),
'Font-face src rules should be in right order.'
);
test.done();
},
template_options: function(test) {
var svgs = grunt.file.expand('test/src/**.*');
var less = grunt.file.read('test/tmp/template_options/icons.less');
var html = grunt.file.read('test/tmp/template_options/icons.html');
test.ok(
find(less, '.glyph-icon {'),
'Class .glyph-icon should be in LESS file.'
);
// Every SVG file should have corresponding entry in LESS and HTML files
svgs.forEach(function(file) {
var id = path.basename(file, '.svg');
// test.ok(
// find(less, '.make-icon-' + id + ' {'),
// 'Mixin .make-icon-' + id + ' should be in LESS file.'
// );
test.ok(
find(less, '.glyph_' + id + ' {'),
'Icon .glyph_' + id + ' should be in LESS file.'
);
test.ok(
find(html, '<div class="icons__item" data-name="' + id + '"><i class="glyph-icon glyph_' + id + '"></i> glyph_' + id + '</div>'),
'Icon .glyph_' + id + ' should be in HTML file.'
);
});
test.done();
},
node: function(test) {
// All out files should be created and should not be empty
'woff,ttf,eot'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/node/icons.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/node/icons.' + type).length, name + ' file not empty.');
});
'css,html'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/node/icons.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/node/icons.' + type).length, name + ' file not empty.');
});
var svgs = grunt.file.expand('test/src/**.*');
var css = grunt.file.read('test/tmp/node/icons.css');
var html = grunt.file.read('test/tmp/node/icons.html');
// CSS links to font files are correct
'woff,ttf,eot'.split(',').forEach(function(type) {
test.ok(
find(css, 'url("icons.' + type),
'File path ' + type + ' shound be in CSS file.'
);
});
// Double EOT (for IE9 compat mode)
test.ok(
find(css, 'src:url("icons.eot");'),
'First EOT declaration.'
);
test.ok(
find(css, 'src:url("icons.eot?#iefix") format("embedded-opentype"),'),
'Second EOT declaration.'
);
// Every SVG file should have corresponding entry in CSS and HTML files
svgs.forEach(function(file) {
var id = path.basename(file, '.svg');
test.ok(
find(css, '.icon_' + id + ':before'),
'Icon ' + id + ' shound be in CSS file.'
);
test.ok(
find(html, '<div class="icons__item" data-name="' + id + '"><i class="icon icon_' + id + '"></i> icon_' + id + '</div>'),
'Icon ' + id + ' shound be in HTML file.'
);
});
test.done();
},
ie7: function(test){
var css = grunt.file.read('test/tmp/ie7/icons.css');
var svgs = grunt.file.expand('test/src/*.svg');
test.ok(find(css, '*zoom'), '*zoom property should be set');
test.ok(find(css, '&#x') , 'HTML char are present');
svgs.forEach(function(file){
var id = path.basename(file, '.svg');
test.ok(
find(css, '.icon_' + id + ' {'),
'Icon ' + id + ' shound be in CSS file.'
);
});
test.done();
},
ie7_bootstrap: function(test){
var css = grunt.file.read('test/tmp/ie7_bootstrap/icons.css');
var svgs = grunt.file.expand('test/src/*.svg');
test.ok(find(css, '*zoom'), '*zoom property should be set');
test.ok(find(css, '&#x') , 'HTML char are present');
svgs.forEach(function(file){
var id = path.basename(file, '.svg');
test.ok(
find(css, '.icon-' + id + ' {'),
'Icon ' + id + ' shound be in CSS file.'
);
});
test.done();
},
optimize_enabled: function(test){
var optimizedPathSegment = '280.2V280.098C349.867 293.072 358.595';
var svg = grunt.file.read('test/tmp/optimize_enabled/icons.svg');
if(svg.indexOf(optimizedPathSegment) === -1) {
test.fail(true, 'SVG element must be contains the optimized path');
}
test.done();
},
optimize_disabled: function(test){
var optimizedPathSegment = '280.2V280.098C349.867 293.072 358.595';
var svg = grunt.file.read('test/tmp/optimize_disabled/icons.svg');
if(svg.indexOf(optimizedPathSegment) > -1) {
test.fail(true, 'SVG element must be contains the un-optimized path');
}
test.done();
},
codepoints: function(test) {
// Default codepoint of 0xE001 can be overidden
var resultSVG = grunt.file.expand('test/tmp/codepoints/icons.svg');
var css = grunt.file.read('test/tmp/codepoints/icons.css');
var html = grunt.file.read('test/tmp/codepoints/icons.html');
var startCodepoint = 0x41;
// Generated SVG font should have glyphs at the overidden codepoints
resultSVG.forEach(function(file) {
var svgSource = grunt.file.read(file);
var glyphs = [];
parseXMLString(svgSource, function(err, result) {
// Normalise glyphs into JS objects
result.svg.defs[0].font[0].glyph.forEach(function(glyph) {
if (glyph.$['glyph-name'].length === 1) { // Skip non-characters (.notdef, .null, etc.)
glyphs.push(glyph.$);
}
});
// Two assertions for each glyph:
// - each glyph has a unique unicode character
// - the correct glyph character code is present in the generated CSS
var unicodeCharArr = glyphs.map(function(g) { return g.unicode; });
for (var index = 0; index < glyphs.length; index ++) {
test.equals(0, findDuplicates(unicodeCharArr).length);
test.ok(
find(css, 'content:"\\' + (startCodepoint + index).toString(16) + '"'),
'Character at index ' + index + ' has its codepoint in the CSS'
);
}
});
});
test.done();
},
camel: function(test) {
var svgs = grunt.file.expand('test/camel/*.svg');
var css = grunt.file.read('test/tmp/camel/icons.css');
// Every SVG file should have corresponding entries in CSS file in the same case
svgs.forEach(function(file) {
var id = path.basename(file, '.svg');
test.ok(
find(css, '.icon_' + id + ':before'),
'Icon ' + id + ' should be in CSS file.'
);
});
test.done();
},
folders: function(test) {
// @todo Temporarily disabled because different fontforge versions produce different paths
test.done();
if (true) return;
var svgFont = grunt.file.read('test/tmp/folders/icons.svg');
var paths = JSON.parse(grunt.file.read('test/src_folders/paths.json'));
var glyphs = [];
parseXMLString(svgFont, function(err, result) {
// Normalise glyphs into JS objects
result.svg.defs[0].font[0].glyph.forEach(function(glyph) {
if (/^uni/.test(glyph.$['glyph-name'])) { // Skip non-characters (.notdef, .null, etc.)
glyphs.push(glyph.$);
}
});
glyphs.forEach(function(glyph) {
test.equals(glyph.d, paths[glyph['glyph-name']], 'Glyph with codepoint ' + glyph.unicode + ' has correct path.');
});
});
test.done();
},
woff2: function(test) {
// All out files should be created and should not be empty
'woff,woff2'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/woff2/icons.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/woff2/icons.' + type).length, name + ' file not empty.');
});
// TTF file should be deleted
'ttf'.split(',').forEach(function(type) {
test.ok(!fs.existsSync('test/tmp/woff2/icons.' + type), type.toUpperCase() + ' file NOT created.');
});
'css,html'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/woff2/icons.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/woff2/icons.' + type).length, name + ' file not empty.');
});
var svgs = grunt.file.expand('test/src/**.*');
var css = grunt.file.read('test/tmp/woff2/icons.css');
var html = grunt.file.read('test/tmp/woff2/icons.html');
// CSS links to font files are correct
'woff2,woff'.split(',').forEach(function(type) {
test.ok(
find(css, 'url("icons.' + type),
'File path ' + type + ' shound be in CSS file.'
);
});
// CSS links to TTF should not be created
'ttf'.split(',').forEach(function(type) {
test.ok(
!find(css, 'url("icons.' + type),
'File path ' + type + ' shound NOT be in CSS file.'
);
});
test.done();
},
woff2_node: function(test) {
// All out files should be created and should not be empty
'woff,woff2'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/woff2_node/icons.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/woff2_node/icons.' + type).length, name + ' file not empty.');
});
// TTF file should be deleted
'ttf'.split(',').forEach(function(type) {
test.ok(!fs.existsSync('test/tmp/woff2_node/icons.' + type), type.toUpperCase() + ' file NOT created.');
});
'css,html'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/woff2_node/icons.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/woff2_node/icons.' + type).length, name + ' file not empty.');
});
var svgs = grunt.file.expand('test/src/**.*');
var css = grunt.file.read('test/tmp/woff2_node/icons.css');
var html = grunt.file.read('test/tmp/woff2_node/icons.html');
// CSS links to font files are correct
'woff2,woff'.split(',').forEach(function(type) {
test.ok(
find(css, 'url("icons.' + type),
'File path ' + type + ' shound be in CSS file.'
);
});
// CSS links to TTF should not be created
'ttf'.split(',').forEach(function(type) {
test.ok(
!find(css, 'url("icons.' + type),
'File path ' + type + ' shound NOT be in CSS file.'
);
});
test.done();
},
target_overrides: function(test) {
var css = grunt.file.read('test/tmp/target_overrides_css/icons.css');
test.ok(fs.existsSync('test/tmp/target_overrides_css/icons.css') + ' file created.');
'woff,ttf,eot'.split(',').forEach(function(type) {
var name = type.toUpperCase();
test.ok(fs.existsSync('test/tmp/target_overrides_icons/icons.' + type), name + ' file created.');
test.ok(grunt.file.read('test/tmp/target_overrides_icons/icons.' + type).length, name + ' file not empty.');
});
test.done();
},
font_family_name: function(test) {
var html = grunt.file.read('test/tmp/font_family_name/icons.html');
// fontFamilyName should be in the HTML file's title
test.ok(
find(html, '<title>customName</title>'),
'fontFamilyName should be in the HTML file title'
);
// File should still have default name if only fontFamilyName is specified
test.ok(fs.existsSync('test/tmp/font_family_name/icons.ttf'));
test.done();
},
custom_outputs: function(test) {
// File should have been created when filename is specified
test.ok(fs.existsSync('test/tmp/custom_output/test-icon-config.js'));
// File should have been created (with template basename) when filename is not specified
test.ok(fs.existsSync('test/tmp/custom_output/custom.json'));
// Files should render with custom context variables
test.ok(fs.existsSync('test/tmp/custom_output/context-test.html'));
test.done();
},
filename_length: function(test) {
// File should have been created.
test.ok(fs.existsSync('test/tmp/filename_length/icons.css'));
// File should have been created.
test.ok(fs.existsSync('test/tmp/filename_length/icons.woff'));
// File should have been created.
test.ok(fs.existsSync('test/tmp/filename_length/icons.html'));
test.done();
},
// If font created multiple times with same value as timestamp option
// then md5 of created files must be the same too
timestamp_same: function (test) {
var promise_1 = new Promise(function(resolve, reject) {
fs.readFile('test/tmp/timestamp_same/same_1.ttf', function (err, data) {
resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex'));
});
});
var promise_2 = new Promise(function(resolve, reject) {
fs.readFile('test/tmp/timestamp_same/same_2.ttf', function (err, data) {
resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex'));
});
});
Promise.all([promise_1, promise_2])
.then(function(data) {
// md5 must be the same
test.ok(data[0] === data[1]);
test.done();
});
},
// If font created multiple times with different value as timestamp option
// then md5 of created files must be different
timestamp_different: function (test) {
var promise_1 = new Promise(function(resolve, reject) {
fs.readFile('test/tmp/timestamp_different/different_1.ttf', function (err, data) {
resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex'));
});
});
var promise_2 = new Promise(function(resolve, reject) {
fs.readFile('test/tmp/timestamp_different/different_2.ttf', function (err, data) {
resolve(crypto.createHash('md5').update(data, 'utf8').digest('hex'));
});
});
Promise.all([promise_1, promise_2])
.then(function(data) {
// md5 must be the different
test.ok(data[0] !== data[1]);
test.done();
});
},
};