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