diff --git a/internal/sync/cfi_converter.go b/internal/sync/cfi_converter.go index 366784d..4a4c141 100644 --- a/internal/sync/cfi_converter.go +++ b/internal/sync/cfi_converter.go @@ -1321,9 +1321,16 @@ func countTextChars(n *html.Node) int { return count } +// countTextCharsBefore counts the text characters preceding target within +// its document: preceding siblings at each ancestor level, strictly +// EXCLUDING target's own subtree (callers add the in-node offset +// separately). Starting the walk at target itself — or recursing after +// counting the current node's subtree — would re-count the accumulated +// document once per ancestor level and multiply the result by the node's +// depth. func countTextCharsBefore(target *html.Node) int { count := 0 - for c := target; c != nil; c = c.PrevSibling { + for c := target.PrevSibling; c != nil; c = c.PrevSibling { count += countTextChars(c) } if target.Parent != nil {