all files / packages/table/ TableHTMLConverter.js

0% Statements 0/34
0% Branches 0/8
0% Functions 0/2
0% Lines 0/34
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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                                                                                                   
import { times } from '../../util'
 
export default {
 
  type: 'table',
  tagName: 'table',
 
  /*
    WARNING: This includes a pretty naive implementation for considering
    rowspans and colspans.
 
    TODO: Create test suite for this converter
  */
  import: function(el, node, converter) {
    let trs = el.find('tbody').getChildren()
    let colCount = 0
    let cells = []
    let rowspans = [] // we remember active rowspans here
    for (let i = 0; i < trs.length; i++) {
      let tds = trs[i].getChildren()
      let row = []
      colCount = Math.max(tds.length, colCount)
      for (let j = 0; j < tds.length; j++) {
        let td = tds[j]
        // if there is an open rowspan
        if (rowspans[j] > 1) {
          row.push(null)
          rowspans[j] -= 1 // count down until exhausted
        }
        let tableCell = converter.convertElement(td)
        row.push(tableCell.id)
        if (tableCell.rowspan > 1) {
          rowspans[j] = tableCell.rowspan
        }
        if (tableCell.colspan > 1) {
          // Add null values for colspans
          times(tableCell.colspan - 1, () => {
            row.push(null)
          })
        }
      }
      cells.push(row)
    }
    node.cells = cells
  },
 
  export: function(node, el, converter) {
    let $$ = converter.$$
    let rowCount = node.getRowCount()
    let colCount = node.getColCount()
    for (let i = 0; i < rowCount; i++) {
      let rowEl = $$('tr')
      for (let j = 0; j < colCount; j++) {
        let cellId = node.cells[i][j]
        // Merged cells (cellId is null) are skipped
        if (cellId) {
          let cellEl = converter.convertNode(cellId)
          rowEl.append(cellEl)
        }
      }
      el.append(rowEl)
    }
    return el
  }
}