Conversation
|
I feel like the expectation might still be that it would clear it out... 🤔 But this seems sensible. |
ekwoka
left a comment
There was a problem hiding this comment.
Can you update the documentation for x-html to describe these behaviors?
I went back and forth on it, so I'm not completely sold on this. I think there should be some way to have the original markup remain displayed while the value of the expression is not set, perhaps a modifier would be better? |
|
You can easily achieve it with the ?? operator if you are unsure when the data will be available <div x-data="{foo: undefined}" x-init="setTimeout(() => {foo = 'loaded'}, 5000)">
<p x-html="foo"><b>loading</b></p>
<p x-html="foo ?? $el.innerHTML"><b>loading</b></p>
</div> |
|
@SimoTod I am aware, that is exactly what I'm doing now to work around it. |
|
@SimoTod Technically speaking, that would destroy and recreate the contained dom tree, which MAY be a problem. |
|
Yeah but it's on page load so it's unlikely to have any meaningful state. After that point, the variable shouldn't really be undefined unless the app follows a weird design. This is obviously not relevant to the PR itself, it was just an interim workaround for that use case. |
|
I think I'll change this PR to be a simple fix for the I will explore adding an |
|
Could you update the docs to cover this? |
- `innerHTML` does not handle `undefined` values as expected. It will display the word `undefined` instead of using an empty string like `textContent` does.
e36d6db to
8d0a9dc
Compare
x-html is set to undefinedx-html handles undefined
|
This PR has been updated to fix an inconsistency in how @ekwoka since this is now a bug fix, what needs to be updated in the docs? |
|
Okay, with that change, I don't think it would be needed. With it preserving the original content it would be valuable to document it. |
PR Review: #4555 — Fix how
|
Ifx-htmlresolves to an empty string ornull, theninnerHTMLis set to an empty string. However, if it resolves toundefined, theninnerHTMLis set to a string with value ofundefined, which is displayed to the user.Sincenullalready acts as expected and displays nothing to the user, this PR updatesx-htmlto leave the currentinnerHTMLas-is if the value resolves toundefined. This way nothing unexpected is displayed to the user and also makes it possible to include fallback HTML on page load that will be displayed until the value resolves to something other thanundefined.- If you want to remove all HTML and show nothing thenx-htmlshould resolve to an empty string ornull.- To leave the initially loaded or current HTML displayed thenx-htmlshould resolve toundefined.This PR has changed focus, see this comment