weasel API - v0.8.0
    Preparing search index...

    Function domPositionToCharOffset

    • Inverse of charOffsetToDomPosition. Walks text nodes in document order; sums the lengths of every text node preceding node and adds offset.

      When node is an element the DOM offset indexes child nodes, not characters, so it can't simply be added — (overlay, 0) is the start of the text and (overlay, childNodes.length) its end. That shape is not exotic: Range.selectNodeContents, which useTextEdit uses for its select-all caret, produces exactly it. So resolve an element position by counting the text that precedes the boundary point. A text node can never contain that point (the container isn't one), so each is wholly before or wholly after it.

      Parameters

      • parent: HTMLElement
      • node: Node
      • offset: number

      Returns number