Sanitize an HTML string by removing dangerous elements and attributes.
Works on a parsed tree (htmlparser2), not on regexes, so odd spellings and
unterminated tags can't slip through. Tags in the allowlist are kept with
their allowed attributes; script, iframe, form controls and similar are
removed with their content; any other tag is dropped and its content kept.
href / src must be http(s):, mailto:, #… or a /… path (not
//…); a kept target gets rel="noopener noreferrer". id is dropped,
so content can't shadow page globals (DOM clobbering). Text is written back
escaped, so the output is always well-formed. The rules are in
./sanitize-policy, shared with the runtime's notes sanitizer.
Designed for build-time use (Node.js). Does NOT rely on DOM APIs.
Sanitize an HTML string by removing dangerous elements and attributes.
Works on a parsed tree (htmlparser2), not on regexes, so odd spellings and unterminated tags can't slip through. Tags in the allowlist are kept with their allowed attributes;
script,iframe, form controls and similar are removed with their content; any other tag is dropped and its content kept.href/srcmust behttp(s):,mailto:,#…or a/…path (not//…); a kepttargetgetsrel="noopener noreferrer".idis dropped, so content can't shadow page globals (DOM clobbering). Text is written back escaped, so the output is always well-formed. The rules are in./sanitize-policy, shared with the runtime's notes sanitizer.Designed for build-time use (Node.js). Does NOT rely on DOM APIs.