-
Notifications
You must be signed in to change notification settings - Fork 59
/
test_scour.py
executable file
·2796 lines (1998 loc) · 121 KB
/
test_scour.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Test Harness for Scour
#
# Copyright 2010 Jeff Schiller
# Copyright 2010 Louis Simard
#
# This file is part of Scour, http://www.codedread.com/scour/
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import print_function # use print() as a function in Python 2 (see PEP 3105)
from __future__ import absolute_import # use absolute imports by default in Python 2 (see PEP 328)
import os
import sys
import unittest
import six
from six.moves import map, range
from scour.scour import (make_well_formed, parse_args, scourString, scourXmlFile, start, run,
XML_ENTS_ESCAPE_APOS, XML_ENTS_ESCAPE_QUOT)
from scour.svg_regex import svg_parser
from scour import __version__
SVGNS = 'http://www.w3.org/2000/svg'
# I couldn't figure out how to get ElementTree to work with the following XPath
# "//*[namespace-uri()='http://example.com']"
# so I decided to use minidom and this helper function that performs a test on a given node
# and all its children
# func must return either True (if pass) or False (if fail)
def walkTree(elem, func):
if func(elem) is False:
return False
for child in elem.childNodes:
if walkTree(child, func) is False:
return False
return True
class ScourOptions:
pass
class EmptyOptions(unittest.TestCase):
MINIMAL_SVG = '<?xml version="1.0" encoding="UTF-8"?>\n' \
'<svg xmlns="http://www.w3.org/2000/svg"/>\n'
def test_scourString(self):
options = ScourOptions
try:
scourString(self.MINIMAL_SVG, options)
fail = False
except Exception:
fail = True
self.assertEqual(fail, False,
'Exception when calling "scourString" with empty options object')
def test_scourXmlFile(self):
options = ScourOptions
try:
scourXmlFile('unittests/minimal.svg', options)
fail = False
except Exception:
fail = True
self.assertEqual(fail, False,
'Exception when calling "scourXmlFile" with empty options object')
def test_start(self):
options = ScourOptions
input = open('unittests/minimal.svg', 'rb')
output = open('testscour_temp.svg', 'wb')
stdout_temp = sys.stdout
sys.stdout = None
try:
start(options, input, output)
fail = False
except Exception:
fail = True
sys.stdout = stdout_temp
os.remove('testscour_temp.svg')
self.assertEqual(fail, False,
'Exception when calling "start" with empty options object')
class InvalidOptions(unittest.TestCase):
def runTest(self):
options = ScourOptions
options.invalidOption = "invalid value"
try:
scourXmlFile('unittests/ids-to-strip.svg', options)
fail = False
except Exception:
fail = True
self.assertEqual(fail, False,
'Exception when calling Scour with invalid options')
class GetElementById(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/ids.svg')
self.assertIsNotNone(doc.getElementById('svg1'), 'Root SVG element not found by ID')
self.assertIsNotNone(doc.getElementById('linearGradient1'), 'linearGradient not found by ID')
self.assertIsNotNone(doc.getElementById('layer1'), 'g not found by ID')
self.assertIsNotNone(doc.getElementById('rect1'), 'rect not found by ID')
self.assertIsNone(doc.getElementById('rect2'), 'Non-existing element found by ID')
class NoInkscapeElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/sodipodi.svg').documentElement,
lambda e: e.namespaceURI != 'http://www.inkscape.org/namespaces/inkscape'),
False,
'Found Inkscape elements')
class NoSodipodiElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/sodipodi.svg').documentElement,
lambda e: e.namespaceURI != 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd'),
False,
'Found Sodipodi elements')
class NoAdobeIllustratorElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/adobe.svg').documentElement,
lambda e: e.namespaceURI != 'http://ns.adobe.com/AdobeIllustrator/10.0/'),
False,
'Found Adobe Illustrator elements')
class NoAdobeGraphsElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/adobe.svg').documentElement,
lambda e: e.namespaceURI != 'http://ns.adobe.com/Graphs/1.0/'),
False,
'Found Adobe Graphs elements')
class NoAdobeSVGViewerElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/adobe.svg').documentElement,
lambda e: e.namespaceURI != 'http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/'),
False,
'Found Adobe SVG Viewer elements')
class NoAdobeVariablesElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/adobe.svg').documentElement,
lambda e: e.namespaceURI != 'http://ns.adobe.com/Variables/1.0/'),
False,
'Found Adobe Variables elements')
class NoAdobeSaveForWebElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/adobe.svg').documentElement,
lambda e: e.namespaceURI != 'http://ns.adobe.com/SaveForWeb/1.0/'),
False,
'Found Adobe Save For Web elements')
class NoAdobeExtensibilityElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/adobe.svg').documentElement,
lambda e: e.namespaceURI != 'http://ns.adobe.com/Extensibility/1.0/'),
False,
'Found Adobe Extensibility elements')
class NoAdobeFlowsElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/adobe.svg').documentElement,
lambda e: e.namespaceURI != 'http://ns.adobe.com/Flows/1.0/'),
False,
'Found Adobe Flows elements')
class NoAdobeImageReplacementElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/adobe.svg').documentElement,
lambda e: e.namespaceURI != 'http://ns.adobe.com/ImageReplacement/1.0/'),
False,
'Found Adobe Image Replacement elements')
class NoAdobeCustomElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/adobe.svg').documentElement,
lambda e: e.namespaceURI != 'http://ns.adobe.com/GenericCustomNamespace/1.0/'),
False,
'Found Adobe Custom elements')
class NoAdobeXPathElements(unittest.TestCase):
def runTest(self):
self.assertNotEqual(walkTree(scourXmlFile('unittests/adobe.svg').documentElement,
lambda e: e.namespaceURI != 'http://ns.adobe.com/XPath/1.0/'),
False,
'Found Adobe XPath elements')
class DoNotRemoveTitleWithOnlyText(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/descriptive-elements-with-text.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'title')), 1,
'Removed title element with only text child')
class RemoveEmptyTitleElement(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/empty-descriptive-elements.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'title')), 0,
'Did not remove empty title element')
class DoNotRemoveDescriptionWithOnlyText(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/descriptive-elements-with-text.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'desc')), 1,
'Removed description element with only text child')
class RemoveEmptyDescriptionElement(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/empty-descriptive-elements.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'desc')), 0,
'Did not remove empty description element')
class DoNotRemoveMetadataWithOnlyText(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/descriptive-elements-with-text.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'metadata')), 1,
'Removed metadata element with only text child')
class RemoveEmptyMetadataElement(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/empty-descriptive-elements.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'metadata')), 0,
'Did not remove empty metadata element')
class DoNotRemoveDescriptiveElementsWithOnlyText(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/descriptive-elements-with-text.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'title')), 1,
'Removed title element with only text child')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'desc')), 1,
'Removed description element with only text child')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'metadata')), 1,
'Removed metadata element with only text child')
class RemoveEmptyDescriptiveElements(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/empty-descriptive-elements.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'title')), 0,
'Did not remove empty title element')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'desc')), 0,
'Did not remove empty description element')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'metadata')), 0,
'Did not remove empty metadata element')
class RemoveEmptyGElements(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/empty-g.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'g')), 1,
'Did not remove empty g element')
class RemoveUnreferencedPattern(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/unreferenced-pattern.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'pattern')), 0,
'Unreferenced pattern not removed')
class RemoveUnreferencedLinearGradient(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/unreferenced-linearGradient.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'linearGradient')), 0,
'Unreferenced linearGradient not removed')
class RemoveUnreferencedRadialGradient(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/unreferenced-radialGradient.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'radialradient')), 0,
'Unreferenced radialGradient not removed')
class RemoveUnreferencedElementInDefs(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/referenced-elements-1.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'rect')), 1,
'Unreferenced rect left in defs')
class RemoveUnreferencedDefs(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/unreferenced-defs.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'linearGradient')), 1,
'Referenced linearGradient removed from defs')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'radialGradient')), 0,
'Unreferenced radialGradient left in defs')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'pattern')), 0,
'Unreferenced pattern left in defs')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'rect')), 1,
'Referenced rect removed from defs')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'circle')), 0,
'Unreferenced circle left in defs')
class KeepUnreferencedDefs(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/unreferenced-defs.svg',
parse_args(['--keep-unreferenced-defs']))
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'linearGradient')), 1,
'Referenced linearGradient removed from defs with `--keep-unreferenced-defs`')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'radialGradient')), 1,
'Unreferenced radialGradient removed from defs with `--keep-unreferenced-defs`')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'pattern')), 1,
'Unreferenced pattern removed from defs with `--keep-unreferenced-defs`')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'rect')), 1,
'Referenced rect removed from defs with `--keep-unreferenced-defs`')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'circle')), 1,
'Unreferenced circle removed from defs with `--keep-unreferenced-defs`')
class DoNotRemoveChainedRefsInDefs(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/refs-in-defs.svg')
g = doc.getElementsByTagNameNS(SVGNS, 'g')[0]
self.assertEqual(g.childNodes.length >= 2, True,
'Chained references not honored in defs')
class KeepTitleInDefs(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/referenced-elements-1.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'title')), 1,
'Title removed from in defs')
class RemoveNestedDefs(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/nested-defs.svg')
allDefs = doc.getElementsByTagNameNS(SVGNS, 'defs')
self.assertEqual(len(allDefs), 1, 'More than one defs left in doc')
class KeepUnreferencedIDsWhenEnabled(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/ids-to-strip.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'svg')[0].getAttribute('id'), 'boo',
'<svg> ID stripped when it should be disabled')
class RemoveUnreferencedIDsWhenEnabled(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/ids-to-strip.svg',
parse_args(['--enable-id-stripping']))
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'svg')[0].getAttribute('id'), '',
'<svg> ID not stripped')
class ProtectIDs(unittest.TestCase):
def test_protect_none(self):
doc = scourXmlFile('unittests/ids-protect.svg',
parse_args(['--enable-id-stripping']))
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[0].getAttribute('id'), '',
"ID 'text1' not stripped when none of the '--protect-ids-_' options was specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[1].getAttribute('id'), '',
"ID 'text2' not stripped when none of the '--protect-ids-_' options was specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[2].getAttribute('id'), '',
"ID 'text3' not stripped when none of the '--protect-ids-_' options was specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[3].getAttribute('id'), '',
"ID 'text_custom' not stripped when none of the '--protect-ids-_' options was specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[4].getAttribute('id'), '',
"ID 'my_text1' not stripped when none of the '--protect-ids-_' options was specified")
def test_protect_ids_noninkscape(self):
doc = scourXmlFile('unittests/ids-protect.svg',
parse_args(['--enable-id-stripping', '--protect-ids-noninkscape']))
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[0].getAttribute('id'), '',
"ID 'text1' should have been stripped despite '--protect-ids-noninkscape' being specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[1].getAttribute('id'), '',
"ID 'text2' should have been stripped despite '--protect-ids-noninkscape' being specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[2].getAttribute('id'), '',
"ID 'text3' should have been stripped despite '--protect-ids-noninkscape' being specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[3].getAttribute('id'), 'text_custom',
"ID 'text_custom' should NOT have been stripped because of '--protect-ids-noninkscape'")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[4].getAttribute('id'), '',
"ID 'my_text1' should have been stripped despite '--protect-ids-noninkscape' being specified")
def test_protect_ids_list(self):
doc = scourXmlFile('unittests/ids-protect.svg',
parse_args(['--enable-id-stripping', '--protect-ids-list=text2,text3']))
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[0].getAttribute('id'), '',
"ID 'text1' should have been stripped despite '--protect-ids-list' being specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[1].getAttribute('id'), 'text2',
"ID 'text2' should NOT have been stripped because of '--protect-ids-list'")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[2].getAttribute('id'), 'text3',
"ID 'text3' should NOT have been stripped because of '--protect-ids-list'")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[3].getAttribute('id'), '',
"ID 'text_custom' should have been stripped despite '--protect-ids-list' being specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[4].getAttribute('id'), '',
"ID 'my_text1' should have been stripped despite '--protect-ids-list' being specified")
def test_protect_ids_prefix(self):
doc = scourXmlFile('unittests/ids-protect.svg',
parse_args(['--enable-id-stripping', '--protect-ids-prefix=my']))
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[0].getAttribute('id'), '',
"ID 'text1' should have been stripped despite '--protect-ids-prefix' being specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[1].getAttribute('id'), '',
"ID 'text2' should have been stripped despite '--protect-ids-prefix' being specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[2].getAttribute('id'), '',
"ID 'text3' should have been stripped despite '--protect-ids-prefix' being specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[3].getAttribute('id'), '',
"ID 'text_custom' should have been stripped despite '--protect-ids-prefix' being specified")
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'text')[4].getAttribute('id'), 'my_text1',
"ID 'my_text1' should NOT have been stripped because of '--protect-ids-prefix'")
class RemoveUselessNestedGroups(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/nested-useless-groups.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'g')), 1,
'Useless nested groups not removed')
class DoNotRemoveUselessNestedGroups(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/nested-useless-groups.svg',
parse_args(['--disable-group-collapsing']))
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'g')), 2,
'Useless nested groups were removed despite --disable-group-collapsing')
class DoNotRemoveNestedGroupsWithTitle(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/groups-with-title-desc.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'g')), 2,
'Nested groups with title was removed')
class DoNotRemoveNestedGroupsWithDesc(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/groups-with-title-desc.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'g')), 2,
'Nested groups with desc was removed')
class RemoveDuplicateLinearGradientStops(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/duplicate-gradient-stops.svg')
grad = doc.getElementsByTagNameNS(SVGNS, 'linearGradient')
self.assertEqual(len(grad[0].getElementsByTagNameNS(SVGNS, 'stop')), 3,
'Duplicate linear gradient stops not removed')
class RemoveDuplicateLinearGradientStopsPct(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/duplicate-gradient-stops-pct.svg')
grad = doc.getElementsByTagNameNS(SVGNS, 'linearGradient')
self.assertEqual(len(grad[0].getElementsByTagNameNS(SVGNS, 'stop')), 3,
'Duplicate linear gradient stops with percentages not removed')
class RemoveDuplicateRadialGradientStops(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/duplicate-gradient-stops.svg')
grad = doc.getElementsByTagNameNS(SVGNS, 'radialGradient')
self.assertEqual(len(grad[0].getElementsByTagNameNS(SVGNS, 'stop')), 3,
'Duplicate radial gradient stops not removed')
class NoSodipodiNamespaceDecl(unittest.TestCase):
def runTest(self):
attrs = scourXmlFile('unittests/sodipodi.svg').documentElement.attributes
for i in range(len(attrs)):
self.assertNotEqual(attrs.item(i).nodeValue,
'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd',
'Sodipodi namespace declaration found')
class NoInkscapeNamespaceDecl(unittest.TestCase):
def runTest(self):
attrs = scourXmlFile('unittests/inkscape.svg').documentElement.attributes
for i in range(len(attrs)):
self.assertNotEqual(attrs.item(i).nodeValue,
'http://www.inkscape.org/namespaces/inkscape',
'Inkscape namespace declaration found')
class NoSodipodiAttributes(unittest.TestCase):
def runTest(self):
def findSodipodiAttr(elem):
attrs = elem.attributes
if attrs is None:
return True
for i in range(len(attrs)):
if attrs.item(i).namespaceURI == 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd':
return False
return True
self.assertNotEqual(walkTree(scourXmlFile('unittests/sodipodi.svg').documentElement, findSodipodiAttr),
False,
'Found Sodipodi attributes')
class NoInkscapeAttributes(unittest.TestCase):
def runTest(self):
def findInkscapeAttr(elem):
attrs = elem.attributes
if attrs is None:
return True
for i in range(len(attrs)):
if attrs.item(i).namespaceURI == 'http://www.inkscape.org/namespaces/inkscape':
return False
return True
self.assertNotEqual(walkTree(scourXmlFile('unittests/inkscape.svg').documentElement, findInkscapeAttr),
False,
'Found Inkscape attributes')
class KeepInkscapeNamespaceDeclarationsWhenKeepEditorData(unittest.TestCase):
def runTest(self):
options = ScourOptions
options.keep_editor_data = True
attrs = scourXmlFile('unittests/inkscape.svg', options).documentElement.attributes
FoundNamespace = False
for i in range(len(attrs)):
if attrs.item(i).nodeValue == 'http://www.inkscape.org/namespaces/inkscape':
FoundNamespace = True
break
self.assertEqual(True, FoundNamespace,
"Did not find Inkscape namespace declaration when using --keep-editor-data")
return False
class KeepSodipodiNamespaceDeclarationsWhenKeepEditorData(unittest.TestCase):
def runTest(self):
options = ScourOptions
options.keep_editor_data = True
attrs = scourXmlFile('unittests/sodipodi.svg', options).documentElement.attributes
FoundNamespace = False
for i in range(len(attrs)):
if attrs.item(i).nodeValue == 'http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd':
FoundNamespace = True
break
self.assertEqual(True, FoundNamespace,
"Did not find Sodipodi namespace declaration when using --keep-editor-data")
return False
class KeepReferencedFonts(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/referenced-font.svg')
fonts = doc.documentElement.getElementsByTagNameNS(SVGNS, 'font')
self.assertEqual(len(fonts), 1,
'Font wrongly removed from <defs>')
class ConvertStyleToAttrs(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-transparent.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('style'), '',
'style attribute not emptied')
class RemoveStrokeWhenStrokeTransparent(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-transparent.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke'), '',
'stroke attribute not emptied when stroke opacity zero')
class RemoveStrokeWidthWhenStrokeTransparent(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-transparent.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-width'), '',
'stroke-width attribute not emptied when stroke opacity zero')
class RemoveStrokeLinecapWhenStrokeTransparent(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-transparent.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-linecap'), '',
'stroke-linecap attribute not emptied when stroke opacity zero')
class RemoveStrokeLinejoinWhenStrokeTransparent(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-transparent.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-linejoin'), '',
'stroke-linejoin attribute not emptied when stroke opacity zero')
class RemoveStrokeDasharrayWhenStrokeTransparent(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-transparent.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-dasharray'), '',
'stroke-dasharray attribute not emptied when stroke opacity zero')
class RemoveStrokeDashoffsetWhenStrokeTransparent(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-transparent.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-dashoffset'), '',
'stroke-dashoffset attribute not emptied when stroke opacity zero')
class RemoveStrokeWhenStrokeWidthZero(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-nowidth.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke'), '',
'stroke attribute not emptied when width zero')
class RemoveStrokeOpacityWhenStrokeWidthZero(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-nowidth.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-opacity'), '',
'stroke-opacity attribute not emptied when width zero')
class RemoveStrokeLinecapWhenStrokeWidthZero(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-nowidth.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-linecap'), '',
'stroke-linecap attribute not emptied when width zero')
class RemoveStrokeLinejoinWhenStrokeWidthZero(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-nowidth.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-linejoin'), '',
'stroke-linejoin attribute not emptied when width zero')
class RemoveStrokeDasharrayWhenStrokeWidthZero(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-nowidth.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-dasharray'), '',
'stroke-dasharray attribute not emptied when width zero')
class RemoveStrokeDashoffsetWhenStrokeWidthZero(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-nowidth.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-dashoffset'), '',
'stroke-dashoffset attribute not emptied when width zero')
class RemoveStrokeWhenStrokeNone(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-none.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke'), '',
'stroke attribute not emptied when no stroke')
class KeepStrokeWhenInheritedFromParent(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-none.svg')
self.assertEqual(doc.getElementById('p1').getAttribute('stroke'), 'none',
'stroke attribute removed despite a different value being inherited from a parent')
class KeepStrokeWhenInheritedByChild(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-none.svg')
self.assertEqual(doc.getElementById('g2').getAttribute('stroke'), 'none',
'stroke attribute removed despite it being inherited by a child')
class RemoveStrokeWidthWhenStrokeNone(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-none.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-width'), '',
'stroke-width attribute not emptied when no stroke')
class KeepStrokeWidthWhenInheritedByChild(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-none.svg')
self.assertEqual(doc.getElementById('g3').getAttribute('stroke-width'), '1px',
'stroke-width attribute removed despite it being inherited by a child')
class RemoveStrokeOpacityWhenStrokeNone(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-none.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-opacity'), '',
'stroke-opacity attribute not emptied when no stroke')
class RemoveStrokeLinecapWhenStrokeNone(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-none.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-linecap'), '',
'stroke-linecap attribute not emptied when no stroke')
class RemoveStrokeLinejoinWhenStrokeNone(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-none.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-linejoin'), '',
'stroke-linejoin attribute not emptied when no stroke')
class RemoveStrokeDasharrayWhenStrokeNone(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-none.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-dasharray'), '',
'stroke-dasharray attribute not emptied when no stroke')
class RemoveStrokeDashoffsetWhenStrokeNone(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/stroke-none.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('stroke-dashoffset'), '',
'stroke-dashoffset attribute not emptied when no stroke')
class RemoveFillRuleWhenFillNone(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/fill-none.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('fill-rule'), '',
'fill-rule attribute not emptied when no fill')
class RemoveFillOpacityWhenFillNone(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/fill-none.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('fill-opacity'), '',
'fill-opacity attribute not emptied when no fill')
class ConvertFillPropertyToAttr(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/fill-none.svg',
parse_args(['--disable-simplify-colors']))
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[1].getAttribute('fill'), 'black',
'fill property not converted to XML attribute')
class ConvertFillOpacityPropertyToAttr(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/fill-none.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[1].getAttribute('fill-opacity'), '.5',
'fill-opacity property not converted to XML attribute')
class ConvertFillRuleOpacityPropertyToAttr(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/fill-none.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'path')[1].getAttribute('fill-rule'), 'evenodd',
'fill-rule property not converted to XML attribute')
class CollapseSinglyReferencedGradients(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/collapse-gradients.svg')
self.assertEqual(len(doc.getElementsByTagNameNS(SVGNS, 'linearGradient')), 0,
'Singly-referenced linear gradient not collapsed')
class InheritGradientUnitsUponCollapsing(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/collapse-gradients.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'radialGradient')[0].getAttribute('gradientUnits'),
'userSpaceOnUse',
'gradientUnits not properly inherited when collapsing gradients')
class OverrideGradientUnitsUponCollapsing(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/collapse-gradients-gradientUnits.svg')
self.assertEqual(doc.getElementsByTagNameNS(SVGNS, 'radialGradient')[0].getAttribute('gradientUnits'), '',
'gradientUnits not properly overrode when collapsing gradients')
class DoNotCollapseMultiplyReferencedGradients(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/dont-collapse-gradients.svg')
self.assertNotEqual(len(doc.getElementsByTagNameNS(SVGNS, 'linearGradient')), 0,
'Multiply-referenced linear gradient collapsed')
class PreserveXLinkHrefWhenCollapsingReferencedGradients(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/collapse-gradients-preserve-xlink-href.svg')
g1 = doc.getElementById("g1")
g2 = doc.getElementById("g2")
g3 = doc.getElementById("g3")
self.assertTrue(g1, 'g1 is still present')
self.assertTrue(g2 is None, 'g2 was removed')
self.assertTrue(g3, 'g3 is still present')
self.assertEqual(g3.getAttributeNS('http://www.w3.org/1999/xlink', 'href'), '#g1',
'g3 has a xlink:href to g1')
class RemoveTrailingZerosFromPath(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/path-truncate-zeros.svg')
path = doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('d')
self.assertEqual(path[:4] == 'm300' and path[4] != '.', True,
'Trailing zeros not removed from path data')
class RemoveTrailingZerosFromPathAfterCalculation(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/path-truncate-zeros-calc.svg')
path = doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('d')
self.assertEqual(path, 'm5.81 0h0.1',
'Trailing zeros not removed from path data after calculation')
class RemoveDelimiterBeforeNegativeCoordsInPath(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/path-truncate-zeros.svg')
path = doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('d')
self.assertEqual(path[4], '-',
'Delimiters not removed before negative coordinates in path data')
class UseScientificNotationToShortenCoordsInPath(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/path-use-scientific-notation.svg')
path = doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('d')
self.assertEqual(path, 'm1e4 0',
'Not using scientific notation for path coord when representation is shorter')
class ConvertAbsoluteToRelativePathCommands(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/path-abs-to-rel.svg')
path = svg_parser.parse(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('d'))
self.assertEqual(path[1][0], 'v',
'Absolute V command not converted to relative v command')
self.assertEqual(float(path[1][1][0]), -20.0,
'Absolute V value not converted to relative v value')
class RoundPathData(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/path-precision.svg')
path = svg_parser.parse(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('d'))
self.assertEqual(float(path[0][1][0]), 100.0,
'Not rounding down')
self.assertEqual(float(path[0][1][1]), 100.0,
'Not rounding up')
class LimitPrecisionInPathData(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/path-precision.svg')
path = svg_parser.parse(doc.getElementsByTagNameNS(SVGNS, 'path')[0].getAttribute('d'))
self.assertEqual(float(path[1][1][0]), 100.01,
'Not correctly limiting precision on path data')
class KeepPrecisionInPathDataIfSameLength(unittest.TestCase):
def runTest(self):
doc = scourXmlFile('unittests/path-precision.svg', parse_args(['--set-precision=1']))
paths = doc.getElementsByTagNameNS(SVGNS, 'path')
for path in paths[1:3]:
self.assertEqual(path.getAttribute('d'), "m1 21 321 4e3 5e4 7e5",
'Precision not correctly reduced with "--set-precision=1" '
'for path with ID ' + path.getAttribute('id'))
self.assertEqual(paths[4].getAttribute('d'), "m-1-21-321-4e3 -5e4 -7e5",
'Precision not correctly reduced with "--set-precision=1" '
'for path with ID ' + paths[4].getAttribute('id'))
self.assertEqual(paths[5].getAttribute('d'), "m123 101-123-101",
'Precision not correctly reduced with "--set-precision=1" '
'for path with ID ' + paths[5].getAttribute('id'))
doc = scourXmlFile('unittests/path-precision.svg', parse_args(['--set-precision=2']))
paths = doc.getElementsByTagNameNS(SVGNS, 'path')
for path in paths[1:3]:
self.assertEqual(path.getAttribute('d'), "m1 21 321 4321 54321 6.5e5",
'Precision not correctly reduced with "--set-precision=2" '
'for path with ID ' + path.getAttribute('id'))
self.assertEqual(paths[4].getAttribute('d'), "m-1-21-321-4321-54321-6.5e5",
'Precision not correctly reduced with "--set-precision=2" '
'for path with ID ' + paths[4].getAttribute('id'))