From 80ec6f09d375f4c5095b9f87fadeb7f733d29fef Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Thu, 21 Oct 2021 10:06:31 +0200 Subject: [PATCH] DEV: removes unnecessary caret position code (#14665) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../discourse/app/lib/utilities.js | 19 +------------------ vendor/assets/javascripts/caret_position.js | 11 ----------- 2 files changed, 1 insertion(+), 29 deletions(-) diff --git a/app/assets/javascripts/discourse/app/lib/utilities.js b/app/assets/javascripts/discourse/app/lib/utilities.js index 654eaeb3530..980ea245b85 100644 --- a/app/assets/javascripts/discourse/app/lib/utilities.js +++ b/app/assets/javascripts/discourse/app/lib/utilities.js @@ -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 diff --git a/vendor/assets/javascripts/caret_position.js b/vendor/assets/javascripts/caret_position.js index 2393c6f4068..3a7ca2cee27 100644 --- a/vendor/assets/javascripts/caret_position.js +++ b/vendor/assets/javascripts/caret_position.js @@ -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]); };