From e8f8a6c40df288c2e5dddf913c03f08c913d9ee0 Mon Sep 17 00:00:00 2001 From: Cosimo Lupo Date: Mon, 22 May 2023 11:57:46 +0100 Subject: [PATCH] xmlReader: join consecutive text data that had been cut by buffered parser Fixes https://github.com/fonttools/fonttools/issues/2614 --- Lib/fontTools/misc/xmlReader.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/Lib/fontTools/misc/xmlReader.py b/Lib/fontTools/misc/xmlReader.py index ce9de579b..d8e502f14 100644 --- a/Lib/fontTools/misc/xmlReader.py +++ b/Lib/fontTools/misc/xmlReader.py @@ -148,7 +148,19 @@ class XMLReader(object): def _characterDataHandler(self, data): if self.stackSize > 1: - self.contentStack[-1].append(data) + # parser parses in chunks, so we may get multiple calls + # for the same text node; thus we need to append the data + # to the last item in the content stack: + # https://github.com/fonttools/fonttools/issues/2614 + if ( + data != "\n" + and self.contentStack[-1] + and isinstance(self.contentStack[-1][-1], str) + and self.contentStack[-1][-1] != "\n" + ): + self.contentStack[-1][-1] += data + else: + self.contentStack[-1].append(data) def _endElementHandler(self, name): self.stackSize = self.stackSize - 1