forked from SideeX/sideex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
escape.js
executable file
·240 lines (219 loc) · 7.58 KB
/
escape.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Copyright 2017 SideeX committers
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
// change HTML entities to sign
function unescapeHtml(str) {
return str
.replace(/&/gi, "&")
.replace(/"/gi, "\"")
.replace(/</gi, "<")
.replace(/>/gi, ">")
.replace(/'/gi, "'");
}
function escapeAttr(str) {
var spaceS = 0;
var spaceE = -1;
var tempStr = str;
var tempAttr = "";
var tempValue = "";
var processedTag = "";
var flag = false;
do {
spaceS = str.indexOf(" ");
spaceE = str.indexOf(" ", spaceS + 1);
if (spaceE >= 0) {
while (str.charAt(spaceE - 1) != "\'" && str.charAt(spaceE - 1) != "\"") {
spaceE = str.indexOf(" ", spaceE + 1);
if (spaceE < 0)
break;
}
}
//if there is space, then split string
if (spaceS >= 0 && spaceE >= 0) {
tempAttr = str.substring(spaceS + 1, spaceE);
tempStr = str.substring(0, spaceS + 1);
str = str.substring(spaceE);
} else if (spaceS >= 0 && spaceE < 0) {
tempAttr = str.substring(spaceS + 1, str.length - 1);
tempStr = str.substring(0, spaceS + 1);
str = "";
} else {
//flag is check that has string been processed
if (flag)
processedTag += ">";
else
processedTag = str;
break;
}
flag = true;
var equal = tempAttr.indexOf("=");
if (tempAttr.charAt(equal + 1) == "\'") {
//divide the single quote
if (tempAttr.indexOf("\'") != -1) {
var quotS = tempAttr.indexOf("\'");
var quotE = tempAttr.lastIndexOf("\'");
tempValue = tempAttr.substring(quotS + 1, quotE);
tempAttr = tempAttr.substring(0, quotS + 1);
tempValue = replaceChar(tempValue);
tempAttr += tempValue + "\'";
}
}
if (tempAttr.charAt(equal + 1) == "\"") {
//divide the double quote
if (tempAttr.indexOf("\"") != -1) {
var dquotS = tempAttr.indexOf("\"");
var dquotE = tempAttr.lastIndexOf("\"");
tempValue = tempAttr.substring(dquotS + 1, dquotE);
tempAttr = tempAttr.substring(0, dquotS + 1);
tempValue = replaceChar(tempValue);
tempAttr += tempValue + "\"";
}
}
//merge the splited string
processedTag += tempStr + tempAttr;
} while (true)
return processedTag;
};
//escape the character "<".">"."&"."'".'"'
function doEscape(str) {
return str.replace(/[&"'<>]/g, (m) => ({ "&": "&", '"': """, "'": "'", "<": "<", ">": ">" })[m]);
}
//append
function checkType(cutStr, replaceStr, mode) {
switch (mode) {
case 1:
return cutStr += replaceStr + "&";
break;
case 2:
return cutStr += replaceStr + """;
break;
case 3:
return cutStr += replaceStr + "'";
break;
case 4:
return cutStr += replaceStr + "<";
break;
case 5:
return cutStr += replaceStr + ">";
break;
default:
return cutStr;
break;
}
}
//avoid & to escape &amp;
function replaceChar(str) {
//escape the character
var pos = -1;
var cutStr = "";
var replaceStr = "";
var doFlag = 0;
var charType;
while (true) {
pos = str.indexOf("&", pos + 1);
charType = 0;
if (pos != -1) {
if (str.substring(pos, pos + 5) == "&") {
charType = 1;
replaceStr = str.substring(0, pos);
str = str.substring(pos + 5);
} else if (str.substring(pos, pos + 6) == """) {
charType = 2;
replaceStr = str.substring(0, pos);
str = str.substring(pos + 6);
} else if (str.substring(pos, pos + 5) == "'") {
charType = 3;
replaceStr = str.substring(0, pos);
str = str.substring(pos + 5);
} else if (str.substring(pos, pos + 4) == "<") {
charType = 4;
replaceStr = str.substring(0, pos);
str = str.substring(pos + 4);
} else if (str.substring(pos, pos + 4) == ">") {
charType = 5;
replaceStr = str.substring(0, pos);
str = str.substring(pos + 4);
}
if (charType != 0) {
//replaceStr = str.substring(0,pos);
//str = str.substring(pos+5);
pos = -1;
//replaceStr = replaceStr.replace(/[&"'<>]/g, (m) => ({ "&": "&", '"': """, "'": "'", "<": "<", ">": ">" })[m]);
replaceStr = doEscape(replaceStr);
//cutStr += replaceStr + "&";
cutStr = checkType(cutStr, replaceStr, charType);
doFlag = 1;
}
} else {
cutStr += str;
break;
}
}
if (doFlag == 0)
//return str.replace(/[&"'<>]/g, (m) => ({ "&": "&", '"': """, "'": "'", "<": "<", ">": ">" })[m]);
return doEscape(str);
else
return cutStr;
}
//check the HTML value
function escapeHTML(str) {
var smallIndex = str.indexOf("<");
var greatIndex = str.indexOf(">");
var tempStr = "";
var tempTag = "";
var processed = "";
var tempSmallIndex = 0;
while (true) {
//find the less target
if (smallIndex >= 0) {
//find the greater target
if (greatIndex >= 0) {
do {
//split foreward string
smallIndex += tempSmallIndex;
tempStr = str.substring(0, smallIndex);
//split the tags
tempTag = str.substring(smallIndex, greatIndex + 1);
tempSmallIndex = tempTag.lastIndexOf("<");
} while (tempSmallIndex != 0)
//escape attributes in the tag
tempTag = escapeAttr(tempTag);
str = str.substring(greatIndex + 1);
//check if the tag is script
// if(tempTag.toLowerCase().indexOf("script")>=0)
// tempTag = replaceChar(tempTag);
//merge them up
processed += replaceChar(tempStr) + tempTag;
} else {
replaceChar(str);
break;
}
} else {
replaceChar(str);
break;
}
//going to do next tag
smallIndex = str.indexOf("<");
greatIndex = 0;
do {
//avoid other >
greatIndex = str.indexOf(">", greatIndex + 1);
} while (greatIndex < smallIndex && greatIndex != -1)
}
if (str != "")
processed += replaceChar(str);
return processed;
};