1

Seeking the net brought me to Selection.addRange() is deprecated and will be removed from Chrome

But this is a bit to broad. I get this error because i have included some bootstrap into my project. Now for me "Range" means [from A to Z] or [from START to END]

Reading the above posted post tells me the trick to solve the problem is to call removeAllRanges(); befor addRange(..); But reading removeALLranges let me suggest that several ranges or values can be added.

I tried to add removeAllRanges(); in between and it brought me several more errors because if i add a range (start) i have to add a second one (end).

Actually its not a range, its more the parameter to set in addRange.

Here is the code in some of bootstrap generated file that causes the issue

(function() {
    var iframe = document.createElement("iframe");
    body.appendChild(iframe);

    var iframeDoc = dom.getIframeDocument(iframe);
    iframeDoc.open();
    iframeDoc.write("<html><head></head><body>12</body></html>");
    iframeDoc.close();

    var sel = dom.getIframeWindow(iframe).getSelection();
    var docEl = iframeDoc.documentElement;
    var iframeBody = docEl.lastChild, textNode = iframeBody.firstChild;

    // Test whether the native selection will allow a collapsed selection within a non-editable element
    var r1 = iframeDoc.createRange();
    r1.setStart(textNode, 1);
    r1.collapse(true);
    sel.addRange(r1);
    collapsedNonEditableSelectionsSupported = (sel.rangeCount == 1);
    sel.removeAllRanges();

    // Test whether the native selection is capable of supporting multiple ranges
    var r2 = r1.cloneRange();
    r1.setStart(textNode, 0);
    r2.setEnd(textNode, 2);
    sel.addRange(r1);
    sel.addRange(r2);

    selectionSupportsMultipleRanges = (sel.rangeCount == 2);

    // Clean up
    r1.detach();
    r2.detach();

    body.removeChild(iframe);
})();

The problem part here is

sel.removeAllRanges();

var r2 = r1.cloneRange();
r1.setStart(textNode, 0);
r2.setEnd(textNode, 2);
sel.addRange(r1);
sel.addRange(r2);      //<<============ HERE

of course i need the second range to let

selectionSupportsMultipleRanges = (sel.rangeCount == 2);

work!

How can i solve this ?

Is there a way to add a real range insead of only a start parameter ?

Dwza
  • 6,494
  • 6
  • 41
  • 73

1 Answers1

1

If you want to have a range (start to end) you can do something like:

let range = document.createRange()
range.setStart(r1);
range.setEnd(r2);
let range2 ....


let sel = window.getSelection()
selection.removeAllRanges();
selection.addRange(range);
selection.addRange(range2);
....

I would look here:

https://developer.mozilla.org/en-US/docs/Web/API/Range https://developer.mozilla.org/en-US/docs/Web/API/Selection/addRange

for more info on ranges.

Hope this answers your question (even if it's late).

meh93
  • 311
  • 4
  • 13
  • first of all, sorry for this post... reading this even makes me kind of confused and truly i dont really know what I expacted than ;D But i guess your answere seems to be right and makes sense so ill accept this one :) – Dwza May 31 '19 at 18:43