From 8679db8ad83316ec066de1b4d766ca3586910974 Mon Sep 17 00:00:00 2001 From: Jason Miller Date: Thu, 21 Jul 2022 09:19:37 -0400 Subject: [PATCH] Switch sanitization to a Set and add cleanup This fixes #20. --- src/vhtml.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/vhtml.js b/src/vhtml.js index 0e03f69..e2bff65 100644 --- a/src/vhtml.js +++ b/src/vhtml.js @@ -9,7 +9,16 @@ let DOMAttributeNames = { htmlFor: 'for' }; -let sanitized = {}; +let sanitized = new Set(); + +let cleanupTimer = 0; +function cleanup() { + cleanupTimer = 0; + sanitized.clear(); +} +function scheduleCleanup() { + if (!cleanupTimer) cleanupTimer = setTimeout(cleanup); +} /** Hyperscript reviver that constructs a sanitized HTML string. */ export default function h(name, attrs) { @@ -47,7 +56,7 @@ export default function h(name, attrs) { for (let i=child.length; i--; ) stack.push(child[i]); } else { - s += sanitized[child]===true ? child : esc(child); + s += sanitized.has(child) ? child : esc(child); } } } @@ -55,6 +64,7 @@ export default function h(name, attrs) { s += name ? `` : ''; } - sanitized[s] = true; + sanitized.add(s); + scheduleCleanup(); return s; }