Sharkey/packages/backend/src/prelude/xml.ts

42 lines
855 B
TypeScript
Raw Normal View History

const map: Record<string, string> = {
'&': '&amp;',
'<': '&lt;',
'>': '&gt;',
'"': '&quot;',
2021-12-09 16:58:30 +02:00
'\'': '&apos;',
};
const beginingOfCDATA = '<![CDATA[';
const endOfCDATA = ']]>';
export function escapeValue(x: string): string {
let insideOfCDATA = false;
let builder = '';
for (
let i = 0;
i < x.length;
) {
if (insideOfCDATA) {
if (x.slice(i, i + beginingOfCDATA.length) === beginingOfCDATA) {
insideOfCDATA = true;
i += beginingOfCDATA.length;
} else {
builder += x[i++];
}
} else {
if (x.slice(i, i + endOfCDATA.length) === endOfCDATA) {
insideOfCDATA = false;
i += endOfCDATA.length;
} else {
const b = x[i++];
builder += map[b] || b;
}
}
}
return builder;
}
export function escapeAttribute(x: string): string {
return Object.entries(map).reduce((a, [k, v]) => a.replace(k, v), x);
}