-
Notifications
You must be signed in to change notification settings - Fork 22.8k
Element.innerHTML - add info trusted types #40423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Preview URLs External URLs (3)URL:
URL:
(comment last updated: 2025-08-01 03:34:43) |
24677d9
to
cfaddcf
Compare
FYI @lukewarlow @wbamberg - this is the trusted types update for All the examples use trustedHTML. This also shows the first live example use of the tinyfill in the docs. This is duplication with the main docs, but it feels very practical to me given the current state of trustedtypes. |
@@ -105,15 +119,35 @@ code { | |||
|
|||
#### JavaScript | |||
|
|||
```js hidden |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decided to hide the tinyfill in this case. Do you think it should be here. Or perhaps a note "if trusted types not supported you should add a tinyfill".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just comments on innerHTML
for now.
</ul> | ||
```js | ||
// Create a TrustedHTML instance using the policy | ||
document.body.textContent = policy.createHTML(policy.createHTML("")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this supposed to be assigning to textContent?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doh. Actually though, you'd be safer to use textContent for this - and less hassle.. I'm going to delete the section as pointless
``` | ||
|
||
Please note that using `innerHTML` to append HTML elements (e.g., `el.innerHTML += "<a href='…'>link</a>"`) will result in the removal of any previously set event listeners. | ||
That is, after you append any HTML element that way you won't be able to listen to the previously set event listeners. | ||
### Logging messages using innerHTML |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this example worth keeping? Is this a pattern we want to encourage, versus explicitly creating the DOM elements to put in the log?
and I also wonder, is it a good idea to recommend people to define a no-op TT policy? To what extent does that open up a hole in the whole thing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've decided to delete for the reasons given - it doesn't teach anything new, except possibly wrong things.
and I also wonder, is it a good idea to recommend people to define a no-op TT policy? To what extent does that open up a hole in the whole thing?
Right now you should mostly be able to avoid recommending this for HTML but not for script trusted types. In future there are likely to be cases where your policy may need to be no-op - such as when using the unsafe HTML parsing methods. Right now and in this doc you make a good point. We shouldn't.
This updates the following properties to explain how they are used with TrustedTypes:
Element.innerHTML
Element.insertAdjacentHTML()
This is in progress:
This does not mention the sanitizer methods as alternatives since the
setHTML()
is not implemented anywhere, and the sanitizer insetHTMLUnsafe()
is not yet implemented. We're in separate discussion on those and when they are in a release we can revisit.Part of #37518 (tracking issue)