* 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)
| 5687 | * @param {object} offsets |
| 5688 | */ |
| 5689 | function setOffsets(node, offsets) { |
| 5690 | if (!window.getSelection) { |
| 5691 | return; |
| 5692 | } |
| 5693 | |
| 5694 | var selection = window.getSelection(); |
| 5695 | var length = node[getTextContentAccessor()].length; |
| 5696 | var start = Math.min(offsets.start, length); |
| 5697 | var end = offsets.end === undefined ? start : Math.min(offsets.end, length); |
| 5698 | |
| 5699 | // IE 11 uses modern selection, but doesn't support the extend method. |
| 5700 | // Flip backward selections, so we can set with a single range. |
| 5701 | if (!selection.extend && start > end) { |
| 5702 | var temp = end; |
| 5703 | end = start; |
| 5704 | start = temp; |
| 5705 | } |
| 5706 | |
| 5707 | var startMarker = getNodeForCharacterOffset(node, start); |
| 5708 | var endMarker = getNodeForCharacterOffset(node, end); |
| 5709 | |
| 5710 | if (startMarker && endMarker) { |
| 5711 | if (selection.rangeCount === 1 && selection.anchorNode === startMarker.node && selection.anchorOffset === startMarker.offset && selection.focusNode === endMarker.node && selection.focusOffset === endMarker.offset) { |
| 5712 | return; |
| 5713 | } |
| 5714 | var range = document.createRange(); |
| 5715 | range.setStart(startMarker.node, startMarker.offset); |
| 5716 | selection.removeAllRanges(); |
| 5717 | |
| 5718 | if (start > end) { |
| 5719 | selection.addRange(range); |
| 5720 | selection.extend(endMarker.node, endMarker.offset); |
| 5721 | } else { |
| 5722 | range.setEnd(endMarker.node, endMarker.offset); |
| 5723 | selection.addRange(range); |
| 5724 | } |
| 5725 | } |
| 5726 | } |
| 5727 | |
| 5728 | function isInDocument(node) { |
| 5729 | return containsNode(document.documentElement, node); |
no test coverage detected