You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
svelte/src/utils/stringify.ts

24 lines
560 B

export function stringify(data: string, options = {}) {
return JSON.stringify(escape(data, options));
}
export function escape(data: string, { onlyEscapeAtSymbol = false } = {}) {
return data.replace(onlyEscapeAtSymbol ? /(%+|@+)/g : /(%+|@+|#+)/g, (match: string) => {
return match + match[0];
});
}
const escaped = {
'&': '&',
'<': '&lt;',
'>': '&gt;',
};
export function escapeHTML(html) {
return String(html).replace(/[&<>]/g, match => escaped[match]);
}
export function escapeTemplate(str) {
return str.replace(/(\${|`|\\)/g, '\\$1');
}