2019-02-20 00:13:43 -07:00
|
|
|
const map: Record<string, string> = {
|
2023-01-12 21:40:33 -07:00
|
|
|
"&": "&",
|
|
|
|
"<": "<",
|
|
|
|
">": ">",
|
|
|
|
'"': """,
|
|
|
|
"'": "'",
|
2019-02-20 00:13:43 -07:00
|
|
|
};
|
|
|
|
|
2023-01-12 21:40:33 -07:00
|
|
|
const beginingOfCDATA = "<![CDATA[";
|
|
|
|
const endOfCDATA = "]]>";
|
2019-02-20 00:13:43 -07:00
|
|
|
|
|
|
|
export function escapeValue(x: string): string {
|
|
|
|
let insideOfCDATA = false;
|
2023-01-12 21:40:33 -07:00
|
|
|
let builder = "";
|
|
|
|
for (let i = 0; i < x.length; ) {
|
2019-02-20 00:13:43 -07:00
|
|
|
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);
|
|
|
|
}
|