* 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)
| 5815 | * @param {object} offsets |
| 5816 | */ |
| 5817 | function setOffsets(node, offsets) { |
| 5818 | if (!window.getSelection) { |
| 5819 | return; |
| 5820 | } |
| 5821 | |
| 5822 | var selection = window.getSelection(); |
| 5823 | var length = node[getTextContentAccessor()].length; |
| 5824 | var start = Math.min(offsets.start, length); |
| 5825 | var end = offsets.end === undefined ? start : Math.min(offsets.end, length); |
| 5826 | |
| 5827 | // IE 11 uses modern selection, but doesn't support the extend method. |
| 5828 | // Flip backward selections, so we can set with a single range. |
| 5829 | if (!selection.extend && start > end) { |
| 5830 | var temp = end; |
| 5831 | end = start; |
| 5832 | start = temp; |
| 5833 | } |
| 5834 | |
| 5835 | var startMarker = getNodeForCharacterOffset(node, start); |
| 5836 | var endMarker = getNodeForCharacterOffset(node, end); |
| 5837 | |
| 5838 | if (startMarker && endMarker) { |
| 5839 | if (selection.rangeCount === 1 && selection.anchorNode === startMarker.node && selection.anchorOffset === startMarker.offset && selection.focusNode === endMarker.node && selection.focusOffset === endMarker.offset) { |
| 5840 | return; |
| 5841 | } |
| 5842 | var range = document.createRange(); |
| 5843 | range.setStart(startMarker.node, startMarker.offset); |
| 5844 | selection.removeAllRanges(); |
| 5845 | |
| 5846 | if (start > end) { |
| 5847 | selection.addRange(range); |
| 5848 | selection.extend(endMarker.node, endMarker.offset); |
| 5849 | } else { |
| 5850 | range.setEnd(endMarker.node, endMarker.offset); |
| 5851 | selection.addRange(range); |
| 5852 | } |
| 5853 | } |
| 5854 | } |
| 5855 | |
| 5856 | function isInDocument(node) { |
| 5857 | return containsNode(document.documentElement, node); |
no test coverage detected