* 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)
| 6592 | * @param {object} offsets |
| 6593 | */ |
| 6594 | function setOffsets(node, offsets) { |
| 6595 | if (!window.getSelection) { |
| 6596 | return; |
| 6597 | } |
| 6598 | |
| 6599 | var selection = window.getSelection(); |
| 6600 | var length = node[getTextContentAccessor()].length; |
| 6601 | var start = Math.min(offsets.start, length); |
| 6602 | var end = offsets.end === undefined ? start : Math.min(offsets.end, length); |
| 6603 | |
| 6604 | // IE 11 uses modern selection, but doesn't support the extend method. |
| 6605 | // Flip backward selections, so we can set with a single range. |
| 6606 | if (!selection.extend && start > end) { |
| 6607 | var temp = end; |
| 6608 | end = start; |
| 6609 | start = temp; |
| 6610 | } |
| 6611 | |
| 6612 | var startMarker = getNodeForCharacterOffset(node, start); |
| 6613 | var endMarker = getNodeForCharacterOffset(node, end); |
| 6614 | |
| 6615 | if (startMarker && endMarker) { |
| 6616 | if (selection.rangeCount === 1 && selection.anchorNode === startMarker.node && selection.anchorOffset === startMarker.offset && selection.focusNode === endMarker.node && selection.focusOffset === endMarker.offset) { |
| 6617 | return; |
| 6618 | } |
| 6619 | var range = document.createRange(); |
| 6620 | range.setStart(startMarker.node, startMarker.offset); |
| 6621 | selection.removeAllRanges(); |
| 6622 | |
| 6623 | if (start > end) { |
| 6624 | selection.addRange(range); |
| 6625 | selection.extend(endMarker.node, endMarker.offset); |
| 6626 | } else { |
| 6627 | range.setEnd(endMarker.node, endMarker.offset); |
| 6628 | selection.addRange(range); |
| 6629 | } |
| 6630 | } |
| 6631 | } |
| 6632 | |
| 6633 | function isInDocument(node) { |
| 6634 | return containsNode(document.documentElement, node); |
no test coverage detected