in src/caret/caret.ts [176:220]
setPosition(position?: Position | number) {
const isContentEditable = this.isContentEditable();
let correctedPosition;
let curNode: Node = this.target && this.target.childNodes[0];
if (position !== undefined) {
if (typeof position === 'object') {
const range = new Range();
const start = this.getRelativePosition(curNode, position.startOffset);
// eslint-disable-next-line no-underscore-dangle
range.setStart(start._curNode, start._correctedPosition);
const end = this.getRelativePosition(curNode, position.endOffset);
// eslint-disable-next-line no-underscore-dangle
range.setEnd(end._curNode, end._correctedPosition);
correctedPosition = range;
} else if (position === -1) {
const value = isContentEditable
? this.target.textContent
: Caret.normalizeNewlines('value' in this.target ? this.target.value : undefined);
correctedPosition = value?.length ?? 0;
} else {
const {_curNode, _correctedPosition} = this.getRelativePosition(curNode, position);
curNode = _curNode;
correctedPosition = _correctedPosition;
}
}
if (isContentEditable) {
this.focus();
try {
if (correctedPosition instanceof Range) {
window.getSelection()?.removeAllRanges();
window.getSelection()?.addRange(correctedPosition);
} else {
window.getSelection()?.collapse(curNode || this.target, correctedPosition);
}
} catch (e) {
// Do nothing
}
} else if ('setSelectionRange' in this.target && typeof correctedPosition === 'number') {
this.target.setSelectionRange(correctedPosition, correctedPosition);
}
return correctedPosition;
}