Document.parseHTMLUnsafe() is a static method that parses HTML into a new Document with optional sanitization. Learn MDN’s injection-sink warnings, when to prefer parseHTML(), TrustedHTML + Trusted Types, default and custom Sanitizer options, declarative shadow roots, and five try-it labs.
01
Kind
Static method
02
Returns
Document
03
Args
input, options
04
Default
No sanitizer
05
Shadow DOM
Declarative roots
06
Status
Baseline 2025
Fundamentals
Introduction
Sometimes you need a full Document from an HTML string—not just inject into one element. Document.parseHTMLUnsafe(input, options) creates that Document, optionally filters unwanted tags, and supports declarative shadow roots in the input (MDN).
The “Unsafe” suffix is deliberate: without a sanitizer, every HTML entity in the string is injected. MDN warns this is an injection sink and a possible XSS vector when the input comes from attackers. On browsers where it exists, MDN says parseHTML() should almost always be used instead.
⚠️
Injection sink (MDN)
Mitigate risk by passing TrustedHTML objects and enforcing Trusted Types with the require-trusted-types-for CSP directive. If you cannot use TrustedHTML, the next safest option is parseHTMLUnsafe(html, { sanitizer: "default" }) or parseHTML().
Document.parseHTMLUnsafe() is Baseline Newly available (MDN: across latest browsers since September 2025). Feature-detect on older engines. Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline 2025
Document.parseHTMLUnsafe()
Parse HTML into a Document — optional sanitization, supports declarative shadow roots.
BaselineNewly available 2025
Google ChromeSupported in current releases — feature-detect older
Yes
Microsoft EdgeSupported in current releases — feature-detect older
Yes
Mozilla FirefoxSupported in current releases — feature-detect older
Yes
Apple SafariSupported in current releases — feature-detect older
Yes
OperaFollow Chromium support
Yes
Internet ExplorerNot supported — use DOMParser + sanitization
No
parseHTMLUnsafe()Baseline
Bottom line: Detect typeof Document.parseHTMLUnsafe === "function". Prefer parseHTML() for user content. Use TrustedHTML or sanitizer: "default" when you must call this API.
Wrap Up
Conclusion
Document.parseHTMLUnsafe() parses HTML strings into a new Document with optional sanitization. MDN treats it as an injection sink—prefer parseHTML() for user content, or use TrustedHTML and sanitizer: "default" when you must call the Unsafe API.
Prefer Document.parseHTML() for untrusted HTML (MDN)
Use TrustedHTML + Trusted Types CSP for injection sinks
Pass { sanitizer: "default" } when calling Unsafe without TrustedHTML
Feature-detect before calling in production
Reuse a Sanitizer instance for repeated configs (MDN)
❌ Don’t
Pass raw user strings without sanitization (MDN XSS warning)
Assume “Unsafe” only means shadow DOM—it means no default filter
Skip parseHTML() when both APIs exist and input is untrusted
Import parsed nodes into the live DOM without reviewing markup
Confuse static parseHTMLUnsafe with setHTMLUnsafe
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about parseHTMLUnsafe()
Static parser — injection sink by default; prefer parseHTML().
5
Core concepts
📝01
Kind
static
Document.
⚠️02
Default
no filter
MDN
📄03
Returns
Document
text/html
✓04
Prefer
parseHTML
User HTML
🛡05
Status
baseline
2025
❓ Frequently Asked Questions
It is a static method that parses HTML input into a new Document instance. Unlike parseHTML(), it does not sanitize by default — all HTML entities in the input are injected unless you pass a sanitizer (MDN).
No. MDN marks Document.parseHTMLUnsafe() as Baseline 2025 (newly available since September 2025). It is not Deprecated, Experimental, or Non-standard.
MDN: almost always. Document.parseHTML() always removes XSS-unsafe HTML entities. Use parseHTMLUnsafe() only for trusted HTML, TrustedHTML workflows, declarative shadow roots, or controlled cases with an explicit sanitizer.
MDN: content type "text/html", character set UTF-8, and URL "about:blank" — same as parseHTML().
No by default. MDN warns it is an injection sink and a possible XSS vector. Mitigate with TrustedHTML + Trusted Types CSP, or pass sanitizer: "default" / a custom Sanitizer.
Optional Sanitizer, SanitizerConfig, or the string "default" (XSS-safe default config). If omitted, no sanitizer runs and all HTML is injected (MDN).
Did you know?
MDN says the suffix “Unsafe” means parseHTMLUnsafe() does not enforce removal of all XSS-unsafe HTML entities—unlike parseHTML(). You can still pass a sanitizer, but if you omit it, every entity in the string is injected into the new Document.