Skip to content

Instantly share code, notes, and snippets.

@tyler-johnson
Created March 14, 2017 21:52
Show Gist options
  • Save tyler-johnson/0a3e8818de3f115b2a2dc47468ac0099 to your computer and use it in GitHub Desktop.
Save tyler-johnson/0a3e8818de3f115b2a2dc47468ac0099 to your computer and use it in GitHub Desktop.
if (window.Selection && !Selection.prototype.extend) {
Selection.prototype.extend = function(el, offset) {
const range = document.createRange();
const anchor = document.createRange();
anchor.setStart(this.anchorNode, this.anchorOffset);
const focus = document.createRange();
focus.setStart(el, offset);
const v = focus.compareBoundaryPoints(Range.START_TO_START, anchor);
if (v >= 0) { // focus is after anchor
range.setStart(this.anchorNode, this.anchorOffset);
range.setEnd(el, offset);
} else { // anchor is after focus
range.setStart(el, offset);
range.setEnd(this.anchorNode, this.anchorOffset);
}
this.removeAllRanges();
this.addRange(range);
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment