@rhinostone/swig-core 2.9.0 → 2.9.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/index.js +1 -1
- package/lib/utils.js +93 -2
- package/package.json +1 -1
package/lib/index.js
CHANGED
package/lib/utils.js
CHANGED
|
@@ -2,13 +2,104 @@ var security = require('./security');
|
|
|
2
2
|
|
|
3
3
|
var isArray;
|
|
4
4
|
|
|
5
|
+
/*!
|
|
6
|
+
* What a \s regex atom matches, tested one code unit at a time.
|
|
7
|
+
* @private
|
|
8
|
+
*/
|
|
9
|
+
var whitespace = /\s/;
|
|
10
|
+
|
|
5
11
|
/**
|
|
6
|
-
* Strip leading and trailing whitespace from a string.
|
|
12
|
+
* Strip leading and trailing whitespace from a string. Linear in the input
|
|
13
|
+
* length.
|
|
7
14
|
* @param {string} input
|
|
8
15
|
* @return {string} Stripped input.
|
|
9
16
|
*/
|
|
10
17
|
exports.strip = function (input) {
|
|
11
|
-
|
|
18
|
+
// A /\s+$/ alternative takes quadratic time on a long run of whitespace
|
|
19
|
+
// that does not end the string; the trailing run is found by trimEnd.
|
|
20
|
+
// return input.replace(/^\s+|\s+$/g, '');
|
|
21
|
+
return exports.trimEnd(input.replace(/^\s+/, ''));
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Remove trailing characters from a string: whitespace (what `\s` matches)
|
|
26
|
+
* when `chars` is omitted, otherwise every UTF-16 code unit that `chars`
|
|
27
|
+
* contains, as a regex character class built from `chars` would. Linear in
|
|
28
|
+
* the input length, where a `/[...]+$/` regex takes quadratic time on a long
|
|
29
|
+
* run of those characters that does not end the string. Used by the flavor
|
|
30
|
+
* filters.
|
|
31
|
+
* @param {string} input
|
|
32
|
+
* @param {string} [chars] Characters to remove; whitespace when omitted.
|
|
33
|
+
* @return {string}
|
|
34
|
+
*/
|
|
35
|
+
exports.trimEnd = function (input, chars) {
|
|
36
|
+
var end = input.length;
|
|
37
|
+
if (chars === undefined) {
|
|
38
|
+
while (end > 0 && whitespace.test(input.charAt(end - 1))) {
|
|
39
|
+
end -= 1;
|
|
40
|
+
}
|
|
41
|
+
} else {
|
|
42
|
+
while (end > 0 && chars.indexOf(input.charAt(end - 1)) !== -1) {
|
|
43
|
+
end -= 1;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return input.slice(0, end);
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Remove every `<...>` span from a string, with the output of
|
|
51
|
+
* `input.replace(/<[^>]+>/g, '')`, or of `/<[^>]*>/g` (which also removes
|
|
52
|
+
* `<>`) when `allowEmpty` is true. Linear in the input length, where those
|
|
53
|
+
* regexes take quadratic time when many `<` have no `>` after them. Used by
|
|
54
|
+
* the flavor filters.
|
|
55
|
+
* @param {string} input
|
|
56
|
+
* @param {boolean} [allowEmpty] Remove `<>` too.
|
|
57
|
+
* @return {string}
|
|
58
|
+
*/
|
|
59
|
+
exports.stripTags = function (input, allowEmpty) {
|
|
60
|
+
var out = '',
|
|
61
|
+
from = 0,
|
|
62
|
+
lt = input.indexOf('<'),
|
|
63
|
+
gt;
|
|
64
|
+
while (lt !== -1) {
|
|
65
|
+
gt = input.indexOf('>', lt + 1);
|
|
66
|
+
if (gt === -1) {
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
if (gt === lt + 1 && !allowEmpty) {
|
|
70
|
+
lt = input.indexOf('<', lt + 1);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
out += input.slice(from, lt);
|
|
74
|
+
from = gt + 1;
|
|
75
|
+
lt = input.indexOf('<', from);
|
|
76
|
+
}
|
|
77
|
+
return out + input.slice(from);
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Remove every `<!-- ... -->` comment from a string, with the output of
|
|
82
|
+
* `input.replace(/<!--[\s\S]*?-->/g, '')`. Linear in the input length, where
|
|
83
|
+
* that regex takes quadratic time when many `<!--` have no `-->` after them.
|
|
84
|
+
* Used by the flavor filters.
|
|
85
|
+
* @param {string} input
|
|
86
|
+
* @return {string}
|
|
87
|
+
*/
|
|
88
|
+
exports.stripComments = function (input) {
|
|
89
|
+
var out = '',
|
|
90
|
+
from = 0,
|
|
91
|
+
open = input.indexOf('<!--'),
|
|
92
|
+
close;
|
|
93
|
+
while (open !== -1) {
|
|
94
|
+
close = input.indexOf('-->', open + 4);
|
|
95
|
+
if (close === -1) {
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
out += input.slice(from, open);
|
|
99
|
+
from = close + 3;
|
|
100
|
+
open = input.indexOf('<!--', from);
|
|
101
|
+
}
|
|
102
|
+
return out + input.slice(from);
|
|
12
103
|
};
|
|
13
104
|
|
|
14
105
|
/**
|