Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support iframes (nested browsing contexts) in selection event handling for React 16 #11410

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
5 changes: 0 additions & 5 deletions packages/react-dom/src/client/ReactDOM.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ var invariant = require('fbjs/lib/invariant');

var ReactDOMComponentTree = require('./ReactDOMComponentTree');
var ReactDOMFiberComponent = require('./ReactDOMFiberComponent');
var ReactInputSelection = require('./ReactInputSelection');
var ReactBrowserEventEmitter = require('../events/ReactBrowserEventEmitter');
var DOMNamespaces = require('../shared/DOMNamespaces');
var {
Expand Down Expand Up @@ -116,7 +115,6 @@ type HostContextProd = string;
type HostContext = HostContextDev | HostContextProd;

let eventsEnabled: ?boolean = null;
let selectionInformation: ?mixed = null;

/**
* True if the supplied DOM node is a valid node element.
Expand Down Expand Up @@ -219,13 +217,10 @@ var DOMRenderer = ReactFiberReconciler({

prepareForCommit(): void {
eventsEnabled = ReactBrowserEventEmitter.isEnabled();
selectionInformation = ReactInputSelection.getSelectionInformation();
ReactBrowserEventEmitter.setEnabled(false);
},

resetAfterCommit(): void {
ReactInputSelection.restoreSelection(selectionInformation);
selectionInformation = null;
ReactBrowserEventEmitter.setEnabled(eventsEnabled);
eventsEnabled = null;
},
Expand Down
14 changes: 10 additions & 4 deletions packages/react-dom/src/client/ReactDOMSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ var {TEXT_NODE} = require('../shared/HTMLNodeType');
* @return {?object}
*/
function getModernOffsets(outerNode) {
var selection = window.getSelection && window.getSelection();
var win = window;
if (outerNode.ownerDocument && outerNode.ownerDocument.defaultView) {
win = outerNode.ownerDocument.defaultView;
}
var selection = win.getSelection && win.getSelection();

if (!selection || selection.rangeCount === 0) {
return null;
Expand Down Expand Up @@ -153,11 +157,13 @@ function getModernOffsetsFromPoints(
* @param {object} offsets
*/
function setModernOffsets(node, offsets) {
if (!window.getSelection) {
var doc = node.ownerDocument || document;

if (!doc.defaultView.getSelection) {
return;
}

var selection = window.getSelection();
var selection = doc.defaultView.getSelection();
var length = node[getTextContentAccessor()].length;
var start = Math.min(offsets.start, length);
var end = offsets.end === undefined ? start : Math.min(offsets.end, length);
Expand All @@ -183,7 +189,7 @@ function setModernOffsets(node, offsets) {
) {
return;
}
var range = document.createRange();
var range = doc.createRange();
range.setStart(startMarker.node, startMarker.offset);
selection.removeAllRanges();

Expand Down
130 changes: 0 additions & 130 deletions packages/react-dom/src/client/ReactInputSelection.js

This file was deleted.

52 changes: 38 additions & 14 deletions packages/react-dom/src/events/SelectEventPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ var shallowEqual = require('fbjs/lib/shallowEqual');

var ReactBrowserEventEmitter = require('./ReactBrowserEventEmitter');
var ReactDOMComponentTree = require('../client/ReactDOMComponentTree');
var ReactInputSelection = require('../client/ReactInputSelection');
var {DOCUMENT_NODE} = require('../shared/HTMLNodeType');

var skipSelectionChangeEvent =
Expand Down Expand Up @@ -53,6 +52,22 @@ var mouseDown = false;
var isListeningToAllDependencies =
ReactBrowserEventEmitter.isListeningToAllDependencies;

/**
* Determine if a node can have a selection associated with it.
*
* @param {DOMElement} node
* @return {boolean} True if the node can have a selection.
*/
function hasSelectionCapabilities(node) {
var nodeName = node && node.nodeName && node.nodeName.toLowerCase();
return (
nodeName &&
((nodeName === 'input' && node.type === 'text') ||
nodeName === 'textarea' ||
node.contentEditable === 'true')
);
}

/**
* Get an object which is a unique representation of the current selection.
*
Expand All @@ -63,40 +78,49 @@ var isListeningToAllDependencies =
* @return {object}
*/
function getSelection(node) {
if (
'selectionStart' in node &&
ReactInputSelection.hasSelectionCapabilities(node)
) {
if ('selectionStart' in node && hasSelectionCapabilities(node)) {
return {
start: node.selectionStart,
end: node.selectionEnd,
};
} else if (window.getSelection) {
var selection = window.getSelection();
return {
anchorNode: selection.anchorNode,
anchorOffset: selection.anchorOffset,
focusNode: selection.focusNode,
focusOffset: selection.focusOffset,
};
} else {
var win = window;
if (node.ownerDocument && node.ownerDocument.defaultView) {
win = node.ownerDocument.defaultView;
}
if (win.getSelection) {
var selection = win.getSelection();
return {
anchorNode: selection.anchorNode,
anchorOffset: selection.anchorOffset,
focusNode: selection.focusNode,
focusOffset: selection.focusOffset,
};
}
}
}

/**
* Poll selection to see whether it's changed.
*
* @param {object} nativeEvent
* @param {object} nativeEventTarget
* @return {?SyntheticEvent}
*/
function constructSelectEvent(nativeEvent, nativeEventTarget) {
// Ensure we have the right element, and that the user is not dragging a
// selection (this matches native `select` event behavior). In HTML5, select
// fires only on input and textarea thus if there's no focused element we
// won't dispatch.
var doc =
nativeEventTarget.ownerDocument ||
nativeEventTarget.document ||
nativeEventTarget;

if (
mouseDown ||
activeElement == null ||
activeElement !== getActiveElement()
activeElement !== getActiveElement(doc)
) {
return null;
}
Expand Down
8 changes: 5 additions & 3 deletions packages/react-dom/src/events/SyntheticClipboardEvent.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ var SyntheticEvent = require('events/SyntheticEvent');
*/
var ClipboardEventInterface = {
clipboardData: function(event) {
return 'clipboardData' in event
? event.clipboardData
: window.clipboardData;
if ('clipboardData' in event) {
return event.clipboardData;
}
var doc = (event.target && event.target.ownerDocument) || document;
return doc.defaultView.clipboardData;
},
};

Expand Down
Loading