diff --git a/Lib/ufoLib/glifLib.py b/Lib/ufoLib/glifLib.py index e3d5ee086..1cb2d7dcc 100755 --- a/Lib/ufoLib/glifLib.py +++ b/Lib/ufoLib/glifLib.py @@ -548,10 +548,7 @@ def readGlyphFromString(aString, glyphObject=None, pointPen=None, formatVersions _readGlyphFromTree(tree, glyphObject, pointPen, formatVersions=formatVersions, validate=validate) -# we use a custom XML declaration for backward compatibility with older -# ufoLib versions which would write it using double quotes. -# https://github.com/unified-font-object/ufoLib/issues/158 -XML_DECLARATION = b"""\n""" +_XML_DECLARATION = plistlib.XML_DECLARATION + b"\n" def _writeGlyphToBytes( @@ -597,7 +594,7 @@ def _writeGlyphToBytes( if getattr(glyphObject, "lib", None): _writeLib(glyphObject, root, validate) # return the text - data = XML_DECLARATION + etree.tostring( + data = _XML_DECLARATION + etree.tostring( root, encoding="utf-8", xml_declaration=False, pretty_print=True ) return data diff --git a/Lib/ufoLib/plistlib.py b/Lib/ufoLib/plistlib.py index 00b9331d5..7cf080a07 100644 --- a/Lib/ufoLib/plistlib.py +++ b/Lib/ufoLib/plistlib.py @@ -13,6 +13,11 @@ from lxml import etree from fontTools.misc.py23 import unicode, basestring, tounicode +# we use a custom XML declaration for backward compatibility with older +# ufoLib versions which would write it using double quotes. +# https://github.com/unified-font-object/ufoLib/issues/158 +XML_DECLARATION = b"""""" + PLIST_DOCTYPE = ( '' @@ -328,11 +333,14 @@ def dump(value, fp, sort_keys=True, skipkeys=False, _pretty_print=True): root = etree.Element("plist", version="1.0") root.append(totree(value, sort_keys=sort_keys, skipkeys=skipkeys)) tree = etree.ElementTree(root) + fp.write(XML_DECLARATION) + if _pretty_print: + fp.write(b"\n") tree.write( fp, encoding="utf-8", pretty_print=_pretty_print, - xml_declaration=True, + xml_declaration=False, doctype=PLIST_DOCTYPE, ) diff --git a/Lib/ufoLib/test/test_glifLib.py b/Lib/ufoLib/test/test_glifLib.py index 62e2d5531..c744510ef 100644 --- a/Lib/ufoLib/test/test_glifLib.py +++ b/Lib/ufoLib/test/test_glifLib.py @@ -6,7 +6,7 @@ from io import open from ufoLib.test.testSupport import getDemoFontGlyphSetPath from ufoLib.glifLib import ( GlyphSet, glyphNameToFileName, readGlyphFromString, writeGlyphToString, - XML_DECLARATION, + _XML_DECLARATION, ) GLYPHSETDIR = getDemoFontGlyphSetPath() @@ -160,7 +160,7 @@ class ReadWriteFuncTest(unittest.TestCase): def testXmlDeclaration(self): s = writeGlyphToString("a", _Glyph()) - self.assertTrue(s.startswith(XML_DECLARATION.decode("utf-8"))) + self.assertTrue(s.startswith(_XML_DECLARATION.decode("utf-8"))) if __name__ == "__main__":