/**1* Copyright 2013-2015, Facebook, Inc.2* All rights reserved.3*4* This source code is licensed under the BSD-style license found in the5* LICENSE file in the root directory of this source tree. An additional grant6* of patent rights can be found in the PATENTS file in the same directory.7*8* @providesModule FallbackCompositionState9* @typechecks static-only10*/1112'use strict';1314var PooledClass = require("./PooledClass");1516var assign = require("./Object.assign");17var getTextContentAccessor = require("./getTextContentAccessor");1819/**20* This helper class stores information about text content of a target node,21* allowing comparison of content before and after a given event.22*23* Identify the node where selection currently begins, then observe24* both its text content and its current position in the DOM. Since the25* browser may natively replace the target node during composition, we can26* use its position to find its replacement.27*28* @param {DOMEventTarget} root29*/30function FallbackCompositionState(root) {31this._root = root;32this._startText = this.getText();33this._fallbackText = null;34}3536assign(FallbackCompositionState.prototype, {37/**38* Get current text of input.39*40* @return {string}41*/42getText: function() {43if ('value' in this._root) {44return this._root.value;45}46return this._root[getTextContentAccessor()];47},4849/**50* Determine the differing substring between the initially stored51* text content and the current content.52*53* @return {string}54*/55getData: function() {56if (this._fallbackText) {57return this._fallbackText;58}5960var start;61var startValue = this._startText;62var startLength = startValue.length;63var end;64var endValue = this.getText();65var endLength = endValue.length;6667for (start = 0; start < startLength; start++) {68if (startValue[start] !== endValue[start]) {69break;70}71}7273var minEnd = startLength - start;74for (end = 1; end <= minEnd; end++) {75if (startValue[startLength - end] !== endValue[endLength - end]) {76break;77}78}7980var sliceTail = end > 1 ? 1 - end : undefined;81this._fallbackText = endValue.slice(start, sliceTail);82return this._fallbackText;83}84});8586PooledClass.addPoolingTo(FallbackCompositionState);8788module.exports = FallbackCompositionState;899091