Steps to reproduce:
- Go to the contentEditable div in http://thottingal.in/projects/js/jquery.ime/examples/
- Move the cursor to the end of the content, and press Enter to create a new paragraph (actually <p><br></p> in most browsers).
- Enable Malayalam input, and press 'x'
- Observe that the text gets inserted at the very start of the contentEditable text, instead of where the cursor is.
This happens because in jquery.ime.js#getDivCaretPosition, traverseTextNodes only checks node === range.startContainer in text nodes. So in the case outlined above, it fails to detect the selection range, and hence the default range of 0-0 is returned.
More generally, the "position" values used by getDivCaretPosition / setDivCaretPosition only count text characters (i.e. code units in text nodes). This would need to change in order to represent offsets that do not lie within a text node. Adding one for each element node entry/exit looks like it can solve this.