Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e1d4110
Use event target doc as getActiveElement context
acusti Oct 24, 2017
ed0cc8a
Use node’s window and document when possible
acusti Oct 30, 2017
8f11b2c
Make getting/setting selection check iframe contents
acusti Oct 4, 2016
83cfb55
Add tests for ReactInputSelection
acusti Nov 1, 2017
374d525
Adapt restoreSelection to work for all activeElements
acusti Oct 10, 2016
f6f9e35
Tests for getting / restoring selections across iframes
acusti Oct 11, 2016
6ac703f
Add guards for Firefox and Safari compatibility
acusti Mar 15, 2017
cec8c3f
Prettier
acusti Nov 1, 2017
0163795
Update test file header
acusti Nov 1, 2017
d777546
Avoid causing focus/blur issues from restoreSelection
acusti Nov 1, 2017
7c59a77
Add early return for restoring selection common case
acusti Nov 1, 2017
131357e
Merge remote-tracking branch 'upstream/master' into iframes
acusti Nov 3, 2017
2d4ac46
Prefer active element as element with selection
acusti Nov 16, 2017
5755556
Merge branch 'master' into iframes
acusti Nov 16, 2017
b3998eb
Prettier
acusti Nov 16, 2017
174375c
Prevent restoreSelection overwriting active element
acusti Nov 17, 2017
39a9589
Code review-based cleanup and improvements
acusti Nov 17, 2017
82443f7
Detect all valid selection-capable input types
acusti Nov 19, 2017
b25279d
Merge branch 'master' into iframes
acusti Nov 19, 2017
026e8f0
Merge branch 'master' into iframes
acusti Nov 28, 2017
689d7c1
Merge branch 'master' into iframes
acusti Dec 30, 2017
074ff51
Guard against null (based on failing test)
acusti Dec 30, 2017
0744710
Linting (var :arrow_right: const)
acusti Dec 31, 2017
c71601b
Merge branch 'master' into iframes
acusti Jan 7, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Merge remote-tracking branch 'upstream/master' into iframes
  • Loading branch information
acusti committed Nov 3, 2017
commit 131357ecac9b055df09266fb66f7ca3774ac3cfe
4 changes: 2 additions & 2 deletions packages/react-dom/src/client/ReactDOMSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {TEXT_NODE} from '../shared/HTMLNodeType';
* @param {DOMElement} outerNode
* @return {?object}
*/
function getModernOffsets(outerNode) {
export function getOffsets(outerNode) {
var win = window;
if (outerNode.ownerDocument && outerNode.ownerDocument.defaultView) {
win = outerNode.ownerDocument.defaultView;
Expand Down Expand Up @@ -156,7 +156,7 @@ export function getModernOffsetsFromPoints(
* @param {DOMElement|DOMTextNode} node
* @param {object} offsets
*/
function setModernOffsets(node, offsets) {
export function setOffsets(node, offsets) {
var doc = node.ownerDocument || document;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When is ownerDocument ever undefined?

Copy link
Contributor Author

@acusti acusti Nov 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to the MDN page about the Node interface:

Node.ownerDocument returns the Document that this node belongs to. If no document is associated with it, returns null.

That doesn’t refer to a node that isn’t in the DOM, which will still have a reference to the ownerDocument where it originated (via document.createElement). In my experience, ownerDocument is only null for Document nodes themselves, which matches what MDN says on the page specifically about Node.ownerDocument:

If this property is used on a node that is itself a document, the result is null.

So now that you mention it, this check could be updated to handle Document specifically, maybe like var doc = node.nodeType === 9 ? node : node.ownerDocument;

However, maybe ownerDocument could also be null if, for example, you had a reference to a node from a document that has since been destroyed (an iframe element that got removed from the DOM and garbage collected, for example)? I’m not sure. Hence the simplified fallback to the global document whenever ownerDocument isn’t truthy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. Thank you. That was extremely helpful!


if (!doc.defaultView.getSelection) {
Expand Down
186 changes: 95 additions & 91 deletions packages/react-dom/src/client/ReactInputSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ function getElementsWithSelections(acc, win) {
element = range.parentElement();
}

if (ReactInputSelection.hasSelectionCapabilities(element)) {
if (hasSelectionCapabilities(element)) {
acc = acc.concat({
element: element,
selectionRange: ReactInputSelection.getSelection(element),
selectionRange: getSelection(element),
});
}

Expand Down Expand Up @@ -111,100 +111,104 @@ function focusNodePreservingScroll(element) {
* Input selection module for React.
*/

getSelectionInformation: function() {
return {
activeElement: getActiveElementDeep(),
elementSelections: getElementsWithSelections(),
};
} else {
// Content editable or old IE textarea.
selection = ReactDOMSelection.getOffsets(input);
}
export function hasSelectionCapabilities(elem) {
var nodeName = elem && elem.nodeName && elem.nodeName.toLowerCase();
return (
nodeName &&
((nodeName === 'input' && elem.type === 'text') ||
nodeName === 'textarea' ||
elem.contentEditable === 'true')
);
}

/**
* @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.
*/
restoreSelection: function(priorSelectionInformation) {
var priorActiveElement = priorSelectionInformation.activeElement;
var elementSelections = priorSelectionInformation.elementSelections;
var curActiveElement = getActiveElementDeep();
var isActiveElementOnlySelection =
elementSelections.length === 1 &&
elementSelections[0] === priorActiveElement;
if (
!isInDocument(priorActiveElement) ||
priorActiveElement === priorActiveElement.ownerDocument.body ||
(isActiveElementOnlySelection && curActiveElement === priorActiveElement)
) {
return;
}
elementSelections.forEach(function(selection) {
var element = selection.element;
if (
isInDocument(element) &&
getActiveElement(element.ownerDocument) !== element
) {
ReactInputSelection.setSelection(element, selection.selectionRange);
if (element !== priorActiveElement) {
focusNodePreservingScroll(element);
curActiveElement = element;
}
}
});
export function getSelectionInformation() {
return {
activeElement: getActiveElementDeep(),
elementSelections: getElementsWithSelections(),
};
}

/**
* @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.
*/
export function restoreSelection(priorSelectionInformation) {
var priorActiveElement = priorSelectionInformation.activeElement;
var elementSelections = priorSelectionInformation.elementSelections;
var curActiveElement = getActiveElementDeep();
var isActiveElementOnlySelection =
elementSelections.length === 1 &&
elementSelections[0] === priorActiveElement;
if (
!isInDocument(priorActiveElement) ||
priorActiveElement === priorActiveElement.ownerDocument.body ||
(isActiveElementOnlySelection && curActiveElement === priorActiveElement)
) {
return;
}
elementSelections.forEach(function(selection) {
var element = selection.element;
if (
curActiveElement !== priorActiveElement &&
isInDocument(priorActiveElement)
isInDocument(element) &&
getActiveElement(element.ownerDocument) !== element
) {
focusNodePreservingScroll(priorActiveElement);
}
},

/**
* @getSelection: Gets the selection bounds of a focused textarea, input or
* contentEditable node.
* -@input: Look up selection bounds of this input
* -@return {start: selectionStart, end: selectionEnd}
*/
getSelection: function(input) {
var selection;

if ('selectionStart' in input) {
// Modern browser with input or textarea.
selection = {
start: input.selectionStart,
end: input.selectionEnd,
};
} else {
// Content editable or old IE textarea.
selection = ReactDOMSelection.getOffsets(input);
setSelection(element, selection.selectionRange);
if (element !== priorActiveElement) {
focusNodePreservingScroll(element);
curActiveElement = element;
}
}
});

return selection || {start: 0, end: 0};
},

/**
* @setSelection: Sets the selection bounds of a textarea or input and focuses
* the input.
* -@input Set selection bounds of this input or textarea
* -@offsets Object of same form that is returned from get*
*/
setSelection: function(input, offsets) {
var start = offsets.start;
var end = offsets.end;
if (end === undefined) {
end = start;
}
if (
curActiveElement !== priorActiveElement &&
isInDocument(priorActiveElement)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Possible to recycle this value from the check on line 147?

Copy link
Contributor Author

@acusti acusti Nov 17, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call! The check is actually useless, because if priorActiveElement is not in document, the function will early return and never reach this if block.

) {
focusNodePreservingScroll(priorActiveElement);
}
}

if ('selectionStart' in input) {
input.selectionStart = start;
input.selectionEnd = Math.min(end, input.value.length);
} else {
ReactDOMSelection.setOffsets(input, offsets);
}
},
};
/**
* @getSelection: Gets the selection bounds of a focused textarea, input or
* contentEditable node.
* -@input: Look up selection bounds of this input
* -@return {start: selectionStart, end: selectionEnd}
*/
export function getSelection(input) {
var selection;

if ('selectionStart' in input) {
// Modern browser with input or textarea.
selection = {
start: input.selectionStart,
end: input.selectionEnd,
};
} else {
// Content editable or old IE textarea.
selection = ReactDOMSelection.getOffsets(input);
}

return selection || {start: 0, end: 0};
}

/**
* @setSelection: Sets the selection bounds of a textarea or input and focuses
* the input.
* -@input Set selection bounds of this input or textarea
* -@offsets Object of same form that is returned from get*
*/
export function setSelection(input, offsets) {
var start = offsets.start;
var end = offsets.end;
if (end === undefined) {
end = start;
}

module.exports = ReactInputSelection;
if ('selectionStart' in input) {
input.selectionStart = start;
input.selectionEnd = Math.min(end, input.value.length);
} else {
ReactDOMSelection.setOffsets(input, offsets);
}
}
You are viewing a condensed version of this merge commit. You can view the full changes here.