The HTML Sanitizer API lets browsers safely turn untrusted HTML strings into DOM content. In interviews, it is often more useful to practice the core traversal and transformation ideas behind that API than to reproduce the full platform surface.
Implement sanitizeHTML(input), a simplified sanitizer inspired by the platform's safe HTML sanitization behavior.
Parse the HTML into a detached DOM tree, sanitize it, and return the resulting HTML string.
The sanitizer should:
script, iframe, object, embed.on.href and src attributes whose trimmed, case-insensitive value starts with javascript:.sanitizeHTML('<p>Hello <strong>world</strong></p>');// '<p>Hello <strong>world</strong></p>'
sanitizeHTML(`<div><!-- secret --><a href=" javascript:alert(1) " onclick="evil()">Click me</a><script>alert(1)</script></div>`);// '<div><a>Click me</a></div>'
sanitizeHTML(input)| Argument | Type | Description |
|---|---|---|
input | string | The HTML string to sanitize. |
Returns a sanitized HTML string.
console.log() statements will appear here.