From ed3b9446a93b2a3de3383f91fe2c9789ff8e6c98 Mon Sep 17 00:00:00 2001 From: ci Date: Wed, 19 Aug 2026 10:37:31 +0800 Subject: [PATCH] Preserve HTML entities in book text --- Reader/EpubBook.cpp | 7 +++++-- Reader/MobiBook.cpp | 5 +++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Reader/EpubBook.cpp b/Reader/EpubBook.cpp index 45d1e1c..47dfbb0 100644 --- a/Reader/EpubBook.cpp +++ b/Reader/EpubBook.cpp @@ -705,7 +705,10 @@ BOOL EpubBook::ParserOps(file_data_t *fdata, wchar_t **text, int *len, wchar_t * } xmlKeepBlanksDefault(1); - doc = xmlReadMemory((const char *)format_str, size, NULL, NULL, XML_PARSE_RECOVER/* | XML_PARSE_NOBLANKS*/); + // htmlDocDumpMemoryFormat may serialize characters such as curly quotes as + // HTML named entities. Reparse the formatted document as HTML so those + // entities survive instead of being discarded by the XML parser. + doc = htmlReadMemory((const char *)format_str, size, NULL, NULL, HTML_PARSE_RECOVER); xmlFree(format_str); if (!doc) goto end; @@ -1048,4 +1051,4 @@ BOOL EpubBook::ParserCover(epub_t &epub) } return m_Cover != NULL; -} \ No newline at end of file +} diff --git a/Reader/MobiBook.cpp b/Reader/MobiBook.cpp index 6d54bb1..53b489e 100644 --- a/Reader/MobiBook.cpp +++ b/Reader/MobiBook.cpp @@ -637,11 +637,12 @@ BOOL MobiBook::ParserOps(file_data_t *fdata, wchar_t **text, int *len, wchar_t * } xmlKeepBlanksDefault(1); - doc = xmlReadMemory((const char *)format_str, size, NULL, NULL, XML_PARSE_RECOVER | XML_PARSE_HUGE /*| XML_PARSE_NOBLANKS */ ); //XML_PARSE_HUGE 大文件支持 + // htmlDocDumpMemoryFormat can emit HTML named entities. The XML parser only + // understands XML's predefined entities and would silently lose others. + doc = htmlReadMemory((const char *)format_str, size, NULL, NULL, HTML_PARSE_RECOVER); xmlFree(format_str); if (!doc) goto end; - xmlDocDumpFormatMemory(doc, &format_str, &size, 1); #endif xpathctx = xmlXPathNewContext(doc);