Skip to content

Commit

Permalink
Fix autoTableHtmlToJson th parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
simonbengtsson committed Apr 30, 2020
1 parent 75ae202 commit cdb7608
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 50 deletions.
12 changes: 3 additions & 9 deletions src/applyPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { parseHtml } from './htmlParser'
import autoTableText, { TextStyles } from './autoTableText'
import { DocHandler, jsPDFConstructor, jsPDFDocument } from './documentHandler'
import { UserOptions } from './config'
import { createTable } from './inputParser'
import { createTable, getColumnDef } from './inputParser'
import { drawTable } from './tableDrawer'

export default function (jsPDF: jsPDFConstructor) {
Expand Down Expand Up @@ -54,11 +54,6 @@ export default function (jsPDF: jsPDFConstructor) {
return null
}

if (!tableElem || !(tableElem instanceof HTMLTableElement)) {
console.error('An HTMLTableElement has to be sent to autoTableHtmlToJson')
return null
}

const doc = new DocHandler(this)
const { head, body, foot } = parseHtml(
doc,
Expand All @@ -67,9 +62,8 @@ export default function (jsPDF: jsPDFConstructor) {
includeHiddenElements,
false
)
const firstRow = head[0] || body[0] || foot[0]

return { columns: firstRow, rows: body, data: body }
const columns = getColumnDef(head, body, foot)
return { columns, rows: body, data: body }
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ export type ColumnInput =
| string
| number
| {
header?: string
title?: string // deprecated (same as header)
footer?: string
header?: CellInput
title?: CellInput // deprecated (same as header)
footer?: CellInput
dataKey?: string | number
key?: string | number // deprecated (same as dataKey)
}
Expand Down
72 changes: 36 additions & 36 deletions src/inputParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ function parseContent(
}
}

const columns = createColumns(options, head, body, foot)
const columnInputs = options.columns || getColumnDef(head, body, foot)
const columns = createColumns(columnInputs)

// If no head or foot is set, try generating it with content from columns
if (head.length === 0 && options.columns) {
Expand Down Expand Up @@ -416,50 +417,49 @@ function getSectionTitle(section: Section, column: ColumnInput) {
return null
}

function createColumns(
settings: UserOptions,
export function getColumnDef(
head: RowInput[],
body: RowInput[],
foot: RowInput[]
) {
if (settings.columns) {
return settings.columns.map((input, index) => {
let key
if (typeof input === 'object') {
key = input.dataKey ?? input.key ?? index
const firstRow: RowInput = head[0] || body[0] || foot[0] || []
const result: ColumnInput[] = []
Object.keys(firstRow)
.filter((key) => key !== '_element')
.forEach((key) => {
let colSpan = 1
let input: CellInput
if (Array.isArray(firstRow)) {
input = firstRow[parseInt(key)]
} else {
key = index
input = firstRow[key]
}
return new Column(key, input, index)
})
} else {
const firstRow: RowInput = head[0] || body[0] || foot[0] || []
const columns: Column[] = []
Object.keys(firstRow)
.filter((key) => key !== '_element')
.forEach((key) => {
let colSpan = 1
let input: CellInput
if (typeof input === 'object' && !Array.isArray(input)) {
colSpan = input?.colSpan || 1
}
for (let i = 0; i < colSpan; i++) {
let id
if (Array.isArray(firstRow)) {
input = firstRow[parseInt(key)]
id = result.length
} else {
input = firstRow[key]
}
if (typeof input === 'object' && !Array.isArray(input)) {
colSpan = input?.colSpan || 1
id = key + (i > 0 ? `_${i}` : '')
}
for (let i = 0; i < colSpan; i++) {
let id
if (Array.isArray(firstRow)) {
id = columns.length
} else {
id = key + (i > 0 ? `_${i}` : '')
}
columns.push(new Column(id, null, columns.length))
}
})
return columns
}
result.push({ dataKey: id, header: input })
}
})
return result
}

function createColumns(columns: ColumnInput[]) {
return columns.map((input, index) => {
let key
if (typeof input === 'object') {
key = input.dataKey ?? input.key ?? index
} else {
key = index
}
return new Column(key, input, index)
})
}

function cellStyles(
Expand Down
4 changes: 2 additions & 2 deletions test/testHtmlParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ describe('html parser', () => {
assert(res.foot.length === 0, 'Should have no foot cells')
})

it.only('autoTableHtmlToJson', () => {
it('autoTableHtmlToJson', () => {
;(global as any).window = dom.window
;(global as any).HTMLTableElement = dom.window.HTMLTableElement
const table = dom.window.document.createElement('table')
Expand All @@ -91,6 +91,6 @@ describe('html parser', () => {
assert.equal(res.data[0].length, 2, 'Should have body cell')
assert.equal(res.columns.length, 2, 'Should have columns cell')
assert.equal(res.data[0][0].content, 'body', 'Should have body content')
assert.equal(res.columns[0].content, 'head', 'Should have head content')
assert.equal(res.columns[0].header.content, 'head', 'Should have head content')
})
})

0 comments on commit cdb7608

Please sign in to comment.