From 80772ff6b808b9d65ebf031ac132009c1d10bd22 Mon Sep 17 00:00:00 2001 From: Stefan Blixten Karlsson Date: Thu, 16 Apr 2026 04:47:10 +0200 Subject: [PATCH] fix: footnote link text (#1666) ## Summary Previouls where ALL whitespace (and square brackes) removed from the footnote link text, however some link texts are multiworded, like "`turn to 252`" which were truncated into "`turnto252`", (an example from the first book "Flight from the Dark" of the Lone Wolf book series by Joe Dever, see [link](https://www.projectaon.org/en/Main/FlightFromTheDark)) * This change will only remove whitespaces from the beginning and end of the string so "` [ 12 ] `" will become "`12`" just like before, and "` turn to 252 `" will become "`turn to 252`". --- ### AI Usage While CrossPoint doesn't have restrictions on AI tools in contributing, please be transparent about their usage as it helps set the right context for reviewers. Did you use AI tools to help write this code? _**NO**_ --- .../Epub/parsers/ChapterHtmlSlimParser.cpp | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp index 957a6b0a3..6245d2ae4 100644 --- a/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp +++ b/lib/Epub/Epub/parsers/ChapterHtmlSlimParser.cpp @@ -697,14 +697,29 @@ 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" 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++) {