From a5dd44b3e0b0ec6dfd6c480a3a8d5706c90b702c Mon Sep 17 00:00:00 2001 From: jpirnay Date: Wed, 15 Apr 2026 16:35:19 +0200 Subject: [PATCH] Implement fix for footnote links (PR 1666 by steka) --- .../Epub/parsers/ChapterHtmlSlimParser.cpp | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp index 1bd3f913..a5553179 100644 --- a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp +++ b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp @@ -956,16 +956,32 @@ void XMLCALL ChapterHtmlSlimParser::characterData(void* userData, const XML_Char } // Collect footnote link display text (for the number label) - // Skip whitespace and brackets to normalize noterefs like "[1]" → "1" + // Remove leading/trailing whitespace and square brackets from the + // footnote link text to normalize noterefs like "[1]" → "1" if (self->insideFootnoteLink) { - for (int i = 0; i < len; i++) { - unsigned char c = static_cast(s[i]); - if (isWhitespace(c) || c == '[' || c == ']') continue; - if (self->currentFootnoteLinkTextLen < static_cast(sizeof(self->currentFootnoteLinkText)) - 1) { - self->currentFootnoteLinkText[self->currentFootnoteLinkTextLen++] = c; - self->currentFootnoteLinkText[self->currentFootnoteLinkTextLen] = '\0'; - } + int start = 0; + int end = len - 1; + + // Example input and output texts: + // " [ 12 ] " => "12" + // " turn to 256 " => "turn to 256" + + // Ignore leading whitespaces and left square brackets + while (start < len && (isWhitespace(s[start]) || (s[start] == '['))) { + ++start; } + + // Ignore trailing whitespaces and right square brackets + while (end >= start && (isWhitespace(s[end]) || (s[end] == ']'))) { + --end; + } + + // Extract footnote link text + for (int i = start; (self->currentFootnoteLinkTextLen < sizeof(self->currentFootnoteLinkText) - 1) && (i <= end); + ++i) { + self->currentFootnoteLinkText[self->currentFootnoteLinkTextLen++] = s[i]; + } + self->currentFootnoteLinkText[self->currentFootnoteLinkTextLen] = '\0'; } for (int i = 0; i < len; i++) {