-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Fix how x-html
handles undefined
#4555
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
I feel like the expectation might still be that it would clear it out... 🤔 But this seems sensible. |
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.
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 undefined
x-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. |
Ifx-html
resolves to an empty string ornull
, theninnerHTML
is set to an empty string. However, if it resolves toundefined
, theninnerHTML
is set to a string with value ofundefined
, which is displayed to the user.Sincenull
already acts as expected and displays nothing to the user, this PR updatesx-html
to leave the currentinnerHTML
as-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-html
should resolve to an empty string ornull
.- To leave the initially loaded or current HTML displayed thenx-html
should resolve toundefined
.This PR has changed focus, see this comment