1

this happens in chrome (71.0.3578.80 on windows):

on range.setStart the caret moves to the reference node, but after typing the new characters appear in the node before, when offset is 0.

the expected behaviour would be, that the characters appear in the reference node of range.setStart.

see here:

http://jsfiddle.net/pgrds9qv/

function setCaret() {
  var el = document.getElementById("editable");
  var range = document.createRange();
  var sel = window.getSelection();
  range.setStart(el.childNodes[2], 0);
  range.collapse(true);
  sel.removeAllRanges();
  sel.addRange(range);
}
div {
  padding: 10px;
}

span {
  border: 1px solid black;
  font-size: 120%;
  padding: 5px;
}
<div id="editable" contenteditable="true">
  <span>first </span><span>second </span><span>third </span>
</div>
<div>
  <button id="button" onclick="setCaret()">focus</button>
</div>

in firefox it works fine. how to get the same behaviour in chrome? or is there an alternative way of achieving this? thanks a lot for any hints!

Kaiido
  • 123,334
  • 13
  • 219
  • 285
bjd1
  • 11
  • 1
  • 3
  • 1
    This happens even when you set the cursor manually... [Here is chromium's bug report board.](http://chromiumbugs.appspot.com/) Feel free to report this to them. It is still reproduceable in latest canary v73 and a workaround would actually be a complicated bit of code, for what really is an unfortunate browser's bug... – Kaiido Dec 15 '18 at 13:53
  • That's a bug. It's even wrong when you set the cursor manually and move it using the arrow keys. Consider filing a bug here: https://bugs.chromium.org/ (I can't find a matching, open one there) – try-catch-finally Dec 15 '18 at 13:57

0 Answers0