зеркало из https://github.com/mozilla/ubiquity.git
Utils: Made escapeHtml()/unescapeHtml() more efficient.
This commit is contained in:
Родитель
05032634de
Коммит
b4153a68e9
|
@ -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, "<")
|
||||
.replace(/>/g, ">")
|
||||
.replace(/\"/g, """)
|
||||
.replace(/\'/g, "'"));
|
||||
function escapeHtml(s) String(s).replace(escapeHtml.re, escapeHtml.fn);
|
||||
escapeHtml.re = /[&<>\"\']/g;
|
||||
escapeHtml.fn = function escapeHtml_sub($) {
|
||||
switch ($) {
|
||||
case "&": return "&";
|
||||
case "<": return "<";
|
||||
case ">": return ">";
|
||||
case '"': return """;
|
||||
case "'": return "'";
|
||||
}
|
||||
};
|
||||
|
||||
// === {{{ Utils.unescapeHtml(string) }}} ===
|
||||
// Returns a version of the {{{string}}} with all occurrences of HTML character
|
||||
// references (♠ ♥ ♣ etc.) in it decoded.
|
||||
// references (e.g. ♠ ♥ ♣ 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 += "♠'♥♣",
|
||||
unescapeHtml(unescapeHtml(escapeHtml(escapeHtml(html)))));
|
||||
}
|
||||
|
||||
function testL10nUtilsPropertySelector() {
|
||||
var ps = LocalizationUtils.propertySelector("data:," + encodeURI(<![CDATA[
|
||||
foo=%S %S
|
||||
|
|
Загрузка…
Ссылка в новой задаче