@scrider/formatter 1.8.5 → 1.8.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -653,6 +653,18 @@ function getGridDimensions(cells) {
653
653
  if (maxRow < 0 || maxCol < 0) return null;
654
654
  return [maxRow + 1, maxCol + 1];
655
655
  }
656
+ function rowspanCrossesHeaderBoundary(cells, headerRows) {
657
+ if (headerRows <= 0) return false;
658
+ for (const [key, cell] of Object.entries(cells)) {
659
+ if (cell === null) continue;
660
+ const parsed = parseCellKey(key);
661
+ if (!parsed) continue;
662
+ const [row] = parsed;
663
+ const rowspan = cell.rowspan ?? 1;
664
+ if (row < headerRows && row + rowspan > headerRows) return true;
665
+ }
666
+ return false;
667
+ }
656
668
  function isValidCellData(cell) {
657
669
  if (typeof cell !== "object" || cell === null || !Array.isArray(cell.ops) || cell.ops.length === 0) {
658
670
  return false;
@@ -955,6 +967,26 @@ function collectTableRows(table) {
955
967
  }
956
968
  return { rows, headerRowCount };
957
969
  }
970
+ function inferHeaderRowsFromThPrefix(rows) {
971
+ let count = 0;
972
+ for (const row of rows) {
973
+ let hasCell = false;
974
+ let allTh = true;
975
+ const cellChildren = row.childNodes;
976
+ for (let i = 0; i < cellChildren.length; i++) {
977
+ const cell = cellChildren[i];
978
+ if (!cell || !isElement(cell)) continue;
979
+ const tag = cell.tagName.toLowerCase();
980
+ if (tag !== "td" && tag !== "th") continue;
981
+ hasCell = true;
982
+ if (tag !== "th") allTh = false;
983
+ }
984
+ if (!hasCell) continue;
985
+ if (!allTh) break;
986
+ count++;
987
+ }
988
+ return count;
989
+ }
958
990
  function extractColWidths(table) {
959
991
  const children = table.childNodes;
960
992
  for (let i = 0; i < children.length; i++) {
@@ -1042,7 +1074,13 @@ function extractInlineHorizontalAlign(element) {
1042
1074
  return null;
1043
1075
  }
1044
1076
  function parseTableElement(table, context) {
1045
- const { rows, headerRowCount } = collectTableRows(table);
1077
+ const collected = collectTableRows(table);
1078
+ const { rows } = collected;
1079
+ let headerRowCount = collected.headerRowCount;
1080
+ if (headerRowCount === 0) {
1081
+ const inferred = inferHeaderRowsFromThPrefix(rows);
1082
+ if (inferred > 0) headerRowCount = inferred;
1083
+ }
1046
1084
  if (rows.length === 0) return null;
1047
1085
  const cells = {};
1048
1086
  const colAligns = [];
@@ -1260,14 +1298,15 @@ var tableBlockHandler = {
1260
1298
  }
1261
1299
  html += `${ind(1)}</colgroup>${nl}`;
1262
1300
  }
1263
- if (headerRows > 0) {
1301
+ const splitHeaderSection = headerRows > 0 && !rowspanCrossesHeaderBoundary(data.cells, headerRows);
1302
+ if (splitHeaderSection) {
1264
1303
  html += `${ind(1)}<thead>${nl}`;
1265
1304
  for (let r = 0; r < headerRows; r++) {
1266
1305
  html += renderExtendedRow(data, r, cols, "th", context, pretty);
1267
1306
  }
1268
1307
  html += `${ind(1)}</thead>${nl}`;
1269
1308
  }
1270
- const bodyStart = headerRows;
1309
+ const bodyStart = splitHeaderSection ? headerRows : 0;
1271
1310
  if (bodyStart < rows) {
1272
1311
  html += `${ind(1)}<tbody>${nl}`;
1273
1312
  for (let r = bodyStart; r < rows; r++) {