1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
/**
* Return the first sibling of el that matches CSS selector, or null if no matches.
* @param {HTMLElement} el
* @param {string} selector
* @returns {HTMLElement|null}
*/
function nextSiblingMatching(el, selector) {
while (el && el.nextElementSibling) {
el = el.nextElementSibling;
if (el.matches(selector)) {
return el;
}
}
return null;
}
/**
* Convert runs of empty <td> elements to a colspan on the first <td>.
*/
function collapseEmptyTableCells() {
document.querySelectorAll(".rst-content tr:has(td:empty)").forEach((tr) => {
for (
let spanStart = tr.querySelector("td");
spanStart;
spanStart = nextSiblingMatching(spanStart, "td")
) {
let emptyCell;
while ((emptyCell = nextSiblingMatching(spanStart, "td:empty"))) {
emptyCell.remove();
spanStart.colSpan++;
}
}
});
}
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", collapseEmptyTableCells);
} else {
collapseEmptyTableCells();
}
|