Make htmlSpecialChars faster
This commit is contained in:
parent
916db3fd48
commit
7aeae3817d
1 changed files with 8 additions and 15 deletions
|
@ -304,24 +304,19 @@ Zotero.Utilities = {
|
|||
* @type String
|
||||
*/
|
||||
"htmlSpecialChars":function(/**String*/ str) {
|
||||
if (typeof str != 'string') {
|
||||
throw "Argument '" + str + "' must be a string in Zotero.Utilities.htmlSpecialChars()";
|
||||
}
|
||||
if (typeof str != 'string') str = str.toString();
|
||||
|
||||
if (!str) {
|
||||
return '';
|
||||
}
|
||||
|
||||
var chars = ['&', '"',"'",'<','>'];
|
||||
var entities = ['amp', 'quot', 'apos', 'lt', 'gt'];
|
||||
|
||||
var newString = str;
|
||||
for (var i = 0; i < chars.length; i++) {
|
||||
var re = new RegExp(chars[i], 'g');
|
||||
newString = newString.replace(re, '&' + entities[i] + ';');
|
||||
}
|
||||
|
||||
newString = newString.replace(/<ZOTERO([^\/]+)\/>/g, function (str, p1, offset, s) {
|
||||
return str
|
||||
.replace(/&/g, '&')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, ''')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
.replace(/<ZOTERO([^\/]+)\/>/g, function (str, p1, offset, s) {
|
||||
switch (p1) {
|
||||
case 'BREAK':
|
||||
return '<br/>';
|
||||
|
@ -331,8 +326,6 @@ Zotero.Utilities = {
|
|||
return p1;
|
||||
}
|
||||
});
|
||||
|
||||
return newString;
|
||||
},
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue