Skip to content

Document sanitization during HTML parsing - #46211

Open
Stackforge-07 wants to merge 4 commits into
mdn:mainfrom
Stackforge-07:document-sanitize-while-parsing
Open

Stackforge-07 wants to merge 4 commits into
mdn:mainfrom
Stackforge-07:document-sanitize-while-parsing

Conversation

@Stackforge-07

@Stackforge-07 Stackforge-07 commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor

Description

Update the HTML Sanitizer API overview and all six Element, ShadowRoot, and Document method references to describe sanitization during parsing rather than a separate pass over the completed DOM. Explain the observable consequences for adjacent text nodes, custom element construction, resource-fetching side effects, and declarative shadow roots, with a focused example showing a replaced <b> producing one text node.

Also correct the default-sanitizer example to pass { sanitizer: "default" } to setHTMLUnsafe(), fix its inconsistent variable names and whitespace output, and clarify fragment parsing's top-level shadow-template behavior.

Motivation

The HTML Standard changed in whatwg/html#12756. The existing descriptions still teach the earlier parse-then-sanitize model and the unsafe example currently omits the sanitizer it claims to use. This documents the current specification and notes that some browsers still sanitize after parsing, producing different DOM structures.

Additional details

Related issues and pull requests

Fixes #46132.

Related to #46148 (the release-note task); this PR updates the overview and API references.

@Stackforge-07
Stackforge-07 requested a review from a team as a code owner October 20, 2026 11:54
@Stackforge-07
Stackforge-07 requested review from hamishwillee and removed request for a team October 20, 2026 11:54
@github-actions github-actions Bot added Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed labels Oct 20, 2026
@zcorpan

zcorpan commented Oct 20, 2026

Copy link
Copy Markdown
Contributor
AI review

Reviewed at cf4bf10

The PR covers what #46132 asked for, and I checked its claims against the spec text from whatwg/html#12756. It also fixes the broken setHTMLUnsafe() example and the parseHTML() parameter that said "shadow root".

Blockers

  • files/en-us/web/api/html_sanitizer_api/index.md:73, :338: "Earlier implementations, including Chrome 146 and Firefox 148" makes it sound like the old model is history, but Firefox release still uses it. Gecko only turns the pref on in 159. Chromium turned on StreamingSanitizer in 152, and some fixes (inert document, where replaced children are placed) only landed in 154–157. Consider keeping the prose version-free ("Some browsers parse the complete input before sanitizing…") and putting the version ranges in BCD notes for setHTML(), setHTMLUnsafe(), and parseHTML()/parseHTMLUnsafe().

Suggestions

  • index.md:71: "instead of attaching a shadow root to the host's parent" describes something no browser or spec version ever did. Under the old model the shadow root was attached to the host and then lost along with it. Consider: "If the host is replaced with its children, the <template> is kept as a regular template where the host's children are inserted."
  • files/en-us/web/api/element/sethtmlunsafe/index.md:65, index.md:48: These say the template doesn't attach, but not what happens to it instead. The spec inserts it as a regular <template> (the hostIsRoot case), which is how the ShadowRoot.setHTMLUnsafe() page already words it. Consider the same wording here. "in a fragment parse" on line 48 is also jargon; "at the top level of the input" would do.
  • files/en-us/web/api/element/sethtml/index.md:54: "a <col> element outside a <table> is not included" reads as if it's about where <col> sits in the input. It actually depends on the target element. Consider: "for example, <col> is dropped unless the element is a <table> or <colgroup>."
  • index.md:20, :33, :301: The PR edits these sentences but keeps "injected", which reads like the attack rather than intended use. Consider "inserted". (This could be done in a separate PR if considered out of scope here.)

@zcorpan

zcorpan commented Oct 20, 2026

Copy link
Copy Markdown
Contributor
AI review

Reviewed at fa821d4

The new commit addresses all the earlier points. Two small things are left.

Nits

  • files/en-us/web/api/element/sethtml/index.md:54: The list should include <template>. In that context the "in template" insertion mode switches to "in column group", so <col> is kept: "unless the target is a <table>, <colgroup>, or <template> element".
  • files/en-us/web/api/html_sanitizer_api/index.md:251: "the unsafe HTML sanitization methods may inject untrusted HTML" is the one "inject" left describing intended use. Consider "may insert".

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

@hamishwillee

hamishwillee commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

There's some work going on on in this area in:

I will review when that is done. @wbamberg I don't think this one is same area but different focus, so will assume you don't want to review it.

@github-actions github-actions Bot added the merge conflicts 🚧 [PR only] label Nov 9, 2026
@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

This pull request has merge conflicts that must be resolved before it can be merged.

@github-actions github-actions Bot removed the merge conflicts 🚧 [PR only] label Nov 10, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Content:WebAPI Web API docs size/m [PR only] 51-500 LoC changed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sanitizer API: sanitization now happens during parsing

3 participants