编辑:删除了我之前的评论,因为它应该是对不同美化线程的回复:)
但是,如果有帮助,这是一个规范化 HTML 字符串的本地 js 解决方案:
// HTML escape code repurposed from http://www.htmlescape.net/htmlescape_tool.html
function htmlEscapeString(unescaped_str)
{
var escaped="";
for(i=0; i<unescaped_str.length; i++)
{
escaped += escapeBR(escapeTags(escapeCharx(unescaped_str.charAt(i))));
}
return escaped;
}
function escapeBR(original)
{
var thechar=original.charCodeAt(0);
switch(thechar) {
case 10: return "";
case '\r': return "";
}
return original;
}
function escapeNBSP(original)
{
var thechar=original.charCodeAt(0);
switch(thechar) {
case 32: return " ";
}
return original;
}
function escapeTags(original)
{
var thechar=original.charCodeAt(0);
switch(thechar) {
case 60:return "<"; //<
case 62:return ">"; //>
case 34:return """; //"
}
return original;
}
function escapeCharx(original)
{
var c = original.charCodeAt(0);
switch(c) {
case 38:return "&";
case 198:return "Æ";
case 193:return "Á";
case 194:return "Â";
case 192:return "À";
case 197:return "Å";
case 195:return "Ã";
case 196:return "Ä";
case 199:return "Ç";
case 208:return "Ð";
case 201:return "É";
case 202:return "Ê";
case 200:return "È";
case 203:return "Ë";
case 205:return "Í";
case 206:return "Î";
case 204:return "Ì";
case 207:return "Ï";
case 209:return "Ñ";
case 211:return "Ó";
case 212:return "Ô";
case 210:return "Ò";
case 216:return "Ø";
case 213:return "Õ";
case 214:return "Ö";
case 222:return "Þ";
case 218:return "Ú";
case 219:return "Û";
case 217:return "Ù";
case 220:return "Ü";
case 221:return "Ý";
case 225:return "á";
case 226:return "â";
case 230:return "æ";
case 224:return "à";
case 229:return "å";
case 227:return "ã";
case 228:return "ä";
case 231:return "ç";
case 233:return "é";
case 234:return "ê";
case 232:return "è";
case 240:return "ð";
case 235:return "ë";
case 237:return "í";
case 238:return "î";
case 236:return "ì";
case 239:return "ï";
case 241:return "ñ";
case 243:return "ó";
case 244:return "ô";
case 242:return "ò";
case 248:return "ø";
case 245:return "õ";
case 246:return "ö";
case 223:return "ß";
case 254:return "þ";
case 250:return "ú";
case 251:return "û";
case 249:return "ù";
case 252:return "ü";
case 253:return "ý";
case 255:return "ÿ";
case 162:return "¢";
default: break;
}
if( c <= 127 )
return original;
var a4=c%16;
c = Math.floor(c/16);
var a3=c%16;
c = Math.floor(c/16);
var a2=c%16;
c = Math.floor(c/16);
var a1=c%16;
return "&#x"+hex[a1]+hex[a2]+hex[a3]+hex[a4]+";";
}