-
Notifications
You must be signed in to change notification settings - Fork 892
Expand file tree
/
Copy pathvectorizer.js
More file actions
1806 lines (1441 loc) · 72.6 KB
/
Copy pathvectorizer.js
File metadata and controls
1806 lines (1441 loc) · 72.6 KB
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
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
'use strict';
QUnit.module('vectorizer', function(hooks) {
var fixture = document.createElement('div');
fixture.id = 'qunit-fixture';
var svgContainer;
var svgPath;
var svgGroup;
var svgCircle;
var svgEllipse;
var svgPolygon;
var svgText;
var svgRectangle;
var svgGroup1;
var svgGroup2;
var svgGroup3;
var svgPath2;
var svgPath3;
var svgLinearGradient;
var childrenTagNames = function(vel) {
var tagNames = [];
Array.prototype.slice.call(vel.node.childNodes).forEach(function(childNode) {
tagNames.push(childNode.tagName.toLowerCase());
});
return tagNames;
};
hooks.beforeEach(function() {
var svgContent = '<path id="svg-path" d="M10 10"/>' +
'<!-- comment -->' +
'<g id="svg-group">' +
'<ellipse id="svg-ellipse" x="10" y="10" rx="30" ry="30"/>' +
'<circle id="svg-circle" cx="10" cy="10" r="2" fill="red"/>' +
'</g>' +
'<polygon id="svg-polygon" points="200,10 250,190 160,210"/>' +
'<text id="svg-text" x="0" y="15" fill="red">Test</text>' +
'<rect id="svg-rectangle" x="100" y="100" width="50" height="100"/>' +
'<g id="svg-group-1" class="group-1">' +
'<g id="svg-group-2" class="group-2">' +
'<g id="svg-group-3" class="group3">' +
'<path id="svg-path-2" d="M 100 100 C 100 100 0 150 100 200 Z"/>' +
'</g>' +
'</g>' +
'</g>' +
'<path id="svg-path-3"/>' +
'<linearGradient id= "svg-linear-gradient"><stop/></linearGradient>';
document.body.appendChild(fixture);
fixture.appendChild(V('svg', { id: 'svg-container' }, V(svgContent)).node);
svgContainer = document.getElementById('svg-container');
svgPath = document.getElementById('svg-path');
svgGroup = document.getElementById('svg-group');
svgCircle = document.getElementById('svg-circle');
svgEllipse = document.getElementById('svg-ellipse');
svgPolygon = document.getElementById('svg-polygon');
svgText = document.getElementById('svg-text');
svgRectangle = document.getElementById('svg-rectangle');
svgGroup1 = document.getElementById('svg-group-1');
svgGroup2 = document.getElementById('svg-group-2');
svgGroup3 = document.getElementById('svg-group-3');
svgPath2 = document.getElementById('svg-path-2');
svgPath3 = document.getElementById('svg-path-3');
svgLinearGradient = document.getElementById('svg-linear-gradient');
});
function serializeNode(node) {
return (new XMLSerializer()).serializeToString(node);
}
QUnit.test('constructor', function(assert) {
var vRect = V('rect');
assert.ok(V.isVElement(vRect), 'Constructor produces a vectorizer element, when a string was provided.');
assert.ok(vRect.node instanceof SVGRectElement, 'The vectorizer element has the attribute "node" that references to an SVGElement.');
assert.ok(V.isVElement(V(vRect)), 'Constructor produces a vectorizer element, when a vectorizer element was provided.');
assert.ok(V(vRect).node instanceof SVGRectElement, 'The vectorizer element has again the attribute "node" that references to an SVGElement.');
var vRect2 = V(' rect ');
assert.ok(V.isVElement(vRect2));
assert.ok(vRect2.node instanceof SVGRectElement);
var vSVG = V('\n svg ');
assert.ok(V.isVElement(vSVG));
assert.ok(vSVG.node instanceof SVGSVGElement);
});
QUnit.test('id', function(assert) {
var vRect = V('rect');
assert.ok(vRect.id);
assert.equal(vRect.id, vRect.node.id);
vRect.id = 'newid';
assert.equal(vRect.node.id, 'newid');
});
QUnit.test('V(\'<invalid markup>\')', function(assert) {
var error;
try {
V('<invalid markup>');
} catch (e) {
error = e;
}
assert.ok(typeof error !== 'undefined', 'Should throw an error when given invalid markup.');
});
QUnit.test('V(\'<valid markup>\')', function(assert) {
var error;
try {
V('<rect width="100%" height="100%" fill="red" />');
} catch (e) {
error = e;
}
assert.ok(typeof error === 'undefined', 'Should not throw an error when given valid markup.');
});
QUnit.test('V.ensureId()', function(assert) {
var node = document.createElementNS('http://www.w3.org/2000/svg', 'g');
assert.notOk(node.id);
var id = V.ensureId(node);
assert.ok(id);
assert.equal(id, node.id);
assert.equal(id, V.ensureId(node));
assert.equal(id, node.id);
});
QUnit.test('V.isSVGGraphicsElement()', function(assert) {
assert.ok(V.isSVGGraphicsElement(svgCircle));
assert.ok(V.isSVGGraphicsElement(V('circle', { class: 'not-in-dom' })));
assert.ok(V.isSVGGraphicsElement(svgGroup));
assert.notOk(V.isSVGGraphicsElement());
assert.notOk(V.isSVGGraphicsElement(svgLinearGradient));
});
QUnit.test('V.attributeNames', function(assert) {
// kebab-case
assert.equal(V.attributeNames['stroke-width'], 'stroke-width');
assert.equal(V.attributeNames['strokeWidth'], 'stroke-width');
assert.equal(V.attributeNames['stroke'], 'stroke');
// camel-case
assert.equal(V.attributeNames['pathLength'], 'pathLength');
// custom
assert.equal(V.attributeNames['custom-attribute'], 'custom-attribute');
assert.equal(V.attributeNames['customAttribute'], 'custom-attribute');
const g1 = V('g').attr('customAttribute', 'value');
assert.equal(g1.attr('customAttribute'), 'value');
assert.equal(g1.node.getAttribute('custom-attribute'), 'value');
assert.equal(g1.node.getAttribute('customAttribute'), null);
// custom override
V.attributeNames['customAttribute'] = 'customAttribute';
assert.equal(V.attributeNames['custom-attribute'], 'custom-attribute');
assert.equal(V.attributeNames['customAttribute'], 'customAttribute');
const g2 = V('g').attr('customAttribute', 'value');
assert.equal(g2.attr('customAttribute'), 'value');
assert.equal(g2.node.getAttribute('customAttribute'), 'value');
assert.equal(g2.node.getAttribute('custom-attribute'), null);
});
QUnit.test('index()', function(assert) {
// svg container
assert.equal(V(svgContainer).index(), 0, 'SVG container contains 5 various nodes and 1 comment. Container itself has index 0.');
// nodes in an svg container
assert.equal(V(svgPath).index(), 0, 'The first node has index 0.');
assert.equal(V(svgGroup).index(), 1, 'The second node has index 1.');
assert.equal(V(svgPolygon).index(), 2, 'The third node has index 2.');
assert.equal(V(svgText).index(), 3, 'The fourth node has index 3.');
assert.equal(V(svgRectangle).index(), 4, 'The fifth node has index 4.');
// nodes in a group
assert.equal(V(svgEllipse).index(), 0, 'The first node in the group has index 0.');
assert.equal(V(svgCircle).index(), 1, 'The second node in the group has index 1.');
});
QUnit.module('tagName()', function() {
QUnit.test('sanity', function(assert) {
assert.equal(typeof V(svgContainer).tagName(), 'string');
assert.equal(typeof V(svgPath).tagName(), 'string');
assert.equal(typeof V(svgGroup).tagName(), 'string');
assert.equal(typeof V(svgCircle).tagName(), 'string');
assert.equal(typeof V(svgEllipse).tagName(), 'string');
assert.equal(typeof V(svgPolygon).tagName(), 'string');
assert.equal(typeof V(svgText).tagName(), 'string');
assert.equal(typeof V(svgRectangle).tagName(), 'string');
assert.equal(typeof V(svgGroup1).tagName(), 'string');
assert.equal(typeof V(svgGroup2).tagName(), 'string');
assert.equal(typeof V(svgGroup3).tagName(), 'string');
assert.equal(typeof V(svgPath2).tagName(), 'string');
assert.equal(typeof V(svgPath3).tagName(), 'string');
});
QUnit.test('correctness', function(assert) {
assert.equal(V(svgContainer).tagName(), 'SVG');
assert.equal(V(svgPath).tagName(), 'PATH');
assert.equal(V(svgGroup).tagName(), 'G');
assert.equal(V(svgCircle).tagName(), 'CIRCLE');
assert.equal(V(svgEllipse).tagName(), 'ELLIPSE');
assert.equal(V(svgPolygon).tagName(), 'POLYGON');
assert.equal(V(svgText).tagName(), 'TEXT');
assert.equal(V(svgRectangle).tagName(), 'RECT');
assert.equal(V(svgGroup1).tagName(), 'G');
assert.equal(V(svgGroup2).tagName(), 'G');
assert.equal(V(svgGroup3).tagName(), 'G');
assert.equal(V(svgPath2).tagName(), 'PATH');
assert.equal(V(svgPath3).tagName(), 'PATH');
});
});
QUnit.module('text', function() {
var getSvg = function() {
var svg = V('svg');
svg.attr('width', 600);
svg.attr('height', 800);
fixture.appendChild(svg.node);
return svg;
};
QUnit.test('single line, styles, position', function(assert) {
var svg = getSvg();
var t = V('text', { x: 250, dy: 100, fill: 'black' });
t.text('abc');
assert.equal(t.node.childNodes.length, 1, 'There is only one child node which is a v-line node.');
assert.equal(t.node.childNodes[0].childNodes.length, 1, 'There is only one child of that v-line node which is a text node.');
assert.equal(serializeNode(t.node.childNodes[0].childNodes[0]), 'abc', 'Generated text is ok for a single line and no annotations.');
assert.equal(t.attr('fill'), 'black', 'fill attribute set');
assert.equal(t.attr('x'), '250', 'x attribute set');
assert.equal(t.attr('dy'), '100', 'dy attribute set');
svg.remove();
});
QUnit.test('multi-line and annotations', function(assert) {
var svg = getSvg();
var t = V('text', { x: 250, dy: 100, fill: 'black' });
t.text('abc\ndef');
assert.equal(t.node.childNodes.length, 2, 'There are two child nodes one for each line.');
t.text('abcdefgh', {
annotations: [
{ start: 1, end: 3, attrs: { fill: 'red', stroke: 'orange' }},
{ start: 2, end: 5, attrs: { fill: 'blue' }}
]
});
assert.equal(t.find('.v-line').length, 1, 'One .v-line element rendered');
assert.equal(t.find('tspan').length, 4, '4 tspans rendered in total');
t.text('abcd\nefgh', {
annotations: [
{ start: 1, end: 3, attrs: { fill: 'red', stroke: 'orange' }},
{ start: 2, end: 5, attrs: { fill: 'blue' }}
]
});
assert.equal(t.find('.v-line').length, 2, 'Two .v-line elements rendered');
assert.equal(t.find('tspan').length, 5, '5 tspans rendered in total');
svg.remove();
});
QUnit.test('line height', function(assert) {
var t = V('text', { 'font-size': 20 });
var linesDy;
var text = 'abcd\nefgh';
var annotations = [
{ start: 0, end: 4, attrs: { fill: 'red' }},
{ start: 5, end: 9, attrs: { fill: 'blue' }}
];
t.text(text, {
lineHeight: '2.1em',
annotations: annotations
});
linesDy = t.children().map(function(vTSpan) {
return vTSpan.attr('dy');
});
assert.deepEqual(linesDy, ['0', '2.1em']); // hard-coded line-height
t.text(text, {
lineHeight: 'auto',
annotations: annotations
});
linesDy = t.children().map(function(vTSpan) {
return vTSpan.attr('dy');
});
assert.deepEqual(linesDy, ['0', '24']); // base font-size * 1.2
t.text(text, {
lineHeight: 'auto',
annotations: [
{ start: 0, end: 4, attrs: { fill: 'red' }},
{ start: 5, end: 9, attrs: { fill: 'blue', 'font-size': 30 }}
]
});
linesDy = t.children().map(function(vTSpan) {
return vTSpan.attr('dy');
});
assert.deepEqual(linesDy, ['0', '36']); // max font-size * 1.2
});
QUnit.test('empty line height', function(assert) {
var fontSize = 20;
var annotationFontSize = 30;
var t = V('text', { 'font-size': fontSize });
var text = '\na\n\nb\n\n';
// Line Height 'Auto'
function testLineHeightAuto(annotations) {
t.text(text, {
lineHeight: 'auto',
annotations: annotations
});
var linesDy = t.children().map(function(vTSpan) {
return vTSpan.attr('dy');
});
assert.deepEqual(linesDy, [
'0',
String(annotationFontSize * 1.2),
String(annotationFontSize * 1.2),
String(annotationFontSize * 1.2),
String(annotationFontSize * 1.2),
String(fontSize * 1.2),
]);
var linesFontSize = t.children().map(function(vTSpan) {
return vTSpan.attr('font-size');
});
assert.deepEqual(linesFontSize, [
String(annotationFontSize),
null,
String(annotationFontSize),
null,
String(annotationFontSize),
String(fontSize),
]);
}
testLineHeightAuto([
{ start:-1, end: 6, attrs: { 'font-size': annotationFontSize }},
]);
testLineHeightAuto([
{ start: -1, end: 6, attrs: { 'font-size': annotationFontSize - 1 }},
{ start: -1, end: 6, attrs: { 'font-size': annotationFontSize }},
]);
testLineHeightAuto([
{ start: -1, end: 6, attrs: { 'font-size': annotationFontSize + 1 }},
{ start: -1, end: 6, attrs: { 'font-size': annotationFontSize }},
]);
testLineHeightAuto([
{ start: -1, end: 6, attrs: { 'font-size': annotationFontSize }},
{ start: -1, end: 6, attrs: { 'no-font-size': true }},
]);
// Line Height '2em'
function testLineHeight2em(annotations) {
t.text(text, {
lineHeight: '2em',
annotations: annotations
});
var linesDy = t.children().map(function(vTSpan) {
return vTSpan.attr('dy');
});
assert.deepEqual(linesDy, [
'0',
'2em',
'2em',
'2em',
'2em',
'2em',
]);
var linesFontSize = t.children().map(function(vTSpan) {
return vTSpan.attr('font-size');
});
assert.deepEqual(linesFontSize, [
String(annotationFontSize),
null,
String(annotationFontSize),
null,
String(annotationFontSize),
String(fontSize),
]);
}
testLineHeight2em([
{ start: -1, end: 6, attrs: { 'font-size': annotationFontSize }},
]);
testLineHeight2em([
{ start: -1, end: 6, attrs: { 'font-size': annotationFontSize - 1 }},
{ start: -1, end: 6, attrs: { 'font-size': annotationFontSize }},
]);
testLineHeight2em([
{ start: -1, end: 6, attrs: { 'font-size': annotationFontSize + 1 }},
{ start: -1, end: 6, attrs: { 'font-size': annotationFontSize }},
]);
testLineHeight2em([
{ start: -1, end: 6, attrs: { 'font-size': annotationFontSize }},
{ start: -1, end: 6, attrs: { 'no-font-size': true }},
]);
});
QUnit.test('custom EOL', function(assert) {
var svg = getSvg();
var t = V('text', { x: 250, dy: 100, fill: 'black' });
t.text('abc\ndef', { eol: 'X' });
assert.equal(t.node.childNodes[0].textContent, 'abcX');
assert.equal(t.node.childNodes[1].textContent, 'def');
t.text('abc\ndef\n', { eol: 'X' });
assert.equal(t.node.childNodes[0].textContent, 'abcX');
assert.equal(t.node.childNodes[1].textContent, 'defX');
svg.remove();
});
QUnit.test('includeAnnotationIndices', function(assert) {
var svg = getSvg();
var t = V('text', { x: 250, dy: 100, fill: 'black' });
t.text('abcdefgh', {
includeAnnotationIndices: true, annotations: [
{ start: 1, end: 3, attrs: { fill: 'red', stroke: 'orange' }},
{ start: 2, end: 5, attrs: { fill: 'blue' }}
]
});
assert.equal(V(t.find('tspan')[1]).attr('annotations'), '0', 'annotation indices added as an attribute');
assert.equal(V(t.find('tspan')[2]).attr('annotations'), '0,1', 'annotation indices added as an attribute');
assert.equal(V(t.find('tspan')[3]).attr('annotations'), '1', 'annotation indices added as an attribute');
t.text('');
assert.equal(t.attr('display'), 'none');
t.text('text');
assert.equal(t.attr('display'), null);
svg.remove();
});
QUnit.test('visibility', function(assert) {
var svg = getSvg();
var t = V('text', { x: 250, dy: 100, fill: 'black' });
t.text('');
assert.equal(t.attr('display'), 'none');
t.text('text');
assert.equal(t.attr('display'), null);
svg.remove();
});
QUnit.test('textVerticalAnchor', function(assert) {
var texts = ['one', 'one\ntwo', 'one\ntwo\nthree'];
var n = texts.length;
var fontSize = 30;
assert.expect(3 * n);
var svg = getSvg();
var t = V('text', { 'font-size': fontSize }).appendTo(svg);
for (var i = 0; i < n; i++) {
var text = texts[i];
var bbox;
// 'bottom'
t.text(text, { textVerticalAnchor: 'bottom' });
bbox = t.getBBox();
assert.ok(Math.abs(bbox.corner().y) < (fontSize * 0.2), 'bottom anchor: ' + text);
// 'top'
t.text(text, { textVerticalAnchor: 'top' });
bbox = t.getBBox();
assert.ok(Math.abs(bbox.origin().y) < (fontSize * 0.2), 'top anchor: ' + text);
// 'middle'
t.text(text, { textVerticalAnchor: 'middle' });
bbox = t.getBBox();
assert.ok(Math.abs(bbox.center().y) < (fontSize * 0.2), 'middle anchor: ' + text);
}
svg.remove();
});
});
QUnit.test('annotateString', function(assert) {
var annotations = V.annotateString('This is a text that goes on multiple lines.', [
{ start: 2, end: 5, attrs: { fill: 'red' }},
{ start: 4, end: 8, attrs: { fill: 'blue' }}
]);
assert.deepEqual(
annotations,
[
'Th',
{ t: 'is', attrs: { fill: 'red' }},
{ t: ' is ', attrs: { fill: 'blue' }},
'a text that goes on multiple lines.'
],
'String cut into pieces and attributed according to the spans.'
);
annotations = V.annotateString('abcdefgh', [
{ start: 1, end: 3, attrs: { 'class': 'one' }},
{ start: 2, end: 5, attrs: { 'class': 'two', fill: 'blue' }}
]);
assert.deepEqual(
annotations,
[
'a',
{ t: 'b', attrs: { 'class': 'one' }},
{ t: 'c', attrs: { 'class': 'one two', fill: 'blue' }},
{ t: 'de', attrs: { 'class': 'two', fill: 'blue' }},
'fgh'
],
'String cut into pieces and attributed according to the annotations including concatenated classes.'
);
annotations = V.annotateString('abcdefgh', [
{ start: 1, end: 3, attrs: { 'class': 'one' }},
{ start: 2, end: 5, attrs: { 'class': 'two', fill: 'blue' }}
], { includeAnnotationIndices: true });
assert.deepEqual(
annotations,
[
'a',
{ t: 'b', attrs: { 'class': 'one' }, annotations: [0] },
{ t: 'c', attrs: { 'class': 'one two', fill: 'blue' }, annotations: [0, 1] },
{ t: 'de', attrs: { 'class': 'two', fill: 'blue' }, annotations: [1] },
'fgh'
],
'annotation indices included'
);
});
QUnit.test('styleToObject', function(assert) {
assert.deepEqual(V.styleToObject('fill=red; stroke=blue'), { fill: 'red', stroke: 'blue' }, 'style string parsed properly');
});
QUnit.test('mergeAttrs', function(assert) {
assert.deepEqual(
V.mergeAttrs({ x: 5, y: 10, style: 'fill=red; stroke=blue' }, { y: 20, style: { stroke: 'orange' }}),
{ x: 5, y: 20, style: { fill: 'red', stroke: 'orange' }},
'style string parsed properly'
);
});
QUnit.test('find()', function(assert) {
var found = V(svgContainer).find('circle');
assert.ok(Array.isArray(found), 'The result should be an array.');
assert.ok(found.length > 0, 'The array should not be empty.');
assert.ok(found.reduce(function(memo, vel) { return memo && V.isVElement(vel); }, true), 'Items in the array should be wrapped in Vectorizer.');
});
QUnit.test('children()', function(assert) {
var checkChildren = svgGroup.childNodes;
assert.ok(checkChildren.length > 0, 'The checkChildren collection should not be empty.');
assert.ok(checkChildren.length === 2, 'The checkChildren collection should have two elements.');
var children = V(svgGroup).children();
assert.ok(Array.isArray(children), 'The result should be an array.');
assert.ok(children.length > 0, 'The array should not be empty.');
assert.ok(children.length === 2, 'The array should have two elements.');
assert.ok(children.reduce(function(memo, vel) { return memo && V.isVElement(vel); }, true), 'Items in the array should be wrapped in Vectorizer.');
var textNode = document.createTextNode('Text node');
svgGroup.appendChild(textNode);
var comment = document.createComment('Comment');
svgGroup.appendChild(comment);
var attribute = document.createAttribute('Attribute');
attribute.value = 'Hello World';
svgGroup.setAttributeNode(attribute);
var checkChildren2 = svgGroup.childNodes;
assert.ok(checkChildren2.length > 0, 'The checkChildren2 collection should not be empty.');
assert.ok(checkChildren2.length === 4, 'The checkChildren2 collection should have four child nodes.');
var numElements = 0;
for (var i = 0; i < checkChildren2.length; i++) {
var currentChild = checkChildren2[i];
if (currentChild.nodeType === 1) {
numElements += 1;
}
}
assert.ok(numElements === 2, 'The checkChildren2 collection should have two child elements.');
var children2 = V(svgGroup).children();
assert.ok(Array.isArray(children2), 'The result should be an array.');
assert.ok(children2.length > 0, 'The array should not be empty.');
assert.ok(children2.length === 2, 'The array should have two child elements.');
assert.ok(children2.reduce(function(memo, vel) { return memo && V.isVElement(vel); }, true), 'Items in the array should be wrapped in Vectorizer.');
var emptyChildren = V(svgCircle).children();
assert.ok(Array.isArray(emptyChildren), 'The result should be an array.');
assert.ok(emptyChildren.length === 0, 'The array should be empty.');
});
QUnit.test('V.transformPoint', function(assert) {
var p = { x: 1, y: 2 };
var t;
var group = V('<g/>');
V(svgContainer).append(group);
t = V.transformPoint(p, group.node.getCTM());
assert.deepEqual({ x: t.x, y: t.y }, { x: 1, y: 2 }, 'transform without transformation returns the point unchanged.');
group.scale(2, 3);
t = V.transformPoint(p, group.node.getCTM());
assert.deepEqual({ x: t.x, y: t.y }, { x: 2, y: 6 }, 'transform with scale transformation returns correct point.');
group.attr('transform', 'rotate(90)');
t = V.transformPoint(p, group.node.getCTM());
assert.deepEqual({ x: t.x, y: t.y }, { x: -2, y: 1 }, 'transform with rotate transformation returns correct point.');
});
QUnit.test('findParentByClass', function(assert) {
assert.equal(
V(svgGroup3).findParentByClass('group-1').node,
svgGroup1,
'parent exists'
);
assert.notOk(
V(svgGroup3).findParentByClass('not-a-parent'),
'parent does not exist'
);
assert.notOk(
V(svgGroup3).findParentByClass('group-1', svgGroup2),
'parent exists, terminator on the way down'
);
assert.equal(
V(svgGroup3).findParentByClass('group-1', svgCircle).node,
svgGroup1,
'parent exists, terminator not on the way down'
);
assert.notOk(
V(svgGroup3).findParentByClass('not-a-parent', svgCircle),
'parent does not exist, terminator not on the way down'
);
});
QUnit.test('contains()', function(assert) {
assert.ok(V(svgContainer).contains(svgGroup1));
assert.ok(V(svgGroup1).contains(svgGroup3));
assert.ok(V(svgGroup1).contains(svgGroup2));
assert.notOk(V(svgGroup3).contains(svgGroup1));
assert.notOk(V(svgGroup2).contains(svgGroup1));
assert.notOk(V(svgGroup1).contains(svgGroup1));
assert.notOk(V(svgGroup1).contains(document));
});
QUnit.module('transform()', function(hooks) {
var vel;
hooks.beforeEach(function() {
vel = V('rect').appendTo(svgContainer);
});
hooks.afterEach(function() {
vel.remove();
});
QUnit.test('as a getter', function(assert) {
assert.deepEqual(vel.transform(), V.createSVGMatrix({
a: 1,
b: 0,
c: 0,
d: 1,
e: 0,
f: 0
}));
});
QUnit.test('single transformation', function(assert) {
vel.transform({ a: 2, b: 0, c: 0, d: 2, e: 0, f: 0 });
assert.deepEqual(vel.transform(), V.createSVGMatrix({
a: 2,
b: 0,
c: 0,
d: 2,
e: 0,
f: 0
}));
});
QUnit.test('multiple transformations', function(assert) {
vel.transform({ a: 2, b: 0, c: 0, d: 2, e: 0, f: 0 });
vel.transform({ a: 1, b: 0, c: 0, d: 1, e: 10, f: 10 });
assert.deepEqual(vel.transform(), V.createSVGMatrix({
a: 2,
b: 0,
c: 0,
d: 2,
e: 20,
f: 20
}));
});
QUnit.test('as a getter (element not in the DOM)', function(assert) {
vel.transform({ a: 2, b: 0, c: 0, d: 2, e: 0, f: 0 });
vel.transform({ a: 1, b: 0, c: 0, d: 1, e: 10, f: 10 });
vel.remove();
assert.deepEqual(vel.transform(), V.createSVGMatrix({
a: 2,
b: 0,
c: 0,
d: 2,
e: 20,
f: 20
}));
});
QUnit.test('opt to clear transformation list', function(assert) {
vel.transform({ a: 2, b: 0, c: 0, d: 2, e: 0, f: 0 });
vel.transform({ a: 1, b: 1, c: 1, d: 1, e: 1, f: 1 }, { absolute: true });
vel.remove();
assert.deepEqual(vel.transform(), V.createSVGMatrix({
a: 1,
b: 1,
c: 1,
d: 1,
e: 1,
f: 1
}), 'should clean transformation list before applying 2nd transformation');
});
});
QUnit.module('empty()', function(hooks) {
var vel;
hooks.beforeEach(function() {
vel = V('g');
V(svgContainer).append(vel);
});
hooks.afterEach(function() {
vel.remove();
});
QUnit.test('should remove all child nodes', function(assert) {
vel.append([
V('rect'),
V('polygon'),
V('circle')
]);
assert.equal(vel.node.childNodes.length, 3);
vel.empty();
assert.equal(vel.node.childNodes.length, 0);
});
});
QUnit.module('attribute', function(hooks) {
var svgToString = function(svg) {
return new XMLSerializer().serializeToString(svg.node);
};
hooks.beforeEach(function() {
this.svg = V('svg');
});
QUnit.module('set', function(hooks) {
QUnit.test('no namespace', function(assert) {
var element = V('a').attr('href', 'www.seznam.cz');
this.svg.append(element);
var text = svgToString(element);
assert.equal(text.indexOf(':href'), -1, 'should find attr without namespace');
assert.ok(text.indexOf('href') > 0, 'attr has been set');
assert.ok(text.indexOf('href') > 0, 'attr values has been set');
});
QUnit.test('with namespace', function(assert) {
var element = V('a').attr('xlink:href', 'www.seznam.cz');
this.svg.append(element);
var text = svgToString(this.svg);
assert.ok(text.indexOf('xlink:href') > 0, 'message');
});
QUnit.test('value "null" removes attr', function(assert) {
var element = V('a').attr('xlink:href', 'www.seznam.cz');
this.svg.append(element);
element.attr('xlink:href', null);
var text = svgToString(this.svg);
assert.ok(text.indexOf('xlink:href') === -1, 'attribute should be removed');
});
QUnit.test('special attr', function(assert) {
var element = V('a').attr('id', 'x');
this.svg.append(element);
var text = svgToString(element);
assert.ok(text.indexOf('id') > 0, 'id has been set');
});
});
QUnit.module('camel case support', function(hooks) {
hooks.before(function() {
V.supportCamelCaseAttributes = true;
});
hooks.after(function() {
V.supportCamelCaseAttributes = false;
});
QUnit.test('constructor', function(assert) {
const vel = V('rect', { strokeWidth: 5 });
assert.equal(vel.node.getAttribute('stroke-width'), 5);
});
QUnit.test('attr()', function(assert) {
const vel = V('rect');
vel.attr('strokeWidth', 5);
assert.equal(vel.attr('strokeWidth'), 5);
assert.equal(vel.attr('stroke-width'), 5);
assert.equal(vel.node.getAttribute('stroke-width'), 5);
vel.attr('stroke-width', 10);
assert.equal(vel.attr('strokeWidth'), 10);
assert.equal(vel.attr('stroke-width'), 10);
assert.equal(vel.node.getAttribute('stroke-width'), 10);
vel.attr('strokeWidth', null);
assert.equal(vel.attr('strokeWidth'), null);
assert.equal(vel.attr('stroke-width'), null);
assert.equal(vel.node.getAttribute('stroke-width'), null);
});
QUnit.test('removeAttr()', function(assert) {
const vel = V('rect');
vel.attr('strokeWidth', 5);
assert.equal(vel.node.getAttribute('stroke-width'), 5);
vel.removeAttr('strokeWidth');
assert.equal(vel.node.getAttribute('stroke-width'), null);
});
});
QUnit.test('remove simple', function(assert) {
var a = V('a').attr('href', 'www.seznam.cz');
this.svg.append(a);
a.removeAttr('href');
var text = svgToString(this.svg);
assert.equal(text.indexOf('href'), -1, 'should be deleted');
});
QUnit.test('try to remove non existing', function(assert) {
var a = V('a').attr('href', 'www.seznam.cz');
this.svg.append(a);
a.removeAttr('blah');
var text = svgToString(this.svg);
assert.ok(text.indexOf('href') > 0, 'should not throw');
});
QUnit.test('remove with namespace', function(assert) {
var a = V('a').attr('xlink:href', 'www.seznam.cz');
this.svg.append(a);
a.removeAttr('xlink:href');
var text = svgToString(this.svg);
assert.equal(text.indexOf('href'), -1, 'message');
assert.equal(text.indexOf('seznam'), -1, 'message');
});
QUnit.test('remove with not known namespace', function(assert) {
var a = V('a').attr('xxx:href', 'www.seznam.cz');
this.svg.append(a);
a.removeAttr('xxx:href');
var text = svgToString(this.svg);
assert.equal(text.indexOf('href'), -1, 'message');
assert.equal(text.indexOf('seznam'), -1, 'message');
});
QUnit.test('apply remove attr', function(assert) {
var element = V('a');
this.svg.append(element);
element.text();
element.text('text');
var text = svgToString(this.svg);
assert.ok(text.indexOf('display="null"') === -1, 'attr display should be removed');
});
});
QUnit.module('append()', function(hooks) {
var groupElement;
hooks.beforeEach(function() {
groupElement = V(svgGroup).clone().empty();
});
QUnit.test('single element', function(assert) {
groupElement.append(V('<rect/>'));
assert.equal(groupElement.node.childNodes.length, 1);
assert.deepEqual(childrenTagNames(groupElement), ['rect']);
groupElement.append(V('<circle/>'));
assert.equal(groupElement.node.childNodes.length, 2);
assert.deepEqual(childrenTagNames(groupElement), ['rect', 'circle']);
});
QUnit.test('multiple elements', function(assert) {
groupElement.append(V('<rect/><circle/>'));
assert.equal(groupElement.node.childNodes.length, 2);
assert.deepEqual(childrenTagNames(groupElement), ['rect', 'circle']);
groupElement.append(V('<line/><polygon/>'));
assert.equal(groupElement.node.childNodes.length, 4);
assert.deepEqual(childrenTagNames(groupElement), ['rect', 'circle', 'line', 'polygon']);
});