1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | var _assign = require('object-assign');
|
14 |
|
15 | var PooledClass = require('./PooledClass');
|
16 |
|
17 | var getTextContentAccessor = require('./getTextContentAccessor');
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | function 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 |
|
45 |
|
46 |
|
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 |
|
57 |
|
58 |
|
59 |
|
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 |
|
92 | PooledClass.addPoolingTo(FallbackCompositionState);
|
93 |
|
94 | module.exports = FallbackCompositionState; |
\ | No newline at end of file |