restore String conversion

pull/5701/head
Ben McCann 4 years ago
parent 61e180b2b9
commit b32049bfdf

@ -76,8 +76,8 @@ const CONTENT_REGEX = /[&<]/g;
* Note: this method is performance sensitive and has been optimized * Note: this method is performance sensitive and has been optimized
* https://github.com/sveltejs/svelte/pull/5701 * https://github.com/sveltejs/svelte/pull/5701
*/ */
export function escape(html: string, is_attr = false) { export function escape(value: unknown, is_attr = false) {
if (typeof html !== 'string') return html; const str = String(value);
const pattern = is_attr ? ATTR_REGEX : CONTENT_REGEX; const pattern = is_attr ? ATTR_REGEX : CONTENT_REGEX;
pattern.lastIndex = 0; pattern.lastIndex = 0;
@ -85,14 +85,14 @@ export function escape(html: string, is_attr = false) {
let escaped = ''; let escaped = '';
let last = 0; let last = 0;
while (pattern.test(html)) { while (pattern.test(str)) {
const i = pattern.lastIndex - 1; const i = pattern.lastIndex - 1;
const ch = html[i]; const ch = str[i];
escaped += html.substring(last, i) + (ch === '&' ? '&amp;' : (ch === '"' ? '&quot;' : '&lt;')); escaped += str.substring(last, i) + (ch === '&' ? '&amp;' : (ch === '"' ? '&quot;' : '&lt;'));
last = i + 1; last = i + 1;
} }
return escaped + html.substring(last); return escaped + str.substring(last);
} }
export function escape_attribute_value(value) { export function escape_attribute_value(value) {

Loading…
Cancel
Save