Skip to content

feat: auto-open a tbody when we detect we need it #12628

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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/svelte/src/compiler/phases/1-parse/state/element.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ export default function element(parser) {
svg: false,
mathml: false,
scoped: false,
has_spread: false
has_spread: false,
auto_opens: null
},
parent: null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,10 @@ export function RegularElement(node, context) {

context.state.template.push('>');

if (node.metadata.auto_opens !== null) {
context.state.template.push(node.metadata.auto_opens);
}

/** @type {SourceLocation[]} */
const child_locations = [];

Expand Down Expand Up @@ -347,10 +351,21 @@ export function RegularElement(node, context) {
arg = b.member(arg, 'content');
}

process_children(trimmed, () => b.call('$.child', arg), true, {
...context,
state: child_state
});
process_children(
trimmed, // TODO: this doesn't work when the table is a sibling, as the expression is then not used
() => {
let call = b.call('$.child', arg);
for (let i = (node.metadata.auto_opens?.split('<').length ?? 1) - 1; i > 0; i--) {
call = b.call('$.child', call);
}
return call;
},
true,
{
...context,
state: child_state
}
);

if (needs_reset) {
child_state.init.push(b.stmt(b.call('$.reset', context.state.node)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ export function RegularElement(node, context) {
return;
}

if (node.metadata.auto_opens !== null) {
context.state.template.push(b.literal(node.metadata.auto_opens));
}

const { hoisted, trimmed } = clean_nodes(
node,
node.fragment.nodes,
Expand Down
3 changes: 3 additions & 0 deletions packages/svelte/src/compiler/types/template.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,10 @@ export namespace AST {
mathml: boolean;
/** `true` if contains a SpreadAttribute */
has_spread: boolean;
/** `true` if should get a hash on the `class` attribute */
scoped: boolean;
/** Contains a string of the tag(s) that are implicitly opened after this element */
auto_opens: string | null;
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { test } from '../../test';

let console_error = console.error;

/**
* @type {any[]}
*/
const log = [];

export default test({
solo: true,
compileOptions: {
dev: true // enable validation to ensure it doesn't throw
},

html: `
<table>
<tbody>
<tr>
<td>works1</td>
</tr>
</tbody>
</table>

<table>
<tbody>
<tr>
<td>works2</td>
</tr>
<tr>
<td>works3</td>
</tr>
</tbody>
</table>

<table>
<tbody>
<tr>
<td>works4</td>
</tr>
</tbody>
<tbody>
<tr>
<td>works5</td>
</tr>
</tbody>
</table>

<table>
<tbody>
<tr>
<td>works6</td>
</tr>
</tbody>
</table>
`
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<table>
<tr>
<td>works1</td>
</tr>
</table>

<table>
{#each ['works2', 'works3'] as cell}
<tr>
<td>{cell}</td>
</tr>
{/each}
</table>

<table>
<tr>
<td>works4</td>
</tr>
<tbody>
<tr>
<td>works5</td>
</tr>
</tbody>
</table>

<table>
<td>works6</td>
</table>
Loading