## Summary * **What is the goal of this PR?** * Fix EPUBs where internal file references are written in URL-style escaped form, like spaces appearing as `%20`, so the reader can find the right files instead of treating those references as missing. * **What changes are included?** * Added a small shared helper that converts those escaped EPUB-internal paths back into their normal filenames before we try to look them up. * Applied that cleanup step across the EPUB parsing flow wherever we resolve internal references, including cover images, manifest items, TOC links, spine entries, and inline HTML images. * Kept the change narrowly focused on EPUB-internal asset resolution rather than changing broader URL or networking behavior. ## Additional Context * The user-facing bug here is that some books package their internal filenames in an escaped form, so a file like `Chapter 1.xhtml` may be referenced more like `Chapter%201.xhtml`. The reader was treating that escaped text as the literal filename, which means otherwise-valid books could lose images, covers, or chapter targets because the lookup no longer matched the real file inside the EPUB. * Risk is intentionally low. The helper only rewrites valid `%XX` escape sequences and leaves malformed input alone, so it should improve compatibility with escaped filenames without broadening the parser’s behavior in unrelated cases. ## Local Testing Performed * This was tested on my device with the user-provided optimized epub that was not rendering images within the text prior to this fix (cover image and chapter headers were rendering fine): [orv_main_baseline.epub.zip](https://github.com/user-attachments/files/28529545/orv_main_baseline.epub.zip) * This was also tested by the user with a local build and the affected epub and confirmed to be working ## Steps for Testing * Try to open the affected epub (linked above) or any epub that has similar percent-encoding on a build prior to this fix. * Apply this fix, clear book cache, and re-open the affected book. * Images should render properly. --- ### 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? _**< YES >**_
172 lines
4.8 KiB
C++
172 lines
4.8 KiB
C++
#include "TocNavParser.h"
|
|
|
|
#include <FsHelpers.h>
|
|
#include <Logging.h>
|
|
#include <XmlParserUtils.h>
|
|
|
|
#include "Epub/BookMetadataCache.h"
|
|
|
|
bool TocNavParser::setup() {
|
|
parser = XML_ParserCreate(nullptr);
|
|
if (!parser) {
|
|
LOG_DBG("NAV", "Couldn't allocate memory for parser");
|
|
return false;
|
|
}
|
|
|
|
XML_SetUserData(parser, this);
|
|
XML_SetElementHandler(parser, startElement, endElement);
|
|
XML_SetCharacterDataHandler(parser, characterData);
|
|
return true;
|
|
}
|
|
|
|
TocNavParser::~TocNavParser() { destroyXmlParser(parser); }
|
|
|
|
size_t TocNavParser::write(const uint8_t data) { return write(&data, 1); }
|
|
|
|
size_t TocNavParser::write(const uint8_t* buffer, const size_t size) {
|
|
if (!parser) return 0;
|
|
|
|
const uint8_t* currentBufferPos = buffer;
|
|
auto remainingInBuffer = size;
|
|
|
|
while (remainingInBuffer > 0) {
|
|
void* const buf = XML_GetBuffer(parser, 1024);
|
|
if (!buf) {
|
|
LOG_DBG("NAV", "Couldn't allocate memory for buffer");
|
|
destroyXmlParser(parser);
|
|
return 0;
|
|
}
|
|
|
|
const auto toRead = remainingInBuffer < 1024 ? remainingInBuffer : 1024;
|
|
memcpy(buf, currentBufferPos, toRead);
|
|
|
|
if (XML_ParseBuffer(parser, static_cast<int>(toRead), remainingSize == toRead) == XML_STATUS_ERROR) {
|
|
LOG_DBG("NAV", "Parse error at line %lu: %s", XML_GetCurrentLineNumber(parser),
|
|
XML_ErrorString(XML_GetErrorCode(parser)));
|
|
destroyXmlParser(parser);
|
|
return 0;
|
|
}
|
|
|
|
currentBufferPos += toRead;
|
|
remainingInBuffer -= toRead;
|
|
remainingSize -= toRead;
|
|
}
|
|
return size;
|
|
}
|
|
|
|
void XMLCALL TocNavParser::startElement(void* userData, const XML_Char* name, const XML_Char** atts) {
|
|
auto* self = static_cast<TocNavParser*>(userData);
|
|
|
|
// Track HTML structure loosely - we mainly care about finding <nav epub:type="toc">
|
|
if (strcmp(name, "html") == 0) {
|
|
self->state = IN_HTML;
|
|
return;
|
|
}
|
|
|
|
if (self->state == IN_HTML && strcmp(name, "body") == 0) {
|
|
self->state = IN_BODY;
|
|
return;
|
|
}
|
|
|
|
// Look for <nav epub:type="toc"> anywhere in body (or nested elements)
|
|
if (self->state >= IN_BODY && strcmp(name, "nav") == 0) {
|
|
for (int i = 0; atts[i]; i += 2) {
|
|
if ((strcmp(atts[i], "epub:type") == 0 || strcmp(atts[i], "type") == 0) && strcmp(atts[i + 1], "toc") == 0) {
|
|
self->state = IN_NAV_TOC;
|
|
LOG_DBG("NAV", "Found nav toc element");
|
|
return;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Only process ol/li/a if we're inside the toc nav
|
|
if (self->state < IN_NAV_TOC) {
|
|
return;
|
|
}
|
|
|
|
if (strcmp(name, "ol") == 0) {
|
|
self->olDepth++;
|
|
self->state = IN_OL;
|
|
return;
|
|
}
|
|
|
|
if (self->state == IN_OL && strcmp(name, "li") == 0) {
|
|
self->state = IN_LI;
|
|
self->currentLabel.clear();
|
|
self->currentHref.clear();
|
|
return;
|
|
}
|
|
|
|
if (self->state == IN_LI && strcmp(name, "a") == 0) {
|
|
self->state = IN_ANCHOR;
|
|
// Get href attribute
|
|
for (int i = 0; atts[i]; i += 2) {
|
|
if (strcmp(atts[i], "href") == 0) {
|
|
self->currentHref = atts[i + 1];
|
|
break;
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
}
|
|
|
|
void XMLCALL TocNavParser::characterData(void* userData, const XML_Char* s, const int len) {
|
|
auto* self = static_cast<TocNavParser*>(userData);
|
|
|
|
// Only collect text when inside an anchor within the TOC nav
|
|
if (self->state == IN_ANCHOR) {
|
|
self->currentLabel.append(s, len);
|
|
}
|
|
}
|
|
|
|
void XMLCALL TocNavParser::endElement(void* userData, const XML_Char* name) {
|
|
auto* self = static_cast<TocNavParser*>(userData);
|
|
|
|
if (strcmp(name, "a") == 0 && self->state == IN_ANCHOR) {
|
|
// Create TOC entry when closing anchor tag (we have all data now)
|
|
if (!self->currentLabel.empty() && !self->currentHref.empty()) {
|
|
const std::string rawTarget = self->baseContentPath + self->currentHref;
|
|
const size_t pos = rawTarget.find('#');
|
|
const std::string rawPath = pos == std::string::npos ? rawTarget : rawTarget.substr(0, pos);
|
|
std::string href = FsHelpers::normalisePath(FsHelpers::decodeUriEscapes(rawPath));
|
|
std::string anchor;
|
|
|
|
if (pos != std::string::npos) {
|
|
anchor = FsHelpers::decodeUriEscapes(rawTarget.substr(pos + 1));
|
|
}
|
|
|
|
if (self->cache) {
|
|
// olDepth gives us the nesting level (1-based from the outer ol)
|
|
self->cache->createTocEntry(self->currentLabel, href, anchor, self->olDepth);
|
|
}
|
|
|
|
self->currentLabel.clear();
|
|
self->currentHref.clear();
|
|
}
|
|
self->state = IN_LI;
|
|
return;
|
|
}
|
|
|
|
if (strcmp(name, "li") == 0 && (self->state == IN_LI || self->state == IN_OL)) {
|
|
self->state = IN_OL;
|
|
return;
|
|
}
|
|
|
|
if (strcmp(name, "ol") == 0 && self->state >= IN_NAV_TOC) {
|
|
self->olDepth--;
|
|
if (self->olDepth == 0) {
|
|
self->state = IN_NAV_TOC;
|
|
} else {
|
|
self->state = IN_LI; // Back to parent li
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (strcmp(name, "nav") == 0 && self->state >= IN_NAV_TOC) {
|
|
self->state = IN_BODY;
|
|
LOG_DBG("NAV", "Finished parsing nav toc");
|
|
return;
|
|
}
|
|
}
|