* In modern non-IE browsers, we can support both forward and backward * selections. * * Note: IE10+ supports the Selection object, but it does not support * the `extend` method, which means that even in modern IE, it's not possible * to programmatically create a backward selection. Thus, for al
(node, offsets)
| 6454 | * @param {object} offsets |
| 6455 | */ |
| 6456 | function setOffsets(node, offsets) { |
| 6457 | if (!window.getSelection) { |
| 6458 | return; |
| 6459 | } |
| 6460 | |
| 6461 | var selection = window.getSelection(); |
| 6462 | var length = node[getTextContentAccessor()].length; |
| 6463 | var start = Math.min(offsets.start, length); |
| 6464 | var end = offsets.end === undefined ? start : Math.min(offsets.end, length); |
| 6465 | |
| 6466 | // IE 11 uses modern selection, but doesn't support the extend method. |
| 6467 | // Flip backward selections, so we can set with a single range. |
| 6468 | if (!selection.extend && start > end) { |
| 6469 | var temp = end; |
| 6470 | end = start; |
| 6471 | start = temp; |
| 6472 | } |
| 6473 | |
| 6474 | var startMarker = getNodeForCharacterOffset(node, start); |
| 6475 | var endMarker = getNodeForCharacterOffset(node, end); |
| 6476 | |
| 6477 | if (startMarker && endMarker) { |
| 6478 | if (selection.rangeCount === 1 && selection.anchorNode === startMarker.node && selection.anchorOffset === startMarker.offset && selection.focusNode === endMarker.node && selection.focusOffset === endMarker.offset) { |
| 6479 | return; |
| 6480 | } |
| 6481 | var range = document.createRange(); |
| 6482 | range.setStart(startMarker.node, startMarker.offset); |
| 6483 | selection.removeAllRanges(); |
| 6484 | |
| 6485 | if (start > end) { |
| 6486 | selection.addRange(range); |
| 6487 | selection.extend(endMarker.node, endMarker.offset); |
| 6488 | } else { |
| 6489 | range.setEnd(endMarker.node, endMarker.offset); |
| 6490 | selection.addRange(range); |
| 6491 | } |
| 6492 | } |
| 6493 | } |
| 6494 | |
| 6495 | function isInDocument(node) { |
| 6496 | return containsNode(document.documentElement, node); |
no test coverage detected