(instance: HotInstance)
| 26 | * @returns {string} OuterHTML of the HTMLTableElement. |
| 27 | */ |
| 28 | export 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('<', '<') |
| 78 | .replaceAll('>', '>') |
| 79 | .replace(/(<br(\s*|\/)>(\r\n|\n)?|\r\n|\n)/g, '<br>\r\n') |
| 80 | .replace(/\x20/gi, ' ') |
| 81 | .replace(/\t/gi, '	'); |
| 82 | |
| 83 | cell = `<td ${attrs.join(' ')}>${value}</td>`; |
| 84 | } |
| 85 | } |
no test coverage detected
searching dependent graphs…