* 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)
| 6489 | * @param {object} offsets |
| 6490 | */ |
| 6491 | function setOffsets(node, offsets) { |
| 6492 | if (!window.getSelection) { |
| 6493 | return; |
| 6494 | } |
| 6495 | |
| 6496 | var selection = window.getSelection(); |
| 6497 | var length = node[getTextContentAccessor()].length; |
| 6498 | var start = Math.min(offsets.start, length); |
| 6499 | var end = offsets.end === undefined ? start : Math.min(offsets.end, length); |
| 6500 | |
| 6501 | // IE 11 uses modern selection, but doesn't support the extend method. |
| 6502 | // Flip backward selections, so we can set with a single range. |
| 6503 | if (!selection.extend && start > end) { |
| 6504 | var temp = end; |
| 6505 | end = start; |
| 6506 | start = temp; |
| 6507 | } |
| 6508 | |
| 6509 | var startMarker = getNodeForCharacterOffset(node, start); |
| 6510 | var endMarker = getNodeForCharacterOffset(node, end); |
| 6511 | |
| 6512 | if (startMarker && endMarker) { |
| 6513 | if (selection.rangeCount === 1 && selection.anchorNode === startMarker.node && selection.anchorOffset === startMarker.offset && selection.focusNode === endMarker.node && selection.focusOffset === endMarker.offset) { |
| 6514 | return; |
| 6515 | } |
| 6516 | var range = document.createRange(); |
| 6517 | range.setStart(startMarker.node, startMarker.offset); |
| 6518 | selection.removeAllRanges(); |
| 6519 | |
| 6520 | if (start > end) { |
| 6521 | selection.addRange(range); |
| 6522 | selection.extend(endMarker.node, endMarker.offset); |
| 6523 | } else { |
| 6524 | range.setEnd(endMarker.node, endMarker.offset); |
| 6525 | selection.addRange(range); |
| 6526 | } |
| 6527 | } |
| 6528 | } |
| 6529 | |
| 6530 | function isInDocument(node) { |
| 6531 | return containsNode(document.documentElement, node); |
no test coverage detected