@@ -371,6 +371,70 @@ def test_svg_default_metadata(monkeypatch):
371371 # Type
372372 assert 'StillImage' in buf
373373
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+ for name in metadata_contains :
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+
423+ def test_svg_clear_all_metadata ():
424+ # Makes sure that setting all default metadata to `None`
425+ # removes the metadata tag from the output.
426+
427+ fig , ax = plt .subplots ()
428+ with BytesIO () as fd :
429+ fig .savefig (fd , format = 'svg' , metadata = {'Date' : None , 'Creator' : None ,
430+ 'Format' : None , 'Type' : None })
431+ buf = fd .getvalue ().decode ()
432+
433+ SVGNS = '{http://www.w3.org/2000/svg}'
434+
435+ root = xml .etree .ElementTree .fromstring (buf )
436+ assert not root .findall (f'./{ SVGNS } metadata' )
437+
374438
375439def test_svg_metadata ():
376440 single_value = ['Coverage' , 'Identifier' , 'Language' , 'Relation' , 'Source' ,
0 commit comments