1 | 'use strict';
|
---|
2 | /*
|
---|
3 | Copyright 2012-2015, Yahoo Inc.
|
---|
4 | Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.
|
---|
5 | */
|
---|
6 | function InsertionText(text, consumeBlanks) {
|
---|
7 | this.text = text;
|
---|
8 | this.origLength = text.length;
|
---|
9 | this.offsets = [];
|
---|
10 | this.consumeBlanks = consumeBlanks;
|
---|
11 | this.startPos = this.findFirstNonBlank();
|
---|
12 | this.endPos = this.findLastNonBlank();
|
---|
13 | }
|
---|
14 |
|
---|
15 | const WHITE_RE = /[ \f\n\r\t\v\u00A0\u2028\u2029]/;
|
---|
16 |
|
---|
17 | InsertionText.prototype = {
|
---|
18 | findFirstNonBlank() {
|
---|
19 | let pos = -1;
|
---|
20 | const text = this.text;
|
---|
21 | const len = text.length;
|
---|
22 | let i;
|
---|
23 | for (i = 0; i < len; i += 1) {
|
---|
24 | if (!text.charAt(i).match(WHITE_RE)) {
|
---|
25 | pos = i;
|
---|
26 | break;
|
---|
27 | }
|
---|
28 | }
|
---|
29 | return pos;
|
---|
30 | },
|
---|
31 | findLastNonBlank() {
|
---|
32 | const text = this.text;
|
---|
33 | const len = text.length;
|
---|
34 | let pos = text.length + 1;
|
---|
35 | let i;
|
---|
36 | for (i = len - 1; i >= 0; i -= 1) {
|
---|
37 | if (!text.charAt(i).match(WHITE_RE)) {
|
---|
38 | pos = i;
|
---|
39 | break;
|
---|
40 | }
|
---|
41 | }
|
---|
42 | return pos;
|
---|
43 | },
|
---|
44 | originalLength() {
|
---|
45 | return this.origLength;
|
---|
46 | },
|
---|
47 |
|
---|
48 | insertAt(col, str, insertBefore, consumeBlanks) {
|
---|
49 | consumeBlanks =
|
---|
50 | typeof consumeBlanks === 'undefined'
|
---|
51 | ? this.consumeBlanks
|
---|
52 | : consumeBlanks;
|
---|
53 | col = col > this.originalLength() ? this.originalLength() : col;
|
---|
54 | col = col < 0 ? 0 : col;
|
---|
55 |
|
---|
56 | if (consumeBlanks) {
|
---|
57 | if (col <= this.startPos) {
|
---|
58 | col = 0;
|
---|
59 | }
|
---|
60 | if (col > this.endPos) {
|
---|
61 | col = this.origLength;
|
---|
62 | }
|
---|
63 | }
|
---|
64 |
|
---|
65 | const len = str.length;
|
---|
66 | const offset = this.findOffset(col, len, insertBefore);
|
---|
67 | const realPos = col + offset;
|
---|
68 | const text = this.text;
|
---|
69 | this.text = text.substring(0, realPos) + str + text.substring(realPos);
|
---|
70 | return this;
|
---|
71 | },
|
---|
72 |
|
---|
73 | findOffset(pos, len, insertBefore) {
|
---|
74 | const offsets = this.offsets;
|
---|
75 | let offsetObj;
|
---|
76 | let cumulativeOffset = 0;
|
---|
77 | let i;
|
---|
78 |
|
---|
79 | for (i = 0; i < offsets.length; i += 1) {
|
---|
80 | offsetObj = offsets[i];
|
---|
81 | if (
|
---|
82 | offsetObj.pos < pos ||
|
---|
83 | (offsetObj.pos === pos && !insertBefore)
|
---|
84 | ) {
|
---|
85 | cumulativeOffset += offsetObj.len;
|
---|
86 | }
|
---|
87 | if (offsetObj.pos >= pos) {
|
---|
88 | break;
|
---|
89 | }
|
---|
90 | }
|
---|
91 | if (offsetObj && offsetObj.pos === pos) {
|
---|
92 | offsetObj.len += len;
|
---|
93 | } else {
|
---|
94 | offsets.splice(i, 0, { pos, len });
|
---|
95 | }
|
---|
96 | return cumulativeOffset;
|
---|
97 | },
|
---|
98 |
|
---|
99 | wrap(startPos, startText, endPos, endText, consumeBlanks) {
|
---|
100 | this.insertAt(startPos, startText, true, consumeBlanks);
|
---|
101 | this.insertAt(endPos, endText, false, consumeBlanks);
|
---|
102 | return this;
|
---|
103 | },
|
---|
104 |
|
---|
105 | wrapLine(startText, endText) {
|
---|
106 | this.wrap(0, startText, this.originalLength(), endText);
|
---|
107 | },
|
---|
108 |
|
---|
109 | toString() {
|
---|
110 | return this.text;
|
---|
111 | }
|
---|
112 | };
|
---|
113 |
|
---|
114 | module.exports = InsertionText;
|
---|