Skip to content
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

fix: allow colspans on any cell #203

Merged
merged 2 commits into from
Oct 11, 2023
Merged
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
9 changes: 6 additions & 3 deletions blocks/table/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function extractColSpans(block) {
return [];
}

const cols = firstCol.nextElementSibling.textContent.split(',');
const cols = firstCol.nextElementSibling.textContent.split(';').map((row) => row.split(','));
firstRow.remove();
return cols;
}
Expand Down Expand Up @@ -111,11 +111,12 @@ export default async function decorate(block) {
const head = rows.shift();
if (!head) return;

const spans = colSpans.shift() || [];
const cells = [...head.querySelectorAll(':scope > div')];
const thead = html` <table>
<thead>
<tr>
${cells.map((cell, i) => `<th${colSpans[i] ? ` colspan="${colSpans[i]}"` : ''}>${cell.innerHTML}</th>`).join('\n')}
${cells.map((cell, i) => `<th${spans[i] ? ` colspan="${spans[i]}"` : ''}>${cell.innerHTML}</th>`).join('\n')}
</tr>
</thead>
</table>`.firstElementChild;
Expand All @@ -128,10 +129,12 @@ export default async function decorate(block) {
table.appendChild(tbody);

rows.forEach((row) => {
const spans = colSpans.shift() || [];
const cells = [...row.querySelectorAll(':scope > div')];

const tr = html` <table>
<tr>
${cells.map((cell) => `<td>${cell.innerHTML}</td>`).join('\n')}
${cells.map((cell, i) => `<td${spans[i] ? ` colspan="${spans[i]}"` : ''}>${cell.innerHTML}</td>`).join('\n')}
</tr>
</table>`.firstElementChild.firstElementChild;
tbody.appendChild(tr);
Expand Down