1
0
mirror of https://github.com/AvengeMedia/DankMaterialShell.git synced 2026-04-14 17:52:10 -04:00

notifications: more comprehensive decoder

This commit is contained in:
bbedward
2026-02-15 16:10:20 -05:00
parent b29c7192c2
commit dbb04f74a2

View File

@@ -906,37 +906,83 @@ Singleton {
} }
} }
function _hasHtmlTags(s) { function _decodeEntities(s) {
return /<\/?[a-z][\s\S]*>/i.test(s);
}
function _hasHtmlEntities(s) {
return /&(#\d+|#x[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]+);/.test(s);
}
function _unescapeHtml(s) {
s = s.replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(parseInt(n, 10))); s = s.replace(/&#(\d+);/g, (_, n) => String.fromCodePoint(parseInt(n, 10)));
s = s.replace(/&#x([0-9a-fA-F]+);/g, (_, n) => String.fromCodePoint(parseInt(n, 16))); s = s.replace(/&#x([0-9a-fA-F]+);/g, (_, n) => String.fromCodePoint(parseInt(n, 16)));
return s.replace(/&([a-zA-Z][a-zA-Z0-9]*);/g, (match, name) => {
const named = { switch (name) {
"lt": "<", case "amp":
"gt": ">", return "&";
"amp": "&", case "lt":
"quot": "\"", return "<";
"apos": "'", case "gt":
"nbsp": "\u00A0" return ">";
}; case "quot":
return s.replace(/&([a-zA-Z][a-zA-Z0-9]+);/g, (_, name) => named[name] !== undefined ? named[name] : "&" + name + ";"); return "\"";
case "apos":
return "'";
case "nbsp":
return "\u00A0";
case "ndash":
return "\u2013";
case "mdash":
return "\u2014";
case "lsquo":
return "\u2018";
case "rsquo":
return "\u2019";
case "ldquo":
return "\u201C";
case "rdquo":
return "\u201D";
case "bull":
return "\u2022";
case "hellip":
return "\u2026";
case "trade":
return "\u2122";
case "copy":
return "\u00A9";
case "reg":
return "\u00AE";
case "deg":
return "\u00B0";
case "plusmn":
return "\u00B1";
case "times":
return "\u00D7";
case "divide":
return "\u00F7";
case "micro":
return "\u00B5";
case "middot":
return "\u00B7";
case "laquo":
return "\u00AB";
case "raquo":
return "\u00BB";
case "larr":
return "\u2190";
case "rarr":
return "\u2192";
case "uarr":
return "\u2191";
case "darr":
return "\u2193";
default:
return match;
}
});
} }
function _resolveHtmlBody(body) { function _resolveHtmlBody(body) {
if (!body) if (!body)
return ""; return "";
if (_hasHtmlTags(body)) if (/<\/?[a-z][\s\S]*>/i.test(body))
return body; return body;
if (_hasHtmlEntities(body)) { if (/&(#\d+|#x[0-9a-fA-F]+|[a-zA-Z][a-zA-Z0-9]+);/.test(body)) {
const decoded = _unescapeHtml(body); const decoded = _decodeEntities(body);
if (_hasHtmlTags(decoded)) if (/<\/?[a-z][\s\S]*>/i.test(decoded))
return decoded; return decoded;
return Markdown2Html.markdownToHtml(decoded); return Markdown2Html.markdownToHtml(decoded);
} }