* @restoreSelection: If any selection information was potentially lost, * restore it. This is useful when performing operations that could remove dom * nodes and place them back in, resulting in focus being lost.
(priorSelectionInformation)
| 10679 | */ |
| 10680 | |
| 10681 | function restoreSelection(priorSelectionInformation) { |
| 10682 | var curFocusedElem = getActiveElementDeep(); |
| 10683 | var priorFocusedElem = priorSelectionInformation.focusedElem; |
| 10684 | var priorSelectionRange = priorSelectionInformation.selectionRange; |
| 10685 | |
| 10686 | if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) { |
| 10687 | if (priorSelectionRange !== null && hasSelectionCapabilities(priorFocusedElem)) { |
| 10688 | setSelection(priorFocusedElem, priorSelectionRange); |
| 10689 | } // Focusing a node can change the scroll position, which is undesirable |
| 10690 | |
| 10691 | |
| 10692 | var ancestors = []; |
| 10693 | var ancestor = priorFocusedElem; |
| 10694 | |
| 10695 | while (ancestor = ancestor.parentNode) { |
| 10696 | if (ancestor.nodeType === ELEMENT_NODE) { |
| 10697 | ancestors.push({ |
| 10698 | element: ancestor, |
| 10699 | left: ancestor.scrollLeft, |
| 10700 | top: ancestor.scrollTop |
| 10701 | }); |
| 10702 | } |
| 10703 | } |
| 10704 | |
| 10705 | if (typeof priorFocusedElem.focus === 'function') { |
| 10706 | priorFocusedElem.focus(); |
| 10707 | } |
| 10708 | |
| 10709 | for (var i = 0; i < ancestors.length; i++) { |
| 10710 | var info = ancestors[i]; |
| 10711 | info.element.scrollLeft = info.left; |
| 10712 | info.element.scrollTop = info.top; |
| 10713 | } |
| 10714 | } |
| 10715 | } |
| 10716 | /** |
| 10717 | * @getSelection: Gets the selection bounds of a focused textarea, input or |
| 10718 | * contentEditable node. |
no test coverage detected