@@ -371,6 +371,73 @@ def test_svg_default_metadata(monkeypatch):
371
371
# Type
372
372
assert 'StillImage' in buf
373
373
374
+ # Now make sure all the default metadata can be cleared.
375
+ with BytesIO () as fd :
376
+ fig .savefig (fd , format = 'svg' , metadata = {'Date' : None , 'Creator' : None ,
377
+ 'Format' : None , 'Type' : None })
378
+ buf = fd .getvalue ().decode ()
379
+
380
+ # Creator
381
+ assert mpl .__version__ not in buf
382
+ # Date
383
+ assert '1970-08-16' not in buf
384
+ # Format
385
+ assert 'image/svg+xml' not in buf
386
+ # Type
387
+ assert 'StillImage' not in buf
388
+
389
+
390
+ def test_svg_clear_default_metadata (monkeypatch ):
391
+ # Makes sure that setting a default metadata to `None`
392
+ # removes the corresponding tag from the metadata.
393
+ monkeypatch .setenv ('SOURCE_DATE_EPOCH' , '19680801' )
394
+
395
+ metadata_contains = {'creator' : mpl .__version__ , 'date' : '1970-08-16' ,
396
+ 'format' : 'image/svg+xml' , 'type' : 'StillImage' }
397
+
398
+ SVGNS = '{http://www.w3.org/2000/svg}'
399
+ RDFNS = '{http://www.w3.org/1999/02/22-rdf-syntax-ns#}'
400
+ CCNS = '{http://creativecommons.org/ns#}'
401
+ DCNS = '{http://purl.org/dc/elements/1.1/}'
402
+
403
+ fig , ax = plt .subplots ()
404
+ def test_clear_1 (name ):
405
+ with BytesIO () as fd :
406
+ fig .savefig (fd , format = 'svg' , metadata = {name .title (): None })
407
+ buf = fd .getvalue ().decode ()
408
+
409
+ root = xml .etree .ElementTree .fromstring (buf )
410
+ work , = root .findall (f'./{ SVGNS } metadata/{ RDFNS } RDF/{ CCNS } Work' )
411
+ for key in metadata_contains :
412
+ data = work .findall (f'./{ DCNS } { key } ' )
413
+ if key == name :
414
+ # The one we cleared is not there
415
+ assert not data
416
+ continue
417
+ # Everything else should be there
418
+ data , = data
419
+ xmlstr = xml .etree .ElementTree .tostring (data , encoding = "unicode" )
420
+ assert metadata_contains [key ] in xmlstr
421
+
422
+ for key in metadata_contains :
423
+ test_clear_1 (key )
424
+
425
+
426
+ def test_svg_clear_all_metadata ():
427
+ # Makes sure that setting all default metadata to `None`
428
+ # removes the metadata tag from the output.
429
+
430
+ fig , ax = plt .subplots ()
431
+ with BytesIO () as fd :
432
+ fig .savefig (fd , format = 'svg' , metadata = {'Date' : None , 'Creator' : None ,
433
+ 'Format' : None , 'Type' : None })
434
+ buf = fd .getvalue ().decode ()
435
+
436
+ SVGNS = '{http://www.w3.org/2000/svg}'
437
+
438
+ root = xml .etree .ElementTree .fromstring (buf )
439
+ assert not root .findall (f'./{ SVGNS } metadata' )
440
+
374
441
375
442
def test_svg_metadata ():
376
443
single_value = ['Coverage' , 'Identifier' , 'Language' , 'Relation' , 'Source' ,
0 commit comments