The CSSOM View module specification adds caretPositionFromPoint(x, y) to the Document interface, which returns the caret position for the specified x and y co -ordinates. WebKit supports caretRangeFromPoint , a close counterpart from an earlier specification that returns Range .
It is possible that the word was moved across two columns, so instead of wrapping the first word in the gap, you might consider a more naive approach to inserting a span directly at the cursor point. Here is an example:
var caretPos = document.caretRangeFromPoint(x, y); if (caretPos) caretPos.insertNode(document.createElement('span'));
Demo (only for WebKit to insert intervals): http://jsfiddle.net/Jordan/Aw9aV/
One final consideration: it is possible that WebKit will eventually stop supporting caretRangeFromPoint instead of caretPositionFromPoint ; if so, you will need to adapt your code. Also note that the latter returns a CaretPosition , which the insertNode method cannot implement. The spectrum is still on WD, so remember that it is still in motion.
source share