fix(sync): anchor device highlight ranges to selection text
KOReader highlights converted to wrong or unresolvable web CFIs whenever a selection crossed text-node boundaries. Three compounding defects: ParseCREXPointer discarded the text()[N] node index and applied .offset as a global block offset; the end anchor converted with an empty context (blind structural landing, zero verification); and the block-containment check accepted wrong-node landings because a selection's text always occurs somewhere in its own block. pos0 and pos1 were converted independently, yielding mismatched start/end base paths and backwards ranges - highlights stored but never painted, or painted at the wrong location, in the web reader. Add ConvertHighlightRange: both endpoints are anchored by locating the selection text in the flattened document text (whitespace-normalized, crossing block boundaries), with the device locators demoted to disambiguation hints. A single document match yields a same-spine, ordered start/end pair by construction. Falls back to the legacy per-endpoint path when the text cannot be located, so behavior is never worse than before. Also fix the shared flatten stream: zero-width format characters (ZWSP/ZWNJ/BOM) and whitespace-only inline spans (<span> </span>) now act as word separators instead of fusing adjacent words, which prevented text matching in real books. Verified against 28 real device highlights from production: converter output now matches the text-search oracle on all of them (previously 8/28 correct, 18 with mismatched base paths, 2 backwards).
This commit is contained in:
1 parent
cbdddc095e
commit
bb3aef517c
4 files changed
+227
-5
No files matched your search
@@ -125,6 +125,38 @@ func ConvertToCanonical(
|
||||
}
|
||||
}
|
||||
|
||||
// ConvertHighlightRangeToCanonical is the highlight-specific twin of
|
||||
// ConvertToCanonical: it anchors a device highlight's pos0/pos1 pair by the
|
||||
// selection text so both endpoints derive from one document match — same
|
||||
// spine, ordered — instead of two independent structural landings. Returns
|
||||
// a "fallback" locator with an empty CFI when the text cannot be located;
|
||||
// callers fall back to ConvertToCanonical per endpoint.
|
||||
func ConvertHighlightRangeToCanonical(
|
||||
epubPath string,
|
||||
formatGroup string,
|
||||
pos0 string,
|
||||
pos1 string,
|
||||
contextText string,
|
||||
percentage float64,
|
||||
) CanonicalLocator {
|
||||
if !isConvertible(formatGroup) || epubPath == "" {
|
||||
return CanonicalLocator{CFI: pos0, Precision: "passthrough", Percentage: percentage}
|
||||
}
|
||||
if !IsCREXPointer(pos0) {
|
||||
return CanonicalLocator{CFI: pos0, Precision: "already-standard", Percentage: percentage}
|
||||
}
|
||||
result := cachedConverter(epubPath).ConvertHighlightRange(pos0, pos1, contextText, percentage)
|
||||
if result == nil || result.EPUBCFI == "" {
|
||||
return CanonicalLocator{CFI: "", Precision: "fallback", Percentage: percentage}
|
||||
}
|
||||
return CanonicalLocator{
|
||||
CFI: result.EPUBCFI,
|
||||
EndCFI: result.EndEPUBCFI,
|
||||
Precision: result.Precision,
|
||||
Percentage: result.Percentage,
|
||||
}
|
||||
}
|
||||
|
||||
func ConvertFromCanonical(
|
||||
source LocatorSource,
|
||||
canonicalCFI string,
|
||||
|
||||
Reference in new issue
Block a user