| 900 | } |
| 901 | |
| 902 | int Epub::resolveHrefToSpineIndex(const std::string& href) const { |
| 903 | if (!bookMetadataCache || !bookMetadataCache->isLoaded()) return -1; |
| 904 | |
| 905 | // Split before decoding so escaped '#' characters in filenames stay part of the path. |
| 906 | const size_t hashPos = href.find('#'); |
| 907 | const std::string rawTarget = hashPos != std::string::npos ? href.substr(0, hashPos) : href; |
| 908 | const std::string target = FsHelpers::normalisePath(FsHelpers::decodeUriEscapes(rawTarget)); |
| 909 | |
| 910 | // Same-file reference (anchor-only) |
| 911 | if (target.empty()) return -1; |
| 912 | |
| 913 | // Extract just the filename for comparison |
| 914 | size_t targetSlash = target.find_last_of('/'); |
| 915 | std::string targetFilename = (targetSlash != std::string::npos) ? target.substr(targetSlash + 1) : target; |
| 916 | |
| 917 | for (int i = 0; i < getSpineItemsCount(); i++) { |
| 918 | const auto& spineHref = getSpineItem(i).href; |
| 919 | // Try exact match first |
| 920 | if (spineHref == target) return i; |
| 921 | // Then filename-only match |
| 922 | size_t spineSlash = spineHref.find_last_of('/'); |
| 923 | std::string spineFilename = (spineSlash != std::string::npos) ? spineHref.substr(spineSlash + 1) : spineHref; |
| 924 | if (spineFilename == targetFilename) return i; |
| 925 | } |
| 926 | return -1; |
| 927 | } |
no test coverage detected