From 7aeae3817dd8c39f62de16a906c951d1d8f44b40 Mon Sep 17 00:00:00 2001 From: Simon Kornblith Date: Mon, 25 Jun 2012 00:35:29 -0400 Subject: [PATCH] Make htmlSpecialChars faster --- chrome/content/zotero/xpcom/utilities.js | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/chrome/content/zotero/xpcom/utilities.js b/chrome/content/zotero/xpcom/utilities.js index ef2882e21f..6d1ee10400 100644 --- a/chrome/content/zotero/xpcom/utilities.js +++ b/chrome/content/zotero/xpcom/utilities.js @@ -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(/<ZOTERO([^\/]+)\/>/g, function (str, p1, offset, s) { switch (p1) { case 'BREAK': return '
'; @@ -331,8 +326,6 @@ Zotero.Utilities = { return p1; } }); - - return newString; }, /**