1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | 'use strict';
|
10 |
|
11 | var _assign = require('object-assign');
|
12 |
|
13 | var PooledClass = require('./PooledClass');
|
14 |
|
15 | var getTextContentAccessor = require('./getTextContentAccessor');
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function FallbackCompositionState(root) {
|
29 | this._root = root;
|
30 | this._startText = this.getText();
|
31 | this._fallbackText = null;
|
32 | }
|
33 |
|
34 | _assign(FallbackCompositionState.prototype, {
|
35 | destructor: function () {
|
36 | this._root = null;
|
37 | this._startText = null;
|
38 | this._fallbackText = null;
|
39 | },
|
40 |
|
41 | |
42 |
|
43 |
|
44 |
|
45 |
|
46 | getText: function () {
|
47 | if ('value' in this._root) {
|
48 | return this._root.value;
|
49 | }
|
50 | return this._root[getTextContentAccessor()];
|
51 | },
|
52 |
|
53 | |
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | getData: function () {
|
60 | if (this._fallbackText) {
|
61 | return this._fallbackText;
|
62 | }
|
63 |
|
64 | var start;
|
65 | var startValue = this._startText;
|
66 | var startLength = startValue.length;
|
67 | var end;
|
68 | var endValue = this.getText();
|
69 | var endLength = endValue.length;
|
70 |
|
71 | for (start = 0; start < startLength; start++) {
|
72 | if (startValue[start] !== endValue[start]) {
|
73 | break;
|
74 | }
|
75 | }
|
76 |
|
77 | var minEnd = startLength - start;
|
78 | for (end = 1; end <= minEnd; end++) {
|
79 | if (startValue[startLength - end] !== endValue[endLength - end]) {
|
80 | break;
|
81 | }
|
82 | }
|
83 |
|
84 | var sliceTail = end > 1 ? 1 - end : undefined;
|
85 | this._fallbackText = endValue.slice(start, sliceTail);
|
86 | return this._fallbackText;
|
87 | }
|
88 | });
|
89 |
|
90 | PooledClass.addPoolingTo(FallbackCompositionState);
|
91 |
|
92 | module.exports = FallbackCompositionState; |
\ | No newline at end of file |