I know how to determine which DOM nodes intersect an absolute position. But say that’s a text node. How can I determine the offset within the text node corresponding to that location? I’d like to construct a DOMRange from a known position to that position.
Share
The first step would be to find all
Elementsthat intersect the position. You should use theElement.getBoundingClientRectmethod to do this. You can then easily get all of theTextnodes that sit within thatElement. The hard part is to figure out what text within thoseTextnodes are within those bounds. You can start with theElement.getClientRectsto get lines of text within thatElement. Here’s a great example.You’ll then want to construct a
Rangeobject, changing thestartOffsetandendOffsetappropriately. You can use thegetClientRectsmethod on theRangeobject, too.