@@ -76,27 +76,27 @@ const CONTENT_REGEX = /[&<]/g;
76
76
* Note: this method is performance sensitive and has been optimized
77
77
* https://github.com/sveltejs/svelte/pull/5701
78
78
*/
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 ( ) ;
81
81
82
82
const pattern = is_attr ? ATTR_REGEX : CONTENT_REGEX ;
83
83
pattern . lastIndex = 0 ;
84
84
85
85
let escaped = '' ;
86
86
let last = 0 ;
87
87
88
- while ( pattern . test ( html ) ) {
89
- const i = pattern . lastIndex - 1 ;
90
- const ch = html [ i ] ;
91
- escaped += html . substring ( last , i ) + ( ch === '&' ? '&' : ( ch === '"' ? '"' : '<' ) ) ;
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 === '&' ? '&' : ( ch === '"' ? '"' : '<' ) ) ;
92
+ last = i + 1 ;
93
+ }
94
94
95
- return escaped + html . substring ( last ) ;
95
+ return escaped + str . substring ( last ) ;
96
96
}
97
97
98
98
export function escape_attribute_value ( value ) {
99
- return typeof value === 'string' ? escape ( value , true ) : value ;
99
+ return escape ( value , true ) ;
100
100
}
101
101
102
102
export function escape_object ( obj ) {
@@ -192,7 +192,7 @@ export function create_ssr_component(fn) {
192
192
193
193
export function add_attribute ( name , value , boolean ) {
194
194
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 ) } "` ;
196
196
return ` ${ name } ${ assignment } ` ;
197
197
}
198
198
0 commit comments