Skip to content

Commit

Permalink
Make getting/setting selection check iframe contents
Browse files Browse the repository at this point in the history
If an input inside a same-domain iframe is the actual activeElement,
this change will make the ReactInputSelection module get from and
restore selection to that input, rather than the iframe element itself
  • Loading branch information
acusti committed May 4, 2017
1 parent e89ab63 commit 8ec5be5
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/renderers/dom/client/ReactInputSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ function isInDocument(node) {
return containsNode(document.documentElement, node);
}

function getFocusedElement() {
var win = window;
var focusedElem = getActiveElement();
while (focusedElem instanceof win.HTMLIFrameElement) {
try {
win = focusedElem.contentDocument.defaultView;
} catch (e) {
return focusedElem;
}
focusedElem = getActiveElement(win.document);
}
return focusedElem;
}

/**
* @ReactInputSelection: React input selection module. Based on Selection.js,
* but modified to be suitable for react and has a couple of bug fixes (doesn't
Expand All @@ -39,7 +53,7 @@ var ReactInputSelection = {
},

getSelectionInformation: function() {
var focusedElem = getActiveElement();
var focusedElem = getFocusedElement();
return {
focusedElem: focusedElem,
selectionRange:
Expand All @@ -55,7 +69,7 @@ var ReactInputSelection = {
* nodes and place them back in, resulting in focus being lost.
*/
restoreSelection: function(priorSelectionInformation) {
var curFocusedElem = getActiveElement();
var curFocusedElem = getFocusedElement();
var priorFocusedElem = priorSelectionInformation.focusedElem;
var priorSelectionRange = priorSelectionInformation.selectionRange;
if (curFocusedElem !== priorFocusedElem &&
Expand Down

0 comments on commit 8ec5be5

Please sign in to comment.