Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selection.addRange() is deprecated and will be removed from Chrome

I recently noticed the following message in chrome's console log, while using aloha editor:

aloha.js:14579 - The behavior that Selection.addRange() merges existing Range and the specified Range is deprecated and will be removed in M58, around April 2017. See https://www.chromestatus.com/features/6680566019653632 for more details.

While trying to find a replacement, i couldn't find anything besides that they are going to remove it, so i would like to know what are the alternatives for Selection.addRange() to get rid of this message.

like image 327
Inc33 Avatar asked Apr 06 '17 16:04

Inc33


1 Answers

The trick is to use removeAllRanges() on your selection before adding your new range using addRange(range). Here is an example when using it to select all content of elem:

selection = window.getSelection();    // Save the selection.
range = document.createRange();
range.selectNodeContents(elem);
selection.removeAllRanges();          // Remove all ranges from the selection.
selection.addRange(range);            // Add the new range.
like image 107
Raymundus Avatar answered Nov 04 '22 06:11

Raymundus