Skip to content

Commit

Permalink
DEV: removes unnecessary caret position code (#14665)
Browse files Browse the repository at this point in the history
We don't support any browser needing this for very long: https://caniuse.com/?search=selectionStart

I'm keeping some protection so It doesn’t crash but ultimately `element.selectionStart` should be enough.

Im not removing this in the commit, but the `caret_position.js` file seems barely used.
  • Loading branch information
jjaffeux committed Oct 21, 2021
1 parent a9d6b23 commit 80ec6f0
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 29 deletions.
19 changes: 1 addition & 18 deletions app/assets/javascripts/discourse/app/lib/utilities.js
Expand Up @@ -224,24 +224,7 @@ export function caretRowCol(el) {

// Determine the position of the caret in an element
export function caretPosition(el) {
let r, rc, re;
if (el.selectionStart) {
return el.selectionStart;
}
if (document.selection) {
el.focus();
r = document.selection.createRange();
if (!r) {
return 0;
}

re = el.createTextRange();
rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint("EndToStart", re);
return rc.text.length;
}
return 0;
return el?.selectionStart || 0;
}

// Set the caret's position
Expand Down
11 changes: 0 additions & 11 deletions vendor/assets/javascripts/caret_position.js
Expand Up @@ -6,22 +6,11 @@ var clone = null;

$.fn.caret = function(elem) {
var getCaret = function(el) {
var r, rc, re;
if (el.selectionStart) {
return el.selectionStart;
} else if (document.selection) {
el.focus();
r = document.selection.createRange();
if (!r) return 0;
re = el.createTextRange();
rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint("EndToStart", re);
return rc.text.length;
}
return 0;
};

return getCaret(elem || this[0]);
};

Expand Down

0 comments on commit 80ec6f0

Please sign in to comment.