File size: 877 Bytes
bc20498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
 * @param {string} data
 * @returns {import('estree').Literal}
 */
export function string_literal(data) {
	return {
		type: 'Literal',
		value: data
	};
}
/**
 * @param {string} data
 * @param {{ only_escape_at_symbol?: boolean }} [options]
 */
export function escape(data, { only_escape_at_symbol = false } = {}) {
	return data.replace(only_escape_at_symbol ? /@+/g : /(@+|#+)/g, (match) => {
		return match + match[0];
	});
}

const escaped = {
	'"': '"',
	"'": ''',
	'&': '&',
	'<': '&lt;',
	'>': '&gt;'
};

const regex_html_characters_to_escape = /["'&<>]/g;

export function escape_html(html) {
	return String(html).replace(regex_html_characters_to_escape, (match) => escaped[match]);
}

const regex_template_characters_to_escape = /(\${|`|\\)/g;

export function escape_template(str) {
	return str.replace(regex_template_characters_to_escape, '\\$1');
}