free.js
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
module.exports = function Free() {
var texts = [];
return {
// Strip content tags by replacing them by the __CSSFREETEXT__
// marker for further restoring. It's done via string scanning
// instead of regexps to speed up the process.
escape: function(data) {
var tempData = [];
var nextStart = 0;
var nextEnd = 0;
var cursor = 0;
var matchedParenthesis = null;
var singleParenthesis = "'";
var doubleParenthesis = '"';
var dataLength = data.length;
for (; nextEnd < data.length; ) {
var nextStartSingle = data.indexOf(singleParenthesis, nextEnd + 1);
var nextStartDouble = data.indexOf(doubleParenthesis, nextEnd + 1);
if (nextStartSingle == -1)
nextStartSingle = dataLength;
if (nextStartDouble == -1)
nextStartDouble = dataLength;
if (nextStartSingle < nextStartDouble) {
nextStart = nextStartSingle;
matchedParenthesis = singleParenthesis;
} else {
nextStart = nextStartDouble;
matchedParenthesis = doubleParenthesis;
}
if (nextStart == -1)
break;
nextEnd = data.indexOf(matchedParenthesis, nextStart + 1);
if (nextStart == -1 || nextEnd == -1)
break;
tempData.push(data.substring(cursor, nextStart));
tempData.push('__CSSFREETEXT__');
texts.push(data.substring(nextStart, nextEnd + 1));
cursor = nextEnd + 1;
}
return tempData.length > 0 ?
tempData.join('') + data.substring(cursor, data.length) :
data;
},
restore: function(data) {
return data.replace(/__CSSFREETEXT__/g, function() {
return texts.shift();
});
}
};
};