xyc
2025-02-21 664db98c9e8595ce4dd636a27f480e3a08b81ff5
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
/* eslint-disable indent */
/* globals jQuery, jsPDF */
/**
 * Numerous tools for working with the editor's "canvas"
 * @module svgcanvas
 *
 * @license MIT
 *
 * @copyright 2010 Alexis Deveria, 2010 Pavol Rusnak, 2010 Jeff Schiller
 *
 */
 
/* Dependencies:
1. Also expects jQuery UI for `svgCanvasToString` and
`convertToGroup` use of `:data()` selector
*/
 
// Todo: Obtain/adapt latest jsPDF to utilize ES Module for `jsPDF`/avoid global
 
import './svgpathseg.js';
import jqPluginSVG from './jQuery.attr.js'; // Needed for SVG attribute setting and array form with `attr`
 
import * as draw from './draw.js';
import * as pathModule from './path.js';
import {sanitizeSvg} from './sanitize.js';
import {getReverseNS, NS} from './namespaces.js';
import {importSetGlobal, importScript} from './external/dynamic-import-polyfill/importModule.js';
import {
  text2xml, assignAttributes, cleanupElement, getElem, getUrlFromAttr,
  findDefs, getHref, setHref, getRefElem, getRotationAngle, getPathBBox,
  preventClickDefault, snapToGrid, walkTree, walkTreePost,
  getBBoxOfElementAsPath, convertToPath, toXml, encode64, decode64,
  dataURLToObjectURL, createObjectURL,
  getVisibleElements, dropXMLInteralSubset,
  init as utilsInit, getBBox as utilsGetBBox, getStrokedBBoxDefaultVisible
} from './utilities.js';
import * as history from './history.js';
import {
  transformPoint, matrixMultiply, hasMatrixTransform, transformListToTransform,
  getMatrix, snapToAngle, isIdentity, rectsIntersect, transformBox
} from './math.js';
import {
  convertToNum, convertAttrs, convertUnit, shortFloat, getTypeMap,
  init as unitsInit
} from './units.js';
import {
  isGecko, isChrome, isIE, isWebkit, supportsNonScalingStroke, supportsGoodTextCharPos
} from './browser.js'; // , supportsEditableText
import {
  getTransformList, resetListMap,
  SVGTransformList as SVGEditTransformList
} from './svgtransformlist.js';
import {
  remapElement,
  init as coordsInit
} from './coords.js';
import {
  recalculateDimensions,
  init as recalculateInit
} from './recalculate.js';
import {
  getSelectorManager,
  init as selectInit
} from './select.js';
 
const $ = jqPluginSVG(jQuery);
const {
  MoveElementCommand, InsertElementCommand, RemoveElementCommand,
  ChangeElementCommand, BatchCommand, UndoManager, HistoryEventTypes
} = history;
 
if (!window.console) {
  window.console = {};
  window.console.log = function (str) {};
  window.console.dir = function (str) {};
}
 
if (window.opera) {
  window.console.log = function (str) { window.opera.postError(str); };
  window.console.dir = function (str) {};
}
 
/**
* The main SvgCanvas class that manages all SVG-related functions.
* @memberof module:svgcanvas
*
* @borrows module:coords.remapElement as #remapElement
* @borrows module:recalculate.recalculateDimensions as #recalculateDimensions
*
* @borrows module:utilities.cleanupElement as #cleanupElement
* @borrows module:utilities.getStrokedBBoxDefaultVisible as #getStrokedBBox
* @borrows module:utilities.getVisibleElements as #getVisibleElements
* @borrows module:utilities.findDefs as #findDefs
* @borrows module:utilities.getUrlFromAttr as #getUrlFromAttr
* @borrows module:utilities.getHref as #getHref
* @borrows module:utilities.setHref as #setHref
* @borrows module:utilities.getRotationAngle as #getRotationAngle
* @borrows module:utilities.getBBox as #getBBox
* @borrows module:utilities.getElem as #getElem
* @borrows module:utilities.getRefElem as #getRefElem
* @borrows module:utilities.assignAttributes as #assignAttributes
*
* @borrows module:SVGTransformList.getTransformList as #getTransformList
* @borrows module:math.matrixMultiply as #matrixMultiply
* @borrows module:math.hasMatrixTransform as #hasMatrixTransform
* @borrows module:math.transformListToTransform as #transformListToTransform
* @borrows module:units.convertToNum as #convertToNum
* @borrows module:sanitize.sanitizeSvg as #sanitizeSvg
* @borrows module:path.pathActions.linkControlPoints as #linkControlPoints
*/
class SvgCanvas {
  /**
  * @param {HTMLElement} container - The container HTML element that should hold the SVG root element
  * @param {module:SVGEditor.curConfig} config - An object that contains configuration data
  */
  constructor (container, config) {
// Alias Namespace constants
 
// Default configuration options
const curConfig = {
  show_outside_canvas: true,
  selectNew: true,
  dimensions: [640, 480]
};
 
// Update config with new one if given
if (config) {
  $.extend(curConfig, config);
}
 
// Array with width/height of canvas
const {dimensions} = curConfig;
 
const canvas = this;
 
// "document" element associated with the container (same as window.document using default svg-editor.js)
// NOTE: This is not actually a SVG document, but an HTML document.
const svgdoc = container.ownerDocument;
 
// This is a container for the document being edited, not the document itself.
/**
 * @name module:svgcanvas~svgroot
 * @type {SVGSVGElement}
 */
const svgroot = svgdoc.importNode(
  text2xml(
    '<svg id="svgroot" xmlns="' + NS.SVG + '" xlinkns="' + NS.XLINK + '" ' +
      'width="' + dimensions[0] + '" height="' + dimensions[1] + '" x="' + dimensions[0] + '" y="' + dimensions[1] + '" overflow="visible">' +
      '<defs>' +
        '<filter id="canvashadow" filterUnits="objectBoundingBox">' +
          '<feGaussianBlur in="SourceAlpha" stdDeviation="4" result="blur"/>' +
          '<feOffset in="blur" dx="5" dy="5" result="offsetBlur"/>' +
          '<feMerge>' +
            '<feMergeNode in="offsetBlur"/>' +
            '<feMergeNode in="SourceGraphic"/>' +
          '</feMerge>' +
        '</filter>' +
      '</defs>' +
    '</svg>'
  ).documentElement,
  true
);
container.append(svgroot);
 
/**
 * The actual element that represents the final output SVG element
 * @name module:svgcanvas~svgcontent
 * @type {SVGSVGElement}
 */
let svgcontent = svgdoc.createElementNS(NS.SVG, 'svg');
 
/**
* This function resets the svgcontent element while keeping it in the DOM.
* @function module:svgcanvas.SvgCanvas#clearSvgContentElement
* @returns {undefined}
*/
const clearSvgContentElement = canvas.clearSvgContentElement = function () {
  $(svgcontent).empty();
 
  // TODO: Clear out all other attributes first?
  $(svgcontent).attr({
    id: 'svgcontent',
    width: dimensions[0],
    height: dimensions[1],
    x: dimensions[0],
    y: dimensions[1],
    overflow: curConfig.show_outside_canvas ? 'visible' : 'hidden',
    xmlns: NS.SVG,
    'xmlns:se': NS.SE,
    'xmlns:xlink': NS.XLINK
  }).appendTo(svgroot);
 
  // TODO: make this string optional and set by the client
  const comment = svgdoc.createComment(' Created with SVG-edit - https://github.com/SVG-Edit/svgedit');
  svgcontent.append(comment);
};
clearSvgContentElement();
 
// Prefix string for element IDs
let idprefix = 'svg_';
 
/**
* Changes the ID prefix to the given value.
* @function module:svgcanvas.SvgCanvas#setIdPrefix
* @param {string} p - String with the new prefix
* @returns {undefined}
*/
canvas.setIdPrefix = function (p) {
  idprefix = p;
};
 
/**
* Current draw.Drawing object
* @type {module:draw.Drawing}
* @name module:svgcanvas.SvgCanvas#current_drawing_
*/
canvas.current_drawing_ = new draw.Drawing(svgcontent, idprefix);
 
/**
* Returns the current Drawing.
* @function module:svgcanvas.SvgCanvas#getCurrentDrawing
* @implements {module:draw.DrawCanvasInit#getCurrentDrawing}
*/
const getCurrentDrawing = canvas.getCurrentDrawing = function () {
  return canvas.current_drawing_;
};
 
/**
* Float displaying the current zoom level (1 = 100%, .5 = 50%, etc)
* @type {Float}
*/
let currentZoom = 1;
 
// pointer to current group (for in-group editing)
let currentGroup = null;
 
// Object containing data for the currently selected styles
const allProperties = {
  shape: {
    fill: (curConfig.initFill.color === 'none' ? '' : '#') + curConfig.initFill.color,
    fill_paint: null,
    fill_opacity: curConfig.initFill.opacity,
    stroke: '#' + curConfig.initStroke.color,
    stroke_paint: null,
    stroke_opacity: curConfig.initStroke.opacity,
    stroke_width: curConfig.initStroke.width,
    stroke_dasharray: 'none',
    stroke_linejoin: 'miter',
    stroke_linecap: 'butt',
    opacity: curConfig.initOpacity
  }
};
 
allProperties.text = $.extend(true, {}, allProperties.shape);
$.extend(allProperties.text, {
  fill: '#000000',
  stroke_width: curConfig.text && curConfig.text.stroke_width,
  font_size: curConfig.text && curConfig.text.font_size,
  font_family: curConfig.text && curConfig.text.font_family
});
 
// Current shape style properties
const curShape = allProperties.shape;
 
// Array with all the currently selected elements
// default size of 1 until it needs to grow bigger
let selectedElements = [];
 
/**
* @typedef {PlainObject} module:svgcanvas.SVGAsJSON
* @property {string} element
* @property {PlainObject.<string, string>} attr
* @property {module:svgcanvas.SVGAsJSON[]} children
*/
 
/**
* @function module:svgcanvas.SvgCanvas#getContentElem
* @param {Text|Element} data
* @returns {module:svgcanvas.SVGAsJSON}
*/
const getJsonFromSvgElement = this.getJsonFromSvgElement = function (data) {
  // Text node
  if (data.nodeType === 3) return data.nodeValue;
 
  const retval = {
    element: data.tagName,
    // namespace: nsMap[data.namespaceURI],
    attr: {},
    children: []
  };
 
  // Iterate attributes
  for (let i = 0, attr; (attr = data.attributes[i]); i++) {
    retval.attr[attr.name] = attr.value;
  }
 
  // Iterate children
  for (let i = 0, node; (node = data.childNodes[i]); i++) {
    retval.children[i] = getJsonFromSvgElement(node);
  }
 
  return retval;
};
 
/**
* This should really be an intersection implementing all rather than a union.
* @function module:svgcanvas.SvgCanvas#addSVGElementFromJson
* @implements {module:utilities.EditorContext#addSVGElementFromJson|module:path.EditorContext#addSVGElementFromJson}
*/
const addSVGElementFromJson = this.addSVGElementFromJson = function (data) {
  if (typeof data === 'string') return svgdoc.createTextNode(data);
 
  let shape = getElem(data.attr.id);
  // if shape is a path but we need to create a rect/ellipse, then remove the path
  const currentLayer = getCurrentDrawing().getCurrentLayer();
  if (shape && data.element !== shape.tagName) {
    shape.remove();
    shape = null;
  }
  if (!shape) {
    const ns = data.namespace || NS.SVG;
    shape = svgdoc.createElementNS(ns, data.element);
    if (currentLayer) {
      (currentGroup || currentLayer).append(shape);
    }
  }
  if (data.curStyles) {
    assignAttributes(shape, {
      fill: curShape.fill,
      stroke: curShape.stroke,
      'stroke-width': curShape.stroke_width,
      'stroke-dasharray': curShape.stroke_dasharray,
      'stroke-linejoin': curShape.stroke_linejoin,
      'stroke-linecap': curShape.stroke_linecap,
      'stroke-opacity': curShape.stroke_opacity,
      'fill-opacity': curShape.fill_opacity,
      opacity: curShape.opacity / 2,
      style: 'pointer-events:inherit'
    }, 100);
  }
  assignAttributes(shape, data.attr, 100);
  cleanupElement(shape);
 
  // Children
  if (data.children) {
    data.children.forEach((child) => {
      shape.append(addSVGElementFromJson(child));
    });
  }
 
  return shape;
};
 
canvas.getTransformList = getTransformList;
 
canvas.matrixMultiply = matrixMultiply;
canvas.hasMatrixTransform = hasMatrixTransform;
canvas.transformListToTransform = transformListToTransform;
 
/**
* @implements {module:utilities.EditorContext#getBaseUnit}
*/
const getBaseUnit = () => { return curConfig.baseUnit; };
 
/**
* initialize from units.js.
* Send in an object implementing the ElementContainer interface (see units.js)
*/
unitsInit(
  /**
  * @implements {module:units.ElementContainer}
  */
  {
    getBaseUnit,
    getElement: getElem,
    getHeight () { return svgcontent.getAttribute('height') / currentZoom; },
    getWidth () { return svgcontent.getAttribute('width') / currentZoom; },
    getRoundDigits () { return saveOptions.round_digits; }
  }
);
 
canvas.convertToNum = convertToNum;
 
/**
* This should really be an intersection implementing all rather than a union.
* @implements {module:draw.DrawCanvasInit#getSVGContent|module:utilities.EditorContext#getSVGContent}
*/
const getSVGContent = () => { return svgcontent; };
 
/**
* Should really be an intersection with all needing to apply rather than a union.
* @function module:svgcanvas.SvgCanvas#getSelectedElements
* @implements {module:utilities.EditorContext#getSelectedElements|module:draw.DrawCanvasInit#getSelectedElements|module:path.EditorContext#getSelectedElements}
*/
const getSelectedElements = this.getSelectedElems = function () {
  return selectedElements;
};
 
const pathActions = pathModule.pathActions;
 
/**
* This should actually be an intersection as all interfaces should be met.
* @implements {module:utilities.EditorContext#getSVGRoot|module:recalculate.EditorContext#getSVGRoot|module:coords.EditorContext#getSVGRoot|module:path.EditorContext#getSVGRoot}
*/
const getSVGRoot = () => svgroot;
 
utilsInit(
  /**
  * @implements {module:utilities.EditorContext}
  */
  {
    pathActions, // Ok since not modifying
    getSVGContent,
    addSVGElementFromJson,
    getSelectedElements,
    getDOMDocument () { return svgdoc; },
    getDOMContainer () { return container; },
    getSVGRoot,
    // TODO: replace this mostly with a way to get the current drawing.
    getBaseUnit,
    getSnappingStep () { return curConfig.snappingStep; }
  }
);
 
canvas.findDefs = findDefs;
canvas.getUrlFromAttr = getUrlFromAttr;
canvas.getHref = getHref;
canvas.setHref = setHref;
/* const getBBox = */ canvas.getBBox = utilsGetBBox;
canvas.getRotationAngle = getRotationAngle;
canvas.getElem = getElem;
canvas.getRefElem = getRefElem;
canvas.assignAttributes = assignAttributes;
 
this.cleanupElement = cleanupElement;
 
/**
* This should actually be an intersection not a union as all should apply.
* @implements {module:coords.EditorContext|module:path.EditorContext}
*/
const getGridSnapping = () => { return curConfig.gridSnapping; };
 
coordsInit(
  /**
  * @implements {module:coords.EditorContext}
  */
  {
    getDrawing () { return getCurrentDrawing(); },
    getSVGRoot,
    getGridSnapping
  }
);
this.remapElement = remapElement;
 
recalculateInit(
  /**
  * @implements {module:recalculate.EditorContext}
  */
  {
    getSVGRoot,
    getStartTransform () { return startTransform; },
    setStartTransform (transform) { startTransform = transform; }
  }
);
this.recalculateDimensions = recalculateDimensions;
 
// import from sanitize.js
const nsMap = getReverseNS();
canvas.sanitizeSvg = sanitizeSvg;
 
/**
* @name undoMgr
* @memberof module:svgcanvas.SvgCanvas#
* @type {module:history.HistoryEventHandler}
*/
const undoMgr = canvas.undoMgr = new UndoManager({
  /**
   * @param {string} eventType One of the HistoryEvent types
   * @param {module:history.HistoryCommand} cmd Fulfills the HistoryCommand interface
   * @fires module:svgcanvas.SvgCanvas#event:changed
   * @returns {undefined}
   */
  handleHistoryEvent (eventType, cmd) {
    const EventTypes = HistoryEventTypes;
    // TODO: handle setBlurOffsets.
    if (eventType === EventTypes.BEFORE_UNAPPLY || eventType === EventTypes.BEFORE_APPLY) {
      canvas.clearSelection();
    } else if (eventType === EventTypes.AFTER_APPLY || eventType === EventTypes.AFTER_UNAPPLY) {
      const elems = cmd.elements();
      canvas.pathActions.clear();
      call('changed', elems);
      const cmdType = cmd.type();
      const isApply = (eventType === EventTypes.AFTER_APPLY);
      if (cmdType === MoveElementCommand.type()) {
        const parent = isApply ? cmd.newParent : cmd.oldParent;
        if (parent === svgcontent) {
          draw.identifyLayers();
        }
      } else if (cmdType === InsertElementCommand.type() ||
          cmdType === RemoveElementCommand.type()) {
        if (cmd.parent === svgcontent) {
          draw.identifyLayers();
        }
        if (cmdType === InsertElementCommand.type()) {
          if (isApply) { restoreRefElems(cmd.elem); }
        } else {
          if (!isApply) { restoreRefElems(cmd.elem); }
        }
        if (cmd.elem.tagName === 'use') {
          setUseData(cmd.elem);
        }
      } else if (cmdType === ChangeElementCommand.type()) {
        // if we are changing layer names, re-identify all layers
        if (cmd.elem.tagName === 'title' &&
          cmd.elem.parentNode.parentNode === svgcontent
        ) {
          draw.identifyLayers();
        }
        const values = isApply ? cmd.newValues : cmd.oldValues;
        // If stdDeviation was changed, update the blur.
        if (values.stdDeviation) {
          canvas.setBlurOffsets(cmd.elem.parentNode, values.stdDeviation);
        }
        // This is resolved in later versions of webkit, perhaps we should
        // have a featured detection for correct 'use' behavior?
        // ——————————
        // Remove & Re-add hack for Webkit (issue 775)
        // if (cmd.elem.tagName === 'use' && isWebkit()) {
        //  const {elem} = cmd;
        //  if (!elem.getAttribute('x') && !elem.getAttribute('y')) {
        //    const parent = elem.parentNode;
        //    const sib = elem.nextSibling;
        //    elem.remove();
        //    parent.insertBefore(elem, sib);
        //    // Ok to replace above with this? `sib.before(elem);`
        //  }
        // }
      }
    }
  }
});
 
/**
* This should really be an intersection applying to all types rather than a union.
* @function module:svgcanvas~addCommandToHistory
* @implements {module:path.EditorContext#addCommandToHistory|module:draw.DrawCanvasInit#addCommandToHistory}
*/
const addCommandToHistory = function (cmd) {
  canvas.undoMgr.addCommandToHistory(cmd);
};
 
/**
* This should really be an intersection applying to all types rather than a union.
* @function module:svgcanvas.SvgCanvas#getZoom
* @implements {module:path.EditorContext#getCurrentZoom|module:select.SVGFactory#getCurrentZoom}
*/
const getCurrentZoom = this.getZoom = function () { return currentZoom; };
 
/**
* This method rounds the incoming value to the nearest value based on the `currentZoom`
* @function module:svgcanvas.SvgCanvas#round
* @implements {module:path.EditorContext#round}
*/
const round = this.round = function (val) {
  return parseInt(val * currentZoom, 10) / currentZoom;
};
 
selectInit(
  curConfig,
  /**
  * Export to select.js
  * @implements {module:select.SVGFactory}
  */
  {
    createSVGElement (jsonMap) { return canvas.addSVGElementFromJson(jsonMap); },
    svgRoot () { return svgroot; },
    svgContent () { return svgcontent; },
    getCurrentZoom
  }
);
/**
* This object manages selectors for us
* @name module:svgcanvas.SvgCanvas#selectorManager
* @type {module:select.SelectorManager}
*/
const selectorManager = this.selectorManager = getSelectorManager();
 
/**
* @function module:svgcanvas.SvgCanvas#getNextId
* @implements {module:path.EditorContext#getNextId}
*/
const getNextId = canvas.getNextId = function () {
  return getCurrentDrawing().getNextId();
};
 
/**
* @function module:svgcanvas.SvgCanvas#getId
* @implements {module:path.EditorContext#getId}
*/
const getId = canvas.getId = function () {
  return getCurrentDrawing().getId();
};
 
/**
* The "implements" should really be an intersection applying to all types rather than a union.
* @function module:svgcanvas.SvgCanvas#call
* @implements {module:draw.DrawCanvasInit#call|module:path.EditorContext#call}
* @param {"selected"|"changed"|"contextset"|"pointsAdded"|"extension_added"|"extensions_added"|"message"|"transition"|"zoomed"|"updateCanvas"|"zoomDone"|"saved"|"exported"|"exportedPDF"|"setnonce"|"unsetnonce"|"cleared"} ev - String with the event name
* @param {module:svgcanvas.SvgCanvas#event:GenericCanvasEvent} arg - Argument to pass through to the callback function.
* @returns {undefined}
*/
const call = function (ev, arg) {
  if (events[ev]) {
    return events[ev](window, arg);
  }
};
 
/**
* Clears the selection. The 'selected' handler is then optionally called.
* This should really be an intersection applying to all types rather than a union.
* @function module:svgcanvas.SvgCanvas#clearSelection
* @implements {module:draw.DrawCanvasInit#clearSelection|module:path.EditorContext#clearSelection}
* @fires module:svgcanvas.SvgCanvas#event:selected
*/
const clearSelection = this.clearSelection = function (noCall) {
  selectedElements.forEach((elem) => {
    if (elem == null) {
      return;
    }
    selectorManager.releaseSelector(elem);
  });
  selectedElements = [];
 
  if (!noCall) { call('selected', selectedElements); }
};
 
/**
* Adds a list of elements to the selection. The 'selected' handler is then called.
* @function module:svgcanvas.SvgCanvas#addToSelection
* @implements {module:path.EditorContext#addToSelection}
* @fires module:svgcanvas.SvgCanvas#event:selected
*/
const addToSelection = this.addToSelection = function (elemsToAdd, showGrips) {
  if (!elemsToAdd.length) { return; }
  // find the first null in our selectedElements array
 
  let j = 0;
  while (j < selectedElements.length) {
    if (selectedElements[j] == null) {
      break;
    }
    ++j;
  }
 
  // now add each element consecutively
  let i = elemsToAdd.length;
  while (i--) {
    let elem = elemsToAdd[i];
    if (!elem) { continue; }
    const bbox = utilsGetBBox(elem);
    if (!bbox) { continue; }
 
    if (elem.tagName === 'a' && elem.childNodes.length === 1) {
      // Make "a" element's child be the selected element
      elem = elem.firstChild;
    }
 
    // if it's not already there, add it
    if (!selectedElements.includes(elem)) {
      selectedElements[j] = elem;
 
      // only the first selectedBBoxes element is ever used in the codebase these days
      // if (j === 0) selectedBBoxes[0] = utilsGetBBox(elem);
      j++;
      const sel = selectorManager.requestSelector(elem, bbox);
 
      if (selectedElements.length > 1) {
        sel.showGrips(false);
      }
    }
  }
  call('selected', selectedElements);
 
  if (showGrips || selectedElements.length === 1) {
    selectorManager.requestSelector(selectedElements[0]).showGrips(true);
  } else {
    selectorManager.requestSelector(selectedElements[0]).showGrips(false);
  }
 
  // make sure the elements are in the correct order
  // See: https://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-compareDocumentPosition
 
  selectedElements.sort(function (a, b) {
    if (a && b && a.compareDocumentPosition) {
      return 3 - (b.compareDocumentPosition(a) & 6);
    }
    if (a == null) {
      return 1;
    }
  });
 
  // Make sure first elements are not null
  while (selectedElements[0] == null) {
    selectedElements.shift(0);
  }
};
 
/**
* @implements {module:path.EditorContext#getOpacity}
*/
const getOpacity = function () {
  return curShape.opacity;
};
 
/**
* @function module:svgcanvas.SvgCanvas#getMouseTarget
* @implements {module:path.EditorContext#getMouseTarget}
*/
const getMouseTarget = this.getMouseTarget = function (evt) {
  if (evt == null) {
    return null;
  }
  let mouseTarget = evt.target;
 
  // if it was a <use>, Opera and WebKit return the SVGElementInstance
  if (mouseTarget.correspondingUseElement) { mouseTarget = mouseTarget.correspondingUseElement; }
 
  // for foreign content, go up until we find the foreignObject
  // WebKit browsers set the mouse target to the svgcanvas div
  if ([NS.MATH, NS.HTML].includes(mouseTarget.namespaceURI) &&
    mouseTarget.id !== 'svgcanvas'
  ) {
    while (mouseTarget.nodeName !== 'foreignObject') {
      mouseTarget = mouseTarget.parentNode;
      if (!mouseTarget) { return svgroot; }
    }
  }
 
  // Get the desired mouseTarget with jQuery selector-fu
  // If it's root-like, select the root
  const currentLayer = getCurrentDrawing().getCurrentLayer();
  if ([svgroot, container, svgcontent, currentLayer].includes(mouseTarget)) {
    return svgroot;
  }
 
  const $target = $(mouseTarget);
 
  // If it's a selection grip, return the grip parent
  if ($target.closest('#selectorParentGroup').length) {
    // While we could instead have just returned mouseTarget,
    // this makes it easier to indentify as being a selector grip
    return selectorManager.selectorParentGroup;
  }
 
  while (mouseTarget.parentNode !== (currentGroup || currentLayer)) {
    mouseTarget = mouseTarget.parentNode;
  }
 
  //
  // // go up until we hit a child of a layer
  // while (mouseTarget.parentNode.parentNode.tagName == 'g') {
  //   mouseTarget = mouseTarget.parentNode;
  // }
  // Webkit bubbles the mouse event all the way up to the div, so we
  // set the mouseTarget to the svgroot like the other browsers
  // if (mouseTarget.nodeName.toLowerCase() == 'div') {
  //   mouseTarget = svgroot;
  // }
 
  return mouseTarget;
};
 
/**
* @namespace {module:path.pathActions} pathActions
* @memberof module:svgcanvas.SvgCanvas#
* @see module:path.pathActions
*/
canvas.pathActions = pathActions;
/**
* @implements {module:path.EditorContext#resetD}
*/
function resetD (p) {
  p.setAttribute('d', pathActions.convertPath(p));
}
pathModule.init(
  /**
  * @implements {module:path.EditorContext}
  */
  {
    selectorManager, // Ok since not changing
    canvas, // Ok since not changing
    call,
    resetD,
    round,
    clearSelection,
    addToSelection,
    addCommandToHistory,
    remapElement,
    addSVGElementFromJson,
    getGridSnapping,
    getOpacity,
    getSelectedElements,
    getContainer () {
      return container;
    },
    setStarted (s) {
      started = s;
    },
    getRubberBox () {
      return rubberBox;
    },
    setRubberBox (rb) {
      rubberBox = rb;
      return rubberBox;
    },
    /**
     * @param {boolean} closedSubpath
     * @param {SVGCircleElement[]} grips
     * @fires module:svgcanvas.SvgCanvas#event:pointsAdded
     * @fires module:svgcanvas.SvgCanvas#event:selected
     * @returns {undefined}
     */
    addPtsToSelection ({closedSubpath, grips}) {
      // TODO: Correct this:
      pathActions.canDeleteNodes = true;
      pathActions.closed_subpath = closedSubpath;
      call('pointsAdded', {closedSubpath, grips});
      call('selected', grips);
    },
    /**
     * @param {ChangeElementCommand} cmd
     * @param {SVGPathElement} elem
     * @fires module:svgcanvas.SvgCanvas#event:changed
     * @returns {undefined}
     */
    endChanges ({cmd, elem}) {
      addCommandToHistory(cmd);
      call('changed', [elem]);
    },
    getCurrentZoom,
    getId,
    getNextId,
    getMouseTarget,
    getCurrentMode () {
      return currentMode;
    },
    setCurrentMode (cm) {
      currentMode = cm;
      return currentMode;
    },
    getDrawnPath () {
      return drawnPath;
    },
    setDrawnPath (dp) {
      drawnPath = dp;
      return drawnPath;
    },
    getSVGRoot
  }
);
 
// Interface strings, usually for title elements
const uiStrings = {};
 
const visElems = 'a,circle,ellipse,foreignObject,g,image,line,path,polygon,polyline,rect,svg,text,tspan,use';
const refAttrs = ['clip-path', 'fill', 'filter', 'marker-end', 'marker-mid', 'marker-start', 'mask', 'stroke'];
 
const elData = $.data;
 
// Animation element to change the opacity of any newly created element
const opacAni = document.createElementNS(NS.SVG, 'animate');
$(opacAni).attr({
  attributeName: 'opacity',
  begin: 'indefinite',
  dur: 1,
  fill: 'freeze'
}).appendTo(svgroot);
 
const restoreRefElems = function (elem) {
  // Look for missing reference elements, restore any found
  const attrs = $(elem).attr(refAttrs);
  for (const o in attrs) {
    const val = attrs[o];
    if (val && val.startsWith('url(')) {
      const id = getUrlFromAttr(val).substr(1);
      const ref = getElem(id);
      if (!ref) {
        findDefs().append(removedElements[id]);
        delete removedElements[id];
      }
    }
  }
 
  const childs = elem.getElementsByTagName('*');
 
  if (childs.length) {
    for (let i = 0, l = childs.length; i < l; i++) {
      restoreRefElems(childs[i]);
    }
  }
};
 
// (function () {
// TODO For Issue 208: this is a start on a thumbnail
//  const svgthumb = svgdoc.createElementNS(NS.SVG, 'use');
//  svgthumb.setAttribute('width', '100');
//  svgthumb.setAttribute('height', '100');
//  setHref(svgthumb, '#svgcontent');
//  svgroot.append(svgthumb);
// }());
 
/**
 * @typedef {PlainObject} module:svgcanvas.SaveOptions
 * @property {boolean} apply
 * @property {"embed"} [image]
 * @property {Integer} round_digits
 */
 
// Object to contain image data for raster images that were found encodable
const encodableImages = {},
 
  // Object with save options
  /**
   * @type {module:svgcanvas.SaveOptions}
   */
  saveOptions = {round_digits: 5},
 
  // Object with IDs for imported files, to see if one was already added
  importIds = {},
 
  // Current text style properties
  curText = allProperties.text,
 
  // Object to contain all included extensions
  extensions = {},
 
  // Map of deleted reference elements
  removedElements = {};
 
let
  // String with image URL of last loadable image
  lastGoodImgUrl = curConfig.imgPath + 'logo.png',
 
  // Boolean indicating whether or not a draw action has been started
  started = false,
 
  // String with an element's initial transform attribute value
  startTransform = null,
 
  // String indicating the current editor mode
  currentMode = 'select',
 
  // String with the current direction in which an element is being resized
  currentResizeMode = 'none',
 
  // Current general properties
  curProperties = curShape,
 
  // Array with selected elements' Bounding box object
  // selectedBBoxes = new Array(1),
 
  // The DOM element that was just selected
  justSelected = null,
 
  // DOM element for selection rectangle drawn by the user
  rubberBox = null,
 
  // Array of current BBoxes, used in getIntersectionList().
  curBBoxes = [],
 
  // Canvas point for the most recent right click
  lastClickPoint = null;
 
/**
* @typedef {module:svgcanvas.ExtensionMouseDownStatus|module:svgcanvas.ExtensionMouseUpStatus|module:svgcanvas.ExtensionIDsUpdatedStatus|module:locale.ExtensionLocaleData[]|undefined} module:svgcanvas.ExtensionStatus
* @tutorial ExtensionDocs
*/
/**
* @callback module:svgcanvas.ExtensionVarBuilder
* @param {string} name The name of the extension
*/
/**
* @todo Consider: Should this return an array by default, so extension results aren't overwritten?
* @todo Would be easier to document if passing in object with key of action and vars as value; could then define an interface which tied both together
* @function module:svgcanvas.SvgCanvas#runExtensions
* @param {"mouseDown"|"mouseMove"|"mouseUp"|"zoomChanged"|"IDsUpdated"|"canvasUpdated"|"toolButtonStateUpdate"|"selectedChanged"|"elementTransition"|"elementChanged"|"langReady"|"langChanged"|"addLangData"|"onNewDocument"|"workareaResized"} action
* @param {module:svgcanvas.SvgCanvas#event:ext-mouseDown|module:svgcanvas.SvgCanvas#event:ext-mouseMove|module:svgcanvas.SvgCanvas#event:ext-mouseUp|module:svgcanvas.SvgCanvas#event:ext-zoomChanged|module:svgcanvas.SvgCanvas#event:ext-IDsUpdated|module:svgcanvas.SvgCanvas#event:ext-canvasUpdated|module:svgcanvas.SvgCanvas#event:ext-toolButtonStateUpdate|module:svgcanvas.SvgCanvas#event:ext-selectedChanged|module:svgcanvas.SvgCanvas#event:ext-elementTransition|module:svgcanvas.SvgCanvas#event:ext-elementChanged|module:svgcanvas.SvgCanvas#event:ext-langReady|module:svgcanvas.SvgCanvas#event:ext-langChanged|module:svgcanvas.SvgCanvas#event:ext-addLangData|module:svgcanvas.SvgCanvas#event:ext-onNewDocument|module:svgcanvas.SvgCanvas#event:ext-workareaResized|module:svgcanvas.ExtensionVarBuilder} [vars]
* @param {boolean} [returnArray]
* @returns {GenericArray.<module:svgcanvas.ExtensionStatus>|module:svgcanvas.ExtensionStatus|false} See {@tutorial ExtensionDocs} on the ExtensionStatus.
*/
const runExtensions = this.runExtensions = function (action, vars, returnArray) {
  let result = returnArray ? [] : false;
  $.each(extensions, function (name, ext) {
    if (ext && action in ext) {
      if (typeof vars === 'function') {
        vars = vars(name); // ext, action
      }
      if (returnArray) {
        result.push(ext[action](vars));
      } else {
        result = ext[action](vars);
      }
    }
  });
  return result;
};
 
/**
* @typedef {PlainObject} module:svgcanvas.ExtensionMouseDownStatus
* @property {boolean} started Indicates that creating/editing has started
*/
/**
* @typedef {PlainObject} module:svgcanvas.ExtensionMouseUpStatus
* @property {boolean} keep Indicates if the current element should be kept
* @property {boolean} started Indicates if editing should still be considered as "started"
* @property {Element} element The element being affected
*/
/**
* @typedef {PlainObject} module:svgcanvas.ExtensionIDsUpdatedStatus
* @property {string[]} remove Contains string IDs (used by `ext-connector.js`)
*/
 
/**
 * @interface module:svgcanvas.ExtensionInitResponse
 * @property {module:SVGEditor.ContextTool[]|PlainObject.<string, module:SVGEditor.ContextTool>} [context_tools]
 * @property {module:SVGEditor.Button[]|PlainObject.<Integer, module:SVGEditor.Button>} [buttons]
 * @property {string} [svgicons] The location of a local SVG or SVGz file
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#mouseDown
 * @param {module:svgcanvas.SvgCanvas#event:ext-mouseDown} arg
 * @returns {undefined|module:svgcanvas.ExtensionMouseDownStatus}
 */
/**
 * @function module:svgcanvas.ExtensionInitResponse#mouseMove
 * @param {module:svgcanvas.SvgCanvas#event:ext-mouseMove} arg
 * @returns {undefined}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#mouseUp
 * @param {module:svgcanvas.SvgCanvas#event:ext-mouseUp} arg
 * @returns {module:svgcanvas.ExtensionMouseUpStatus}
 */
/**
 * @function module:svgcanvas.ExtensionInitResponse#zoomChanged
 * @param {module:svgcanvas.SvgCanvas#event:ext-zoomChanged} arg
 * @returns {undefined}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#IDsUpdated
 * @param {module:svgcanvas.SvgCanvas#event:ext-IDsUpdated} arg
 * @returns {module:svgcanvas.ExtensionIDsUpdatedStatus}
 */
/**
 * @function module:svgcanvas.ExtensionInitResponse#canvasUpdated
 * @param {module:svgcanvas.SvgCanvas#event:ext-canvasUpdated} arg
 * @returns {undefined}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#toolButtonStateUpdate
 * @param {module:svgcanvas.SvgCanvas#event:ext-toolButtonStateUpdate} arg
 * @returns {undefined}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#selectedChanged
 * @param {module:svgcanvas.SvgCanvas#event:ext-selectedChanged} arg
 * @returns {undefined}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#elementTransition
 * @param {module:svgcanvas.SvgCanvas#event:ext-elementTransition} arg
 * @returns {undefined}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#elementChanged
 * @param {module:svgcanvas.SvgCanvas#event:ext-elementChanged} arg
 * @returns {undefined}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#langReady
 * @param {module:svgcanvas.SvgCanvas#event:ext-langReady} arg
 * @returns {undefined}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#langChanged
 * @param {module:svgcanvas.SvgCanvas#event:ext-langChanged} arg
 * @returns {undefined}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#addLangData
 * @param {module:svgcanvas.SvgCanvas#event:ext-addLangData} arg
 * @returns {Promise} Resolves to {@link module:locale.ExtensionLocaleData}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#onNewDocument
 * @param {module:svgcanvas.SvgCanvas#event:ext-onNewDocument} arg
 * @returns {undefined}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#workareaResized
 * @param {module:svgcanvas.SvgCanvas#event:ext-workareaResized} arg
 * @returns {undefined}
*/
/**
 * @function module:svgcanvas.ExtensionInitResponse#callback
 * @this module:SVGEditor
 * @param {module:svgcanvas.SvgCanvas#event:ext-callback} arg
 * @returns {undefined}
*/
 
/**
* @callback module:svgcanvas.ExtensionInitCallback
* @this module:SVGEditor
* @param {module:svgcanvas.ExtensionArgumentObject} arg
* @returns {Promise} Resolves to [ExtensionInitResponse]{@link module:svgcanvas.ExtensionInitResponse} or `undefined`
*/
/**
* Add an extension to the editor.
* @function module:svgcanvas.SvgCanvas#addExtension
* @param {string} name - String with the ID of the extension. Used internally; no need for i18n.
* @param {module:svgcanvas.ExtensionInitCallback} [extInitFunc] - Function supplied by the extension with its data
* @param {module:SVGEditor~ImportLocale} importLocale
* @fires module:svgcanvas.SvgCanvas#event:extension_added
* @throws {TypeError} If `extInitFunc` is not a function
* @returns {Promise} Resolves to `undefined`
*/
this.addExtension = async function (name, extInitFunc, importLocale) {
  if (typeof extInitFunc !== 'function') {
    throw new TypeError('Function argument expected for `svgcanvas.addExtension`');
  }
  if (!(name in extensions)) {
    // Provide private vars/funcs here. Is there a better way to do this?
    /**
     * @typedef {module:svgcanvas.PrivateMethods} module:svgcanvas.ExtensionArgumentObject
     * @property {SVGSVGElement} svgroot See {@link module:svgcanvas~svgroot}
     * @property {SVGSVGElement} svgcontent See {@link module:svgcanvas~svgcontent}
     * @property {!(string|Integer)} nonce See {@link module:draw.Drawing#getNonce}
     * @property {module:select.SelectorManager} selectorManager
     * @property {module:SVGEditor~ImportLocale} importLocale
     */
    /**
     * @type {module:svgcanvas.ExtensionArgumentObject}
     * @see {@link module:svgcanvas.PrivateMethods} source for the other methods/properties
     */
    const argObj = $.extend(canvas.getPrivateMethods(), {
      importLocale,
      svgroot,
      svgcontent,
      nonce: getCurrentDrawing().getNonce(),
      selectorManager
    });
    const extObj = await extInitFunc(argObj);
    if (extObj) {
      extObj.name = name;
    }
 
    extensions[name] = extObj;
    return call('extension_added', extObj);
  } else {
    console.log('Cannot add extension "' + name + '", an extension by that name already exists.');
  }
};
 
/**
* This method sends back an array or a NodeList full of elements that
* intersect the multi-select rubber-band-box on the currentLayer only.
*
* We brute-force `getIntersectionList` for browsers that do not support it (Firefox).
*
* Reference:
* Firefox does not implement `getIntersectionList()`, see {@link https://bugzilla.mozilla.org/show_bug.cgi?id=501421}.
* @function module:svgcanvas.SvgCanvas#getIntersectionList
* @param {SVGRect} rect
* @returns {Element[]|NodeList} Bbox elements
*/
const getIntersectionList = this.getIntersectionList = function (rect) {
  if (rubberBox == null) { return null; }
 
  const parent = currentGroup || getCurrentDrawing().getCurrentLayer();
 
  let rubberBBox;
  if (!rect) {
    rubberBBox = rubberBox.getBBox();
    const bb = svgcontent.createSVGRect();
 
    for (const o in rubberBBox) {
      bb[o] = rubberBBox[o] / currentZoom;
    }
    rubberBBox = bb;
  } else {
    rubberBBox = svgcontent.createSVGRect();
    rubberBBox.x = rect.x;
    rubberBBox.y = rect.y;
    rubberBBox.width = rect.width;
    rubberBBox.height = rect.height;
  }
 
  let resultList = null;
  if (!isIE) {
    if (typeof svgroot.getIntersectionList === 'function') {
      // Offset the bbox of the rubber box by the offset of the svgcontent element.
      rubberBBox.x += parseInt(svgcontent.getAttribute('x'), 10);
      rubberBBox.y += parseInt(svgcontent.getAttribute('y'), 10);
 
      resultList = svgroot.getIntersectionList(rubberBBox, parent);
    }
  }
 
  if (resultList == null || typeof resultList.item !== 'function') {
    resultList = [];
 
    if (!curBBoxes.length) {
      // Cache all bboxes
      curBBoxes = getVisibleElementsAndBBoxes(parent);
    }
    let i = curBBoxes.length;
    while (i--) {
      if (!rubberBBox.width) { continue; }
      if (rectsIntersect(rubberBBox, curBBoxes[i].bbox)) {
        resultList.push(curBBoxes[i].elem);
      }
    }
  }
 
  // addToSelection expects an array, but it's ok to pass a NodeList
  // because using square-bracket notation is allowed:
  // https://www.w3.org/TR/DOM-Level-2-Core/ecma-script-binding.html
  return resultList;
};
 
this.getStrokedBBox = getStrokedBBoxDefaultVisible;
 
this.getVisibleElements = getVisibleElements;
 
/**
* @typedef {PlainObject} ElementAndBBox
* @property {Element} elem - The element
* @property {module:utilities.BBoxObject} bbox - The element's BBox as retrieved from `getStrokedBBoxDefaultVisible`
*/
 
/**
* Get all elements that have a BBox (excludes `<defs>`, `<title>`, etc).
* Note that 0-opacity, off-screen etc elements are still considered "visible"
* for this function.
* @function module:svgcanvas.SvgCanvas#getVisibleElementsAndBBoxes
* @param {Element} parent - The parent DOM element to search within
* @returns {ElementAndBBox[]} An array with objects that include:
*/
const getVisibleElementsAndBBoxes = this.getVisibleElementsAndBBoxes = function (parent) {
  if (!parent) {
    parent = $(svgcontent).children(); // Prevent layers from being included
  }
  const contentElems = [];
  $(parent).children().each(function (i, elem) {
    if (elem.getBBox) {
      contentElems.push({elem, bbox: getStrokedBBoxDefaultVisible([elem])});
    }
  });
  return contentElems.reverse();
};
 
/**
* Wrap an SVG element into a group element, mark the group as 'gsvg'.
* @function module:svgcanvas.SvgCanvas#groupSvgElem
* @param {Element} elem - SVG element to wrap
* @returns {undefined}
*/
const groupSvgElem = this.groupSvgElem = function (elem) {
  const g = document.createElementNS(NS.SVG, 'g');
  elem.replaceWith(g);
  $(g).append(elem).data('gsvg', elem)[0].id = getNextId();
};
 
// Set scope for these functions
 
// Object to contain editor event names and callback functions
const events = {};
 
canvas.call = call;
/**
 * Array of what was changed (elements, layers)
 * @event module:svgcanvas.SvgCanvas#event:changed
 * @type {Element[]}
 */
/**
 * Array of selected elements
 * @event module:svgcanvas.SvgCanvas#event:selected
 * @type {Element[]}
 */
/**
 * Array of selected elements
 * @event module:svgcanvas.SvgCanvas#event:transition
 * @type {Element[]}
 */
/**
 * The Element is always `SVGGElement`?
 * If not `null`, will be the set current group element
 * @event module:svgcanvas.SvgCanvas#event:contextset
 * @type {null|Element}
 */
/**
 * @event module:svgcanvas.SvgCanvas#event:pointsAdded
 * @type {PlainObject}
 * @property {boolean} closedSubpath
 * @property {SVGCircleElement[]} grips Grips elements
 */
 
/**
 * @event module:svgcanvas.SvgCanvas#event:zoomed
 * @type {PlainObject}
 * @property {Float} x
 * @property {Float} y
 * @property {Float} width
 * @property {Float} height
 * @property {0.5|2} factor
 * @see module:SVGEditor.BBoxObjectWithFactor
 */
/**
 * @event module:svgcanvas.SvgCanvas#event:updateCanvas
 * @type {PlainObject}
 * @property {false} center
 * @property {module:math.XYObject} newCtr
 */
/**
 * @typedef {PlainObject} module:svgcanvas.ExtensionInitResponsePlusName
 * @implements {module:svgcanvas.ExtensionInitResponse}
 * @property {string} name The extension's resolved ID (whether explicit or based on file name)
 */
/**
 * Generalized extension object response of
 * [`init()`]{@link module:svgcanvas.ExtensionInitCallback}
 * along with the name of the extension.
 * @event module:svgcanvas.SvgCanvas#event:extension_added
 * @type {module:svgcanvas.ExtensionInitResponsePlusName|undefined}
 */
/**
 * @event module:svgcanvas.SvgCanvas#event:extensions_added
 * @type {undefined}
*/
/**
 * @typedef {PlainObject} module:svgcanvas.Message
 * @property {Any} data The data
 * @property {string} origin The origin
 */
/**
 * @event module:svgcanvas.SvgCanvas#event:message
 * @type {module:svgcanvas.Message}
 */
/**
 * SVG canvas converted to string
 * @event module:svgcanvas.SvgCanvas#event:saved
 * @type {string}
 */
/**
 * @event module:svgcanvas.SvgCanvas#event:setnonce
 * @type {!(string|Integer)}
 */
/**
 * @event module:svgcanvas.SvgCanvas#event:unsetnonce
 * @type {undefined}
 */
/**
 * @event module:svgcanvas.SvgCanvas#event:zoomDone
 * @type {undefined}
*/
/**
 * @event module:svgcanvas.SvgCanvas#event:cleared
 * @type {undefined}
*/
 
/**
 * @event module:svgcanvas.SvgCanvas#event:exported
 * @type {module:svgcanvas.ImageExportedResults}
 */
/**
 * @event module:svgcanvas.SvgCanvas#event:exportedPDF
 * @type {module:svgcanvas.PDFExportedResults}
 */
/**
 * Creating a cover-all class until {@link https://github.com/jsdoc3/jsdoc/issues/1545} may be supported.
 * `undefined` may be returned by {@link module:svgcanvas.SvgCanvas#event:extension_added} if the extension's `init` returns `undefined` It is also the type for the following events "zoomDone", "unsetnonce", "cleared", and "extensions_added".
 * @event module:svgcanvas.SvgCanvas#event:GenericCanvasEvent
 * @type {module:svgcanvas.SvgCanvas#event:selected|module:svgcanvas.SvgCanvas#event:changed|module:svgcanvas.SvgCanvas#event:contextset|module:svgcanvas.SvgCanvas#event:pointsAdded|module:svgcanvas.SvgCanvas#event:extension_added|module:svgcanvas.SvgCanvas#event:extensions_added|module:svgcanvas.SvgCanvas#event:message|module:svgcanvas.SvgCanvas#event:transition|module:svgcanvas.SvgCanvas#event:zoomed|module:svgcanvas.SvgCanvas#event:updateCanvas|module:svgcanvas.SvgCanvas#event:saved|module:svgcanvas.SvgCanvas#event:exported|module:svgcanvas.SvgCanvas#event:exportedPDF|module:svgcanvas.SvgCanvas#event:setnonce|module:svgcanvas.SvgCanvas#event:unsetnonce|undefined}
 */
 
/**
* @callback module:svgcanvas.EventHandler
* @param {external:Window} win
* @param {module:svgcanvas.SvgCanvas#event:GenericCanvasEvent} arg
* @listens module:svgcanvas.SvgCanvas#event:GenericCanvasEvent
*/
 
/**
* Attaches a callback function to an event.
* @function module:svgcanvas.SvgCanvas#bind
* @param {"changed"|"contextset"|"selected"|"pointsAdded"|"extension_added"|"extensions_added"|"message"|"transition"|"zoomed"|"updateCanvas"|"zoomDone"|"saved"|"exported"|"exportedPDF"|"setnonce"|"unsetnonce"|"cleared"} ev - String indicating the name of the event
* @param {module:svgcanvas.EventHandler} f - The callback function to bind to the event
* @returns {module:svgcanvas.EventHandler} The previous event
*/
canvas.bind = function (ev, f) {
  const old = events[ev];
  events[ev] = f;
  return old;
};
 
/**
* Runs the SVG Document through the sanitizer and then updates its paths.
* @function module:svgcanvas.SvgCanvas#prepareSvg
* @param {XMLDocument} newDoc - The SVG DOM document
* @returns {undefined}
*/
this.prepareSvg = function (newDoc) {
  this.sanitizeSvg(newDoc.documentElement);
 
  // convert paths into absolute commands
  const paths = [...newDoc.getElementsByTagNameNS(NS.SVG, 'path')];
  paths.forEach((path) => {
    path.setAttribute('d', pathActions.convertPath(path));
    pathActions.fixEnd(path);
  });
};
 
/**
* Hack for Firefox bugs where text element features aren't updated or get
* messed up. See issue 136 and issue 137.
* This function clones the element and re-selects it.
* @function module:svgcanvas~ffClone
* @todo Test for this bug on load and add it to "support" object instead of
* browser sniffing
* @param {Element} elem - The (text) DOM element to clone
* @returns {Element} Cloned element
*/
const ffClone = function (elem) {
  if (!isGecko()) { return elem; }
  const clone = elem.cloneNode(true);
  elem.before(clone);
  elem.remove();
  selectorManager.releaseSelector(elem);
  selectedElements[0] = clone;
  selectorManager.requestSelector(clone).showGrips(true);
  return clone;
};
 
// `this.each` is deprecated, if any extension used this it can be recreated by doing this:
// * @example $(canvas.getRootElem()).children().each(...)
// * @function module:svgcanvas.SvgCanvas#each
// this.each = function (cb) {
//  $(svgroot).children().each(cb);
// };
 
/**
* Removes any old rotations if present, prepends a new rotation at the
* transformed center.
* @function module:svgcanvas.SvgCanvas#setRotationAngle
* @param {string|Float} val - The new rotation angle in degrees
* @param {boolean} preventUndo - Indicates whether the action should be undoable or not
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {undefined}
*/
this.setRotationAngle = function (val, preventUndo) {
  // ensure val is the proper type
  val = parseFloat(val);
  const elem = selectedElements[0];
  const oldTransform = elem.getAttribute('transform');
  const bbox = utilsGetBBox(elem);
  const cx = bbox.x + bbox.width / 2, cy = bbox.y + bbox.height / 2;
  const tlist = getTransformList(elem);
 
  // only remove the real rotational transform if present (i.e. at index=0)
  if (tlist.numberOfItems > 0) {
    const xform = tlist.getItem(0);
    if (xform.type === 4) {
      tlist.removeItem(0);
    }
  }
  // find Rnc and insert it
  if (val !== 0) {
    const center = transformPoint(cx, cy, transformListToTransform(tlist).matrix);
    const Rnc = svgroot.createSVGTransform();
    Rnc.setRotate(val, center.x, center.y);
    if (tlist.numberOfItems) {
      tlist.insertItemBefore(Rnc, 0);
    } else {
      tlist.appendItem(Rnc);
    }
  } else if (tlist.numberOfItems === 0) {
    elem.removeAttribute('transform');
  }
 
  if (!preventUndo) {
    // we need to undo it, then redo it so it can be undo-able! :)
    // TODO: figure out how to make changes to transform list undo-able cross-browser?
    const newTransform = elem.getAttribute('transform');
    elem.setAttribute('transform', oldTransform);
    changeSelectedAttribute('transform', newTransform, selectedElements);
    call('changed', selectedElements);
  }
  // const pointGripContainer = getElem('pathpointgrip_container');
  // if (elem.nodeName === 'path' && pointGripContainer) {
  //   pathActions.setPointContainerTransform(elem.getAttribute('transform'));
  // }
  const selector = selectorManager.requestSelector(selectedElements[0]);
  selector.resize();
  selector.updateGripCursors(val);
};
 
/**
* Runs `recalculateDimensions` on the selected elements,
* adding the changes to a single batch command.
* @function module:svgcanvas.SvgCanvas#recalculateAllSelectedDimensions
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {undefined}
*/
const recalculateAllSelectedDimensions = this.recalculateAllSelectedDimensions = function () {
  const text = (currentResizeMode === 'none' ? 'position' : 'size');
  const batchCmd = new BatchCommand(text);
 
  let i = selectedElements.length;
  while (i--) {
    const elem = selectedElements[i];
    // if (getRotationAngle(elem) && !hasMatrixTransform(getTransformList(elem))) { continue; }
    const cmd = recalculateDimensions(elem);
    if (cmd) {
      batchCmd.addSubCommand(cmd);
    }
  }
 
  if (!batchCmd.isEmpty()) {
    addCommandToHistory(batchCmd);
    call('changed', selectedElements);
  }
};
 
/**
 * Debug tool to easily see the current matrix in the browser's console.
 * @function module:svgcanvas~logMatrix
 * @param {SVGMatrix} m The matrix
 * @returns {undefined}
 */
const logMatrix = function (m) {
  console.log([m.a, m.b, m.c, m.d, m.e, m.f]);
};
 
// Root Current Transformation Matrix in user units
let rootSctm = null;
 
/**
* Group: Selection
*/
 
// TODO: do we need to worry about selectedBBoxes here?
 
/**
* Selects only the given elements, shortcut for `clearSelection(); addToSelection()`.
* @function module:svgcanvas.SvgCanvas#selectOnly
* @param {Element[]} elems - an array of DOM elements to be selected
* @param {boolean} showGrips - Indicates whether the resize grips should be shown
* @returns {undefined}
*/
const selectOnly = this.selectOnly = function (elems, showGrips) {
  clearSelection(true);
  addToSelection(elems, showGrips);
};
 
// TODO: could use slice here to make this faster?
// TODO: should the 'selected' handler
 
/**
* Removes elements from the selection.
* @function module:svgcanvas.SvgCanvas#removeFromSelection
* @param {Element[]} elemsToRemove - An array of elements to remove from selection
* @returns {undefined}
*/
/* const removeFromSelection = */ this.removeFromSelection = function (elemsToRemove) {
  if (selectedElements[0] == null) { return; }
  if (!elemsToRemove.length) { return; }
 
  // find every element and remove it from our array copy
  const newSelectedItems = [],
    len = selectedElements.length;
  for (let i = 0; i < len; ++i) {
    const elem = selectedElements[i];
    if (elem) {
      // keep the item
      if (!elemsToRemove.includes(elem)) {
        newSelectedItems.push(elem);
      } else { // remove the item and its selector
        selectorManager.releaseSelector(elem);
      }
    }
  }
  // the copy becomes the master now
  selectedElements = newSelectedItems;
};
 
/**
* Clears the selection, then adds all elements in the current layer to the selection.
* @function module:svgcanvas.SvgCanvas#selectAllInCurrentLayer
* @returns {undefined}
*/
this.selectAllInCurrentLayer = function () {
  const currentLayer = getCurrentDrawing().getCurrentLayer();
  if (currentLayer) {
    currentMode = 'select';
    selectOnly($(currentGroup || currentLayer).children());
  }
};
 
let drawnPath = null;
 
// Mouse events
(function () {
const freehand = {
  minx: null,
  miny: null,
  maxx: null,
  maxy: null
};
const THRESHOLD_DIST = 0.8,
  STEP_COUNT = 10;
let dAttr = null,
  startX = null,
  startY = null,
  rStartX = null,
  rStartY = null,
  initBbox = {},
  sumDistance = 0,
  controllPoint2 = {x: 0, y: 0},
  controllPoint1 = {x: 0, y: 0},
  start = {x: 0, y: 0},
  end = {x: 0, y: 0},
  bSpline = {x: 0, y: 0},
  nextPos = {x: 0, y: 0},
  parameter,
  nextParameter;
 
const getBsplinePoint = function (t) {
  const spline = {x: 0, y: 0},
    p0 = controllPoint2,
    p1 = controllPoint1,
    p2 = start,
    p3 = end,
    S = 1.0 / 6.0,
    t2 = t * t,
    t3 = t2 * t;
 
  const m = [
    [-1, 3, -3, 1],
    [3, -6, 3, 0],
    [-3, 0, 3, 0],
    [1, 4, 1, 0]
  ];
 
  spline.x = S * (
    (p0.x * m[0][0] + p1.x * m[0][1] + p2.x * m[0][2] + p3.x * m[0][3]) * t3 +
      (p0.x * m[1][0] + p1.x * m[1][1] + p2.x * m[1][2] + p3.x * m[1][3]) * t2 +
      (p0.x * m[2][0] + p1.x * m[2][1] + p2.x * m[2][2] + p3.x * m[2][3]) * t +
      (p0.x * m[3][0] + p1.x * m[3][1] + p2.x * m[3][2] + p3.x * m[3][3])
  );
  spline.y = S * (
    (p0.y * m[0][0] + p1.y * m[0][1] + p2.y * m[0][2] + p3.y * m[0][3]) * t3 +
      (p0.y * m[1][0] + p1.y * m[1][1] + p2.y * m[1][2] + p3.y * m[1][3]) * t2 +
      (p0.y * m[2][0] + p1.y * m[2][1] + p2.y * m[2][2] + p3.y * m[2][3]) * t +
      (p0.y * m[3][0] + p1.y * m[3][1] + p2.y * m[3][2] + p3.y * m[3][3])
  );
 
  return {
    x: spline.x,
    y: spline.y
  };
};
/**
 * Follows these conditions:
 * - When we are in a create mode, the element is added to the canvas but the
 *   action is not recorded until mousing up.
 * - When we are in select mode, select the element, remember the position
 *   and do nothing else.
 * @param {MouseEvent} evt
 * @fires module:svgcanvas.SvgCanvas#event:ext-mouseDown
 * @returns {undefined}
 */
const mouseDown = function (evt) {
  if (canvas.spaceKey || evt.button === 1) { return; }
 
  const rightClick = evt.button === 2;
 
  if (evt.altKey) { // duplicate when dragging
    canvas.cloneSelectedElements(0, 0);
  }
 
  rootSctm = $('#svgcontent g')[0].getScreenCTM().inverse();
 
  const pt = transformPoint(evt.pageX, evt.pageY, rootSctm),
    mouseX = pt.x * currentZoom,
    mouseY = pt.y * currentZoom;
 
  evt.preventDefault();
 
  if (rightClick) {
    currentMode = 'select';
    lastClickPoint = pt;
  }
 
  // This would seem to be unnecessary...
  // if (!['select', 'resize'].includes(currentMode)) {
  //   setGradient();
  // }
 
  let x = mouseX / currentZoom,
    y = mouseY / currentZoom;
  let mouseTarget = getMouseTarget(evt);
 
  if (mouseTarget.tagName === 'a' && mouseTarget.childNodes.length === 1) {
    mouseTarget = mouseTarget.firstChild;
  }
 
  // realX/y ignores grid-snap value
  const realX = x;
  rStartX = startX = x;
  const realY = y;
  rStartY = startY = y;
 
  if (curConfig.gridSnapping) {
    x = snapToGrid(x);
    y = snapToGrid(y);
    startX = snapToGrid(startX);
    startY = snapToGrid(startY);
  }
 
  // if it is a selector grip, then it must be a single element selected,
  // set the mouseTarget to that and update the mode to rotate/resize
 
  if (mouseTarget === selectorManager.selectorParentGroup && selectedElements[0] != null) {
    const grip = evt.target;
    const griptype = elData(grip, 'type');
    // rotating
    if (griptype === 'rotate') {
      currentMode = 'rotate';
    // resizing
    } else if (griptype === 'resize') {
      currentMode = 'resize';
      currentResizeMode = elData(grip, 'dir');
    }
    mouseTarget = selectedElements[0];
  }
 
  startTransform = mouseTarget.getAttribute('transform');
  let i, strokeW;
  const tlist = getTransformList(mouseTarget);
  switch (currentMode) {
  case 'select':
    started = true;
    currentResizeMode = 'none';
    if (rightClick) { started = false; }
 
    if (mouseTarget !== svgroot) {
      // if this element is not yet selected, clear selection and select it
      if (!selectedElements.includes(mouseTarget)) {
        // only clear selection if shift is not pressed (otherwise, add
        // element to selection)
        if (!evt.shiftKey) {
          // No need to do the call here as it will be done on addToSelection
          clearSelection(true);
        }
        addToSelection([mouseTarget]);
        justSelected = mouseTarget;
        pathActions.clear();
      }
      // else if it's a path, go into pathedit mode in mouseup
 
      if (!rightClick) {
        // insert a dummy transform so if the element(s) are moved it will have
        // a transform to use for its translate
        for (i = 0; i < selectedElements.length; ++i) {
          if (selectedElements[i] == null) { continue; }
          const slist = getTransformList(selectedElements[i]);
          if (slist.numberOfItems) {
            slist.insertItemBefore(svgroot.createSVGTransform(), 0);
          } else {
            slist.appendItem(svgroot.createSVGTransform());
          }
        }
      }
    } else if (!rightClick) {
      clearSelection();
      currentMode = 'multiselect';
      if (rubberBox == null) {
        rubberBox = selectorManager.getRubberBandBox();
      }
      rStartX *= currentZoom;
      rStartY *= currentZoom;
      // console.log('p',[evt.pageX, evt.pageY]);
      // console.log('c',[evt.clientX, evt.clientY]);
      // console.log('o',[evt.offsetX, evt.offsetY]);
      // console.log('s',[startX, startY]);
 
      assignAttributes(rubberBox, {
        x: rStartX,
        y: rStartY,
        width: 0,
        height: 0,
        display: 'inline'
      }, 100);
    }
    break;
  case 'zoom':
    started = true;
    if (rubberBox == null) {
      rubberBox = selectorManager.getRubberBandBox();
    }
    assignAttributes(rubberBox, {
      x: realX * currentZoom,
      y: realX * currentZoom,
      width: 0,
      height: 0,
      display: 'inline'
    }, 100);
    break;
  case 'resize':
    started = true;
    startX = x;
    startY = y;
 
    // Getting the BBox from the selection box, since we know we
    // want to orient around it
    initBbox = utilsGetBBox($('#selectedBox0')[0]);
    const bb = {};
    $.each(initBbox, function (key, val) {
      bb[key] = val / currentZoom;
    });
    initBbox = bb;
 
    // append three dummy transforms to the tlist so that
    // we can translate,scale,translate in mousemove
    const pos = getRotationAngle(mouseTarget) ? 1 : 0;
 
    if (hasMatrixTransform(tlist)) {
      tlist.insertItemBefore(svgroot.createSVGTransform(), pos);
      tlist.insertItemBefore(svgroot.createSVGTransform(), pos);
      tlist.insertItemBefore(svgroot.createSVGTransform(), pos);
    } else {
      tlist.appendItem(svgroot.createSVGTransform());
      tlist.appendItem(svgroot.createSVGTransform());
      tlist.appendItem(svgroot.createSVGTransform());
 
      if (supportsNonScalingStroke()) {
        // Handle crash for newer Chrome and Safari 6 (Mobile and Desktop):
        // https://code.google.com/p/svg-edit/issues/detail?id=904
        // Chromium issue: https://code.google.com/p/chromium/issues/detail?id=114625
        // TODO: Remove this workaround once vendor fixes the issue
        const iswebkit = isWebkit();
 
        let delayedStroke;
        if (iswebkit) {
          delayedStroke = function (ele) {
            const _stroke = ele.getAttribute('stroke');
            ele.removeAttribute('stroke');
            // Re-apply stroke after delay. Anything higher than 1 seems to cause flicker
            if (_stroke !== null) setTimeout(function () { ele.setAttribute('stroke', _stroke); }, 0);
          };
        }
        mouseTarget.style.vectorEffect = 'non-scaling-stroke';
        if (iswebkit) { delayedStroke(mouseTarget); }
 
        const all = mouseTarget.getElementsByTagName('*'),
          len = all.length;
        for (i = 0; i < len; i++) {
          if (!all[i].style) { // mathML
            continue;
          }
          all[i].style.vectorEffect = 'non-scaling-stroke';
          if (iswebkit) { delayedStroke(all[i]); }
        }
      }
    }
    break;
  case 'fhellipse':
  case 'fhrect':
  case 'fhpath':
    start.x = realX;
    start.y = realY;
    started = true;
    dAttr = realX + ',' + realY + ' ';
    // Commented out as doing nothing now:
    // strokeW = parseFloat(curShape.stroke_width) === 0 ? 1 : curShape.stroke_width;
    addSVGElementFromJson({
      element: 'polyline',
      curStyles: true,
      attr: {
        points: dAttr,
        id: getNextId(),
        fill: 'none',
        opacity: curShape.opacity / 2,
        'stroke-linecap': 'round',
        style: 'pointer-events:none'
      }
    });
    freehand.minx = realX;
    freehand.maxx = realX;
    freehand.miny = realY;
    freehand.maxy = realY;
    break;
  case 'image':
    started = true;
    const newImage = addSVGElementFromJson({
      element: 'image',
      attr: {
        x,
        y,
        width: 0,
        height: 0,
        id: getNextId(),
        opacity: curShape.opacity / 2,
        style: 'pointer-events:inherit'
      }
    });
    setHref(newImage, lastGoodImgUrl);
    preventClickDefault(newImage);
    break;
  case 'square':
    // FIXME: once we create the rect, we lose information that this was a square
    // (for resizing purposes this could be important)
    // Fallthrough
  case 'rect':
    started = true;
    startX = x;
    startY = y;
    addSVGElementFromJson({
      element: 'rect',
      curStyles: true,
      attr: {
        x,
        y,
        width: 0,
        height: 0,
        id: getNextId(),
        opacity: curShape.opacity / 2
      }
    });
    break;
  case 'line':
    started = true;
    strokeW = Number(curShape.stroke_width) === 0 ? 1 : curShape.stroke_width;
    addSVGElementFromJson({
      element: 'line',
      curStyles: true,
      attr: {
        x1: x,
        y1: y,
        x2: x,
        y2: y,
        id: getNextId(),
        stroke: curShape.stroke,
        'stroke-width': strokeW,
        'stroke-dasharray': curShape.stroke_dasharray,
        'stroke-linejoin': curShape.stroke_linejoin,
        'stroke-linecap': curShape.stroke_linecap,
        'stroke-opacity': curShape.stroke_opacity,
        fill: 'none',
        opacity: curShape.opacity / 2,
        style: 'pointer-events:none'
      }
    });
    break;
  case 'circle':
    started = true;
    addSVGElementFromJson({
      element: 'circle',
      curStyles: true,
      attr: {
        cx: x,
        cy: y,
        r: 0,
        id: getNextId(),
        opacity: curShape.opacity / 2
      }
    });
    break;
  case 'ellipse':
    started = true;
    addSVGElementFromJson({
      element: 'ellipse',
      curStyles: true,
      attr: {
        cx: x,
        cy: y,
        rx: 0,
        ry: 0,
        id: getNextId(),
        opacity: curShape.opacity / 2
      }
    });
    break;
  case 'text':
    started = true;
    /* const newText = */ addSVGElementFromJson({
      element: 'text',
      curStyles: true,
      attr: {
        x,
        y,
        id: getNextId(),
        fill: curText.fill,
        'stroke-width': curText.stroke_width,
        'font-size': curText.font_size,
        'font-family': curText.font_family,
        'text-anchor': 'middle',
        'xml:space': 'preserve',
        opacity: curShape.opacity
      }
    });
    // newText.textContent = 'text';
    break;
  case 'path':
    // Fall through
  case 'pathedit':
    startX *= currentZoom;
    startY *= currentZoom;
    pathActions.mouseDown(evt, mouseTarget, startX, startY);
    started = true;
    break;
  case 'textedit':
    startX *= currentZoom;
    startY *= currentZoom;
    textActions.mouseDown(evt, mouseTarget, startX, startY);
    started = true;
    break;
  case 'rotate':
    started = true;
    // we are starting an undoable change (a drag-rotation)
    canvas.undoMgr.beginUndoableChange('transform', selectedElements);
    break;
  default:
    // This could occur in an extension
    break;
  }
 
  /**
   * The main (left) mouse button is held down on the canvas area
   * @event module:svgcanvas.SvgCanvas#event:ext-mouseDown
   * @type {PlainObject}
   * @property {MouseEvent} event The event object
   * @property {Float} start_x x coordinate on canvas
   * @property {Float} start_y y coordinate on canvas
   * @property {Element[]} selectedElements An array of the selected Elements
  */
  const extResult = runExtensions('mouseDown', /** @type {module:svgcanvas.SvgCanvas#event:ext-mouseDown} */ {
    event: evt,
    start_x: startX,
    start_y: startY,
    selectedElements
  }, true);
 
  $.each(extResult, function (i, r) {
    if (r && r.started) {
      started = true;
    }
  });
};
 
// in this function we do not record any state changes yet (but we do update
// any elements that are still being created, moved or resized on the canvas)
/**
 *
 * @param {MouseEvent} evt
 * @fires module:svgcanvas.SvgCanvas#event:transition
 * @fires module:svgcanvas.SvgCanvas#event:ext-mouseMove
 * @returns {undefined}
 */
const mouseMove = function (evt) {
  if (!started) { return; }
  if (evt.button === 1 || canvas.spaceKey) { return; }
 
  let i, xya, c, cx, cy, dx, dy, len, angle, box,
    selected = selectedElements[0];
  const
    pt = transformPoint(evt.pageX, evt.pageY, rootSctm),
    mouseX = pt.x * currentZoom,
    mouseY = pt.y * currentZoom,
    shape = getElem(getId());
 
  let realX = mouseX / currentZoom;
  let x = realX;
  let realY = mouseY / currentZoom;
  let y = realY;
 
  if (curConfig.gridSnapping) {
    x = snapToGrid(x);
    y = snapToGrid(y);
  }
 
  evt.preventDefault();
  let tlist;
  switch (currentMode) {
  case 'select': {
    // we temporarily use a translate on the element(s) being dragged
    // this transform is removed upon mousing up and the element is
    // relocated to the new location
    if (selectedElements[0] !== null) {
      dx = x - startX;
      dy = y - startY;
 
      if (curConfig.gridSnapping) {
        dx = snapToGrid(dx);
        dy = snapToGrid(dy);
      }
 
      /*
      // Commenting out as currently has no effect
      if (evt.shiftKey) {
        xya = snapToAngle(startX, startY, x, y);
        ({x, y} = xya);
      }
      */
 
      if (dx !== 0 || dy !== 0) {
        len = selectedElements.length;
        for (i = 0; i < len; ++i) {
          selected = selectedElements[i];
          if (selected == null) { break; }
          // if (i === 0) {
          //   const box = utilsGetBBox(selected);
          //     selectedBBoxes[i].x = box.x + dx;
          //     selectedBBoxes[i].y = box.y + dy;
          // }
 
          // update the dummy transform in our transform list
          // to be a translate
          const xform = svgroot.createSVGTransform();
          tlist = getTransformList(selected);
          // Note that if Webkit and there's no ID for this
          // element, the dummy transform may have gotten lost.
          // This results in unexpected behaviour
 
          xform.setTranslate(dx, dy);
          if (tlist.numberOfItems) {
            tlist.replaceItem(xform, 0);
          } else {
            tlist.appendItem(xform);
          }
 
          // update our internal bbox that we're tracking while dragging
          selectorManager.requestSelector(selected).resize();
        }
 
        call('transition', selectedElements);
      }
    }
    break;
  } case 'multiselect': {
    realX *= currentZoom;
    realY *= currentZoom;
    assignAttributes(rubberBox, {
      x: Math.min(rStartX, realX),
      y: Math.min(rStartY, realY),
      width: Math.abs(realX - rStartX),
      height: Math.abs(realY - rStartY)
    }, 100);
 
    // for each selected:
    // - if newList contains selected, do nothing
    // - if newList doesn't contain selected, remove it from selected
    // - for any newList that was not in selectedElements, add it to selected
    const elemsToRemove = selectedElements.slice(), elemsToAdd = [],
      newList = getIntersectionList();
 
    // For every element in the intersection, add if not present in selectedElements.
    len = newList.length;
    for (i = 0; i < len; ++i) {
      const intElem = newList[i];
      // Found an element that was not selected before, so we should add it.
      if (!selectedElements.includes(intElem)) {
        elemsToAdd.push(intElem);
      }
      // Found an element that was already selected, so we shouldn't remove it.
      const foundInd = elemsToRemove.indexOf(intElem);
      if (foundInd !== -1) {
        elemsToRemove.splice(foundInd, 1);
      }
    }
 
    if (elemsToRemove.length > 0) {
      canvas.removeFromSelection(elemsToRemove);
    }
 
    if (elemsToAdd.length > 0) {
      canvas.addToSelection(elemsToAdd);
    }
 
    break;
  } case 'resize': {
    // we track the resize bounding box and translate/scale the selected element
    // while the mouse is down, when mouse goes up, we use this to recalculate
    // the shape's coordinates
    tlist = getTransformList(selected);
    const hasMatrix = hasMatrixTransform(tlist);
    box = hasMatrix ? initBbox : utilsGetBBox(selected);
    let left = box.x,
      top = box.y,
      {width, height} = box;
    dx = (x - startX);
    dy = (y - startY);
 
    if (curConfig.gridSnapping) {
      dx = snapToGrid(dx);
      dy = snapToGrid(dy);
      height = snapToGrid(height);
      width = snapToGrid(width);
    }
 
    // if rotated, adjust the dx,dy values
    angle = getRotationAngle(selected);
    if (angle) {
      const r = Math.sqrt(dx * dx + dy * dy),
        theta = Math.atan2(dy, dx) - angle * Math.PI / 180.0;
      dx = r * Math.cos(theta);
      dy = r * Math.sin(theta);
    }
 
    // if not stretching in y direction, set dy to 0
    // if not stretching in x direction, set dx to 0
    if (!currentResizeMode.includes('n') && !currentResizeMode.includes('s')) {
      dy = 0;
    }
    if (!currentResizeMode.includes('e') && !currentResizeMode.includes('w')) {
      dx = 0;
    }
 
    let // ts = null,
      tx = 0, ty = 0,
      sy = height ? (height + dy) / height : 1,
      sx = width ? (width + dx) / width : 1;
    // if we are dragging on the north side, then adjust the scale factor and ty
    if (currentResizeMode.includes('n')) {
      sy = height ? (height - dy) / height : 1;
      ty = height;
    }
 
    // if we dragging on the east side, then adjust the scale factor and tx
    if (currentResizeMode.includes('w')) {
      sx = width ? (width - dx) / width : 1;
      tx = width;
    }
 
    // update the transform list with translate,scale,translate
    const translateOrigin = svgroot.createSVGTransform(),
      scale = svgroot.createSVGTransform(),
      translateBack = svgroot.createSVGTransform();
 
    if (curConfig.gridSnapping) {
      left = snapToGrid(left);
      tx = snapToGrid(tx);
      top = snapToGrid(top);
      ty = snapToGrid(ty);
    }
 
    translateOrigin.setTranslate(-(left + tx), -(top + ty));
    if (evt.shiftKey) {
      if (sx === 1) {
        sx = sy;
      } else { sy = sx; }
    }
    scale.setScale(sx, sy);
 
    translateBack.setTranslate(left + tx, top + ty);
    if (hasMatrix) {
      const diff = angle ? 1 : 0;
      tlist.replaceItem(translateOrigin, 2 + diff);
      tlist.replaceItem(scale, 1 + diff);
      tlist.replaceItem(translateBack, Number(diff));
    } else {
      const N = tlist.numberOfItems;
      tlist.replaceItem(translateBack, N - 3);
      tlist.replaceItem(scale, N - 2);
      tlist.replaceItem(translateOrigin, N - 1);
    }
 
    selectorManager.requestSelector(selected).resize();
 
    call('transition', selectedElements);
 
    break;
  } case 'zoom': {
    realX *= currentZoom;
    realY *= currentZoom;
    assignAttributes(rubberBox, {
      x: Math.min(rStartX * currentZoom, realX),
      y: Math.min(rStartY * currentZoom, realY),
      width: Math.abs(realX - rStartX * currentZoom),
      height: Math.abs(realY - rStartY * currentZoom)
    }, 100);
    break;
  } case 'text': {
    assignAttributes(shape, {
      x,
      y
    }, 1000);
    break;
  } case 'line': {
    if (curConfig.gridSnapping) {
      x = snapToGrid(x);
      y = snapToGrid(y);
    }
 
    let x2 = x;
    let y2 = y;
 
    if (evt.shiftKey) {
      xya = snapToAngle(startX, startY, x2, y2);
      x2 = xya.x;
      y2 = xya.y;
    }
 
    shape.setAttribute('x2', x2);
    shape.setAttribute('y2', y2);
    break;
  } case 'foreignObject':
    // fall through
  case 'square':
    // fall through
  case 'rect':
    // fall through
  case 'image': {
    const square = (currentMode === 'square') || evt.shiftKey;
    let
      w = Math.abs(x - startX),
      h = Math.abs(y - startY);
    let newX, newY;
    if (square) {
      w = h = Math.max(w, h);
      newX = startX < x ? startX : startX - w;
      newY = startY < y ? startY : startY - h;
    } else {
      newX = Math.min(startX, x);
      newY = Math.min(startY, y);
    }
 
    if (curConfig.gridSnapping) {
      w = snapToGrid(w);
      h = snapToGrid(h);
      newX = snapToGrid(newX);
      newY = snapToGrid(newY);
    }
 
    assignAttributes(shape, {
      width: w,
      height: h,
      x: newX,
      y: newY
    }, 1000);
 
    break;
  } case 'circle': {
    c = $(shape).attr(['cx', 'cy']);
    ({cx, cy} = c);
    let rad = Math.sqrt((x - cx) * (x - cx) + (y - cy) * (y - cy));
    if (curConfig.gridSnapping) {
      rad = snapToGrid(rad);
    }
    shape.setAttribute('r', rad);
    break;
  } case 'ellipse': {
    c = $(shape).attr(['cx', 'cy']);
    ({cx, cy} = c);
    if (curConfig.gridSnapping) {
      x = snapToGrid(x);
      cx = snapToGrid(cx);
      y = snapToGrid(y);
      cy = snapToGrid(cy);
    }
    shape.setAttribute('rx', Math.abs(x - cx));
    const ry = Math.abs(evt.shiftKey ? (x - cx) : (y - cy));
    shape.setAttribute('ry', ry);
    break;
  }
  case 'fhellipse':
  case 'fhrect': {
    freehand.minx = Math.min(realX, freehand.minx);
    freehand.maxx = Math.max(realX, freehand.maxx);
    freehand.miny = Math.min(realY, freehand.miny);
    freehand.maxy = Math.max(realY, freehand.maxy);
  }
  // Fallthrough
  case 'fhpath': {
    // dAttr += + realX + ',' + realY + ' ';
    // shape.setAttribute('points', dAttr);
    end.x = realX; end.y = realY;
    if (controllPoint2.x && controllPoint2.y) {
      for (i = 0; i < STEP_COUNT - 1; i++) {
        parameter = i / STEP_COUNT;
        nextParameter = (i + 1) / STEP_COUNT;
        bSpline = getBsplinePoint(nextParameter);
        nextPos = bSpline;
        bSpline = getBsplinePoint(parameter);
        sumDistance += Math.sqrt((nextPos.x - bSpline.x) * (nextPos.x - bSpline.x) + (nextPos.y - bSpline.y) * (nextPos.y - bSpline.y));
        if (sumDistance > THRESHOLD_DIST) {
          sumDistance -= THRESHOLD_DIST;
 
          // Faster than completely re-writing the points attribute.
          const point = svgcontent.createSVGPoint();
          point.x = bSpline.x;
          point.y = bSpline.y;
          shape.points.appendItem(point);
        }
      }
    }
    controllPoint2 = {x: controllPoint1.x, y: controllPoint1.y};
    controllPoint1 = {x: start.x, y: start.y};
    start = {x: end.x, y: end.y};
    break;
  // update path stretch line coordinates
  } case 'path': {
  }
  // fall through
  case 'pathedit': {
    x *= currentZoom;
    y *= currentZoom;
 
    if (curConfig.gridSnapping) {
      x = snapToGrid(x);
      y = snapToGrid(y);
      startX = snapToGrid(startX);
      startY = snapToGrid(startY);
    }
    if (evt.shiftKey) {
      const {path} = pathModule;
      let x1, y1;
      if (path) {
        x1 = path.dragging ? path.dragging[0] : startX;
        y1 = path.dragging ? path.dragging[1] : startY;
      } else {
        x1 = startX;
        y1 = startY;
      }
      xya = snapToAngle(x1, y1, x, y);
      ({x, y} = xya);
    }
 
    if (rubberBox && rubberBox.getAttribute('display') !== 'none') {
      realX *= currentZoom;
      realY *= currentZoom;
      assignAttributes(rubberBox, {
        x: Math.min(rStartX * currentZoom, realX),
        y: Math.min(rStartY * currentZoom, realY),
        width: Math.abs(realX - rStartX * currentZoom),
        height: Math.abs(realY - rStartY * currentZoom)
      }, 100);
    }
    pathActions.mouseMove(x, y);
 
    break;
  } case 'textedit': {
    x *= currentZoom;
    y *= currentZoom;
    // if (rubberBox && rubberBox.getAttribute('display') !== 'none') {
    //   assignAttributes(rubberBox, {
    //     x: Math.min(startX, x),
    //     y: Math.min(startY, y),
    //     width: Math.abs(x - startX),
    //     height: Math.abs(y - startY)
    //   }, 100);
    // }
 
    textActions.mouseMove(mouseX, mouseY);
 
    break;
  } case 'rotate': {
    box = utilsGetBBox(selected);
    cx = box.x + box.width / 2;
    cy = box.y + box.height / 2;
    const m = getMatrix(selected),
      center = transformPoint(cx, cy, m);
    cx = center.x;
    cy = center.y;
    angle = ((Math.atan2(cy - y, cx - x) * (180 / Math.PI)) - 90) % 360;
    if (curConfig.gridSnapping) {
      angle = snapToGrid(angle);
    }
    if (evt.shiftKey) { // restrict rotations to nice angles (WRS)
      const snap = 45;
      angle = Math.round(angle / snap) * snap;
    }
 
    canvas.setRotationAngle(angle < -180 ? (360 + angle) : angle, true);
    call('transition', selectedElements);
    break;
  } default:
    break;
  }
 
  /**
  * The mouse has moved on the canvas area
  * @event module:svgcanvas.SvgCanvas#event:ext-mouseMove
  * @type {PlainObject}
  * @property {MouseEvent} event The event object
  * @property {Float} mouse_x x coordinate on canvas
  * @property {Float} mouse_y y coordinate on canvas
  * @property {Element} selected Refers to the first selected element
  */
  runExtensions('mouseMove', /** @type {module:svgcanvas.SvgCanvas#event:ext-mouseMove} */ {
    event: evt,
    mouse_x: mouseX,
    mouse_y: mouseY,
    selected
  });
}; // mouseMove()
 
// - in create mode, the element's opacity is set properly, we create an InsertElementCommand
// and store it on the Undo stack
// - in move/resize mode, the element's attributes which were affected by the move/resize are
// identified, a ChangeElementCommand is created and stored on the stack for those attrs
// this is done in when we recalculate the selected dimensions()
/**
 *
 * @param {MouseEvent} evt
 * @fires module:svgcanvas.SvgCanvas#event:zoomed
 * @fires module:svgcanvas.SvgCanvas#event:changed
 * @fires module:svgcanvas.SvgCanvas#event:ext-mouseUp
 * @returns {undefined}
 */
const mouseUp = function (evt) {
  if (evt.button === 2) { return; }
  const tempJustSelected = justSelected;
  justSelected = null;
  if (!started) { return; }
  const pt = transformPoint(evt.pageX, evt.pageY, rootSctm),
    mouseX = pt.x * currentZoom,
    mouseY = pt.y * currentZoom,
    x = mouseX / currentZoom,
    y = mouseY / currentZoom;
 
  let element = getElem(getId());
  let keep = false;
 
  const realX = x;
  const realY = y;
 
  // TODO: Make true when in multi-unit mode
  const useUnit = false; // (curConfig.baseUnit !== 'px');
  started = false;
  let attrs, t;
  switch (currentMode) {
  // intentionally fall-through to select here
  case 'resize':
  case 'multiselect':
    if (rubberBox != null) {
      rubberBox.setAttribute('display', 'none');
      curBBoxes = [];
    }
    currentMode = 'select';
    // Fallthrough
  case 'select':
    if (selectedElements[0] != null) {
      // if we only have one selected element
      if (selectedElements[1] == null) {
        // set our current stroke/fill properties to the element's
        const selected = selectedElements[0];
        switch (selected.tagName) {
        case 'g':
        case 'use':
        case 'image':
        case 'foreignObject':
          break;
        default:
          curProperties.fill = selected.getAttribute('fill');
          curProperties.fill_opacity = selected.getAttribute('fill-opacity');
          curProperties.stroke = selected.getAttribute('stroke');
          curProperties.stroke_opacity = selected.getAttribute('stroke-opacity');
          curProperties.stroke_width = selected.getAttribute('stroke-width');
          curProperties.stroke_dasharray = selected.getAttribute('stroke-dasharray');
          curProperties.stroke_linejoin = selected.getAttribute('stroke-linejoin');
          curProperties.stroke_linecap = selected.getAttribute('stroke-linecap');
        }
 
        if (selected.tagName === 'text') {
          curText.font_size = selected.getAttribute('font-size');
          curText.font_family = selected.getAttribute('font-family');
        }
        selectorManager.requestSelector(selected).showGrips(true);
 
        // This shouldn't be necessary as it was done on mouseDown...
        // call('selected', [selected]);
      }
      // always recalculate dimensions to strip off stray identity transforms
      recalculateAllSelectedDimensions();
      // if it was being dragged/resized
      if (realX !== rStartX || realY !== rStartY) {
        const len = selectedElements.length;
        for (let i = 0; i < len; ++i) {
          if (selectedElements[i] == null) { break; }
          if (!selectedElements[i].firstChild) {
            // Not needed for groups (incorrectly resizes elems), possibly not needed at all?
            selectorManager.requestSelector(selectedElements[i]).resize();
          }
        }
      // no change in position/size, so maybe we should move to pathedit
      } else {
        t = evt.target;
        if (selectedElements[0].nodeName === 'path' && selectedElements[1] == null) {
          pathActions.select(selectedElements[0]);
        // if it was a path
        // else, if it was selected and this is a shift-click, remove it from selection
        } else if (evt.shiftKey) {
          if (tempJustSelected !== t) {
            canvas.removeFromSelection([t]);
          }
        }
      } // no change in mouse position
 
      // Remove non-scaling stroke
      if (supportsNonScalingStroke()) {
        const elem = selectedElements[0];
        if (elem) {
          elem.removeAttribute('style');
          walkTree(elem, function (elem) {
            elem.removeAttribute('style');
          });
        }
      }
    }
    return;
  case 'zoom':
    if (rubberBox != null) {
      rubberBox.setAttribute('display', 'none');
    }
    const factor = evt.shiftKey ? 0.5 : 2;
    call('zoomed', {
      x: Math.min(rStartX, realX),
      y: Math.min(rStartY, realY),
      width: Math.abs(realX - rStartX),
      height: Math.abs(realY - rStartY),
      factor
    });
    return;
  case 'fhpath':
    // Check that the path contains at least 2 points; a degenerate one-point path
    // causes problems.
    // Webkit ignores how we set the points attribute with commas and uses space
    // to separate all coordinates, see https://bugs.webkit.org/show_bug.cgi?id=29870
    sumDistance = 0;
    controllPoint2 = {x: 0, y: 0};
    controllPoint1 = {x: 0, y: 0};
    start = {x: 0, y: 0};
    end = {x: 0, y: 0};
    const coords = element.getAttribute('points');
    const commaIndex = coords.indexOf(',');
    if (commaIndex >= 0) {
      keep = coords.indexOf(',', commaIndex + 1) >= 0;
    } else {
      keep = coords.indexOf(' ', coords.indexOf(' ') + 1) >= 0;
    }
    if (keep) {
      element = pathActions.smoothPolylineIntoPath(element);
    }
    break;
  case 'line':
    attrs = $(element).attr(['x1', 'x2', 'y1', 'y2']);
    keep = (attrs.x1 !== attrs.x2 || attrs.y1 !== attrs.y2);
    break;
  case 'foreignObject':
  case 'square':
  case 'rect':
  case 'image':
    attrs = $(element).attr(['width', 'height']);
    // Image should be kept regardless of size (use inherit dimensions later)
    keep = (attrs.width || attrs.height) || currentMode === 'image';
    break;
  case 'circle':
    keep = (element.getAttribute('r') !== '0');
    break;
  case 'ellipse':
    attrs = $(element).attr(['rx', 'ry']);
    keep = (attrs.rx || attrs.ry);
    break;
  case 'fhellipse':
    if ((freehand.maxx - freehand.minx) > 0 &&
      (freehand.maxy - freehand.miny) > 0) {
      element = addSVGElementFromJson({
        element: 'ellipse',
        curStyles: true,
        attr: {
          cx: (freehand.minx + freehand.maxx) / 2,
          cy: (freehand.miny + freehand.maxy) / 2,
          rx: (freehand.maxx - freehand.minx) / 2,
          ry: (freehand.maxy - freehand.miny) / 2,
          id: getId()
        }
      });
      call('changed', [element]);
      keep = true;
    }
    break;
  case 'fhrect':
    if ((freehand.maxx - freehand.minx) > 0 &&
      (freehand.maxy - freehand.miny) > 0) {
      element = addSVGElementFromJson({
        element: 'rect',
        curStyles: true,
        attr: {
          x: freehand.minx,
          y: freehand.miny,
          width: (freehand.maxx - freehand.minx),
          height: (freehand.maxy - freehand.miny),
          id: getId()
        }
      });
      call('changed', [element]);
      keep = true;
    }
    break;
  case 'text':
    keep = true;
    selectOnly([element]);
    textActions.start(element);
    break;
  case 'path':
    // set element to null here so that it is not removed nor finalized
    element = null;
    // continue to be set to true so that mouseMove happens
    started = true;
 
    const res = pathActions.mouseUp(evt, element, mouseX, mouseY);
    ({element} = res);
    ({keep} = res);
    break;
  case 'pathedit':
    keep = true;
    element = null;
    pathActions.mouseUp(evt);
    break;
  case 'textedit':
    keep = false;
    element = null;
    textActions.mouseUp(evt, mouseX, mouseY);
    break;
  case 'rotate':
    keep = true;
    element = null;
    currentMode = 'select';
    const batchCmd = canvas.undoMgr.finishUndoableChange();
    if (!batchCmd.isEmpty()) {
      addCommandToHistory(batchCmd);
    }
    // perform recalculation to weed out any stray identity transforms that might get stuck
    recalculateAllSelectedDimensions();
    call('changed', selectedElements);
    break;
  default:
    // This could occur in an extension
    break;
  }
 
  /**
  * The main (left) mouse button is released (anywhere)
  * @event module:svgcanvas.SvgCanvas#event:ext-mouseUp
  * @type {PlainObject}
  * @property {MouseEvent} event The event object
  * @property {Float} mouse_x x coordinate on canvas
  * @property {Float} mouse_y y coordinate on canvas
  */
  const extResult = runExtensions('mouseUp', /** @type {module:svgcanvas.SvgCanvas#event:ext-mouseUp} */ {
    event: evt,
    mouse_x: mouseX,
    mouse_y: mouseY
  }, true);
 
  $.each(extResult, function (i, r) {
    if (r) {
      keep = r.keep || keep;
      ({element} = r);
      started = r.started || started;
    }
  });
 
  if (!keep && element != null) {
    getCurrentDrawing().releaseId(getId());
    element.remove();
    element = null;
 
    t = evt.target;
 
    // if this element is in a group, go up until we reach the top-level group
    // just below the layer groups
    // TODO: once we implement links, we also would have to check for <a> elements
    while (t && t.parentNode && t.parentNode.parentNode && t.parentNode.parentNode.tagName === 'g') {
      t = t.parentNode;
    }
    // if we are not in the middle of creating a path, and we've clicked on some shape,
    // then go to Select mode.
    // WebKit returns <div> when the canvas is clicked, Firefox/Opera return <svg>
    if ((currentMode !== 'path' || !drawnPath) &&
      t && t.parentNode &&
      t.parentNode.id !== 'selectorParentGroup' &&
      t.id !== 'svgcanvas' && t.id !== 'svgroot'
    ) {
      // switch into "select" mode if we've clicked on an element
      canvas.setMode('select');
      selectOnly([t], true);
    }
  } else if (element != null) {
    /**
    * @name module:svgcanvas.SvgCanvas#addedNew
    * @type {boolean}
    */
    canvas.addedNew = true;
 
    if (useUnit) { convertAttrs(element); }
 
    let aniDur = 0.2;
    let cAni;
    if (opacAni.beginElement && parseFloat(element.getAttribute('opacity')) !== curShape.opacity) {
      cAni = $(opacAni).clone().attr({
        to: curShape.opacity,
        dur: aniDur
      }).appendTo(element);
      try {
        // Fails in FF4 on foreignObject
        cAni[0].beginElement();
      } catch (e) {}
    } else {
      aniDur = 0;
    }
 
    // Ideally this would be done on the endEvent of the animation,
    // but that doesn't seem to be supported in Webkit
    setTimeout(function () {
      if (cAni) { cAni.remove(); }
      element.setAttribute('opacity', curShape.opacity);
      element.setAttribute('style', 'pointer-events:inherit');
      cleanupElement(element);
      if (currentMode === 'path') {
        pathActions.toEditMode(element);
      } else if (curConfig.selectNew) {
        selectOnly([element], true);
      }
      // we create the insert command that is stored on the stack
      // undo means to call cmd.unapply(), redo means to call cmd.apply()
      addCommandToHistory(new InsertElementCommand(element));
 
      call('changed', [element]);
    }, aniDur * 1000);
  }
 
  startTransform = null;
};
 
const dblClick = function (evt) {
  const evtTarget = evt.target;
  const parent = evtTarget.parentNode;
 
  // Do nothing if already in current group
  if (parent === currentGroup) { return; }
 
  let mouseTarget = getMouseTarget(evt);
  const {tagName} = mouseTarget;
 
  if (tagName === 'text' && currentMode !== 'textedit') {
    const pt = transformPoint(evt.pageX, evt.pageY, rootSctm);
    textActions.select(mouseTarget, pt.x, pt.y);
  }
 
  if ((tagName === 'g' || tagName === 'a') &&
    getRotationAngle(mouseTarget)
  ) {
    // TODO: Allow method of in-group editing without having to do
    // this (similar to editing rotated paths)
 
    // Ungroup and regroup
    pushGroupProperties(mouseTarget);
    mouseTarget = selectedElements[0];
    clearSelection(true);
  }
  // Reset context
  if (currentGroup) {
    draw.leaveContext();
  }
 
  if ((parent.tagName !== 'g' && parent.tagName !== 'a') ||
    parent === getCurrentDrawing().getCurrentLayer() ||
    mouseTarget === selectorManager.selectorParentGroup
  ) {
    // Escape from in-group edit
    return;
  }
  draw.setContext(mouseTarget);
};
 
// prevent links from being followed in the canvas
const handleLinkInCanvas = function (e) {
  e.preventDefault();
  return false;
};
 
// Added mouseup to the container here.
// TODO(codedread): Figure out why after the Closure compiler, the window mouseup is ignored.
$(container).mousedown(mouseDown).mousemove(mouseMove).click(handleLinkInCanvas).dblclick(dblClick).mouseup(mouseUp);
// $(window).mouseup(mouseUp);
 
// TODO(rafaelcastrocouto): User preference for shift key and zoom factor
$(container).bind(
  'mousewheel DOMMouseScroll',
  /**
   * @param {Event} e
   * @fires module:svgcanvas.SvgCanvas#event:updateCanvas
   * @fires module:svgcanvas.SvgCanvas#event:zoomDone
   * @returns {undefined}
   */
  function (e) {
    if (!e.shiftKey) { return; }
 
    e.preventDefault();
    const evt = e.originalEvent;
 
    rootSctm = $('#svgcontent g')[0].getScreenCTM().inverse();
 
    const workarea = $('#workarea');
    const scrbar = 15;
    const rulerwidth = curConfig.showRulers ? 16 : 0;
 
    // mouse relative to content area in content pixels
    const pt = transformPoint(evt.pageX, evt.pageY, rootSctm);
 
    // full work area width in screen pixels
    const editorFullW = workarea.width();
    const editorFullH = workarea.height();
 
    // work area width minus scroll and ruler in screen pixels
    const editorW = editorFullW - scrbar - rulerwidth;
    const editorH = editorFullH - scrbar - rulerwidth;
 
    // work area width in content pixels
    const workareaViewW = editorW * rootSctm.a;
    const workareaViewH = editorH * rootSctm.d;
 
    // content offset from canvas in screen pixels
    const wOffset = workarea.offset();
    const wOffsetLeft = wOffset['left'] + rulerwidth;
    const wOffsetTop = wOffset['top'] + rulerwidth;
 
    const delta = (evt.wheelDelta) ? evt.wheelDelta : (evt.detail) ? -evt.detail : 0;
    if (!delta) { return; }
 
    let factor = Math.max(3 / 4, Math.min(4 / 3, (delta)));
 
    let wZoom, hZoom;
    if (factor > 1) {
      wZoom = Math.ceil(editorW / workareaViewW * factor * 100) / 100;
      hZoom = Math.ceil(editorH / workareaViewH * factor * 100) / 100;
    } else {
      wZoom = Math.floor(editorW / workareaViewW * factor * 100) / 100;
      hZoom = Math.floor(editorH / workareaViewH * factor * 100) / 100;
    }
    let zoomlevel = Math.min(wZoom, hZoom);
    zoomlevel = Math.min(10, Math.max(0.01, zoomlevel));
    if (zoomlevel === currentZoom) {
      return;
    }
    factor = zoomlevel / currentZoom;
 
    // top left of workarea in content pixels before zoom
    const topLeftOld = transformPoint(wOffsetLeft, wOffsetTop, rootSctm);
 
    // top left of workarea in content pixels after zoom
    const topLeftNew = {
      x: pt.x - (pt.x - topLeftOld.x) / factor,
      y: pt.y - (pt.y - topLeftOld.y) / factor
    };
 
    // top left of workarea in canvas pixels relative to content after zoom
    const topLeftNewCanvas = {
      x: topLeftNew.x * zoomlevel,
      y: topLeftNew.y * zoomlevel
    };
 
    // new center in canvas pixels
    const newCtr = {
      x: topLeftNewCanvas.x - rulerwidth + editorFullW / 2,
      y: topLeftNewCanvas.y - rulerwidth + editorFullH / 2
    };
 
    canvas.setZoom(zoomlevel);
    $('#zoom').val((zoomlevel * 100).toFixed(1));
 
    call('updateCanvas', {center: false, newCtr});
    call('zoomDone');
  }
);
}());
 
/**
* Group: Text edit functions
* Functions relating to editing text elements
* @namespace {PlainObject} textActions
* @memberof module:svgcanvas.SvgCanvas#
*/
const textActions = canvas.textActions = (function () {
let curtext;
let textinput;
let cursor;
let selblock;
let blinker;
let chardata = [];
let textbb; // , transbb;
let matrix;
let lastX, lastY;
let allowDbl;
 
function setCursor (index) {
  const empty = (textinput.value === '');
  $(textinput).focus();
 
  if (!arguments.length) {
    if (empty) {
      index = 0;
    } else {
      if (textinput.selectionEnd !== textinput.selectionStart) { return; }
      index = textinput.selectionEnd;
    }
  }
 
  const charbb = chardata[index];
  if (!empty) {
    textinput.setSelectionRange(index, index);
  }
  cursor = getElem('text_cursor');
  if (!cursor) {
    cursor = document.createElementNS(NS.SVG, 'line');
    assignAttributes(cursor, {
      id: 'text_cursor',
      stroke: '#333',
      'stroke-width': 1
    });
    cursor = getElem('selectorParentGroup').appendChild(cursor);
  }
 
  if (!blinker) {
    blinker = setInterval(function () {
      const show = (cursor.getAttribute('display') === 'none');
      cursor.setAttribute('display', show ? 'inline' : 'none');
    }, 600);
  }
 
  const startPt = ptToScreen(charbb.x, textbb.y);
  const endPt = ptToScreen(charbb.x, (textbb.y + textbb.height));
 
  assignAttributes(cursor, {
    x1: startPt.x,
    y1: startPt.y,
    x2: endPt.x,
    y2: endPt.y,
    visibility: 'visible',
    display: 'inline'
  });
 
  if (selblock) { selblock.setAttribute('d', ''); }
}
 
function setSelection (start, end, skipInput) {
  if (start === end) {
    setCursor(end);
    return;
  }
 
  if (!skipInput) {
    textinput.setSelectionRange(start, end);
  }
 
  selblock = getElem('text_selectblock');
  if (!selblock) {
    selblock = document.createElementNS(NS.SVG, 'path');
    assignAttributes(selblock, {
      id: 'text_selectblock',
      fill: 'green',
      opacity: 0.5,
      style: 'pointer-events:none'
    });
    getElem('selectorParentGroup').append(selblock);
  }
 
  const startbb = chardata[start];
  const endbb = chardata[end];
 
  cursor.setAttribute('visibility', 'hidden');
 
  const tl = ptToScreen(startbb.x, textbb.y),
    tr = ptToScreen(startbb.x + (endbb.x - startbb.x), textbb.y),
    bl = ptToScreen(startbb.x, textbb.y + textbb.height),
    br = ptToScreen(startbb.x + (endbb.x - startbb.x), textbb.y + textbb.height);
 
  const dstr = 'M' + tl.x + ',' + tl.y +
    ' L' + tr.x + ',' + tr.y +
    ' ' + br.x + ',' + br.y +
    ' ' + bl.x + ',' + bl.y + 'z';
 
  assignAttributes(selblock, {
    d: dstr,
    display: 'inline'
  });
}
 
function getIndexFromPoint (mouseX, mouseY) {
  // Position cursor here
  const pt = svgroot.createSVGPoint();
  pt.x = mouseX;
  pt.y = mouseY;
 
  // No content, so return 0
  if (chardata.length === 1) { return 0; }
  // Determine if cursor should be on left or right of character
  let charpos = curtext.getCharNumAtPosition(pt);
  if (charpos < 0) {
    // Out of text range, look at mouse coords
    charpos = chardata.length - 2;
    if (mouseX <= chardata[0].x) {
      charpos = 0;
    }
  } else if (charpos >= chardata.length - 2) {
    charpos = chardata.length - 2;
  }
  const charbb = chardata[charpos];
  const mid = charbb.x + (charbb.width / 2);
  if (mouseX > mid) {
    charpos++;
  }
  return charpos;
}
 
function setCursorFromPoint (mouseX, mouseY) {
  setCursor(getIndexFromPoint(mouseX, mouseY));
}
 
function setEndSelectionFromPoint (x, y, apply) {
  const i1 = textinput.selectionStart;
  const i2 = getIndexFromPoint(x, y);
 
  const start = Math.min(i1, i2);
  const end = Math.max(i1, i2);
  setSelection(start, end, !apply);
}
 
function screenToPt (xIn, yIn) {
  const out = {
    x: xIn,
    y: yIn
  };
 
  out.x /= currentZoom;
  out.y /= currentZoom;
 
  if (matrix) {
    const pt = transformPoint(out.x, out.y, matrix.inverse());
    out.x = pt.x;
    out.y = pt.y;
  }
 
  return out;
}
 
function ptToScreen (xIn, yIn) {
  const out = {
    x: xIn,
    y: yIn
  };
 
  if (matrix) {
    const pt = transformPoint(out.x, out.y, matrix);
    out.x = pt.x;
    out.y = pt.y;
  }
 
  out.x *= currentZoom;
  out.y *= currentZoom;
 
  return out;
}
 
/*
// Not currently in use
function hideCursor () {
  if (cursor) {
    cursor.setAttribute('visibility', 'hidden');
  }
}
*/
 
function selectAll (evt) {
  setSelection(0, curtext.textContent.length);
  $(this).unbind(evt);
}
 
function selectWord (evt) {
  if (!allowDbl || !curtext) { return; }
 
  const ept = transformPoint(evt.pageX, evt.pageY, rootSctm),
    mouseX = ept.x * currentZoom,
    mouseY = ept.y * currentZoom;
  const pt = screenToPt(mouseX, mouseY);
 
  const index = getIndexFromPoint(pt.x, pt.y);
  const str = curtext.textContent;
  const first = str.substr(0, index).replace(/[a-z0-9]+$/i, '').length;
  const m = str.substr(index).match(/^[a-z0-9]+/i);
  const last = (m ? m[0].length : 0) + index;
  setSelection(first, last);
 
  // Set tripleclick
  $(evt.target).click(selectAll);
  setTimeout(function () {
    $(evt.target).unbind('click', selectAll);
  }, 300);
}
 
return /** @lends module:svgcanvas.SvgCanvas#textActions */ {
  /**
  * @param {Element} target
  * @param {Float} x
  * @param {Float} y
  * @returns {undefined}
  */
  select (target, x, y) {
    curtext = target;
    textActions.toEditMode(x, y);
  },
  /**
  * @param {Element} elem
  * @returns {undefined}
  */
  start (elem) {
    curtext = elem;
    textActions.toEditMode();
  },
  /**
  * @param {external:MouseEvent} evt
  * @param {Element} mouseTarget
  * @param {Float} startX
  * @param {Float} startY
  * @returns {undefined}
  */
  mouseDown (evt, mouseTarget, startX, startY) {
    const pt = screenToPt(startX, startY);
 
    textinput.focus();
    setCursorFromPoint(pt.x, pt.y);
    lastX = startX;
    lastY = startY;
 
    // TODO: Find way to block native selection
  },
  /**
  * @param {Float} mouseX
  * @param {Float} mouseY
  * @returns {undefined}
  */
  mouseMove (mouseX, mouseY) {
    const pt = screenToPt(mouseX, mouseY);
    setEndSelectionFromPoint(pt.x, pt.y);
  },
  /**
  * @param {external:MouseEvent} evt
  * @param {Float} mouseX
  * @param {Float} mouseY
  * @returns {undefined}
  */
  mouseUp (evt, mouseX, mouseY) {
    const pt = screenToPt(mouseX, mouseY);
 
    setEndSelectionFromPoint(pt.x, pt.y, true);
 
    // TODO: Find a way to make this work: Use transformed BBox instead of evt.target
    // if (lastX === mouseX && lastY === mouseY
    //   && !rectsIntersect(transbb, {x: pt.x, y: pt.y, width: 0, height: 0})) {
    //   textActions.toSelectMode(true);
    // }
 
    if (
      evt.target !== curtext &&
      mouseX < lastX + 2 &&
      mouseX > lastX - 2 &&
      mouseY < lastY + 2 &&
      mouseY > lastY - 2
    ) {
      textActions.toSelectMode(true);
    }
  },
  /**
  * @function
  * @param {Integer} index
  * @returns {undefined}
  */
  setCursor,
  /**
  * @param {Float} x
  * @param {Float} y
  * @returns {undefined}
  */
  toEditMode (x, y) {
    allowDbl = false;
    currentMode = 'textedit';
    selectorManager.requestSelector(curtext).showGrips(false);
    // Make selector group accept clicks
    /* const selector = */ selectorManager.requestSelector(curtext); // Do we need this? Has side effect of setting lock, so keeping for now, but next line wasn't being used
    // const sel = selector.selectorRect;
 
    textActions.init();
 
    $(curtext).css('cursor', 'text');
 
    // if (supportsEditableText()) {
    //   curtext.setAttribute('editable', 'simple');
    //   return;
    // }
 
    if (!arguments.length) {
      setCursor();
    } else {
      const pt = screenToPt(x, y);
      setCursorFromPoint(pt.x, pt.y);
    }
 
    setTimeout(function () {
      allowDbl = true;
    }, 300);
  },
  /**
  * @param {boolean|Element} selectElem
  * @fires module:svgcanvas.SvgCanvas#event:selected
  * @returns {undefined}
  */
  toSelectMode (selectElem) {
    currentMode = 'select';
    clearInterval(blinker);
    blinker = null;
    if (selblock) { $(selblock).attr('display', 'none'); }
    if (cursor) { $(cursor).attr('visibility', 'hidden'); }
    $(curtext).css('cursor', 'move');
 
    if (selectElem) {
      clearSelection();
      $(curtext).css('cursor', 'move');
 
      call('selected', [curtext]);
      addToSelection([curtext], true);
    }
    if (curtext && !curtext.textContent.length) {
      // No content, so delete
      canvas.deleteSelectedElements();
    }
 
    $(textinput).blur();
 
    curtext = false;
 
    // if (supportsEditableText()) {
    //   curtext.removeAttribute('editable');
    // }
  },
  /**
  * @param {Element} elem
  * @returns {undefined}
  */
  setInputElem (elem) {
    textinput = elem;
    // $(textinput).blur(hideCursor);
  },
  /**
  * @returns {undefined}
  */
  clear () {
    if (currentMode === 'textedit') {
      textActions.toSelectMode();
    }
  },
  /**
  * @param {Element} inputElem Not in use
  * @returns {undefined}
  */
  init (inputElem) {
    if (!curtext) { return; }
    let i, end;
    // if (supportsEditableText()) {
    //   curtext.select();
    //   return;
    // }
 
    if (!curtext.parentNode) {
      // Result of the ffClone, need to get correct element
      curtext = selectedElements[0];
      selectorManager.requestSelector(curtext).showGrips(false);
    }
 
    const str = curtext.textContent;
    const len = str.length;
 
    const xform = curtext.getAttribute('transform');
 
    textbb = utilsGetBBox(curtext);
 
    matrix = xform ? getMatrix(curtext) : null;
 
    chardata = [];
    chardata.length = len;
    textinput.focus();
 
    $(curtext).unbind('dblclick', selectWord).dblclick(selectWord);
 
    if (!len) {
      end = {x: textbb.x + (textbb.width / 2), width: 0};
    }
 
    for (i = 0; i < len; i++) {
      const start = curtext.getStartPositionOfChar(i);
      end = curtext.getEndPositionOfChar(i);
 
      if (!supportsGoodTextCharPos()) {
        const offset = canvas.contentW * currentZoom;
        start.x -= offset;
        end.x -= offset;
 
        start.x /= currentZoom;
        end.x /= currentZoom;
      }
 
      // Get a "bbox" equivalent for each character. Uses the
      // bbox data of the actual text for y, height purposes
 
      // TODO: Decide if y, width and height are actually necessary
      chardata[i] = {
        x: start.x,
        y: textbb.y, // start.y?
        width: end.x - start.x,
        height: textbb.height
      };
    }
 
    // Add a last bbox for cursor at end of text
    chardata.push({
      x: end.x,
      width: 0
    });
    setSelection(textinput.selectionStart, textinput.selectionEnd, true);
  }
};
}());
 
/**
* Group: Serialization
*/
 
/**
* Looks at DOM elements inside the `<defs>` to see if they are referred to,
* removes them from the DOM if they are not.
* @function module:svgcanvas.SvgCanvas#removeUnusedDefElems
* @returns {Integer} The number of elements that were removed
*/
const removeUnusedDefElems = this.removeUnusedDefElems = function () {
  const defs = svgcontent.getElementsByTagNameNS(NS.SVG, 'defs');
  if (!defs || !defs.length) { return 0; }
 
  // if (!defs.firstChild) { return; }
 
  const defelemUses = [];
  let numRemoved = 0;
  const attrs = ['fill', 'stroke', 'filter', 'marker-start', 'marker-mid', 'marker-end'];
  const alen = attrs.length;
 
  const allEls = svgcontent.getElementsByTagNameNS(NS.SVG, '*');
  const allLen = allEls.length;
 
  let i, j;
  for (i = 0; i < allLen; i++) {
    const el = allEls[i];
    for (j = 0; j < alen; j++) {
      const ref = getUrlFromAttr(el.getAttribute(attrs[j]));
      if (ref) {
        defelemUses.push(ref.substr(1));
      }
    }
 
    // gradients can refer to other gradients
    const href = getHref(el);
    if (href && href.startsWith('#')) {
      defelemUses.push(href.substr(1));
    }
  }
 
  const defelems = $(defs).find('linearGradient, radialGradient, filter, marker, svg, symbol');
  i = defelems.length;
  while (i--) {
    const defelem = defelems[i];
    const {id} = defelem;
    if (!defelemUses.includes(id)) {
      // Not found, so remove (but remember)
      removedElements[id] = defelem;
      defelem.remove();
      numRemoved++;
    }
  }
 
  return numRemoved;
};
 
/**
* Main function to set up the SVG content for output.
* @function module:svgcanvas.SvgCanvas#svgCanvasToString
* @returns {string} The SVG image for output
*/
this.svgCanvasToString = function () {
  // keep calling it until there are none to remove
  while (removeUnusedDefElems() > 0) {}
 
  pathActions.clear(true);
 
  // Keep SVG-Edit comment on top
  $.each(svgcontent.childNodes, function (i, node) {
    if (i && node.nodeType === 8 && node.data.includes('Created with')) {
      svgcontent.firstChild.before(node);
    }
  });
 
  // Move out of in-group editing mode
  if (currentGroup) {
    draw.leaveContext();
    selectOnly([currentGroup]);
  }
 
  const nakedSvgs = [];
 
  // Unwrap gsvg if it has no special attributes (only id and style)
  $(svgcontent).find('g:data(gsvg)').each(function () {
    const attrs = this.attributes;
    let len = attrs.length;
    for (let i = 0; i < len; i++) {
      if (attrs[i].nodeName === 'id' || attrs[i].nodeName === 'style') {
        len--;
      }
    }
    // No significant attributes, so ungroup
    if (len <= 0) {
      const svg = this.firstChild;
      nakedSvgs.push(svg);
      $(this).replaceWith(svg);
    }
  });
  const output = this.svgToString(svgcontent, 0);
 
  // Rewrap gsvg
  if (nakedSvgs.length) {
    $(nakedSvgs).each(function () {
      groupSvgElem(this);
    });
  }
 
  return output;
};
 
/**
* Sub function ran on each SVG element to convert it to a string as desired.
* @function module:svgcanvas.SvgCanvas#svgToString
* @param {Element} elem - The SVG element to convert
* @param {Integer} indent - Number of spaces to indent this tag
* @returns {string} The given element as an SVG tag
*/
this.svgToString = function (elem, indent) {
  const out = [];
  const unit = curConfig.baseUnit;
  const unitRe = new RegExp('^-?[\\d\\.]+' + unit + '$');
 
  if (elem) {
    cleanupElement(elem);
    const attrs = Array.from(elem.attributes);
    let i;
    const childs = elem.childNodes;
    attrs.sort((a, b) => a.name > b.name ? -1 : 1);
 
    for (i = 0; i < indent; i++) { out.push(' '); }
    out.push('<'); out.push(elem.nodeName);
    if (elem.id === 'svgcontent') {
      // Process root element separately
      const res = getResolution();
 
      const vb = '';
      // TODO: Allow this by dividing all values by current baseVal
      // Note that this also means we should properly deal with this on import
      // if (curConfig.baseUnit !== 'px') {
      //   const unit = curConfig.baseUnit;
      //   const unitM = getTypeMap()[unit];
      //   res.w = shortFloat(res.w / unitM);
      //   res.h = shortFloat(res.h / unitM);
      //   vb = ' viewBox="' + [0, 0, res.w, res.h].join(' ') + '"';
      //   res.w += unit;
      //   res.h += unit;
      // }
 
      if (unit !== 'px') {
        res.w = convertUnit(res.w, unit) + unit;
        res.h = convertUnit(res.h, unit) + unit;
      }
 
      out.push(' width="' + res.w + '" height="' + res.h + '"' + vb + ' xmlns="' + NS.SVG + '"');
 
      const nsuris = {};
 
      // Check elements for namespaces, add if found
      $(elem).find('*').andSelf().each(function () {
        // const el = this;
        // for some elements have no attribute
        const uri = this.namespaceURI;
        if (uri && !nsuris[uri] && nsMap[uri] && nsMap[uri] !== 'xmlns' && nsMap[uri] !== 'xml') {
          nsuris[uri] = true;
          out.push(' xmlns:' + nsMap[uri] + '="' + uri + '"');
        }
 
        $.each(this.attributes, function (i, attr) {
          const uri = attr.namespaceURI;
          if (uri && !nsuris[uri] && nsMap[uri] !== 'xmlns' && nsMap[uri] !== 'xml') {
            nsuris[uri] = true;
            out.push(' xmlns:' + nsMap[uri] + '="' + uri + '"');
          }
        });
      });
 
      i = attrs.length;
      const attrNames = ['width', 'height', 'xmlns', 'x', 'y', 'viewBox', 'id', 'overflow'];
      while (i--) {
        const attr = attrs[i];
        const attrVal = toXml(attr.value);
 
        // Namespaces have already been dealt with, so skip
        if (attr.nodeName.startsWith('xmlns:')) { continue; }
 
        // only serialize attributes we don't use internally
        if (attrVal !== '' && !attrNames.includes(attr.localName)) {
          if (!attr.namespaceURI || nsMap[attr.namespaceURI]) {
            out.push(' ');
            out.push(attr.nodeName); out.push('="');
            out.push(attrVal); out.push('"');
          }
        }
      }
    } else {
      // Skip empty defs
      if (elem.nodeName === 'defs' && !elem.firstChild) { return; }
 
      const mozAttrs = ['-moz-math-font-style', '_moz-math-font-style'];
      for (i = attrs.length - 1; i >= 0; i--) {
        const attr = attrs[i];
        let attrVal = toXml(attr.value);
        // remove bogus attributes added by Gecko
        if (mozAttrs.includes(attr.localName)) { continue; }
        if (attrVal !== '') {
          if (attrVal.startsWith('pointer-events')) { continue; }
          if (attr.localName === 'class' && attrVal.startsWith('se_')) { continue; }
          out.push(' ');
          if (attr.localName === 'd') { attrVal = pathActions.convertPath(elem, true); }
          if (!isNaN(attrVal)) {
            attrVal = shortFloat(attrVal);
          } else if (unitRe.test(attrVal)) {
            attrVal = shortFloat(attrVal) + unit;
          }
 
          // Embed images when saving
          if (saveOptions.apply &&
            elem.nodeName === 'image' &&
            attr.localName === 'href' &&
            saveOptions.images &&
            saveOptions.images === 'embed'
          ) {
            const img = encodableImages[attrVal];
            if (img) { attrVal = img; }
          }
 
          // map various namespaces to our fixed namespace prefixes
          // (the default xmlns attribute itself does not get a prefix)
          if (!attr.namespaceURI || attr.namespaceURI === NS.SVG || nsMap[attr.namespaceURI]) {
            out.push(attr.nodeName); out.push('="');
            out.push(attrVal); out.push('"');
          }
        }
      }
    }
 
    if (elem.hasChildNodes()) {
      out.push('>');
      indent++;
      let bOneLine = false;
 
      for (i = 0; i < childs.length; i++) {
        const child = childs.item(i);
        switch (child.nodeType) {
        case 1: // element node
          out.push('\n');
          out.push(this.svgToString(childs.item(i), indent));
          break;
        case 3: // text node
          const str = child.nodeValue.replace(/^\s+|\s+$/g, '');
          if (str !== '') {
            bOneLine = true;
            out.push(String(toXml(str)));
          }
          break;
        case 4: // cdata node
          out.push('\n');
          out.push(new Array(indent + 1).join(' '));
          out.push('<![CDATA[');
          out.push(child.nodeValue);
          out.push(']]>');
          break;
        case 8: // comment
          out.push('\n');
          out.push(new Array(indent + 1).join(' '));
          out.push('<!--');
          out.push(child.data);
          out.push('-->');
          break;
        } // switch on node type
      }
      indent--;
      if (!bOneLine) {
        out.push('\n');
        for (i = 0; i < indent; i++) { out.push(' '); }
      }
      out.push('</'); out.push(elem.nodeName); out.push('>');
    } else {
      out.push('/>');
    }
  }
  return out.join('');
}; // end svgToString()
 
/**
 * Function to run when image data is found
 * @callback module:svgcanvas.ImageEmbeddedCallback
 * @param {string|false} result Data URL
 * @returns {undefined}
 */
/**
* Converts a given image file to a data URL when possible, then runs a given callback.
* @function module:svgcanvas.SvgCanvas#embedImage
* @param {string} src - The path/URL of the image
* @param {module:svgcanvas.ImageEmbeddedCallback} [callback] - Function to run when image data is found
* @returns {Promise} Resolves to Data URL (string|false)
*/
this.embedImage = function (src, callback) {
  return new Promise(function (resolve, reject) {
    // load in the image and once it's loaded, get the dimensions
    $(new Image()).load(function (response, status, xhr) {
      if (status === 'error') {
        reject(new Error('Error loading image: ' + xhr.status + ' ' + xhr.statusText));
        return;
      }
      // create a canvas the same size as the raster image
      const cvs = document.createElement('canvas');
      cvs.width = this.width;
      cvs.height = this.height;
      // load the raster image into the canvas
      cvs.getContext('2d').drawImage(this, 0, 0);
      // retrieve the data: URL
      try {
        let urldata = ';svgedit_url=' + encodeURIComponent(src);
        urldata = cvs.toDataURL().replace(';base64', urldata + ';base64');
        encodableImages[src] = urldata;
      } catch (e) {
        encodableImages[src] = false;
      }
      lastGoodImgUrl = src;
      if (callback) { callback(encodableImages[src]); }
      resolve(encodableImages[src]);
    }).attr('src', src);
  });
};
 
/**
* Sets a given URL to be a "last good image" URL.
* @function module:svgcanvas.SvgCanvas#setGoodImage
* @param {string} val
* @returns {undefined}
*/
this.setGoodImage = function (val) {
  lastGoodImgUrl = val;
};
 
/**
* Does nothing by default, handled by optional widget/extension.
* @function module:svgcanvas.SvgCanvas#open
* @returns {undefined}
*/
this.open = function () {
};
 
/**
* Serializes the current drawing into SVG XML text and passes it to the 'saved' handler.
* This function also includes the XML prolog. Clients of the `SvgCanvas` bind their save
* function to the 'saved' event.
* @function module:svgcanvas.SvgCanvas#save
* @param {module:svgcanvas.SaveOptions} opts
* @fires module:svgcanvas.SvgCanvas#event:saved
* @returns {undefined}
*/
this.save = function (opts) {
  // remove the selected outline before serializing
  clearSelection();
  // Update save options if provided
  if (opts) { $.extend(saveOptions, opts); }
  saveOptions.apply = true;
 
  // no need for doctype, see https://jwatt.org/svg/authoring/#doctype-declaration
  const str = this.svgCanvasToString();
  call('saved', str);
};
 
/**
* @typedef {GenericObject} module:svgcanvas.IssuesAndCodes
* @property {string[]} issueCodes The locale-independent code names
* @property {string[]} issues The localized descriptions
*/
 
/**
* Codes only is useful for locale-independent detection.
* @returns {module:svgcanvas.IssuesAndCodes}
*/
function getIssues () {
  // remove the selected outline before serializing
  clearSelection();
 
  // Check for known CanVG issues
  const issues = [];
  const issueCodes = [];
 
  // Selector and notice
  const issueList = {
    feGaussianBlur: uiStrings.exportNoBlur,
    foreignObject: uiStrings.exportNoforeignObject,
    '[stroke-dasharray]': uiStrings.exportNoDashArray
  };
  const content = $(svgcontent);
 
  // Add font/text check if Canvas Text API is not implemented
  if (!('font' in $('<canvas>')[0].getContext('2d'))) {
    issueList.text = uiStrings.exportNoText;
  }
 
  $.each(issueList, function (sel, descr) {
    if (content.find(sel).length) {
      issueCodes.push(sel);
      issues.push(descr);
    }
  });
  return {issues, issueCodes};
}
 
let canvg;
/**
* @typedef {"feGaussianBlur"|"foreignObject"|"[stroke-dasharray]"|"text"} module:svgcanvas.IssueCode
*/
/**
* @typedef {PlainObject} module:svgcanvas.ImageExportedResults
* @property {string} datauri Contents as a Data URL
* @property {string} bloburl May be the empty string
* @property {string} svg The SVG contents as a string
* @property {string[]} issues The localization messages of `issueCodes`
* @property {module:svgcanvas.IssueCode[]} issueCodes CanVG issues found with the SVG
* @property {"PNG"|"JPEG"|"BMP"|"WEBP"|"ICO"} type The chosen image type
* @property {"image/png"|"image/jpeg"|"image/bmp"|"image/webp"} mimeType The image MIME type
* @property {Float} quality A decimal between 0 and 1 (for use with JPEG or WEBP)
* @property {string} exportWindowName A convenience for passing along a `window.name` to target a window on which the export could be added
*/
 
/**
 * Function to run when image data is found
 * @callback module:svgcanvas.ImageExportedCallback
 * @param {module:svgcanvas.ImageExportedResults} obj
 * @returns {undefined}
 */
/**
* Generates a PNG (or JPG, BMP, WEBP) Data URL based on the current image,
* then calls "exported" with an object including the string, image
* information, and any issues found.
* @function module:svgcanvas.SvgCanvas#rasterExport
* @param {"PNG"|"JPEG"|"BMP"|"WEBP"|"ICO"} [imgType="PNG"]
* @param {Float} [quality] Between 0 and 1
* @param {string} [exportWindowName]
* @param {module:svgcanvas.ImageExportedCallback} [cb]
* @param {PlainObject} [opts]
* @param {boolean} [opts.avoidEvent]
* @fires module:svgcanvas.SvgCanvas#event:exported
* @todo Confirm/fix ICO type
* @returns {Promise} Resolves to {@link module:svgcanvas.ImageExportedResults}
*/
this.rasterExport = function (imgType, quality, exportWindowName, cb, opts = {}) {
  const type = imgType === 'ICO' ? 'BMP' : (imgType || 'PNG');
  const mimeType = 'image/' + type.toLowerCase();
  const {issues, issueCodes} = getIssues();
  const svg = this.svgCanvasToString();
 
  return new Promise(async (resolve, reject) => {
    if (!canvg) {
      ({canvg} = await importSetGlobal(curConfig.canvgPath + 'canvg.js', {
        global: 'canvg'
      }));
    }
    if (!$('#export_canvas').length) {
      $('<canvas>', {id: 'export_canvas'}).hide().appendTo('body');
    }
    const c = $('#export_canvas')[0];
    c.width = canvas.contentW;
    c.height = canvas.contentH;
 
    await canvg(c, svg);
    const dataURLType = type.toLowerCase();
    const datauri = quality
      ? c.toDataURL('image/' + dataURLType, quality)
      : c.toDataURL('image/' + dataURLType);
    let bloburl;
    function done () {
      const obj = {
        datauri, bloburl, svg, issues, issueCodes, type: imgType,
        mimeType, quality, exportWindowName
      };
      if (!opts.avoidEvent) {
        call('exported', obj);
      }
      if (cb) {
        cb(obj);
      }
      resolve(obj);
    }
    if (c.toBlob) {
      c.toBlob((blob) => {
        bloburl = createObjectURL(blob);
        done();
      }, mimeType, quality);
      return;
    }
    bloburl = dataURLToObjectURL(datauri);
    done();
  });
};
/**
 * @external jsPDF
 */
/**
 * @typedef {undefined|"save"|"arraybuffer"|"blob"|"datauristring"|"dataurlstring"|"dataurlnewwindow"|"datauri"|"dataurl"} external:jsPDF.OutputType
 * @todo Newer version to add also allows these `outputType` values "bloburi"|"bloburl" which return strings, so document here and for `outputType` of `module:svgcanvas.PDFExportedResults` below if added
*/
/**
* @typedef {PlainObject} module:svgcanvas.PDFExportedResults
* @property {string} svg The SVG PDF output
* @property {string|ArrayBuffer|Blob|window} output The output based on the `outputType`;
* if `undefined`, "datauristring", "dataurlstring", "datauri",
* or "dataurl", will be a string (`undefined` gives a document, while the others
* build as Data URLs; "datauri" and "dataurl" change the location of the current page); if
* "arraybuffer", will return `ArrayBuffer`; if "blob", returns a `Blob`;
* if "dataurlnewwindow", will change the current page's location and return a string
* if in Safari and no window object is found; otherwise opens in, and returns, a new `window`
* object; if "save", will have the same return as "dataurlnewwindow" if
* `navigator.getUserMedia` support is found without `URL.createObjectURL` support; otherwise
* returns `undefined` but attempts to save
* @property {external:jsPDF.OutputType} outputType
* @property {string[]} issues The human-readable localization messages of corresponding `issueCodes`
* @property {module:svgcanvas.IssueCode[]} issueCodes
* @property {string} exportWindowName
*/
 
/**
 * Function to run when PDF data is found
 * @callback module:svgcanvas.PDFExportedCallback
 * @param {module:svgcanvas.PDFExportedResults} obj
 * @returns {undefined}
 */
/**
* Generates a PDF based on the current image, then calls "exportedPDF" with
* an object including the string, the data URL, and any issues found.
* @function module:svgcanvas.SvgCanvas#exportPDF
* @param {string} [exportWindowName] Will also be used for the download file name here
* @param {external:jsPDF.OutputType} [outputType="dataurlstring"]
* @param {module:svgcanvas.PDFExportedCallback} [cb]
* @fires module:svgcanvas.SvgCanvas#event:exportedPDF
* @returns {Promise} Resolves to {@link module:svgcanvas.PDFExportedResults}
*/
this.exportPDF = function (
  exportWindowName,
  outputType = isChrome() ? 'save' : undefined,
  cb
) {
  const that = this;
  return new Promise(async (resolve, reject) => {
    if (!window.jsPDF) {
      // Todo: Switch to `import()` when widely supported and available (also allow customization of path)
      await importScript([
        // We do not currently have these paths configurable as they are
        //   currently global-only, so not Rolled-up
        'jspdf/underscore-min.js',
        'jspdf/jspdf.min.js'
      ]);
 
      const modularVersion = !('svgEditor' in window) ||
        !window.svgEditor ||
        window.svgEditor.modules !== false;
      // Todo: Switch to `import()` when widely supported and available (also allow customization of path)
      await importScript(curConfig.jspdfPath + 'jspdf.plugin.svgToPdf.js', {
        type: modularVersion
          ? 'module'
          : 'text/javascript'
      });
      // await importModule('jspdf/jspdf.plugin.svgToPdf.js');
    }
 
    const res = getResolution();
    const orientation = res.w > res.h ? 'landscape' : 'portrait';
    const unit = 'pt'; // curConfig.baseUnit; // We could use baseUnit, but that is presumably not intended for export purposes
 
    // Todo: Give options to use predefined jsPDF formats like "a4", etc. from pull-down (with option to keep customizable)
    const doc = jsPDF({
      orientation,
      unit,
      format: [res.w, res.h]
      // , compressPdf: true
    });
    const docTitle = getDocumentTitle();
    doc.setProperties({
      title: docTitle /* ,
      subject: '',
      author: '',
      keywords: '',
      creator: '' */
    });
    const {issues, issueCodes} = getIssues();
    const svg = that.svgCanvasToString();
    doc.addSVG(svg, 0, 0);
 
    // doc.output('save'); // Works to open in a new
    //  window; todo: configure this and other export
    //  options to optionally work in this manner as
    //  opposed to opening a new tab
    outputType = outputType || 'dataurlstring';
    const obj = {svg, issues, issueCodes, exportWindowName, outputType};
    obj.output = doc.output(outputType, outputType === 'save' ? (exportWindowName || 'svg.pdf') : undefined);
    if (cb) {
      cb(obj);
    }
    resolve(obj);
    call('exportedPDF', obj);
  });
};
 
/**
* Returns the current drawing as raw SVG XML text.
* @function module:svgcanvas.SvgCanvas#getSvgString
* @returns {string} The current drawing as raw SVG XML text.
*/
this.getSvgString = function () {
  saveOptions.apply = false;
  return this.svgCanvasToString();
};
 
/**
* This function determines whether to use a nonce in the prefix, when
* generating IDs for future documents in SVG-Edit.
* If you're controlling SVG-Edit externally, and want randomized IDs, call
* this BEFORE calling `svgCanvas.setSvgString`.
* @function module:svgcanvas.SvgCanvas#randomizeIds
* @param {boolean} [enableRandomization] If true, adds a nonce to the prefix. Thus
* `svgCanvas.randomizeIds() <==> svgCanvas.randomizeIds(true)`
* @returns {undefined}
*/
this.randomizeIds = function (enableRandomization) {
  if (arguments.length > 0 && enableRandomization === false) {
    draw.randomizeIds(false, getCurrentDrawing());
  } else {
    draw.randomizeIds(true, getCurrentDrawing());
  }
};
 
/**
* Ensure each element has a unique ID.
* @function module:svgcanvas.SvgCanvas#uniquifyElems
* @param {Element} g - The parent element of the tree to give unique IDs
* @returns {undefined}
*/
const uniquifyElems = this.uniquifyElems = function (g) {
  const ids = {};
  // TODO: Handle markers and connectors. These are not yet re-identified properly
  // as their referring elements do not get remapped.
  //
  // <marker id='se_marker_end_svg_7'/>
  // <polyline id='svg_7' se:connector='svg_1 svg_6' marker-end='url(#se_marker_end_svg_7)'/>
  //
  // Problem #1: if svg_1 gets renamed, we do not update the polyline's se:connector attribute
  // Problem #2: if the polyline svg_7 gets renamed, we do not update the marker id nor the polyline's marker-end attribute
  const refElems = ['filter', 'linearGradient', 'pattern', 'radialGradient', 'symbol', 'textPath', 'use'];
 
  walkTree(g, function (n) {
    // if it's an element node
    if (n.nodeType === 1) {
      // and the element has an ID
      if (n.id) {
        // and we haven't tracked this ID yet
        if (!(n.id in ids)) {
          // add this id to our map
          ids[n.id] = {elem: null, attrs: [], hrefs: []};
        }
        ids[n.id].elem = n;
      }
 
      // now search for all attributes on this element that might refer
      // to other elements
      $.each(refAttrs, function (i, attr) {
        const attrnode = n.getAttributeNode(attr);
        if (attrnode) {
          // the incoming file has been sanitized, so we should be able to safely just strip off the leading #
          const url = getUrlFromAttr(attrnode.value),
            refid = url ? url.substr(1) : null;
          if (refid) {
            if (!(refid in ids)) {
              // add this id to our map
              ids[refid] = {elem: null, attrs: [], hrefs: []};
            }
            ids[refid].attrs.push(attrnode);
          }
        }
      });
 
      // check xlink:href now
      const href = getHref(n);
      // TODO: what if an <image> or <a> element refers to an element internally?
      if (href && refElems.includes(n.nodeName)) {
        const refid = href.substr(1);
        if (refid) {
          if (!(refid in ids)) {
            // add this id to our map
            ids[refid] = {elem: null, attrs: [], hrefs: []};
          }
          ids[refid].hrefs.push(n);
        }
      }
    }
  });
 
  // in ids, we now have a map of ids, elements and attributes, let's re-identify
  for (const oldid in ids) {
    if (!oldid) { continue; }
    const {elem} = ids[oldid];
    if (elem) {
      const newid = getNextId();
 
      // assign element its new id
      elem.id = newid;
 
      // remap all url() attributes
      const {attrs} = ids[oldid];
      let j = attrs.length;
      while (j--) {
        const attr = attrs[j];
        attr.ownerElement.setAttribute(attr.name, 'url(#' + newid + ')');
      }
 
      // remap all href attributes
      const hreffers = ids[oldid].hrefs;
      let k = hreffers.length;
      while (k--) {
        const hreffer = hreffers[k];
        setHref(hreffer, '#' + newid);
      }
    }
  }
};
 
/**
* Assigns reference data for each use element.
* @function module:svgcanvas.SvgCanvas#setUseData
* @param {Element} parent
* @returns {undefined}
*/
const setUseData = this.setUseData = function (parent) {
  let elems = $(parent);
 
  if (parent.tagName !== 'use') {
    elems = elems.find('use');
  }
 
  elems.each(function () {
    const id = getHref(this).substr(1);
    const refElem = getElem(id);
    if (!refElem) { return; }
    $(this).data('ref', refElem);
    if (refElem.tagName === 'symbol' || refElem.tagName === 'svg') {
      $(this).data('symbol', refElem).data('ref', refElem);
    }
  });
};
 
/**
* Converts gradients from userSpaceOnUse to objectBoundingBox.
* @function module:svgcanvas.SvgCanvas#convertGradients
* @param {Element} elem
* @returns {undefined}
*/
const convertGradients = this.convertGradients = function (elem) {
  let elems = $(elem).find('linearGradient, radialGradient');
  if (!elems.length && isWebkit()) {
    // Bug in webkit prevents regular *Gradient selector search
    elems = $(elem).find('*').filter(function () {
      return (this.tagName.includes('Gradient'));
    });
  }
 
  elems.each(function () {
    const grad = this;
    if ($(grad).attr('gradientUnits') === 'userSpaceOnUse') {
      // TODO: Support more than one element with this ref by duplicating parent grad
      const elems = $(svgcontent).find('[fill="url(#' + grad.id + ')"],[stroke="url(#' + grad.id + ')"]');
      if (!elems.length) { return; }
 
      // get object's bounding box
      const bb = utilsGetBBox(elems[0]);
 
      // This will occur if the element is inside a <defs> or a <symbol>,
      // in which we shouldn't need to convert anyway.
      if (!bb) { return; }
 
      if (grad.tagName === 'linearGradient') {
        const gCoords = $(grad).attr(['x1', 'y1', 'x2', 'y2']);
 
        // If has transform, convert
        const tlist = grad.gradientTransform.baseVal;
        if (tlist && tlist.numberOfItems > 0) {
          const m = transformListToTransform(tlist).matrix;
          const pt1 = transformPoint(gCoords.x1, gCoords.y1, m);
          const pt2 = transformPoint(gCoords.x2, gCoords.y2, m);
 
          gCoords.x1 = pt1.x;
          gCoords.y1 = pt1.y;
          gCoords.x2 = pt2.x;
          gCoords.y2 = pt2.y;
          grad.removeAttribute('gradientTransform');
        }
 
        $(grad).attr({
          x1: (gCoords.x1 - bb.x) / bb.width,
          y1: (gCoords.y1 - bb.y) / bb.height,
          x2: (gCoords.x2 - bb.x) / bb.width,
          y2: (gCoords.y2 - bb.y) / bb.height
        });
        grad.removeAttribute('gradientUnits');
      }
      // else {
      //   Note: radialGradient elements cannot be easily converted
      //   because userSpaceOnUse will keep circular gradients, while
      //   objectBoundingBox will x/y scale the gradient according to
      //   its bbox.
      //
      //   For now we'll do nothing, though we should probably have
      //   the gradient be updated as the element is moved, as
      //   inkscape/illustrator do.
      //
      //   const gCoords = $(grad).attr(['cx', 'cy', 'r']);
      //
      //   $(grad).attr({
      //     cx: (gCoords.cx - bb.x) / bb.width,
      //     cy: (gCoords.cy - bb.y) / bb.height,
      //     r: gCoords.r
      //   });
      //
      //   grad.removeAttribute('gradientUnits');
      // }
    }
  });
};
 
/**
* Converts selected/given `<use>` or child SVG element to a group.
* @function module:svgcanvas.SvgCanvas#convertToGroup
* @param {Element} elem
* @fires module:svgcanvas.SvgCanvas#event:selected
* @returns {undefined}
*/
const convertToGroup = this.convertToGroup = function (elem) {
  if (!elem) {
    elem = selectedElements[0];
  }
  const $elem = $(elem);
  const batchCmd = new BatchCommand();
  let ts;
 
  if ($elem.data('gsvg')) {
    // Use the gsvg as the new group
    const svg = elem.firstChild;
    const pt = $(svg).attr(['x', 'y']);
 
    $(elem.firstChild.firstChild).unwrap();
    $(elem).removeData('gsvg');
 
    const tlist = getTransformList(elem);
    const xform = svgroot.createSVGTransform();
    xform.setTranslate(pt.x, pt.y);
    tlist.appendItem(xform);
    recalculateDimensions(elem);
    call('selected', [elem]);
  } else if ($elem.data('symbol')) {
    elem = $elem.data('symbol');
 
    ts = $elem.attr('transform');
    const pos = $elem.attr(['x', 'y']);
 
    const vb = elem.getAttribute('viewBox');
 
    if (vb) {
      const nums = vb.split(' ');
      pos.x -= +nums[0];
      pos.y -= +nums[1];
    }
 
    // Not ideal, but works
    ts += ' translate(' + (pos.x || 0) + ',' + (pos.y || 0) + ')';
 
    const prev = $elem.prev();
 
    // Remove <use> element
    batchCmd.addSubCommand(new RemoveElementCommand($elem[0], $elem[0].nextSibling, $elem[0].parentNode));
    $elem.remove();
 
    // See if other elements reference this symbol
    const hasMore = $(svgcontent).find('use:data(symbol)').length;
 
    const g = svgdoc.createElementNS(NS.SVG, 'g');
    const childs = elem.childNodes;
 
    let i;
    for (i = 0; i < childs.length; i++) {
      g.append(childs[i].cloneNode(true));
    }
 
    // Duplicate the gradients for Gecko, since they weren't included in the <symbol>
    if (isGecko()) {
      const dupeGrads = $(findDefs()).children('linearGradient,radialGradient,pattern').clone();
      $(g).append(dupeGrads);
    }
 
    if (ts) {
      g.setAttribute('transform', ts);
    }
 
    const parent = elem.parentNode;
 
    uniquifyElems(g);
 
    // Put the dupe gradients back into <defs> (after uniquifying them)
    if (isGecko()) {
      $(findDefs()).append($(g).find('linearGradient,radialGradient,pattern'));
    }
 
    // now give the g itself a new id
    g.id = getNextId();
 
    prev.after(g);
 
    if (parent) {
      if (!hasMore) {
        // remove symbol/svg element
        const {nextSibling} = elem;
        elem.remove();
        batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent));
      }
      batchCmd.addSubCommand(new InsertElementCommand(g));
    }
 
    setUseData(g);
 
    if (isGecko()) {
      convertGradients(findDefs());
    } else {
      convertGradients(g);
    }
 
    // recalculate dimensions on the top-level children so that unnecessary transforms
    // are removed
    walkTreePost(g, function (n) {
      try {
        recalculateDimensions(n);
      } catch (e) {
        console.log(e);
      }
    });
 
    // Give ID for any visible element missing one
    $(g).find(visElems).each(function () {
      if (!this.id) { this.id = getNextId(); }
    });
 
    selectOnly([g]);
 
    const cm = pushGroupProperties(g, true);
    if (cm) {
      batchCmd.addSubCommand(cm);
    }
 
    addCommandToHistory(batchCmd);
  } else {
    console.log('Unexpected element to ungroup:', elem);
  }
};
 
/**
* This function sets the current drawing as the input SVG XML.
* @function module:svgcanvas.SvgCanvas#setSvgString
* @param {string} xmlString - The SVG as XML text.
* @param {boolean} [preventUndo=false] - Indicates if we want to do the
* changes without adding them to the undo stack - e.g. for initializing a
* drawing on page load.
* @fires module:svgcanvas.SvgCanvas#event:setnonce
* @fires module:svgcanvas.SvgCanvas#event:unsetnonce
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {boolean} This function returns `false` if the set was
*     unsuccessful, `true` otherwise.
*/
this.setSvgString = function (xmlString, preventUndo) {
  try {
    // convert string into XML document
    const newDoc = text2xml(xmlString);
    if (newDoc.firstElementChild &&
      newDoc.firstElementChild.namespaceURI !== NS.SVG) {
      return false;
    }
 
    this.prepareSvg(newDoc);
 
    const batchCmd = new BatchCommand('Change Source');
 
    // remove old svg document
    const {nextSibling} = svgcontent;
    const oldzoom = svgroot.removeChild(svgcontent);
    batchCmd.addSubCommand(new RemoveElementCommand(oldzoom, nextSibling, svgroot));
 
    // set new svg document
    // If DOM3 adoptNode() available, use it. Otherwise fall back to DOM2 importNode()
    if (svgdoc.adoptNode) {
      svgcontent = svgdoc.adoptNode(newDoc.documentElement);
    } else {
      svgcontent = svgdoc.importNode(newDoc.documentElement, true);
    }
 
    svgroot.append(svgcontent);
    const content = $(svgcontent);
 
    canvas.current_drawing_ = new draw.Drawing(svgcontent, idprefix);
 
    // retrieve or set the nonce
    const nonce = getCurrentDrawing().getNonce();
    if (nonce) {
      call('setnonce', nonce);
    } else {
      call('unsetnonce');
    }
 
    // change image href vals if possible
    content.find('image').each(function () {
      const image = this;
      preventClickDefault(image);
      const val = getHref(this);
      if (val) {
        if (val.startsWith('data:')) {
          // Check if an SVG-edit data URI
          const m = val.match(/svgedit_url=(.*?);/);
          if (m) {
            const url = decodeURIComponent(m[1]);
            $(new Image()).load(function () {
              image.setAttributeNS(NS.XLINK, 'xlink:href', url);
            }).attr('src', url);
          }
        }
        // Add to encodableImages if it loads
        canvas.embedImage(val);
      }
    });
 
    // Wrap child SVGs in group elements
    content.find('svg').each(function () {
      // Skip if it's in a <defs>
      if ($(this).closest('defs').length) { return; }
 
      uniquifyElems(this);
 
      // Check if it already has a gsvg group
      const pa = this.parentNode;
      if (pa.childNodes.length === 1 && pa.nodeName === 'g') {
        $(pa).data('gsvg', this);
        pa.id = pa.id || getNextId();
      } else {
        groupSvgElem(this);
      }
    });
 
    // For Firefox: Put all paint elems in defs
    if (isGecko()) {
      content.find('linearGradient, radialGradient, pattern').appendTo(findDefs());
    }
 
    // Set ref element for <use> elements
 
    // TODO: This should also be done if the object is re-added through "redo"
    setUseData(content);
 
    convertGradients(content[0]);
 
    const attrs = {
      id: 'svgcontent',
      overflow: curConfig.show_outside_canvas ? 'visible' : 'hidden'
    };
 
    let percs = false;
 
    // determine proper size
    if (content.attr('viewBox')) {
      const vb = content.attr('viewBox').split(' ');
      attrs.width = vb[2];
      attrs.height = vb[3];
    // handle content that doesn't have a viewBox
    } else {
      $.each(['width', 'height'], function (i, dim) {
        // Set to 100 if not given
        const val = content.attr(dim) || '100%';
 
        if (String(val).substr(-1) === '%') {
          // Use user units if percentage given
          percs = true;
        } else {
          attrs[dim] = convertToNum(dim, val);
        }
      });
    }
 
    // identify layers
    draw.identifyLayers();
 
    // Give ID for any visible layer children missing one
    content.children().find(visElems).each(function () {
      if (!this.id) { this.id = getNextId(); }
    });
 
    // Percentage width/height, so let's base it on visible elements
    if (percs) {
      const bb = getStrokedBBoxDefaultVisible();
      attrs.width = bb.width + bb.x;
      attrs.height = bb.height + bb.y;
    }
 
    // Just in case negative numbers are given or
    // result from the percs calculation
    if (attrs.width <= 0) { attrs.width = 100; }
    if (attrs.height <= 0) { attrs.height = 100; }
 
    content.attr(attrs);
    this.contentW = attrs.width;
    this.contentH = attrs.height;
 
    batchCmd.addSubCommand(new InsertElementCommand(svgcontent));
    // update root to the correct size
    const changes = content.attr(['width', 'height']);
    batchCmd.addSubCommand(new ChangeElementCommand(svgroot, changes));
 
    // reset zoom
    currentZoom = 1;
 
    // reset transform lists
    resetListMap();
    clearSelection();
    pathModule.clearData();
    svgroot.append(selectorManager.selectorParentGroup);
 
    if (!preventUndo) addCommandToHistory(batchCmd);
    call('changed', [svgcontent]);
  } catch (e) {
    console.log(e);
    return false;
  }
 
  return true;
};
 
/**
* This function imports the input SVG XML as a `<symbol>` in the `<defs>`, then adds a
* `<use>` to the current layer.
* @function module:svgcanvas.SvgCanvas#importSvgString
* @param {string} xmlString - The SVG as XML text.
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {null|Element} This function returns null if the import was unsuccessful, or the element otherwise.
* @todo
* - properly handle if namespace is introduced by imported content (must add to svgcontent
* and update all prefixes in the imported node)
* - properly handle recalculating dimensions, `recalculateDimensions()` doesn't handle
* arbitrary transform lists, but makes some assumptions about how the transform list
* was obtained
*/
this.importSvgString = function (xmlString) {
  let j, ts, useEl;
  try {
    // Get unique ID
    const uid = encode64(xmlString.length + xmlString).substr(0, 32);
 
    let useExisting = false;
    // Look for symbol and make sure symbol exists in image
    if (importIds[uid]) {
      if ($(importIds[uid].symbol).parents('#svgroot').length) {
        useExisting = true;
      }
    }
 
    const batchCmd = new BatchCommand('Import Image');
    let symbol;
    if (useExisting) {
      ({symbol} = importIds[uid]);
      ts = importIds[uid].xform;
    } else {
      // convert string into XML document
      const newDoc = text2xml(xmlString);
 
      this.prepareSvg(newDoc);
 
      // import new svg document into our document
      let svg;
      // If DOM3 adoptNode() available, use it. Otherwise fall back to DOM2 importNode()
      if (svgdoc.adoptNode) {
        svg = svgdoc.adoptNode(newDoc.documentElement);
      } else {
        svg = svgdoc.importNode(newDoc.documentElement, true);
      }
 
      uniquifyElems(svg);
 
      const innerw = convertToNum('width', svg.getAttribute('width')),
        innerh = convertToNum('height', svg.getAttribute('height')),
        innervb = svg.getAttribute('viewBox'),
        // if no explicit viewbox, create one out of the width and height
        vb = innervb ? innervb.split(' ') : [0, 0, innerw, innerh];
      for (j = 0; j < 4; ++j) {
        vb[j] = +(vb[j]);
      }
 
      // TODO: properly handle preserveAspectRatio
      const // canvasw = +svgcontent.getAttribute('width'),
        canvash = +svgcontent.getAttribute('height');
      // imported content should be 1/3 of the canvas on its largest dimension
 
      if (innerh > innerw) {
        ts = 'scale(' + (canvash / 3) / vb[3] + ')';
      } else {
        ts = 'scale(' + (canvash / 3) / vb[2] + ')';
      }
 
      // Hack to make recalculateDimensions understand how to scale
      ts = 'translate(0) ' + ts + ' translate(0)';
 
      symbol = svgdoc.createElementNS(NS.SVG, 'symbol');
      const defs = findDefs();
 
      if (isGecko()) {
        // Move all gradients into root for Firefox, workaround for this bug:
        // https://bugzilla.mozilla.org/show_bug.cgi?id=353575
        // TODO: Make this properly undo-able.
        $(svg).find('linearGradient, radialGradient, pattern').appendTo(defs);
      }
 
      while (svg.firstChild) {
        const first = svg.firstChild;
        symbol.append(first);
      }
      const attrs = svg.attributes;
      for (let i = 0; i < attrs.length; i++) {
        const attr = attrs[i];
        symbol.setAttribute(attr.nodeName, attr.value);
      }
      symbol.id = getNextId();
 
      // Store data
      importIds[uid] = {
        symbol,
        xform: ts
      };
 
      findDefs().append(symbol);
      batchCmd.addSubCommand(new InsertElementCommand(symbol));
    }
 
    useEl = svgdoc.createElementNS(NS.SVG, 'use');
    useEl.id = getNextId();
    setHref(useEl, '#' + symbol.id);
 
    (currentGroup || getCurrentDrawing().getCurrentLayer()).append(useEl);
    batchCmd.addSubCommand(new InsertElementCommand(useEl));
    clearSelection();
 
    useEl.setAttribute('transform', ts);
    recalculateDimensions(useEl);
    $(useEl).data('symbol', symbol).data('ref', symbol);
    addToSelection([useEl]);
 
    // TODO: Find way to add this in a recalculateDimensions-parsable way
    // if (vb[0] !== 0 || vb[1] !== 0) {
    //   ts = 'translate(' + (-vb[0]) + ',' + (-vb[1]) + ') ' + ts;
    // }
    addCommandToHistory(batchCmd);
    call('changed', [svgcontent]);
  } catch (e) {
    console.log(e);
    return null;
  }
 
  // we want to return the element so we can automatically select it
  return useEl;
};
 
// Could deprecate, but besides external uses, their usage makes clear that
//  canvas is a dependency for all of these
[
  'identifyLayers', 'createLayer', 'cloneLayer', 'deleteCurrentLayer',
  'setCurrentLayer', 'renameCurrentLayer', 'setCurrentLayerPosition',
  'setLayerVisibility', 'moveSelectedToLayer', 'mergeLayer', 'mergeAllLayers',
  'leaveContext', 'setContext'
].forEach((prop) => {
  canvas[prop] = draw[prop];
});
draw.init(
  /**
  * @implements {module:draw.DrawCanvasInit}
  */
  {
    pathActions,
    getCurrentGroup () {
      return currentGroup;
    },
    setCurrentGroup (cg) {
      currentGroup = cg;
    },
    getSelectedElements,
    getSVGContent,
    undoMgr,
    elData,
    getCurrentDrawing,
    clearSelection,
    call,
    addCommandToHistory,
    /**
     * @fires module:svgcanvas.SvgCanvas#event:changed
     * @returns {undefined}
     */
    changeSVGContent () {
      call('changed', [svgcontent]);
    }
  }
);
 
/**
* Group: Document functions
*/
 
/**
* Clears the current document. This is not an undoable action.
* @function module:svgcanvas.SvgCanvas#clear
* @fires module:svgcanvas.SvgCanvas#event:cleared
* @returns {undefined}
*/
this.clear = function () {
  pathActions.clear();
 
  clearSelection();
 
  // clear the svgcontent node
  canvas.clearSvgContentElement();
 
  // create new document
  canvas.current_drawing_ = new draw.Drawing(svgcontent);
 
  // create empty first layer
  canvas.createLayer('Layer 1');
 
  // clear the undo stack
  canvas.undoMgr.resetUndoStack();
 
  // reset the selector manager
  selectorManager.initGroup();
 
  // reset the rubber band box
  rubberBox = selectorManager.getRubberBandBox();
 
  call('cleared');
};
 
// Alias function
this.linkControlPoints = pathActions.linkControlPoints;
 
/**
* @function module:svgcanvas.SvgCanvas#getContentElem
* @returns {Element} The content DOM element
*/
this.getContentElem = function () { return svgcontent; };
 
/**
* @function module:svgcanvas.SvgCanvas#getRootElem
* @returns {SVGSVGElement} The root DOM element
*/
this.getRootElem = function () { return svgroot; };
 
/**
* @typedef {PlainObject} DimensionsAndZoom
* @property {Float} w Width
* @property {Float} h Height
* @property {Float} zoom Zoom
*/
 
/**
* @function module:svgcanvas.SvgCanvas#getResolution
* @returns {DimensionsAndZoom} The current dimensions and zoom level in an object
*/
const getResolution = this.getResolution = function () {
//    const vb = svgcontent.getAttribute('viewBox').split(' ');
//    return {w:vb[2], h:vb[3], zoom: currentZoom};
 
  const w = svgcontent.getAttribute('width') / currentZoom;
  const h = svgcontent.getAttribute('height') / currentZoom;
 
  return {
    w,
    h,
    zoom: currentZoom
  };
};
 
/**
* @function module:svgcanvas.SvgCanvas#getSnapToGrid
* @returns {boolean} The current snap to grid setting
*/
this.getSnapToGrid = function () { return curConfig.gridSnapping; };
 
/**
* @function module:svgcanvas.SvgCanvas#getVersion
* @returns {string} A string which describes the revision number of SvgCanvas.
*/
this.getVersion = function () {
  return 'svgcanvas.js ($Rev$)';
};
 
/**
* Update interface strings with given values.
* @function module:svgcanvas.SvgCanvas#setUiStrings
* @param {module:path.uiStrings} strs - Object with strings (see the [locales API]{@link module:locale.LocaleStrings} and the [tutorial]{@tutorial LocaleDocs})
* @returns {undefined}
*/
this.setUiStrings = function (strs) {
  Object.assign(uiStrings, strs.notification);
  pathModule.setUiStrings(strs);
};
 
/**
* Update configuration options with given values.
* @function module:svgcanvas.SvgCanvas#setConfig
* @param {module:SVGEditor.Config} opts - Object with options
* @returns {undefined}
*/
this.setConfig = function (opts) {
  Object.assign(curConfig, opts);
};
 
/**
* @function module:svgcanvas.SvgCanvas#getTitle
* @param {Element} elem
* @returns {string|undefined} the current group/SVG's title contents
*/
this.getTitle = function (elem) {
  elem = elem || selectedElements[0];
  if (!elem) { return; }
  elem = $(elem).data('gsvg') || $(elem).data('symbol') || elem;
  const childs = elem.childNodes;
  for (let i = 0; i < childs.length; i++) {
    if (childs[i].nodeName === 'title') {
      return childs[i].textContent;
    }
  }
  return '';
};
 
/**
* Sets the group/SVG's title content.
* @function module:svgcanvas.SvgCanvas#setGroupTitle
* @param {string} val
* @todo Combine this with `setDocumentTitle`
* @returns {undefined}
*/
this.setGroupTitle = function (val) {
  let elem = selectedElements[0];
  elem = $(elem).data('gsvg') || elem;
 
  const ts = $(elem).children('title');
 
  const batchCmd = new BatchCommand('Set Label');
 
  let title;
  if (!val.length) {
    // Remove title element
    const tsNextSibling = ts.nextSibling;
    batchCmd.addSubCommand(new RemoveElementCommand(ts[0], tsNextSibling, elem));
    ts.remove();
  } else if (ts.length) {
    // Change title contents
    title = ts[0];
    batchCmd.addSubCommand(new ChangeElementCommand(title, {'#text': title.textContent}));
    title.textContent = val;
  } else {
    // Add title element
    title = svgdoc.createElementNS(NS.SVG, 'title');
    title.textContent = val;
    $(elem).prepend(title);
    batchCmd.addSubCommand(new InsertElementCommand(title));
  }
 
  addCommandToHistory(batchCmd);
};
 
/**
* @function module:svgcanvas.SvgCanvas#getDocumentTitle
* @returns {string|undefined} The current document title or an empty string if not found
*/
const getDocumentTitle = this.getDocumentTitle = function () {
  return canvas.getTitle(svgcontent);
};
 
/**
* Adds/updates a title element for the document with the given name.
* This is an undoable action.
* @function module:svgcanvas.SvgCanvas#setDocumentTitle
* @param {string} newTitle - String with the new title
* @returns {undefined}
*/
this.setDocumentTitle = function (newTitle) {
  const childs = svgcontent.childNodes;
  let docTitle = false, oldTitle = '';
 
  const batchCmd = new BatchCommand('Change Image Title');
 
  for (let i = 0; i < childs.length; i++) {
    if (childs[i].nodeName === 'title') {
      docTitle = childs[i];
      oldTitle = docTitle.textContent;
      break;
    }
  }
  if (!docTitle) {
    docTitle = svgdoc.createElementNS(NS.SVG, 'title');
    svgcontent.insertBefore(docTitle, svgcontent.firstChild);
    // svgcontent.firstChild.before(docTitle); // Ok to replace above with this?
  }
 
  if (newTitle.length) {
    docTitle.textContent = newTitle;
  } else {
    // No title given, so element is not necessary
    docTitle.remove();
  }
  batchCmd.addSubCommand(new ChangeElementCommand(docTitle, {'#text': oldTitle}));
  addCommandToHistory(batchCmd);
};
 
/**
* Returns the editor's namespace URL, optionally adding it to the root element.
* @function module:svgcanvas.SvgCanvas#getEditorNS
* @param {boolean} [add] - Indicates whether or not to add the namespace value
* @returns {string} The editor's namespace URL
*/
this.getEditorNS = function (add) {
  if (add) {
    svgcontent.setAttribute('xmlns:se', NS.SE);
  }
  return NS.SE;
};
 
/**
* Changes the document's dimensions to the given size.
* @function module:svgcanvas.SvgCanvas#setResolution
* @param {Float|"fit"} x - Number with the width of the new dimensions in user units.
* Can also be the string "fit" to indicate "fit to content".
* @param {Float} y - Number with the height of the new dimensions in user units.
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {boolean} Indicates if resolution change was successful.
* It will fail on "fit to content" option with no content to fit to.
*/
this.setResolution = function (x, y) {
  const res = getResolution();
  const {w, h} = res;
  let batchCmd;
 
  if (x === 'fit') {
    // Get bounding box
    const bbox = getStrokedBBoxDefaultVisible();
 
    if (bbox) {
      batchCmd = new BatchCommand('Fit Canvas to Content');
      const visEls = getVisibleElements();
      addToSelection(visEls);
      const dx = [], dy = [];
      $.each(visEls, function (i, item) {
        dx.push(bbox.x * -1);
        dy.push(bbox.y * -1);
      });
 
      const cmd = canvas.moveSelectedElements(dx, dy, true);
      batchCmd.addSubCommand(cmd);
      clearSelection();
 
      x = Math.round(bbox.width);
      y = Math.round(bbox.height);
    } else {
      return false;
    }
  }
  if (x !== w || y !== h) {
    if (!batchCmd) {
      batchCmd = new BatchCommand('Change Image Dimensions');
    }
 
    x = convertToNum('width', x);
    y = convertToNum('height', y);
 
    svgcontent.setAttribute('width', x);
    svgcontent.setAttribute('height', y);
 
    this.contentW = x;
    this.contentH = y;
    batchCmd.addSubCommand(new ChangeElementCommand(svgcontent, {width: w, height: h}));
 
    svgcontent.setAttribute('viewBox', [0, 0, x / currentZoom, y / currentZoom].join(' '));
    batchCmd.addSubCommand(new ChangeElementCommand(svgcontent, {viewBox: ['0 0', w, h].join(' ')}));
 
    addCommandToHistory(batchCmd);
    call('changed', [svgcontent]);
  }
  return true;
};
 
/**
* @typedef {module:jQueryAttr.Attributes} module:svgcanvas.ElementPositionInCanvas
* @property {Float} x
* @property {Float} y
*/
 
/**
* @function module:svgcanvas.SvgCanvas#getOffset
* @returns {module:svgcanvas.ElementPositionInCanvas} An object with `x`, `y` values indicating the svgcontent element's
* position in the editor's canvas.
*/
this.getOffset = function () {
  return $(svgcontent).attr(['x', 'y']);
};
 
/**
 * @typedef {PlainObject} module:svgcanvas.ZoomAndBBox
 * @property {Float} zoom
 * @property {module:utilities.BBoxObject} bbox
 */
/**
* Sets the zoom level on the canvas-side based on the given value.
* @function module:svgcanvas.SvgCanvas#setBBoxZoom
* @param {"selection"|"canvas"|"content"|"layer"|module:SVGEditor.BBoxObjectWithFactor} val - Bounding box object to zoom to or string indicating zoom option. Note: the object value type is defined in `svg-editor.js`
* @param {Integer} editorW - The editor's workarea box's width
* @param {Integer} editorH - The editor's workarea box's height
* @returns {module:svgcanvas.ZoomAndBBox|undefined}
*/
this.setBBoxZoom = function (val, editorW, editorH) {
  let spacer = 0.85;
  let bb;
  const calcZoom = function (bb) {
    if (!bb) { return false; }
    const wZoom = Math.round((editorW / bb.width) * 100 * spacer) / 100;
    const hZoom = Math.round((editorH / bb.height) * 100 * spacer) / 100;
    const zoom = Math.min(wZoom, hZoom);
    canvas.setZoom(zoom);
    return {zoom, bbox: bb};
  };
 
  if (typeof val === 'object') {
    bb = val;
    if (bb.width === 0 || bb.height === 0) {
      const newzoom = bb.zoom ? bb.zoom : currentZoom * bb.factor;
      canvas.setZoom(newzoom);
      return {zoom: currentZoom, bbox: bb};
    }
    return calcZoom(bb);
  }
 
  switch (val) {
  case 'selection':
    if (!selectedElements[0]) { return; }
    const selectedElems = $.map(selectedElements, function (n) { if (n) { return n; } });
    bb = getStrokedBBoxDefaultVisible(selectedElems);
    break;
  case 'canvas':
    const res = getResolution();
    spacer = 0.95;
    bb = {width: res.w, height: res.h, x: 0, y: 0};
    break;
  case 'content':
    bb = getStrokedBBoxDefaultVisible();
    break;
  case 'layer':
    bb = getStrokedBBoxDefaultVisible(getVisibleElements(getCurrentDrawing().getCurrentLayer()));
    break;
  default:
    return;
  }
  return calcZoom(bb);
};
 
/**
* The zoom level has changed. Supplies the new zoom level as a number (not percentage).
* @event module:svgcanvas.SvgCanvas#event:ext-zoomChanged
* @type {Float}
*/
/**
* The bottom panel was updated
* @event module:svgcanvas.SvgCanvas#event:ext-toolButtonStateUpdate
* @type {PlainObject}
* @property {boolean} nofill Indicates fill is disabled
* @property {boolean} nostroke Indicates stroke is disabled
*/
/**
* The element selection has changed (elements were added/removed from selection)
* @event module:svgcanvas.SvgCanvas#event:ext-selectedChanged
* @type {PlainObject}
* @property {Element[]} elems Array of the newly selected elements
* @property {Element|null} selectedElement The single selected element
* @property {boolean} multiselected Indicates whether one or more elements were selected
*/
/**
* Called when part of element is in process of changing, generally on
* mousemove actions like rotate, move, etc.
* @event module:svgcanvas.SvgCanvas#event:ext-elementTransition
* @type {PlainObject}
* @property {Element[]} elems Array of transitioning elements
*/
/**
* One or more elements were changed
* @event module:svgcanvas.SvgCanvas#event:ext-elementChanged
* @type {PlainObject}
* @property {Element[]} elems Array of the affected elements
*/
/**
* Invoked as soon as the locale is ready
* @event module:svgcanvas.SvgCanvas#event:ext-langReady
* @type {PlainObject}
* @property {string} lang The two-letter language code
* @property {module:SVGEditor.uiStrings} uiStrings
* @property {module:SVGEditor~ImportLocale} importLocale
*/
/**
* The language was changed. Two-letter code of the new language.
* @event module:svgcanvas.SvgCanvas#event:ext-langChanged
* @type {string}
*/
/**
* Means for an extension to add locale data. The two-letter language code.
* @event module:svgcanvas.SvgCanvas#event:ext-addLangData
* @type {PlainObject}
* @property {string} lang
* @property {module:SVGEditor~ImportLocale} importLocale
*/
/**
 * Called when new image is created
 * @event module:svgcanvas.SvgCanvas#event:ext-onNewDocument
 * @type {undefined}
 */
/**
 * Called when sidepanel is resized or toggled
 * @event module:svgcanvas.SvgCanvas#event:ext-workareaResized
 * @type {undefined}
*/
/**
 * Called upon addition of the extension, or, if svgicons are set,
 * after the icons are ready when extension SVG icons have loaded.
 * @event module:svgcanvas.SvgCanvas#event:ext-callback
 * @type {undefined}
*/
 
/**
* Sets the zoom to the given level.
* @function module:svgcanvas.SvgCanvas#setZoom
* @param {Float} zoomLevel - Float indicating the zoom level to change to
* @fires module:svgcanvas.SvgCanvas#event:ext-zoomChanged
* @returns {undefined}
*/
this.setZoom = function (zoomLevel) {
  const res = getResolution();
  svgcontent.setAttribute('viewBox', '0 0 ' + res.w / zoomLevel + ' ' + res.h / zoomLevel);
  currentZoom = zoomLevel;
  $.each(selectedElements, function (i, elem) {
    if (!elem) { return; }
    selectorManager.requestSelector(elem).resize();
  });
  pathActions.zoomChange();
  runExtensions('zoomChanged', /** @type {module:svgcanvas.SvgCanvas#event:ext-zoomChanged} */ zoomLevel);
};
 
/**
* @function module:svgcanvas.SvgCanvas#getMode
* @returns {string} The current editor mode string
*/
this.getMode = function () {
  return currentMode;
};
 
/**
* Sets the editor's mode to the given string.
* @function module:svgcanvas.SvgCanvas#setMode
* @param {string} name - String with the new mode to change to
* @returns {undefined}
*/
this.setMode = function (name) {
  pathActions.clear(true);
  textActions.clear();
  curProperties = (selectedElements[0] && selectedElements[0].nodeName === 'text') ? curText : curShape;
  currentMode = name;
};
 
/**
* Group: Element Styling
*/
 
/**
* @typedef {PlainObject} module:svgcanvas.PaintOptions
* @property {"solidColor"} type
*/
 
/**
* @function module:svgcanvas.SvgCanvas#getColor
* @param {string} type
* @returns {string|module:svgcanvas.PaintOptions|Float|module:jGraduate~Paint} The current fill/stroke option
*/
this.getColor = function (type) {
  return curProperties[type];
};
 
/**
* Change the current stroke/fill color/gradient value.
* @function module:svgcanvas.SvgCanvas#setColor
* @param {string} type - String indicating fill or stroke
* @param {string} val - The value to set the stroke attribute to
* @param {boolean} preventUndo - Boolean indicating whether or not this should be an undoable option
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {undefined}
*/
this.setColor = function (type, val, preventUndo) {
  curShape[type] = val;
  curProperties[type + '_paint'] = {type: 'solidColor'};
  const elems = [];
  function addNonG (e) {
    if (e.nodeName !== 'g') {
      elems.push(e);
    }
  }
  let i = selectedElements.length;
  while (i--) {
    const elem = selectedElements[i];
    if (elem) {
      if (elem.tagName === 'g') {
        walkTree(elem, addNonG);
      } else {
        if (type === 'fill') {
          if (elem.tagName !== 'polyline' && elem.tagName !== 'line') {
            elems.push(elem);
          }
        } else {
          elems.push(elem);
        }
      }
    }
  }
  if (elems.length > 0) {
    if (!preventUndo) {
      changeSelectedAttribute(type, val, elems);
      call('changed', elems);
    } else {
      changeSelectedAttributeNoUndo(type, val, elems);
    }
  }
};
 
/**
* Apply the current gradient to selected element's fill or stroke.
* @function module:svgcanvas.SvgCanvas#setGradient
* @param {"fill"|"stroke"} type - String indicating "fill" or "stroke" to apply to an element
* @returns {undefined}
*/
const setGradient = this.setGradient = function (type) {
  if (!curProperties[type + '_paint'] || curProperties[type + '_paint'].type === 'solidColor') { return; }
  let grad = canvas[type + 'Grad'];
  // find out if there is a duplicate gradient already in the defs
  const duplicateGrad = findDuplicateGradient(grad);
  const defs = findDefs();
  // no duplicate found, so import gradient into defs
  if (!duplicateGrad) {
    // const origGrad = grad;
    grad = defs.appendChild(svgdoc.importNode(grad, true));
    // get next id and set it on the grad
    grad.id = getNextId();
  } else { // use existing gradient
    grad = duplicateGrad;
  }
  canvas.setColor(type, 'url(#' + grad.id + ')');
};
 
/**
* Check if exact gradient already exists.
* @function module:svgcanvas~findDuplicateGradient
* @param {SVGGradientElement} grad - The gradient DOM element to compare to others
* @returns {SVGGradientElement} The existing gradient if found, `null` if not
*/
const findDuplicateGradient = function (grad) {
  const defs = findDefs();
  const existingGrads = $(defs).find('linearGradient, radialGradient');
  let i = existingGrads.length;
  const radAttrs = ['r', 'cx', 'cy', 'fx', 'fy'];
  while (i--) {
    const og = existingGrads[i];
    if (grad.tagName === 'linearGradient') {
      if (grad.getAttribute('x1') !== og.getAttribute('x1') ||
        grad.getAttribute('y1') !== og.getAttribute('y1') ||
        grad.getAttribute('x2') !== og.getAttribute('x2') ||
        grad.getAttribute('y2') !== og.getAttribute('y2')
      ) {
        continue;
      }
    } else {
      const gradAttrs = $(grad).attr(radAttrs);
      const ogAttrs = $(og).attr(radAttrs);
 
      let diff = false;
      $.each(radAttrs, function (i, attr) {
        if (gradAttrs[attr] !== ogAttrs[attr]) { diff = true; }
      });
 
      if (diff) { continue; }
    }
 
    // else could be a duplicate, iterate through stops
    const stops = grad.getElementsByTagNameNS(NS.SVG, 'stop');
    const ostops = og.getElementsByTagNameNS(NS.SVG, 'stop');
 
    if (stops.length !== ostops.length) {
      continue;
    }
 
    let j = stops.length;
    while (j--) {
      const stop = stops[j];
      const ostop = ostops[j];
 
      if (stop.getAttribute('offset') !== ostop.getAttribute('offset') ||
        stop.getAttribute('stop-opacity') !== ostop.getAttribute('stop-opacity') ||
        stop.getAttribute('stop-color') !== ostop.getAttribute('stop-color')) {
        break;
      }
    }
 
    if (j === -1) {
      return og;
    }
  } // for each gradient in defs
 
  return null;
};
 
/**
* Set a color/gradient to a fill/stroke.
* @function module:svgcanvas.SvgCanvas#setPaint
* @param {"fill"|"stroke"} type - String with "fill" or "stroke"
* @param {module:jGraduate.jGraduatePaintOptions} paint - The jGraduate paint object to apply
* @returns {undefined}
*/
this.setPaint = function (type, paint) {
  // make a copy
  const p = new $.jGraduate.Paint(paint);
  this.setPaintOpacity(type, p.alpha / 100, true);
 
  // now set the current paint object
  curProperties[type + '_paint'] = p;
  switch (p.type) {
  case 'solidColor':
    this.setColor(type, p.solidColor !== 'none' ? '#' + p.solidColor : 'none');
    break;
  case 'linearGradient':
  case 'radialGradient':
    canvas[type + 'Grad'] = p[p.type];
    setGradient(type);
    break;
  }
};
 
/**
* @function module:svgcanvas.SvgCanvas#setStrokePaint
* @param {module:jGraduate~Paint} paint
* @returns {undefined}
*/
this.setStrokePaint = function (paint) {
  this.setPaint('stroke', paint);
};
 
/**
* @function module:svgcanvas.SvgCanvas#setFillPaint
* @param {module:jGraduate~Paint} paint
* @returns {undefined}
*/
this.setFillPaint = function (paint) {
  this.setPaint('fill', paint);
};
 
/**
* @function module:svgcanvas.SvgCanvas#getStrokeWidth
* @returns {Float|string} The current stroke-width value
*/
this.getStrokeWidth = function () {
  return curProperties.stroke_width;
};
 
/**
* Sets the stroke width for the current selected elements.
* When attempting to set a line's width to 0, this changes it to 1 instead.
* @function module:svgcanvas.SvgCanvas#setStrokeWidth
* @param {Float} val - A Float indicating the new stroke width value
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {undefined}
*/
this.setStrokeWidth = function (val) {
  if (val === 0 && ['line', 'path'].includes(currentMode)) {
    canvas.setStrokeWidth(1);
    return;
  }
  curProperties.stroke_width = val;
 
  const elems = [];
  function addNonG (e) {
    if (e.nodeName !== 'g') {
      elems.push(e);
    }
  }
  let i = selectedElements.length;
  while (i--) {
    const elem = selectedElements[i];
    if (elem) {
      if (elem.tagName === 'g') {
        walkTree(elem, addNonG);
      } else {
        elems.push(elem);
      }
    }
  }
  if (elems.length > 0) {
    changeSelectedAttribute('stroke-width', val, elems);
    call('changed', selectedElements);
  }
};
 
/**
* Set the given stroke-related attribute the given value for selected elements.
* @function module:svgcanvas.SvgCanvas#setStrokeAttr
* @param {string} attr - String with the attribute name
* @param {string|Float} val - String or number with the attribute value
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {undefined}
*/
this.setStrokeAttr = function (attr, val) {
  curShape[attr.replace('-', '_')] = val;
  const elems = [];
 
  let i = selectedElements.length;
  while (i--) {
    const elem = selectedElements[i];
    if (elem) {
      if (elem.tagName === 'g') {
        walkTree(elem, function (e) { if (e.nodeName !== 'g') { elems.push(e); } });
      } else {
        elems.push(elem);
      }
    }
  }
  if (elems.length > 0) {
    changeSelectedAttribute(attr, val, elems);
    call('changed', selectedElements);
  }
};
 
/**
* @typedef {PlainObject} module:svgcanvas.StyleOptions
* @property {string} fill
* @property {Float} fill_opacity
* @property {string} stroke
* @property {Float} stroke_width
* @property {string} stroke_dasharray
* @property {string} stroke_linejoin
* @property {string} stroke_linecap
* @property {Float} stroke_opacity
* @property {Float} opacity
*/
 
/**
* @function module:svgcanvas.SvgCanvas#getStyle
* @returns {module:svgcanvas.StyleOptions} current style options
*/
this.getStyle = function () {
  return curShape;
};
 
/**
* @function module:svgcanvas.SvgCanvas#getOpacity
* @returns {Float} the current opacity
*/
this.getOpacity = getOpacity;
 
/**
* Sets the given opacity on the current selected elements.
* @function module:svgcanvas.SvgCanvas#setOpacity
* @param {string} val
* @returns {undefined}
*/
this.setOpacity = function (val) {
  curShape.opacity = val;
  changeSelectedAttribute('opacity', val);
};
 
/**
* @function module:svgcanvas.SvgCanvas#getFillOpacity
* @returns {Float} the current fill opacity
*/
this.getFillOpacity = function () {
  return curShape.fill_opacity;
};
 
/**
* @function module:svgcanvas.SvgCanvas#getStrokeOpacity
* @returns {string} the current stroke opacity
*/
this.getStrokeOpacity = function () {
  return curShape.stroke_opacity;
};
 
/**
* Sets the current fill/stroke opacity.
* @function module:svgcanvas.SvgCanvas#setPaintOpacity
* @param {string} type - String with "fill" or "stroke"
* @param {Float} val - Float with the new opacity value
* @param {boolean} preventUndo - Indicates whether or not this should be an undoable action
* @returns {undefined}
*/
this.setPaintOpacity = function (type, val, preventUndo) {
  curShape[type + '_opacity'] = val;
  if (!preventUndo) {
    changeSelectedAttribute(type + '-opacity', val);
  } else {
    changeSelectedAttributeNoUndo(type + '-opacity', val);
  }
};
 
/**
* Gets the current fill/stroke opacity.
* @function module:svgcanvas.SvgCanvas#getPaintOpacity
* @param {"fill"|"stroke"} type - String with "fill" or "stroke"
* @returns {Float} Fill/stroke opacity
*/
this.getPaintOpacity = function (type) {
  return type === 'fill' ? this.getFillOpacity() : this.getStrokeOpacity();
};
 
/**
* Gets the `stdDeviation` blur value of the given element.
* @function module:svgcanvas.SvgCanvas#getBlur
* @param {Element} elem - The element to check the blur value for
* @returns {string} stdDeviation blur attribute value
*/
this.getBlur = function (elem) {
  let val = 0;
  // const elem = selectedElements[0];
 
  if (elem) {
    const filterUrl = elem.getAttribute('filter');
    if (filterUrl) {
      const blur = getElem(elem.id + '_blur');
      if (blur) {
        val = blur.firstChild.getAttribute('stdDeviation');
      }
    }
  }
  return val;
};
 
(function () {
let curCommand = null;
let filter = null;
let filterHidden = false;
 
/**
* Sets the `stdDeviation` blur value on the selected element without being undoable.
* @function module:svgcanvas.SvgCanvas#setBlurNoUndo
* @param {Float} val - The new `stdDeviation` value
* @returns {undefined}
*/
canvas.setBlurNoUndo = function (val) {
  if (!filter) {
    canvas.setBlur(val);
    return;
  }
  if (val === 0) {
    // Don't change the StdDev, as that will hide the element.
    // Instead, just remove the value for "filter"
    changeSelectedAttributeNoUndo('filter', '');
    filterHidden = true;
  } else {
    const elem = selectedElements[0];
    if (filterHidden) {
      changeSelectedAttributeNoUndo('filter', 'url(#' + elem.id + '_blur)');
    }
    if (isWebkit()) {
      console.log('e', elem);
      elem.removeAttribute('filter');
      elem.setAttribute('filter', 'url(#' + elem.id + '_blur)');
    }
    changeSelectedAttributeNoUndo('stdDeviation', val, [filter.firstChild]);
    canvas.setBlurOffsets(filter, val);
  }
};
 
function finishChange () {
  const bCmd = canvas.undoMgr.finishUndoableChange();
  curCommand.addSubCommand(bCmd);
  addCommandToHistory(curCommand);
  curCommand = null;
  filter = null;
}
 
/**
* Sets the `x`, `y`, `width`, `height` values of the filter element in order to
* make the blur not be clipped. Removes them if not neeeded.
* @function module:svgcanvas.SvgCanvas#setBlurOffsets
* @param {Element} filter - The filter DOM element to update
* @param {Float} stdDev - The standard deviation value on which to base the offset size
* @returns {undefined}
*/
canvas.setBlurOffsets = function (filter, stdDev) {
  if (stdDev > 3) {
    // TODO: Create algorithm here where size is based on expected blur
    assignAttributes(filter, {
      x: '-50%',
      y: '-50%',
      width: '200%',
      height: '200%'
    }, 100);
  } else {
    // Removing these attributes hides text in Chrome (see Issue 579)
    if (!isWebkit()) {
      filter.removeAttribute('x');
      filter.removeAttribute('y');
      filter.removeAttribute('width');
      filter.removeAttribute('height');
    }
  }
};
 
/**
* Adds/updates the blur filter to the selected element.
* @function module:svgcanvas.SvgCanvas#setBlur
* @param {Float} val - Float with the new `stdDeviation` blur value
* @param {boolean} complete - Whether or not the action should be completed (to add to the undo manager)
* @returns {undefined}
*/
canvas.setBlur = function (val, complete) {
  if (curCommand) {
    finishChange();
    return;
  }
 
  // Looks for associated blur, creates one if not found
  const elem = selectedElements[0];
  const elemId = elem.id;
  filter = getElem(elemId + '_blur');
 
  val -= 0;
 
  const batchCmd = new BatchCommand();
 
  // Blur found!
  if (filter) {
    if (val === 0) {
      filter = null;
    }
  } else {
    // Not found, so create
    const newblur = addSVGElementFromJson({element: 'feGaussianBlur',
      attr: {
        in: 'SourceGraphic',
        stdDeviation: val
      }
    });
 
    filter = addSVGElementFromJson({element: 'filter',
      attr: {
        id: elemId + '_blur'
      }
    });
 
    filter.append(newblur);
    findDefs().append(filter);
 
    batchCmd.addSubCommand(new InsertElementCommand(filter));
  }
 
  const changes = {filter: elem.getAttribute('filter')};
 
  if (val === 0) {
    elem.removeAttribute('filter');
    batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
    return;
  }
 
  changeSelectedAttribute('filter', 'url(#' + elemId + '_blur)');
  batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
  canvas.setBlurOffsets(filter, val);
 
  curCommand = batchCmd;
  canvas.undoMgr.beginUndoableChange('stdDeviation', [filter ? filter.firstChild : null]);
  if (complete) {
    canvas.setBlurNoUndo(val);
    finishChange();
  }
};
}());
 
/**
* Check whether selected element is bold or not.
* @function module:svgcanvas.SvgCanvas#getBold
* @returns {boolean} Indicates whether or not element is bold
*/
this.getBold = function () {
  // should only have one element selected
  const selected = selectedElements[0];
  if (selected != null && selected.tagName === 'text' &&
    selectedElements[1] == null) {
    return (selected.getAttribute('font-weight') === 'bold');
  }
  return false;
};
 
/**
* Make the selected element bold or normal.
* @function module:svgcanvas.SvgCanvas#setBold
* @param {boolean} b - Indicates bold (`true`) or normal (`false`)
* @returns {undefined}
*/
this.setBold = function (b) {
  const selected = selectedElements[0];
  if (selected != null && selected.tagName === 'text' &&
    selectedElements[1] == null) {
    changeSelectedAttribute('font-weight', b ? 'bold' : 'normal');
  }
  if (!selectedElements[0].textContent) {
    textActions.setCursor();
  }
};
 
/**
* Check whether selected element is in italics or not.
* @function module:svgcanvas.SvgCanvas#getItalic
* @returns {boolean} Indicates whether or not element is italic
*/
this.getItalic = function () {
  const selected = selectedElements[0];
  if (selected != null && selected.tagName === 'text' &&
    selectedElements[1] == null) {
    return (selected.getAttribute('font-style') === 'italic');
  }
  return false;
};
 
/**
* Make the selected element italic or normal.
* @function module:svgcanvas.SvgCanvas#setItalic
* @param {boolean} i - Indicates italic (`true`) or normal (`false`)
* @returns {undefined}
*/
this.setItalic = function (i) {
  const selected = selectedElements[0];
  if (selected != null && selected.tagName === 'text' &&
    selectedElements[1] == null) {
    changeSelectedAttribute('font-style', i ? 'italic' : 'normal');
  }
  if (!selectedElements[0].textContent) {
    textActions.setCursor();
  }
};
 
/**
* @function module:svgcanvas.SvgCanvas#getFontFamily
* @returns {string} The current font family
*/
this.getFontFamily = function () {
  return curText.font_family;
};
 
/**
* Set the new font family.
* @function module:svgcanvas.SvgCanvas#setFontFamily
* @param {string} val - String with the new font family
* @returns {undefined}
*/
this.setFontFamily = function (val) {
  curText.font_family = val;
  changeSelectedAttribute('font-family', val);
  if (selectedElements[0] && !selectedElements[0].textContent) {
    textActions.setCursor();
  }
};
 
/**
* Set the new font color.
* @function module:svgcanvas.SvgCanvas#setFontColor
* @param {string} val - String with the new font color
* @returns {undefined}
*/
this.setFontColor = function (val) {
  curText.fill = val;
  changeSelectedAttribute('fill', val);
};
 
/**
* @function module:svgcanvas.SvgCanvas#getFontColor
* @returns {string} The current font color
*/
this.getFontColor = function () {
  return curText.fill;
};
 
/**
* @function module:svgcanvas.SvgCanvas#getFontSize
* @returns {Float} The current font size
*/
this.getFontSize = function () {
  return curText.font_size;
};
 
/**
* Applies the given font size to the selected element.
* @function module:svgcanvas.SvgCanvas#setFontSize
* @param {Float} val - Float with the new font size
* @returns {undefined}
*/
this.setFontSize = function (val) {
  curText.font_size = val;
  changeSelectedAttribute('font-size', val);
  if (!selectedElements[0].textContent) {
    textActions.setCursor();
  }
};
 
/**
* @function module:svgcanvas.SvgCanvas#getText
* @returns {string} The current text (`textContent`) of the selected element
*/
this.getText = function () {
  const selected = selectedElements[0];
  if (selected == null) { return ''; }
  return selected.textContent;
};
 
/**
* Updates the text element with the given string.
* @function module:svgcanvas.SvgCanvas#setTextContent
* @param {string} val - String with the new text
* @returns {undefined}
*/
this.setTextContent = function (val) {
  changeSelectedAttribute('#text', val);
  textActions.init(val);
  textActions.setCursor();
};
 
/**
* Sets the new image URL for the selected image element. Updates its size if
* a new URL is given.
* @function module:svgcanvas.SvgCanvas#setImageURL
* @param {string} val - String with the image URL/path
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {undefined}
*/
this.setImageURL = function (val) {
  const elem = selectedElements[0];
  if (!elem) { return; }
 
  const attrs = $(elem).attr(['width', 'height']);
  const setsize = (!attrs.width || !attrs.height);
 
  const curHref = getHref(elem);
 
  // Do nothing if no URL change or size change
  if (curHref === val && !setsize) {
    return;
  }
 
  const batchCmd = new BatchCommand('Change Image URL');
 
  setHref(elem, val);
  batchCmd.addSubCommand(new ChangeElementCommand(elem, {
    '#href': curHref
  }));
 
  $(new Image()).load(function () {
    const changes = $(elem).attr(['width', 'height']);
 
    $(elem).attr({
      width: this.width,
      height: this.height
    });
 
    selectorManager.requestSelector(elem).resize();
 
    batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
    addCommandToHistory(batchCmd);
    call('changed', [elem]);
  }).attr('src', val);
};
 
/**
* Sets the new link URL for the selected anchor element.
* @function module:svgcanvas.SvgCanvas#setLinkURL
* @param {string} val - String with the link URL/path
* @returns {undefined}
*/
this.setLinkURL = function (val) {
  let elem = selectedElements[0];
  if (!elem) { return; }
  if (elem.tagName !== 'a') {
    // See if parent is an anchor
    const parentsA = $(elem).parents('a');
    if (parentsA.length) {
      elem = parentsA[0];
    } else {
      return;
    }
  }
 
  const curHref = getHref(elem);
 
  if (curHref === val) { return; }
 
  const batchCmd = new BatchCommand('Change Link URL');
 
  setHref(elem, val);
  batchCmd.addSubCommand(new ChangeElementCommand(elem, {
    '#href': curHref
  }));
 
  addCommandToHistory(batchCmd);
};
 
/**
* Sets the `rx` and `ry` values to the selected `rect` element
* to change its corner radius.
* @function module:svgcanvas.SvgCanvas#setRectRadius
* @param {string|Float} val - The new radius
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {undefined}
*/
this.setRectRadius = function (val) {
  const selected = selectedElements[0];
  if (selected != null && selected.tagName === 'rect') {
    const r = selected.getAttribute('rx');
    if (r !== String(val)) {
      selected.setAttribute('rx', val);
      selected.setAttribute('ry', val);
      addCommandToHistory(new ChangeElementCommand(selected, {rx: r, ry: r}, 'Radius'));
      call('changed', [selected]);
    }
  }
};
 
/**
* Wraps the selected element(s) in an anchor element or converts group to one.
* @function module:svgcanvas.SvgCanvas#makeHyperlink
* @param {string} url
* @returns {undefined}
*/
this.makeHyperlink = function (url) {
  canvas.groupSelectedElements('a', url);
 
  // TODO: If element is a single "g", convert to "a"
  //  if (selectedElements.length > 1 && selectedElements[1]) {
};
 
/**
* @function module:svgcanvas.SvgCanvas#removeHyperlink
* @returns {undefined}
*/
this.removeHyperlink = function () {
  canvas.ungroupSelectedElement();
};
 
/**
* Group: Element manipulation
*/
 
/**
* Sets the new segment type to the selected segment(s).
* @function module:svgcanvas.SvgCanvas#setSegType
* @param {Integer} newType - New segment type. See {@link https://www.w3.org/TR/SVG/paths.html#InterfaceSVGPathSeg} for list
* @returns {undefined}
*/
this.setSegType = function (newType) {
  pathActions.setSegType(newType);
};
 
/**
* Convert selected element to a path, or get the BBox of an element-as-path.
* @function module:svgcanvas.SvgCanvas#convertToPath
* @todo (codedread): Remove the getBBox argument and split this function into two.
* @param {Element} elem - The DOM element to be converted
* @param {boolean} getBBox - Boolean on whether or not to only return the path's BBox
* @returns {undefined|DOMRect|false|SVGPathElement|null} If the getBBox flag is true, the resulting path's bounding box object.
* Otherwise the resulting path element is returned.
*/
this.convertToPath = function (elem, getBBox) {
  if (elem == null) {
    const elems = selectedElements;
    $.each(elems, function (i, elem) {
      if (elem) { canvas.convertToPath(elem); }
    });
    return;
  }
  if (getBBox) {
    return getBBoxOfElementAsPath(elem, addSVGElementFromJson, pathActions);
  }
  // TODO: Why is this applying attributes from curShape, then inside utilities.convertToPath it's pulling addition attributes from elem?
  // TODO: If convertToPath is called with one elem, curShape and elem are probably the same; but calling with multiple is a bug or cool feature.
  const attrs = {
    fill: curShape.fill,
    'fill-opacity': curShape.fill_opacity,
    stroke: curShape.stroke,
    'stroke-width': curShape.stroke_width,
    'stroke-dasharray': curShape.stroke_dasharray,
    'stroke-linejoin': curShape.stroke_linejoin,
    'stroke-linecap': curShape.stroke_linecap,
    'stroke-opacity': curShape.stroke_opacity,
    opacity: curShape.opacity,
    visibility: 'hidden'
  };
  return convertToPath(elem, attrs, addSVGElementFromJson, pathActions, clearSelection, addToSelection, history, addCommandToHistory);
};
 
/**
* This function makes the changes to the elements. It does not add the change
* to the history stack.
* @param {string} attr - Attribute name
* @param {string|Float} newValue - String or number with the new attribute value
* @param {Element[]} elems - The DOM elements to apply the change to
* @returns {undefined}
*/
const changeSelectedAttributeNoUndo = function (attr, newValue, elems) {
  if (currentMode === 'pathedit') {
    // Editing node
    pathActions.moveNode(attr, newValue);
  }
  elems = elems || selectedElements;
  let i = elems.length;
  const noXYElems = ['g', 'polyline', 'path'];
  const goodGAttrs = ['transform', 'opacity', 'filter'];
 
  while (i--) {
    let elem = elems[i];
    if (elem == null) { continue; }
 
    // Set x,y vals on elements that don't have them
    if ((attr === 'x' || attr === 'y') && noXYElems.includes(elem.tagName)) {
      const bbox = getStrokedBBoxDefaultVisible([elem]);
      const diffX = attr === 'x' ? newValue - bbox.x : 0;
      const diffY = attr === 'y' ? newValue - bbox.y : 0;
      canvas.moveSelectedElements(diffX * currentZoom, diffY * currentZoom, true);
      continue;
    }
 
    // only allow the transform/opacity/filter attribute to change on <g> elements, slightly hacky
    // TODO: FIXME: This doesn't seem right. Where's the body of this if statement?
    if (elem.tagName === 'g' && goodGAttrs.includes(attr)) {}
    let oldval = attr === '#text' ? elem.textContent : elem.getAttribute(attr);
    if (oldval == null) { oldval = ''; }
    if (oldval !== String(newValue)) {
      if (attr === '#text') {
        // const oldW = utilsGetBBox(elem).width;
        elem.textContent = newValue;
 
        // FF bug occurs on on rotated elements
        if ((/rotate/).test(elem.getAttribute('transform'))) {
          elem = ffClone(elem);
        }
        // Hoped to solve the issue of moving text with text-anchor="start",
        // but this doesn't actually fix it. Hopefully on the right track, though. -Fyrd
        // const box = getBBox(elem), left = box.x, top = box.y, {width, height} = box,
        //   dx = width - oldW, dy = 0;
        // const angle = getRotationAngle(elem, true);
        // if (angle) {
        //   const r = Math.sqrt(dx * dx + dy * dy);
        //   const theta = Math.atan2(dy, dx) - angle;
        //   dx = r * Math.cos(theta);
        //   dy = r * Math.sin(theta);
        //
        //   elem.setAttribute('x', elem.getAttribute('x') - dx);
        //   elem.setAttribute('y', elem.getAttribute('y') - dy);
        // }
      } else if (attr === '#href') {
        setHref(elem, newValue);
      } else { elem.setAttribute(attr, newValue); }
 
      // Go into "select" mode for text changes
      // NOTE: Important that this happens AFTER elem.setAttribute() or else attributes like
      // font-size can get reset to their old value, ultimately by svgEditor.updateContextPanel(),
      // after calling textActions.toSelectMode() below
      if (currentMode === 'textedit' && attr !== '#text' && elem.textContent.length) {
        textActions.toSelectMode(elem);
      }
 
      // if (i === 0) {
      //   selectedBBoxes[0] = utilsGetBBox(elem);
      // }
 
      // Use the Firefox ffClone hack for text elements with gradients or
      // where other text attributes are changed.
      if (isGecko() && elem.nodeName === 'text' && (/rotate/).test(elem.getAttribute('transform'))) {
        if (String(newValue).startsWith('url') || (['font-size', 'font-family', 'x', 'y'].includes(attr) && elem.textContent)) {
          elem = ffClone(elem);
        }
      }
      // Timeout needed for Opera & Firefox
      // codedread: it is now possible for this function to be called with elements
      // that are not in the selectedElements array, we need to only request a
      // selector if the element is in that array
      if (selectedElements.includes(elem)) {
        setTimeout(function () {
          // Due to element replacement, this element may no longer
          // be part of the DOM
          if (!elem.parentNode) { return; }
          selectorManager.requestSelector(elem).resize();
        }, 0);
      }
      // if this element was rotated, and we changed the position of this element
      // we need to update the rotational transform attribute
      const angle = getRotationAngle(elem);
      if (angle !== 0 && attr !== 'transform') {
        const tlist = getTransformList(elem);
        let n = tlist.numberOfItems;
        while (n--) {
          const xform = tlist.getItem(n);
          if (xform.type === 4) {
            // remove old rotate
            tlist.removeItem(n);
 
            const box = utilsGetBBox(elem);
            const center = transformPoint(box.x + box.width / 2, box.y + box.height / 2, transformListToTransform(tlist).matrix);
            const cx = center.x,
              cy = center.y;
            const newrot = svgroot.createSVGTransform();
            newrot.setRotate(angle, cx, cy);
            tlist.insertItemBefore(newrot, n);
            break;
          }
        }
      }
    } // if oldValue != newValue
  } // for each elem
};
 
/**
* Change the given/selected element and add the original value to the history stack.
* If you want to change all `selectedElements`, ignore the `elems` argument.
* If you want to change only a subset of `selectedElements`, then send the
* subset to this function in the `elems` argument.
* @function module:svgcanvas.SvgCanvas#changeSelectedAttribute
* @param {string} attr - String with the attribute name
* @param {string|Float} val - String or number with the new attribute value
* @param {Element[]} elems - The DOM elements to apply the change to
* @returns {undefined}
*/
const changeSelectedAttribute = this.changeSelectedAttribute = function (attr, val, elems) {
  elems = elems || selectedElements;
  canvas.undoMgr.beginUndoableChange(attr, elems);
  // const i = elems.length;
 
  changeSelectedAttributeNoUndo(attr, val, elems);
 
  const batchCmd = canvas.undoMgr.finishUndoableChange();
  if (!batchCmd.isEmpty()) {
    addCommandToHistory(batchCmd);
  }
};
 
/**
* Removes all selected elements from the DOM and adds the change to the
* history stack.
* @function module:svgcanvas.SvgCanvas#deleteSelectedElements
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {undefined}
*/
this.deleteSelectedElements = function () {
  const batchCmd = new BatchCommand('Delete Elements');
  const len = selectedElements.length;
  const selectedCopy = []; // selectedElements is being deleted
 
  for (let i = 0; i < len; ++i) {
    const selected = selectedElements[i];
    if (selected == null) { break; }
 
    let parent = selected.parentNode;
    let t = selected;
 
    // this will unselect the element and remove the selectedOutline
    selectorManager.releaseSelector(t);
 
    // Remove the path if present.
    pathModule.removePath_(t.id);
 
    // Get the parent if it's a single-child anchor
    if (parent.tagName === 'a' && parent.childNodes.length === 1) {
      t = parent;
      parent = parent.parentNode;
    }
 
    const {nextSibling} = t;
    const elem = parent.removeChild(t);
    selectedCopy.push(selected); // for the copy
    batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent));
  }
  selectedElements = [];
 
  if (!batchCmd.isEmpty()) { addCommandToHistory(batchCmd); }
  call('changed', selectedCopy);
  clearSelection();
};
 
/**
* Removes all selected elements from the DOM and adds the change to the
* history stack. Remembers removed elements on the clipboard.
* @function module:svgcanvas.SvgCanvas#cutSelectedElements
* @returns {undefined}
*/
this.cutSelectedElements = function () {
  canvas.copySelectedElements();
  canvas.deleteSelectedElements();
};
 
/**
* Remembers the current selected elements on the clipboard.
* @function module:svgcanvas.SvgCanvas#copySelectedElements
* @returns {undefined}
*/
this.copySelectedElements = function () {
  localStorage.setItem('svgedit_clipboard', JSON.stringify(
    selectedElements.map(function (x) { return getJsonFromSvgElement(x); })
  ));
 
  $('#cmenu_canvas').enableContextMenuItems('#paste,#paste_in_place');
};
 
/**
* @function module:svgcanvas.SvgCanvas#pasteElements
* @param {"in_place"|"point"|undefined} type
* @param {Integer|undefined} x Expected if type is "point"
* @param {Integer|undefined} y Expected if type is "point"
* @fires module:svgcanvas.SvgCanvas#event:changed
* @fires module:svgcanvas.SvgCanvas#event:ext-IDsUpdated
* @returns {undefined}
*/
this.pasteElements = function (type, x, y) {
  let clipb = JSON.parse(localStorage.getItem('svgedit_clipboard'));
  let len = clipb.length;
  if (!len) { return; }
 
  const pasted = [];
  const batchCmd = new BatchCommand('Paste elements');
  // const drawing = getCurrentDrawing();
  /**
  * @typedef {PlainObject.<string, string>} module:svgcanvas.ChangedIDs
  */
  /**
   * @type {module:svgcanvas.ChangedIDs}
   */
  const changedIDs = {};
 
  // Recursively replace IDs and record the changes
  function checkIDs (elem) {
    if (elem.attr && elem.attr.id) {
      changedIDs[elem.attr.id] = getNextId();
      elem.attr.id = changedIDs[elem.attr.id];
    }
    if (elem.children) elem.children.forEach(checkIDs);
  }
  clipb.forEach(checkIDs);
 
  // Give extensions like the connector extension a chance to reflect new IDs and remove invalid elements
  /**
  * Triggered when `pasteElements` is called from a paste action (context menu or key)
  * @event module:svgcanvas.SvgCanvas#event:ext-IDsUpdated
  * @type {PlainObject}
  * @property {module:svgcanvas.SVGAsJSON[]} elems
  * @property {module:svgcanvas.ChangedIDs} changes Maps past ID (on attribute) to current ID
  */
  runExtensions(
    'IDsUpdated',
    /** @type {module:svgcanvas.SvgCanvas#event:ext-IDsUpdated} */
    {elems: clipb, changes: changedIDs},
    true
  ).forEach(function (extChanges) {
    if (!extChanges || !('remove' in extChanges)) return;
 
    extChanges.remove.forEach(function (removeID) {
      clipb = clipb.filter(function (clipBoardItem) {
        return clipBoardItem.attr.id !== removeID;
      });
    });
  });
 
  // Move elements to lastClickPoint
  while (len--) {
    const elem = clipb[len];
    if (!elem) { continue; }
 
    const copy = addSVGElementFromJson(elem);
    pasted.push(copy);
    batchCmd.addSubCommand(new InsertElementCommand(copy));
 
    restoreRefElems(copy);
  }
 
  selectOnly(pasted);
 
  if (type !== 'in_place') {
    let ctrX, ctrY;
 
    if (!type) {
      ctrX = lastClickPoint.x;
      ctrY = lastClickPoint.y;
    } else if (type === 'point') {
      ctrX = x;
      ctrY = y;
    }
 
    const bbox = getStrokedBBoxDefaultVisible(pasted);
    const cx = ctrX - (bbox.x + bbox.width / 2),
      cy = ctrY - (bbox.y + bbox.height / 2),
      dx = [],
      dy = [];
 
    $.each(pasted, function (i, item) {
      dx.push(cx);
      dy.push(cy);
    });
 
    const cmd = canvas.moveSelectedElements(dx, dy, false);
    if (cmd) batchCmd.addSubCommand(cmd);
  }
 
  addCommandToHistory(batchCmd);
  call('changed', pasted);
};
 
/**
* Wraps all the selected elements in a group (`g`) element.
* @function module:svgcanvas.SvgCanvas#groupSelectedElements
* @param {"a"|"g"} [type="g"] - type of element to group into, defaults to `<g>`
* @param {string} [urlArg]
* @returns {undefined}
*/
this.groupSelectedElements = function (type, urlArg) {
  if (!type) { type = 'g'; }
  let cmdStr = '';
  let url;
 
  switch (type) {
  case 'a': {
    cmdStr = 'Make hyperlink';
    url = urlArg || '';
    break;
  } default: {
    type = 'g';
    cmdStr = 'Group Elements';
    break;
  }
  }
 
  const batchCmd = new BatchCommand(cmdStr);
 
  // create and insert the group element
  const g = addSVGElementFromJson({
    element: type,
    attr: {
      id: getNextId()
    }
  });
  if (type === 'a') {
    setHref(g, url);
  }
  batchCmd.addSubCommand(new InsertElementCommand(g));
 
  // now move all children into the group
  let i = selectedElements.length;
  while (i--) {
    let elem = selectedElements[i];
    if (elem == null) { continue; }
 
    if (elem.parentNode.tagName === 'a' && elem.parentNode.childNodes.length === 1) {
      elem = elem.parentNode;
    }
 
    const oldNextSibling = elem.nextSibling;
    const oldParent = elem.parentNode;
    g.append(elem);
    batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
  }
  if (!batchCmd.isEmpty()) { addCommandToHistory(batchCmd); }
 
  // update selection
  selectOnly([g], true);
};
 
/**
* Pushes all appropriate parent group properties down to its children, then
* removes them from the group.
* @function module:svgcanvas.SvgCanvas#pushGroupProperties
* @param {SVGAElement|SVGGElement} g
* @param {boolean} undoable
* @returns {undefined}
*/
const pushGroupProperties = this.pushGroupProperties = function (g, undoable) {
  const children = g.childNodes;
  const len = children.length;
  const xform = g.getAttribute('transform');
 
  const glist = getTransformList(g);
  const m = transformListToTransform(glist).matrix;
 
  const batchCmd = new BatchCommand('Push group properties');
 
  // TODO: get all fill/stroke properties from the group that we are about to destroy
  // "fill", "fill-opacity", "fill-rule", "stroke", "stroke-dasharray", "stroke-dashoffset",
  // "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity",
  // "stroke-width"
  // and then for each child, if they do not have the attribute (or the value is 'inherit')
  // then set the child's attribute
 
  const gangle = getRotationAngle(g);
 
  const gattrs = $(g).attr(['filter', 'opacity']);
  let gfilter, gblur, changes;
  const drawing = getCurrentDrawing();
 
  for (let i = 0; i < len; i++) {
    const elem = children[i];
 
    if (elem.nodeType !== 1) { continue; }
 
    if (gattrs.opacity !== null && gattrs.opacity !== 1) {
      // const c_opac = elem.getAttribute('opacity') || 1;
      const newOpac = Math.round((elem.getAttribute('opacity') || 1) * gattrs.opacity * 100) / 100;
      changeSelectedAttribute('opacity', newOpac, [elem]);
    }
 
    if (gattrs.filter) {
      let cblur = this.getBlur(elem);
      const origCblur = cblur;
      if (!gblur) { gblur = this.getBlur(g); }
      if (cblur) {
        // Is this formula correct?
        cblur = Number(gblur) + Number(cblur);
      } else if (cblur === 0) {
        cblur = gblur;
      }
 
      // If child has no current filter, get group's filter or clone it.
      if (!origCblur) {
        // Set group's filter to use first child's ID
        if (!gfilter) {
          gfilter = getRefElem(gattrs.filter);
        } else {
          // Clone the group's filter
          gfilter = drawing.copyElem(gfilter);
          findDefs().append(gfilter);
        }
      } else {
        gfilter = getRefElem(elem.getAttribute('filter'));
      }
 
      // Change this in future for different filters
      const suffix = (gfilter.firstChild.tagName === 'feGaussianBlur') ? 'blur' : 'filter';
      gfilter.id = elem.id + '_' + suffix;
      changeSelectedAttribute('filter', 'url(#' + gfilter.id + ')', [elem]);
 
      // Update blur value
      if (cblur) {
        changeSelectedAttribute('stdDeviation', cblur, [gfilter.firstChild]);
        canvas.setBlurOffsets(gfilter, cblur);
      }
    }
 
    let chtlist = getTransformList(elem);
 
    // Don't process gradient transforms
    if (elem.tagName.includes('Gradient')) { chtlist = null; }
 
    // Hopefully not a problem to add this. Necessary for elements like <desc/>
    if (!chtlist) { continue; }
 
    // Apparently <defs> can get get a transformlist, but we don't want it to have one!
    if (elem.tagName === 'defs') { continue; }
 
    if (glist.numberOfItems) {
      // TODO: if the group's transform is just a rotate, we can always transfer the
      // rotate() down to the children (collapsing consecutive rotates and factoring
      // out any translates)
      if (gangle && glist.numberOfItems === 1) {
        // [Rg] [Rc] [Mc]
        // we want [Tr] [Rc2] [Mc] where:
        //  - [Rc2] is at the child's current center but has the
        // sum of the group and child's rotation angles
        //  - [Tr] is the equivalent translation that this child
        // undergoes if the group wasn't there
 
        // [Tr] = [Rg] [Rc] [Rc2_inv]
 
        // get group's rotation matrix (Rg)
        const rgm = glist.getItem(0).matrix;
 
        // get child's rotation matrix (Rc)
        let rcm = svgroot.createSVGMatrix();
        const cangle = getRotationAngle(elem);
        if (cangle) {
          rcm = chtlist.getItem(0).matrix;
        }
 
        // get child's old center of rotation
        const cbox = utilsGetBBox(elem);
        const ceqm = transformListToTransform(chtlist).matrix;
        const coldc = transformPoint(cbox.x + cbox.width / 2, cbox.y + cbox.height / 2, ceqm);
 
        // sum group and child's angles
        const sangle = gangle + cangle;
 
        // get child's rotation at the old center (Rc2_inv)
        const r2 = svgroot.createSVGTransform();
        r2.setRotate(sangle, coldc.x, coldc.y);
 
        // calculate equivalent translate
        const trm = matrixMultiply(rgm, rcm, r2.matrix.inverse());
 
        // set up tlist
        if (cangle) {
          chtlist.removeItem(0);
        }
 
        if (sangle) {
          if (chtlist.numberOfItems) {
            chtlist.insertItemBefore(r2, 0);
          } else {
            chtlist.appendItem(r2);
          }
        }
 
        if (trm.e || trm.f) {
          const tr = svgroot.createSVGTransform();
          tr.setTranslate(trm.e, trm.f);
          if (chtlist.numberOfItems) {
            chtlist.insertItemBefore(tr, 0);
          } else {
            chtlist.appendItem(tr);
          }
        }
      } else { // more complicated than just a rotate
        // transfer the group's transform down to each child and then
        // call recalculateDimensions()
        const oldxform = elem.getAttribute('transform');
        changes = {};
        changes.transform = oldxform || '';
 
        const newxform = svgroot.createSVGTransform();
 
        // [ gm ] [ chm ] = [ chm ] [ gm' ]
        // [ gm' ] = [ chmInv ] [ gm ] [ chm ]
        const chm = transformListToTransform(chtlist).matrix,
          chmInv = chm.inverse();
        const gm = matrixMultiply(chmInv, m, chm);
        newxform.setMatrix(gm);
        chtlist.appendItem(newxform);
      }
      const cmd = recalculateDimensions(elem);
      if (cmd) { batchCmd.addSubCommand(cmd); }
    }
  }
 
  // remove transform and make it undo-able
  if (xform) {
    changes = {};
    changes.transform = xform;
    g.setAttribute('transform', '');
    g.removeAttribute('transform');
    batchCmd.addSubCommand(new ChangeElementCommand(g, changes));
  }
 
  if (undoable && !batchCmd.isEmpty()) {
    return batchCmd;
  }
};
 
/**
* Unwraps all the elements in a selected group (`g`) element. This requires
* significant recalculations to apply group's transforms, etc. to its children.
* @function module:svgcanvas.SvgCanvas#ungroupSelectedElement
* @returns {undefined}
*/
this.ungroupSelectedElement = function () {
  let g = selectedElements[0];
  if (!g) {
    return;
  }
  if ($(g).data('gsvg') || $(g).data('symbol')) {
    // Is svg, so actually convert to group
    convertToGroup(g);
    return;
  }
  if (g.tagName === 'use') {
    // Somehow doesn't have data set, so retrieve
    const symbol = getElem(getHref(g).substr(1));
    $(g).data('symbol', symbol).data('ref', symbol);
    convertToGroup(g);
    return;
  }
  const parentsA = $(g).parents('a');
  if (parentsA.length) {
    g = parentsA[0];
  }
 
  // Look for parent "a"
  if (g.tagName === 'g' || g.tagName === 'a') {
    const batchCmd = new BatchCommand('Ungroup Elements');
    const cmd = pushGroupProperties(g, true);
    if (cmd) { batchCmd.addSubCommand(cmd); }
 
    const parent = g.parentNode;
    const anchor = g.nextSibling;
    const children = new Array(g.childNodes.length);
 
    let i = 0;
    while (g.firstChild) {
      let elem = g.firstChild;
      const oldNextSibling = elem.nextSibling;
      const oldParent = elem.parentNode;
 
      // Remove child title elements
      if (elem.tagName === 'title') {
        const {nextSibling} = elem;
        batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, oldParent));
        elem.remove();
        continue;
      }
 
      children[i++] = elem = parent.insertBefore(elem, anchor);
      batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
    }
 
    // remove the group from the selection
    clearSelection();
 
    // delete the group element (but make undo-able)
    const gNextSibling = g.nextSibling;
    g = parent.removeChild(g);
    batchCmd.addSubCommand(new RemoveElementCommand(g, gNextSibling, parent));
 
    if (!batchCmd.isEmpty()) { addCommandToHistory(batchCmd); }
 
    // update selection
    addToSelection(children);
  }
};
 
/**
* Repositions the selected element to the bottom in the DOM to appear on top of
* other elements.
* @function module:svgcanvas.SvgCanvas#moveToTopSelectedElement
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {undefined}
*/
this.moveToTopSelectedElement = function () {
  const [selected] = selectedElements;
  if (selected != null) {
    let t = selected;
    const oldParent = t.parentNode;
    const oldNextSibling = t.nextSibling;
    t = t.parentNode.appendChild(t);
    // If the element actually moved position, add the command and fire the changed
    // event handler.
    if (oldNextSibling !== t.nextSibling) {
      addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, 'top'));
      call('changed', [t]);
    }
  }
};
 
/**
* Repositions the selected element to the top in the DOM to appear under
* other elements.
* @function module:svgcanvas.SvgCanvas#moveToBottomSelectedElement
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {undefined}
*/
this.moveToBottomSelectedElement = function () {
  const [selected] = selectedElements;
  if (selected != null) {
    let t = selected;
    const oldParent = t.parentNode;
    const oldNextSibling = t.nextSibling;
    let {firstChild} = t.parentNode;
    if (firstChild.tagName === 'title') {
      firstChild = firstChild.nextSibling;
    }
    // This can probably be removed, as the defs should not ever apppear
    // inside a layer group
    if (firstChild.tagName === 'defs') {
      firstChild = firstChild.nextSibling;
    }
    t = t.parentNode.insertBefore(t, firstChild);
    // If the element actually moved position, add the command and fire the changed
    // event handler.
    if (oldNextSibling !== t.nextSibling) {
      addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, 'bottom'));
      call('changed', [t]);
    }
  }
};
 
/**
* Moves the select element up or down the stack, based on the visibly
* intersecting elements.
* @function module:svgcanvas.SvgCanvas#moveUpDownSelected
* @param {"Up"|"Down"} dir - String that's either 'Up' or 'Down'
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {undefined}
*/
this.moveUpDownSelected = function (dir) {
  const selected = selectedElements[0];
  if (!selected) { return; }
 
  curBBoxes = [];
  let closest, foundCur;
  // jQuery sorts this list
  const list = $(getIntersectionList(getStrokedBBoxDefaultVisible([selected]))).toArray();
  if (dir === 'Down') { list.reverse(); }
 
  $.each(list, function () {
    if (!foundCur) {
      if (this === selected) {
        foundCur = true;
      }
      return;
    }
    closest = this;
    return false;
  });
  if (!closest) { return; }
 
  const t = selected;
  const oldParent = t.parentNode;
  const oldNextSibling = t.nextSibling;
  $(closest)[dir === 'Down' ? 'before' : 'after'](t);
  // If the element actually moved position, add the command and fire the changed
  // event handler.
  if (oldNextSibling !== t.nextSibling) {
    addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, 'Move ' + dir));
    call('changed', [t]);
  }
};
 
/**
* Moves selected elements on the X/Y axis.
* @function module:svgcanvas.SvgCanvas#moveSelectedElements
* @param {Float} dx - Float with the distance to move on the x-axis
* @param {Float} dy - Float with the distance to move on the y-axis
* @param {boolean} undoable - Boolean indicating whether or not the action should be undoable
* @fires module:svgcanvas.SvgCanvas#event:changed
* @returns {BatchCommand} Batch command for the move
*/
this.moveSelectedElements = function (dx, dy, undoable) {
  // if undoable is not sent, default to true
  // if single values, scale them to the zoom
  if (dx.constructor !== Array) {
    dx /= currentZoom;
    dy /= currentZoom;
  }
  undoable = undoable || true;
  const batchCmd = new BatchCommand('position');
  let i = selectedElements.length;
  while (i--) {
    const selected = selectedElements[i];
    if (selected != null) {
      // if (i === 0) {
      //   selectedBBoxes[0] = utilsGetBBox(selected);
      // }
      // const b = {};
      // for (const j in selectedBBoxes[i]) b[j] = selectedBBoxes[i][j];
      // selectedBBoxes[i] = b;
 
      const xform = svgroot.createSVGTransform();
      const tlist = getTransformList(selected);
 
      // dx and dy could be arrays
      if (dx.constructor === Array) {
        // if (i === 0) {
        //   selectedBBoxes[0].x += dx[0];
        //   selectedBBoxes[0].y += dy[0];
        // }
        xform.setTranslate(dx[i], dy[i]);
      } else {
        // if (i === 0) {
        //   selectedBBoxes[0].x += dx;
        //   selectedBBoxes[0].y += dy;
        // }
        xform.setTranslate(dx, dy);
      }
 
      if (tlist.numberOfItems) {
        tlist.insertItemBefore(xform, 0);
      } else {
        tlist.appendItem(xform);
      }
 
      const cmd = recalculateDimensions(selected);
      if (cmd) {
        batchCmd.addSubCommand(cmd);
      }
 
      selectorManager.requestSelector(selected).resize();
    }
  }
  if (!batchCmd.isEmpty()) {
    if (undoable) {
      addCommandToHistory(batchCmd);
    }
    call('changed', selectedElements);
    return batchCmd;
  }
};
 
/**
* Create deep DOM copies (clones) of all selected elements and move them slightly
* from their originals.
* @function module:svgcanvas.SvgCanvas#cloneSelectedElements
* @param {Float} x Float with the distance to move on the x-axis
* @param {Float} y Float with the distance to move on the y-axis
* @returns {undefined}
*/
this.cloneSelectedElements = function (x, y) {
  let i, elem;
  const batchCmd = new BatchCommand('Clone Elements');
  // find all the elements selected (stop at first null)
  const len = selectedElements.length;
  function sortfunction (a, b) {
    return ($(b).index() - $(a).index()); // causes an array to be sorted numerically and ascending
  }
  selectedElements.sort(sortfunction);
  for (i = 0; i < len; ++i) {
    elem = selectedElements[i];
    if (elem == null) { break; }
  }
  // use slice to quickly get the subset of elements we need
  const copiedElements = selectedElements.slice(0, i);
  this.clearSelection(true);
  // note that we loop in the reverse way because of the way elements are added
  // to the selectedElements array (top-first)
  const drawing = getCurrentDrawing();
  i = copiedElements.length;
  while (i--) {
    // clone each element and replace it within copiedElements
    elem = copiedElements[i] = drawing.copyElem(copiedElements[i]);
    (currentGroup || drawing.getCurrentLayer()).append(elem);
    batchCmd.addSubCommand(new InsertElementCommand(elem));
  }
 
  if (!batchCmd.isEmpty()) {
    addToSelection(copiedElements.reverse()); // Need to reverse for correct selection-adding
    this.moveSelectedElements(x, y, false);
    addCommandToHistory(batchCmd);
  }
};
 
/**
* Aligns selected elements.
* @function module:svgcanvas.SvgCanvas#alignSelectedElements
* @param {string} type - String with single character indicating the alignment type
* @param {"selected"|"largest"|"smallest"|"page"} relativeTo
* @returns {undefined}
*/
this.alignSelectedElements = function (type, relativeTo) {
  const bboxes = []; // angles = [];
  const len = selectedElements.length;
  if (!len) { return; }
  let minx = Number.MAX_VALUE, maxx = Number.MIN_VALUE,
    miny = Number.MAX_VALUE, maxy = Number.MIN_VALUE;
  let curwidth = Number.MIN_VALUE, curheight = Number.MIN_VALUE;
  for (let i = 0; i < len; ++i) {
    if (selectedElements[i] == null) { break; }
    const elem = selectedElements[i];
    bboxes[i] = getStrokedBBoxDefaultVisible([elem]);
 
    // now bbox is axis-aligned and handles rotation
    switch (relativeTo) {
    case 'smallest':
      if (((type === 'l' || type === 'c' || type === 'r') &&
        (curwidth === Number.MIN_VALUE || curwidth > bboxes[i].width)) ||
        ((type === 't' || type === 'm' || type === 'b') &&
        (curheight === Number.MIN_VALUE || curheight > bboxes[i].height))
      ) {
        minx = bboxes[i].x;
        miny = bboxes[i].y;
        maxx = bboxes[i].x + bboxes[i].width;
        maxy = bboxes[i].y + bboxes[i].height;
        curwidth = bboxes[i].width;
        curheight = bboxes[i].height;
      }
      break;
    case 'largest':
      if (((type === 'l' || type === 'c' || type === 'r') &&
        (curwidth === Number.MIN_VALUE || curwidth < bboxes[i].width)) ||
        ((type === 't' || type === 'm' || type === 'b') &&
        (curheight === Number.MIN_VALUE || curheight < bboxes[i].height))
      ) {
        minx = bboxes[i].x;
        miny = bboxes[i].y;
        maxx = bboxes[i].x + bboxes[i].width;
        maxy = bboxes[i].y + bboxes[i].height;
        curwidth = bboxes[i].width;
        curheight = bboxes[i].height;
      }
      break;
    default: // 'selected'
      if (bboxes[i].x < minx) { minx = bboxes[i].x; }
      if (bboxes[i].y < miny) { miny = bboxes[i].y; }
      if (bboxes[i].x + bboxes[i].width > maxx) { maxx = bboxes[i].x + bboxes[i].width; }
      if (bboxes[i].y + bboxes[i].height > maxy) { maxy = bboxes[i].y + bboxes[i].height; }
      break;
    }
  } // loop for each element to find the bbox and adjust min/max
 
  if (relativeTo === 'page') {
    minx = 0;
    miny = 0;
    maxx = canvas.contentW;
    maxy = canvas.contentH;
  }
 
  const dx = new Array(len);
  const dy = new Array(len);
  for (let i = 0; i < len; ++i) {
    if (selectedElements[i] == null) { break; }
    // const elem = selectedElements[i];
    const bbox = bboxes[i];
    dx[i] = 0;
    dy[i] = 0;
    switch (type) {
    case 'l': // left (horizontal)
      dx[i] = minx - bbox.x;
      break;
    case 'c': // center (horizontal)
      dx[i] = (minx + maxx) / 2 - (bbox.x + bbox.width / 2);
      break;
    case 'r': // right (horizontal)
      dx[i] = maxx - (bbox.x + bbox.width);
      break;
    case 't': // top (vertical)
      dy[i] = miny - bbox.y;
      break;
    case 'm': // middle (vertical)
      dy[i] = (miny + maxy) / 2 - (bbox.y + bbox.height / 2);
      break;
    case 'b': // bottom (vertical)
      dy[i] = maxy - (bbox.y + bbox.height);
      break;
    }
  }
  this.moveSelectedElements(dx, dy);
};
 
/**
* Group: Additional editor tools
*/
 
/**
* @name module:svgcanvas.SvgCanvas#contentW
* @type {Float}
*/
this.contentW = getResolution().w;
/**
* @name module:svgcanvas.SvgCanvas#contentH
* @type {Float}
*/
this.contentH = getResolution().h;
 
/**
* @typedef {PlainObject} module:svgcanvas.CanvasInfo
* @property {Float} x - The canvas' new x coordinate
* @property {Float} y - The canvas' new y coordinate
* @property {string} oldX - The canvas' old x coordinate
* @property {string} oldY - The canvas' old y coordinate
* @property {Float} d_x - The x position difference
* @property {Float} d_y - The y position difference
*/
 
/**
* Updates the editor canvas width/height/position after a zoom has occurred.
* @function module:svgcanvas.SvgCanvas#updateCanvas
* @param {Float} w - Float with the new width
* @param {Float} h - Float with the new height
* @fires module:svgcanvas.SvgCanvas#event:ext-canvasUpdated
* @returns {module:svgcanvas.CanvasInfo}
*/
this.updateCanvas = function (w, h) {
  svgroot.setAttribute('width', w);
  svgroot.setAttribute('height', h);
  const bg = $('#canvasBackground')[0];
  const oldX = svgcontent.getAttribute('x');
  const oldY = svgcontent.getAttribute('y');
  const x = ((w - this.contentW * currentZoom) / 2);
  const y = ((h - this.contentH * currentZoom) / 2);
 
  assignAttributes(svgcontent, {
    width: this.contentW * currentZoom,
    height: this.contentH * currentZoom,
    x,
    y,
    viewBox: '0 0 ' + this.contentW + ' ' + this.contentH
  });
 
  assignAttributes(bg, {
    width: svgcontent.getAttribute('width'),
    height: svgcontent.getAttribute('height'),
    x,
    y
  });
 
  const bgImg = getElem('background_image');
  if (bgImg) {
    assignAttributes(bgImg, {
      width: '100%',
      height: '100%'
    });
  }
 
  selectorManager.selectorParentGroup.setAttribute('transform', 'translate(' + x + ',' + y + ')');
 
  /**
  * Invoked upon updates to the canvas.
  * @event module:svgcanvas.SvgCanvas#event:ext-canvasUpdated
  * @type {PlainObject}
  * @property {Integer} new_x
  * @property {Integer} new_y
  * @property {string} old_x (Of Integer)
  * @property {string} old_y (Of Integer)
  * @property {Integer} d_x
  * @property {Integer} d_y
  */
  runExtensions(
    'canvasUpdated',
    /**
     * @type {module:svgcanvas.SvgCanvas#event:ext-canvasUpdated}
     */
    {new_x: x, new_y: y, old_x: oldX, old_y: oldY, d_x: x - oldX, d_y: y - oldY}
  );
  return {x, y, old_x: oldX, old_y: oldY, d_x: x - oldX, d_y: y - oldY};
};
 
/**
* Set the background of the editor (NOT the actual document).
* @function module:svgcanvas.SvgCanvas#setBackground
* @param {string} color - String with fill color to apply
* @param {string} url - URL or path to image to use
* @returns {undefined}
*/
this.setBackground = function (color, url) {
  const bg = getElem('canvasBackground');
  const border = $(bg).find('rect')[0];
  let bgImg = getElem('background_image');
  border.setAttribute('fill', color);
  if (url) {
    if (!bgImg) {
      bgImg = svgdoc.createElementNS(NS.SVG, 'image');
      assignAttributes(bgImg, {
        id: 'background_image',
        width: '100%',
        height: '100%',
        preserveAspectRatio: 'xMinYMin',
        style: 'pointer-events:none'
      });
    }
    setHref(bgImg, url);
    bg.append(bgImg);
  } else if (bgImg) {
    bgImg.remove();
  }
};
 
/**
* Select the next/previous element within the current layer.
* @function module:svgcanvas.SvgCanvas#cycleElement
* @param {boolean} next - true = next and false = previous element
* @fires module:svgcanvas.SvgCanvas#event:selected
* @returns {undefined}
*/
this.cycleElement = function (next) {
  let num;
  const curElem = selectedElements[0];
  let elem = false;
  const allElems = getVisibleElements(currentGroup || getCurrentDrawing().getCurrentLayer());
  if (!allElems.length) { return; }
  if (curElem == null) {
    num = next ? allElems.length - 1 : 0;
    elem = allElems[num];
  } else {
    let i = allElems.length;
    while (i--) {
      if (allElems[i] === curElem) {
        num = next ? i - 1 : i + 1;
        if (num >= allElems.length) {
          num = 0;
        } else if (num < 0) {
          num = allElems.length - 1;
        }
        elem = allElems[num];
        break;
      }
    }
  }
  selectOnly([elem], true);
  call('selected', selectedElements);
};
 
this.clear();
 
/**
* @interface module:svgcanvas.PrivateMethods
* @type {PlainObject}
* @property {module:svgcanvas~addCommandToHistory} addCommandToHistory
* @property {module:history.HistoryCommand} BatchCommand
* @property {module:history.HistoryCommand} ChangeElementCommand
* @property {module:utilities.decode64} decode64
* @property {module:utilities.dropXMLInteralSubset} dropXMLInteralSubset
* @property {module:utilities.encode64} encode64
* @property {module:svgcanvas~ffClone} ffClone
* @property {module:svgcanvas~findDuplicateGradient} findDuplicateGradient
* @property {module:utilities.getPathBBox} getPathBBox
* @property {module:units.getTypeMap} getTypeMap
* @property {module:draw.identifyLayers} identifyLayers
* @property {module:history.HistoryCommand} InsertElementCommand
* @property {module:browser.isChrome} isChrome
* @property {module:math.isIdentity} isIdentity
* @property {module:browser.isIE} isIE
* @property {module:svgcanvas~logMatrix} logMatrix
* @property {module:history.HistoryCommand} MoveElementCommand
* @property {module:namespaces.NS} NS
* @property {module:utilities.preventClickDefault} preventClickDefault
* @property {module:history.HistoryCommand} RemoveElementCommand
* @property {module:SVGTransformList.SVGEditTransformList} SVGEditTransformList
* @property {module:utilities.text2xml} text2xml
* @property {module:math.transformBox} transformBox
* @property {module:math.transformPoint} transformPoint
* @property {module:utilities.walkTree} walkTree
*/
/**
* @deprecated getPrivateMethods
* Since all methods are/should be public somehow, this function should be removed;
*  we might require `import` in place of this in the future once ES6 Modules
*  widespread
 
* Being able to access private methods publicly seems wrong somehow,
* but currently appears to be the best way to allow testing and provide
* access to them to plugins.
* @function module:svgcanvas.SvgCanvas#getPrivateMethods
* @returns {module:svgcanvas.PrivateMethods}
*/
this.getPrivateMethods = function () {
  const obj = {
    addCommandToHistory,
    BatchCommand,
    ChangeElementCommand,
    decode64,
    dropXMLInteralSubset,
    encode64,
    ffClone,
    findDefs,
    findDuplicateGradient,
    getElem,
    getPathBBox,
    getTypeMap,
    getUrlFromAttr,
    identifyLayers: draw.identifyLayers,
    InsertElementCommand,
    isChrome,
    isIdentity,
    isIE,
    logMatrix,
    MoveElementCommand,
    NS,
    preventClickDefault,
    RemoveElementCommand,
    SVGEditTransformList,
    text2xml,
    transformBox,
    transformPoint,
    walkTree
  };
  return obj;
};
  } // End constructor
} // End class
 
export default SvgCanvas;