2023-07-27 14:31:52 +09:00
|
|
|
/*
|
2024-02-14 00:59:27 +09:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 14:31:52 +09:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2019-02-20 16:13:43 +09:00
|
|
|
const map: Record<string, string> = {
|
|
|
|
'&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
'"': '"',
|
2021-12-09 23:58:30 +09:00
|
|
|
'\'': ''',
|
2019-02-20 16:13:43 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|