MCPcopy Create free account
hub / github.com/handsontable/handsontable / instanceToHTML

Function instanceToHTML

handsontable/src/utils/parseTable.ts:28–103  ·  view source on GitHub ↗
(instance: HotInstance)

Source from the content-addressed store, hash-verified

26 * @returns {string} OuterHTML of the HTMLTableElement.
27 */
28export function instanceToHTML(instance: HotInstance): string {
29 const hasColumnHeaders = instance.hasColHeaders();
30 const hasRowHeaders = instance.hasRowHeaders();
31 const coords = [
32 hasColumnHeaders ? -1 : 0,
33 hasRowHeaders ? -1 : 0,
34 instance.countRows() - 1,
35 instance.countCols() - 1,
36 ];
37 const data = instance.getData(...coords);
38 const countRows = data.length;
39 const countCols = countRows > 0 ? data[0].length : 0;
40 const TABLE = ['<table>', '</table>'];
41 const THEAD = hasColumnHeaders ? ['<thead>', '</thead>'] : [];
42 const TBODY = ['<tbody>', '</tbody>'];
43 const rowModifier = hasRowHeaders ? 1 : 0;
44 const columnModifier = hasColumnHeaders ? 1 : 0;
45
46 for (let row = 0; row < countRows; row += 1) {
47 const isColumnHeadersRow = hasColumnHeaders && row === 0;
48 const CELLS = [];
49
50 for (let column = 0; column < countCols; column += 1) {
51 const isRowHeadersColumn = !isColumnHeadersRow && hasRowHeaders && column === 0;
52 let cell = '';
53
54 if (isColumnHeadersRow) {
55 cell = `<th>${instance.getColHeader(column - rowModifier)}</th>`;
56
57 } else if (isRowHeadersColumn) {
58 cell = `<th>${instance.getRowHeader(row - columnModifier)}</th>`;
59
60 } else {
61 const cellData = data[row][column];
62 const { hidden, rowspan, colspan } = instance.getCellMeta(row - columnModifier, column - rowModifier);
63
64 if (!hidden) {
65 const attrs = [];
66
67 if (rowspan) {
68 attrs.push(`rowspan="${rowspan}"`);
69 }
70 if (colspan) {
71 attrs.push(`colspan="${colspan}"`);
72 }
73 if (isEmpty(cellData)) {
74 cell = `<td ${attrs.join(' ')}></td>`;
75 } else {
76 const value = String(cellData)
77 .replaceAll('<', '&lt;')
78 .replaceAll('>', '&gt;')
79 .replace(/(<br(\s*|\/)>(\r\n|\n)?|\r\n|\n)/g, '<br>\r\n')
80 .replace(/\x20/gi, '&nbsp;')
81 .replace(/\t/gi, '&#9;');
82
83 cell = `<td ${attrs.join(' ')}>${value}</td>`;
84 }
85 }

Callers 3

CoreFunction · 0.90
parseTable.unit.tsFile · 0.90
helpers.types.tsFile · 0.90

Calls 12

isEmptyFunction · 0.90
hasColHeadersMethod · 0.80
hasRowHeadersMethod · 0.80
getColHeaderMethod · 0.80
replaceMethod · 0.80
countRowsMethod · 0.65
countColsMethod · 0.65
getDataMethod · 0.65
getRowHeaderMethod · 0.65
getCellMetaMethod · 0.65
pushMethod · 0.45
spliceMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…