fix incorrect caret positioning in some cases