INIT
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
const ATTR_REGEX = /[&"<]/g;
|
||||
const CONTENT_REGEX = /[&<]/g;
|
||||
|
||||
/**
|
||||
* @template V
|
||||
* @param {V} value
|
||||
* @param {boolean} [is_attr]
|
||||
*/
|
||||
export function escape_html(value, is_attr) {
|
||||
const str = String(value ?? '');
|
||||
|
||||
const pattern = is_attr ? ATTR_REGEX : CONTENT_REGEX;
|
||||
pattern.lastIndex = 0;
|
||||
|
||||
let escaped = '';
|
||||
let last = 0;
|
||||
|
||||
while (pattern.test(str)) {
|
||||
const i = pattern.lastIndex - 1;
|
||||
const ch = str[i];
|
||||
escaped += str.substring(last, i) + (ch === '&' ? '&' : ch === '"' ? '"' : '<');
|
||||
last = i + 1;
|
||||
}
|
||||
|
||||
return escaped + str.substring(last);
|
||||
}
|
||||
Reference in New Issue
Block a user