UNPKG

2.43 kBJavaScriptView Raw
1/**
2 * Copyright 2013-present, Facebook, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the root directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
8 *
9 */
10
11'use strict';
12
13var _assign = require('object-assign');
14
15var PooledClass = require('./PooledClass');
16
17var getTextContentAccessor = require('./getTextContentAccessor');
18
19/**
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 observe
24 * both its text content and its current position in the DOM. Since the
25 * browser may natively replace the target node during composition, we can
26 * use its position to find its replacement.
27 *
28 * @param {DOMEventTarget} root
29 */
30function FallbackCompositionState(root) {
31 this._root = root;
32 this._startText = this.getText();
33 this._fallbackText = null;
34}
35
36_assign(FallbackCompositionState.prototype, {
37 destructor: function () {
38 this._root = null;
39 this._startText = null;
40 this._fallbackText = null;
41 },
42
43 /**
44 * Get current text of input.
45 *
46 * @return {string}
47 */
48 getText: function () {
49 if ('value' in this._root) {
50 return this._root.value;
51 }
52 return this._root[getTextContentAccessor()];
53 },
54
55 /**
56 * Determine the differing substring between the initially stored
57 * text content and the current content.
58 *
59 * @return {string}
60 */
61 getData: function () {
62 if (this._fallbackText) {
63 return this._fallbackText;
64 }
65
66 var start;
67 var startValue = this._startText;
68 var startLength = startValue.length;
69 var end;
70 var endValue = this.getText();
71 var endLength = endValue.length;
72
73 for (start = 0; start < startLength; start++) {
74 if (startValue[start] !== endValue[start]) {
75 break;
76 }
77 }
78
79 var minEnd = startLength - start;
80 for (end = 1; end <= minEnd; end++) {
81 if (startValue[startLength - end] !== endValue[endLength - end]) {
82 break;
83 }
84 }
85
86 var sliceTail = end > 1 ? 1 - end : undefined;
87 this._fallbackText = endValue.slice(start, sliceTail);
88 return this._fallbackText;
89 }
90});
91
92PooledClass.addPoolingTo(FallbackCompositionState);
93
94module.exports = FallbackCompositionState;
\No newline at end of file