* @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)
| 6516 | * nodes and place them back in, resulting in focus being lost. |
| 6517 | */ |
| 6518 | function restoreSelection(priorSelectionInformation) { |
| 6519 | var curFocusedElem = getActiveElement(); |
| 6520 | var priorFocusedElem = priorSelectionInformation.focusedElem; |
| 6521 | var priorSelectionRange = priorSelectionInformation.selectionRange; |
| 6522 | if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) { |
| 6523 | if (hasSelectionCapabilities(priorFocusedElem)) { |
| 6524 | setSelection(priorFocusedElem, priorSelectionRange); |
| 6525 | } |
| 6526 | |
| 6527 | // Focusing a node can change the scroll position, which is undesirable |
| 6528 | var ancestors = []; |
| 6529 | var ancestor = priorFocusedElem; |
| 6530 | while (ancestor = ancestor.parentNode) { |
| 6531 | if (ancestor.nodeType === ELEMENT_NODE) { |
| 6532 | ancestors.push({ |
| 6533 | element: ancestor, |
| 6534 | left: ancestor.scrollLeft, |
| 6535 | top: ancestor.scrollTop |
| 6536 | }); |
| 6537 | } |
| 6538 | } |
| 6539 | |
| 6540 | priorFocusedElem.focus(); |
| 6541 | |
| 6542 | for (var i = 0; i < ancestors.length; i++) { |
| 6543 | var info = ancestors[i]; |
| 6544 | info.element.scrollLeft = info.left; |
| 6545 | info.element.scrollTop = info.top; |
| 6546 | } |
| 6547 | } |
| 6548 | } |
| 6549 | |
| 6550 | /** |
| 6551 | * @getSelection: Gets the selection bounds of a focused textarea, input or |
no test coverage detected