Skip to content

Commit cebd6f3

Browse files
committed
address comments
1 parent 61e180b commit cebd6f3

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

src/runtime/internal/ssr.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,27 @@ const CONTENT_REGEX = /[&<]/g;
7676
* Note: this method is performance sensitive and has been optimized
7777
* https://github.com/sveltejs/svelte/pull/5701
7878
*/
79-
export function escape(html: string, is_attr = false) {
80-
if (typeof html !== 'string') return html;
79+
export function escape(value: unknown, is_attr = false) {
80+
const str: string = typeof value === 'string' ? value : value.toString();
8181

8282
const pattern = is_attr ? ATTR_REGEX : CONTENT_REGEX;
8383
pattern.lastIndex = 0;
8484

8585
let escaped = '';
8686
let last = 0;
8787

88-
while (pattern.test(html)) {
89-
const i = pattern.lastIndex - 1;
90-
const ch = html[i];
91-
escaped += html.substring(last, i) + (ch === '&' ? '&amp;' : (ch === '"' ? '&quot;' : '&lt;'));
92-
last = i + 1;
93-
}
88+
while (pattern.test(str)) {
89+
const i = pattern.lastIndex - 1;
90+
const ch = str[i];
91+
escaped += str.substring(last, i) + (ch === '&' ? '&amp;' : (ch === '"' ? '&quot;' : '&lt;'));
92+
last = i + 1;
93+
}
9494

95-
return escaped + html.substring(last);
95+
return escaped + str.substring(last);
9696
}
9797

9898
export function escape_attribute_value(value) {
99-
return typeof value === 'string' ? escape(value, true) : value;
99+
return escape(value, true);
100100
}
101101

102102
export function escape_object(obj) {
@@ -192,7 +192,7 @@ export function create_ssr_component(fn) {
192192

193193
export function add_attribute(name, value, boolean) {
194194
if (value == null || (boolean && !value)) return '';
195-
const assignment = (boolean && value === true) ? '' : `="${escape_attribute_value(value.toString())}"`;
195+
const assignment = (boolean && value === true) ? '' : `="${escape_attribute_value(value)}"`;
196196
return ` ${name}${assignment}`;
197197
}
198198

0 commit comments

Comments
 (0)