mirror of
https://github.com/facebook/react.git
synced 2025-11-01 09:12:30 +00:00
* Convert EventPlugin{Hub,Registry} to named exports
* Convert EventPluginUtils to named exports
* Convert EventPropagators to named exports
* Convert ReactControlledComponent to named exports
* Convert ReactGenericBatching to named exports
* Convert ReactDOMComponentTree to named exports
* Convert ReactNativeComponentTree to named exports
* Convert ReactNativeRTComponentTree to named exports
* Convert FallbackCompositionState to named exports
* Convert ReactEventEmitterMixin to named exports
* Convert ReactBrowserEventEmitter to named exports
* Convert ReactNativeEventEmitter to named exports
* Convert ReactDOMEventListener to named exports
* Convert DOMMarkupOperations to named exports
* Convert DOMProperty to named exports
* Add suppression for existing Flow violation
Flow didn't see it before.
* Update sizes
75 lines
1.9 KiB
JavaScript
75 lines
1.9 KiB
JavaScript
/**
|
|
* Copyright (c) 2013-present, Facebook, Inc.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import getTextContentAccessor from '../client/getTextContentAccessor';
|
|
|
|
/**
|
|
* This helper object stores information about text content of a target node,
|
|
* allowing comparison of content before and after a given event.
|
|
*
|
|
* Identify the node where selection currently begins, then observe
|
|
* both its text content and its current position in the DOM. Since the
|
|
* browser may natively replace the target node during composition, we can
|
|
* use its position to find its replacement.
|
|
*
|
|
*
|
|
*/
|
|
var compositionState = {
|
|
_root: null,
|
|
_startText: null,
|
|
_fallbackText: null,
|
|
};
|
|
|
|
export function initialize(nativeEventTarget) {
|
|
compositionState._root = nativeEventTarget;
|
|
compositionState._startText = getText();
|
|
return true;
|
|
}
|
|
|
|
export function reset() {
|
|
compositionState._root = null;
|
|
compositionState._startText = null;
|
|
compositionState._fallbackText = null;
|
|
}
|
|
|
|
export function getData() {
|
|
if (compositionState._fallbackText) {
|
|
return compositionState._fallbackText;
|
|
}
|
|
|
|
var start;
|
|
var startValue = compositionState._startText;
|
|
var startLength = startValue.length;
|
|
var end;
|
|
var endValue = getText();
|
|
var endLength = endValue.length;
|
|
|
|
for (start = 0; start < startLength; start++) {
|
|
if (startValue[start] !== endValue[start]) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
var minEnd = startLength - start;
|
|
for (end = 1; end <= minEnd; end++) {
|
|
if (startValue[startLength - end] !== endValue[endLength - end]) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
var sliceTail = end > 1 ? 1 - end : undefined;
|
|
compositionState._fallbackText = endValue.slice(start, sliceTail);
|
|
return compositionState._fallbackText;
|
|
}
|
|
|
|
export function getText() {
|
|
if ('value' in compositionState._root) {
|
|
return compositionState._root.value;
|
|
}
|
|
return compositionState._root[getTextContentAccessor()];
|
|
}
|