I don’t think Svelte should add the missing tbody here. That would be very confusing to have the framework adding tags not present in the source. Since browsers are accepting a <tr> directly inside a <table> svelte should leave the markup unchanged as does Svelte 4 and other UI frameworks.
It would be nice to have this as a warning when using Svelte 4, so one can start fixing their code right now instead of getting errors about it during the migration.
Although if this is an unavoidable technical limitation of the new rendering model that should be explicitly documented, especially in the migration guide.
Maybe the compiler can just do all the structure correction under the hood. However, it can lead to a confusing experience when a CSS rule doesn’t apply to an element or apply to another element because the DOM structure in the source and in the browser differs. So it makes sense to write a markup that will not change its structure.
If the compiler were to also include display: contents; on the automatically inserted <tbody>, then that shouldn’t be an issue.
From @trueadm on an earlier discussion we had about this:
The browser always inserts a <tbody> when you don’t put one in. We need to have a <tbody> then to avoid breaking our template mechanic in Svelte 5. There’s not a super simple solution here. We either have to try and detect this at runtime or we try and do it statically between modules somehow, but both are pretty difficult. Solid, etc. have the same error. They push for the developer to fix their code and they class this as invalid, as does React.
One possible way would be when we append templates, we can see what the nodes are that we append and check if the parent we’re appending to is a <table> and appropriately add a tbody element, and vice versa for when we traverse the elements. This will incur a small overhead cost for checking though, so maybe we can somehow get the compiler to give us a good estimate by looking at the template to see if we’re dealing with <tr> elements.
This is the result of the limitation of the new way of component rendering - creating all the elements in one go and extracting references to the elements. But when a browser corrects the template structure, it breaks the reference extraction.
Maybe the compiler can just do all the structure correction under the hood. However, it can lead to a confusing experience when a CSS rule doesn’t apply to an element or apply to another element because the DOM structure in the source and in the browser differs. So it makes sense to write a markup that will not change its structure.
I don’t think Svelte should add the missing
tbodyhere. That would be very confusing to have the framework adding tags not present in the source. Since browsers are accepting a<tr>directly inside a<table>svelte should leave the markup unchanged as does Svelte 4 and other UI frameworks.It would be nice to have this as a warning when using Svelte 4, so one can start fixing their code right now instead of getting errors about it during the migration.
On MDN
<tbody>are omitted in some of the examples https://developer.mozilla.org/en-US/docs/Web/HTML/Element/table#simple_table, so I think it is a reasonable way to write the markup for a table, and Svelte rejecting it as invalid is unexpected.Although if this is an unavoidable technical limitation of the new rendering model that should be explicitly documented, especially in the migration guide.
Right, it’s a documentation issue so I’d assume it’s being added to the breaking changes list 😃
It still will be issue for cases like this:
And you will be puzzled why it doesn’t work on the following markup:
If the compiler were to also include
display: contents;on the automatically inserted<tbody>, then that shouldn’t be an issue.@R3D2 you forgot about
<td>/<th>. You cannot add text right to<tr>.From @trueadm on an earlier discussion we had about this:
The browser always inserts a
<tbody>when you don’t put one in. We need to have a<tbody>then to avoid breaking our template mechanic in Svelte 5. There’s not a super simple solution here. We either have to try and detect this at runtime or we try and do it statically between modules somehow, but both are pretty difficult. Solid, etc. have the same error. They push for the developer to fix their code and they class this as invalid, as does React.One possible way would be when we append templates, we can see what the nodes are that we append and check if the parent we’re appending to is a
<table>and appropriately add atbodyelement, and vice versa for when we traverse the elements. This will incur a small overhead cost for checking though, so maybe we can somehow get the compiler to give us a good estimate by looking at the template to see if we’re dealing with<tr>elements.This is the result of the limitation of the new way of component rendering - creating all the elements in one go and extracting references to the elements. But when a browser corrects the template structure, it breaks the reference extraction.
Maybe the compiler can just do all the structure correction under the hood. However, it can lead to a confusing experience when a CSS rule doesn’t apply to an element or apply to another element because the DOM structure in the source and in the browser differs. So it makes sense to write a markup that will not change its structure.