123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- export const xmlReplacer = /["&'<>$\x80-\uFFFF]/g;
- const xmlCodeMap = new Map([
- [34, """],
- [38, "&"],
- [39, "'"],
- [60, "<"],
- [62, ">"],
- ]);
- export const getCodePoint =
- String.prototype.codePointAt != null
- ? (str, index) => str.codePointAt(index)
- :
- (c, index) => (c.charCodeAt(index) & 0xfc00) === 0xd800
- ? (c.charCodeAt(index) - 0xd800) * 0x400 +
- c.charCodeAt(index + 1) -
- 0xdc00 +
- 0x10000
- : c.charCodeAt(index);
- export function encodeXML(str) {
- let ret = "";
- let lastIdx = 0;
- let match;
- while ((match = xmlReplacer.exec(str)) !== null) {
- const i = match.index;
- const char = str.charCodeAt(i);
- const next = xmlCodeMap.get(char);
- if (next !== undefined) {
- ret += str.substring(lastIdx, i) + next;
- lastIdx = i + 1;
- }
- else {
- ret += `${str.substring(lastIdx, i)}&#x${getCodePoint(str, i).toString(16)};`;
-
- lastIdx = xmlReplacer.lastIndex += Number((char & 0xfc00) === 0xd800);
- }
- }
- return ret + str.substr(lastIdx);
- }
- export const escape = encodeXML;
- function getEscaper(regex, map) {
- return function escape(data) {
- let match;
- let lastIdx = 0;
- let result = "";
- while ((match = regex.exec(data))) {
- if (lastIdx !== match.index) {
- result += data.substring(lastIdx, match.index);
- }
-
- result += map.get(match[0].charCodeAt(0));
-
- lastIdx = match.index + 1;
- }
- return result + data.substring(lastIdx);
- };
- }
- export const escapeUTF8 = getEscaper(/[&<>'"]/g, xmlCodeMap);
- export const escapeAttribute = getEscaper(/["&\u00A0]/g, new Map([
- [34, """],
- [38, "&"],
- [160, " "],
- ]));
- export const escapeText = getEscaper(/[&<>\u00A0]/g, new Map([
- [38, "&"],
- [60, "<"],
- [62, ">"],
- [160, " "],
- ]));
|