@fnlb-project/stanza
Version:
Modern XMPP in the browser, with a JSON API
76 lines (75 loc) • 2.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.escapeXML = escapeXML;
exports.unescapeXML = unescapeXML;
exports.escapeXMLText = escapeXMLText;
exports.basicLanguageResolver = basicLanguageResolver;
const tslib_1 = require("tslib");
const Error_1 = tslib_1.__importDefault(require("./Error"));
const ESCAPE_XML_CHAR = {
'"': '"',
'&': '&',
"'": ''',
'<': '<',
'>': '>'
};
const UNESCAPE_XML_CHAR = {
'&': '&',
''': "'",
'>': '>',
'<': '<',
'"': '"'
};
const ESCAPE_SEQUENCE = /&([a-zA-Z0-9]+|#[0-9]+|#x[0-9a-fA-F]+);/g;
const NEED_ESCAPING = /&|<|>|"|'/g;
const NEED_ESCAPING_TEXT = /&|<|>/g;
function escapeXMLReplaceChar(match) {
return ESCAPE_XML_CHAR[match];
}
function unescapeXMLReplaceChar(match) {
if (UNESCAPE_XML_CHAR[match]) {
return UNESCAPE_XML_CHAR[match];
}
const hex = match.startsWith('&#x');
const code = parseInt(match.substring(hex ? 3 : 2, match.length - 1), hex ? 16 : 10);
if (code === 0x9 ||
code === 0xa ||
code === 0xd ||
(0x20 <= code && code <= 0xd7ff) ||
(0xe000 <= code && code <= 0xfffd) ||
(0x10000 <= code && code <= 0x10ffff)) {
return String.fromCodePoint(code);
}
throw Error_1.default.restrictedXML('Prohibited entity: ' + match);
}
function escapeXML(text) {
return text.replace(NEED_ESCAPING, escapeXMLReplaceChar);
}
function unescapeXML(text) {
return text.replace(ESCAPE_SEQUENCE, match => {
return unescapeXMLReplaceChar(match);
});
}
function escapeXMLText(text) {
return text.replace(NEED_ESCAPING_TEXT, escapeXMLReplaceChar);
}
function basicLanguageResolver(available, accept = [], current = '') {
const avail = new Set(available.map(a => a.toLowerCase()));
for (let acceptLang of accept.map(a => a.toLowerCase())) {
if (acceptLang === '*') {
continue;
}
while (acceptLang.length > 0) {
if (avail.has(acceptLang)) {
return acceptLang;
}
// Remove ending tag
acceptLang = acceptLang.substring(0, acceptLang.lastIndexOf('-')).toLowerCase();
// Remove leftover single character tag
if (acceptLang.lastIndexOf('-') === acceptLang.length - 2) {
acceptLang = acceptLang.substring(0, acceptLang.lastIndexOf('-'));
}
}
}
return current;
}