Background:
My program offers smart brace completion, that is automatic addition of a ]
on typing a [
.
Problem:
Consider this scenario:
Notice the editor on the left where the caret is placed. As you can see in the Inspect Element on the right, the caret is placed right between two consecutive <br>
s. There is no text node or element node between them. The caret belongs to neither of the <br>
s. It is a no-man's land. What is surprising is that the caret belongs to the parent editor!
Above you can see range.startContainer
and range.endContainer
both point to the parent content editable editor div.editor.show
. Also, the second print line that mentions 2
is the caret position that I got using range.startOffset
. I have a faint guess that the 2
refers to one text node and one <br>
that precede the caret.
What happens due to problem:
The second ]
that has to be inserted after [
gets inserted at the second index in the entire div.editor
, meaning right after Th
at the beginning. So, after I locate my caret in the no-man's land and press [
, this happens:
Question:
How am I supposed to detect and provide a fix for this no-man's land problem?
Note: this does not occur in <textarea>
s.
` when it is created/inserted? – YakovL May 27 '16 at 15:41
` was present in the text beforehand. Then, it's no use checking if a br was created or inserted. – Gaurang Tandon May 28 '16 at 04:40
`s and then insert an ` ` between them? But I don't want to modify what the user is writing. So, how about inserting an empty text node instead? – Gaurang Tandon May 28 '16 at 12:34