File: table-formatting.js

package info (click to toggle)
django-anymail 13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,552 kB
  • sloc: python: 28,882; makefile: 132; javascript: 33; sh: 9
file content (40 lines) | stat: -rw-r--r-- 1,032 bytes parent folder | download | duplicates (2)
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();
}