summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc/json-stringify-html-safe.ts
blob: aac12d57db024857ee04936f204edef2d09fd486 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * SPDX-FileCopyrightText: syuilo and misskey-project
 * SPDX-License-Identifier: AGPL-3.0-only
 */

const ESCAPE_LOOKUP = {
	'&': '\\u0026',
	'>': '\\u003e',
	'<': '\\u003c',
	'\u2028': '\\u2028',
	'\u2029': '\\u2029',
} as Record<string, string>;

const ESCAPE_REGEX = /[&><\u2028\u2029]/g;

export function htmlSafeJsonStringify(obj: any): string {
	return JSON.stringify(obj).replace(ESCAPE_REGEX, x => ESCAPE_LOOKUP[x]);
}