* 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)
| 6705 | * @param {object} offsets |
| 6706 | */ |
| 6707 | function setOffsets(node, offsets) { |
| 6708 | if (!window.getSelection) { |
| 6709 | return; |
| 6710 | } |
| 6711 | |
| 6712 | var selection = window.getSelection(); |
| 6713 | var length = node[getTextContentAccessor()].length; |
| 6714 | var start = Math.min(offsets.start, length); |
| 6715 | var end = offsets.end === undefined ? start : Math.min(offsets.end, length); |
| 6716 | |
| 6717 | // IE 11 uses modern selection, but doesn't support the extend method. |
| 6718 | // Flip backward selections, so we can set with a single range. |
| 6719 | if (!selection.extend && start > end) { |
| 6720 | var temp = end; |
| 6721 | end = start; |
| 6722 | start = temp; |
| 6723 | } |
| 6724 | |
| 6725 | var startMarker = getNodeForCharacterOffset(node, start); |
| 6726 | var endMarker = getNodeForCharacterOffset(node, end); |
| 6727 | |
| 6728 | if (startMarker && endMarker) { |
| 6729 | if (selection.rangeCount === 1 && selection.anchorNode === startMarker.node && selection.anchorOffset === startMarker.offset && selection.focusNode === endMarker.node && selection.focusOffset === endMarker.offset) { |
| 6730 | return; |
| 6731 | } |
| 6732 | var range = document.createRange(); |
| 6733 | range.setStart(startMarker.node, startMarker.offset); |
| 6734 | selection.removeAllRanges(); |
| 6735 | |
| 6736 | if (start > end) { |
| 6737 | selection.addRange(range); |
| 6738 | selection.extend(endMarker.node, endMarker.offset); |
| 6739 | } else { |
| 6740 | range.setEnd(endMarker.node, endMarker.offset); |
| 6741 | selection.addRange(range); |
| 6742 | } |
| 6743 | } |
| 6744 | } |
| 6745 | |
| 6746 | function isInDocument(node) { |
| 6747 | return containsNode(document.documentElement, node); |
no test coverage detected