Utils: Made escapeHtml()/unescapeHtml() more efficient.

This commit is contained in:
satyr 2010-03-29 04:30:44 +09:00
Родитель 05032634de
Коммит b4153a68e9
2 изменённых файлов: 26 добавлений и 12 удалений

Просмотреть файл

@ -826,26 +826,30 @@ function signHMAC(algo, key, str) {
// Useful when you just want to concatenate a bunch of strings into
// an HTML fragment and ensure that everything's escaped properly.
function escapeHtml(string) (
String(string)
.replace(/&/g, "&")
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")
.replace(/\"/g, "&quot;")
.replace(/\'/g, "&#39;"));
function escapeHtml(s) String(s).replace(escapeHtml.re, escapeHtml.fn);
escapeHtml.re = /[&<>\"\']/g;
escapeHtml.fn = function escapeHtml_sub($) {
switch ($) {
case "&": return "&amp;";
case "<": return "&lt;";
case ">": return "&gt;";
case '"': return "&quot;";
case "'": return "&#39;";
}
};
// === {{{ Utils.unescapeHtml(string) }}} ===
// Returns a version of the {{{string}}} with all occurrences of HTML character
// references (&spades; &#x2665; &#9827; etc.) in it decoded.
// references (e.g. &spades; &#x2665; &#9827; etc.) in it decoded.
function unescapeHtml(string) (
String.replace(string, /&#?\w+;/g, unescapeHtml.parse));
unescapeHtml.parse = function unescapeHtml_parse(ref) {
function unescapeHtml(s) String(s).replace(unescapeHtml.re, unescapeHtml.fn);
unescapeHtml.re = /(?:&#?\w+;)+/g;
unescapeHtml.fn = function unescapeHtml_parse(ref) {
var {div} = unescapeHtml_parse;
div.innerHTML = ref;
return div.textContent;
};
defineLazyProperty(unescapeHtml.parse, function div() (
defineLazyProperty(unescapeHtml.fn, function div() (
Utils.hiddenWindow.document.createElementNS(
"http://www.w3.org/1999/xhtml", "div")));

Просмотреть файл

@ -611,6 +611,16 @@ function testUtilsPrefs() {
}
}
function testUtilsEscapeUnescapeHtml() {
this.skipIfXPCShell();
var {escapeHtml, unescapeHtml} = Utils;
var html = Utils.hiddenWindow.document.documentElement.innerHTML;
this.assertEquals(
html += "&spades;'&#x2665;&#9827;",
unescapeHtml(unescapeHtml(escapeHtml(escapeHtml(html)))));
}
function testL10nUtilsPropertySelector() {
var ps = LocalizationUtils.propertySelector("data:," + encodeURI(<![CDATA[
foo=%S %S