MCPcopy Create free account
hub / github.com/microsoft/SandDance / restoreSelection

Function restoreSelection

docs/external/js/react-dom.development.js:7109–7143  ·  view source on GitHub ↗

* @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)

Source from the content-addressed store, hash-verified

7107 */
7108
7109 function restoreSelection(priorSelectionInformation) {
7110 var curFocusedElem = getActiveElementDeep();
7111 var priorFocusedElem = priorSelectionInformation.focusedElem;
7112 var priorSelectionRange = priorSelectionInformation.selectionRange;
7113
7114 if (curFocusedElem !== priorFocusedElem && isInDocument(priorFocusedElem)) {
7115 if (priorSelectionRange !== null && hasSelectionCapabilities(priorFocusedElem)) {
7116 setSelection(priorFocusedElem, priorSelectionRange);
7117 } // Focusing a node can change the scroll position, which is undesirable
7118
7119
7120 var ancestors = [];
7121 var ancestor = priorFocusedElem;
7122
7123 while (ancestor = ancestor.parentNode) {
7124 if (ancestor.nodeType === ELEMENT_NODE) {
7125 ancestors.push({
7126 element: ancestor,
7127 left: ancestor.scrollLeft,
7128 top: ancestor.scrollTop
7129 });
7130 }
7131 }
7132
7133 if (typeof priorFocusedElem.focus === 'function') {
7134 priorFocusedElem.focus();
7135 }
7136
7137 for (var i = 0; i < ancestors.length; i++) {
7138 var info = ancestors[i];
7139 info.element.scrollLeft = info.left;
7140 info.element.scrollTop = info.top;
7141 }
7142 }
7143 }
7144 /**
7145 * @getSelection: Gets the selection bounds of a focused textarea, input or
7146 * contentEditable node.

Callers 1

resetAfterCommitFunction · 0.85

Calls 5

getActiveElementDeepFunction · 0.85
isInDocumentFunction · 0.85
hasSelectionCapabilitiesFunction · 0.85
setSelectionFunction · 0.85
focusMethod · 0.45

Tested by

no test coverage detected