Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80529 views
1
/**
2
* Copyright 2013-2015, 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
* @providesModule FallbackCompositionState
10
* @typechecks static-only
11
*/
12
13
'use strict';
14
15
var PooledClass = require("./PooledClass");
16
17
var assign = require("./Object.assign");
18
var getTextContentAccessor = require("./getTextContentAccessor");
19
20
/**
21
* This helper class stores information about text content of a target node,
22
* allowing comparison of content before and after a given event.
23
*
24
* Identify the node where selection currently begins, then observe
25
* both its text content and its current position in the DOM. Since the
26
* browser may natively replace the target node during composition, we can
27
* use its position to find its replacement.
28
*
29
* @param {DOMEventTarget} root
30
*/
31
function FallbackCompositionState(root) {
32
this._root = root;
33
this._startText = this.getText();
34
this._fallbackText = null;
35
}
36
37
assign(FallbackCompositionState.prototype, {
38
/**
39
* Get current text of input.
40
*
41
* @return {string}
42
*/
43
getText: function() {
44
if ('value' in this._root) {
45
return this._root.value;
46
}
47
return this._root[getTextContentAccessor()];
48
},
49
50
/**
51
* Determine the differing substring between the initially stored
52
* text content and the current content.
53
*
54
* @return {string}
55
*/
56
getData: function() {
57
if (this._fallbackText) {
58
return this._fallbackText;
59
}
60
61
var start;
62
var startValue = this._startText;
63
var startLength = startValue.length;
64
var end;
65
var endValue = this.getText();
66
var endLength = endValue.length;
67
68
for (start = 0; start < startLength; start++) {
69
if (startValue[start] !== endValue[start]) {
70
break;
71
}
72
}
73
74
var minEnd = startLength - start;
75
for (end = 1; end <= minEnd; end++) {
76
if (startValue[startLength - end] !== endValue[endLength - end]) {
77
break;
78
}
79
}
80
81
var sliceTail = end > 1 ? 1 - end : undefined;
82
this._fallbackText = endValue.slice(start, sliceTail);
83
return this._fallbackText;
84
}
85
});
86
87
PooledClass.addPoolingTo(FallbackCompositionState);
88
89
module.exports = FallbackCompositionState;
90
91