Skip to content

Commit 6194594

Browse files
committed
used docpa to parse and traverse
1 parent 610aab8 commit 6194594

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jsonfromtable",
3-
"version": "3.0.0",
3+
"version": "3.1.0",
44
"description": "Convert html tables to object (or array)",
55
"main": "dist/index.js",
66
"scripts": {
@@ -37,9 +37,7 @@
3737
},
3838
"homepage": "https://github.com/coderosh/jsonfromtable#readme",
3939
"dependencies": {
40-
"css-select": "^4.2.0",
41-
"dom-serializer": "^1.3.2",
42-
"htmlparser2": "^7.2.0",
40+
"docpa": "^1.0.0",
4341
"node-fetch": "^2.6.1"
4442
},
4543
"gitHooks": {

src/utils.ts

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
1-
import render from 'dom-serializer'
2-
import { selectAll, selectOne } from 'css-select'
3-
import { DomUtils, parseDocument } from 'htmlparser2'
4-
5-
const { getAttributeValue, textContent } = DomUtils
1+
import Docpa from 'docpa'
62

73
function getRowWithColumns(
8-
tableDoc: ReturnType<typeof parseDocument>,
4+
tableDoc: Docpa,
95
selectors: [string, string],
106
shouldBeText: boolean,
117
trim: boolean
128
) {
139
const rowSelector = selectors[0] || 'tr'
1410
const colSelector = selectors[1] || 'td,th'
1511

16-
return selectAll(rowSelector, tableDoc).map((tr) =>
17-
selectAll(colSelector, tr).map((td) => {
18-
const rowspan = (td && +getAttributeValue(td as any, 'rowspan')!) || 1
19-
const colspan = (td && +getAttributeValue(td as any, 'colspan')!) || 1
20-
const value =
21-
(td && (shouldBeText ? textContent(td) : render(td.children))) || ''
12+
return tableDoc.querySelectorAll(rowSelector).map((tr) => {
13+
if (!tr) return []
14+
return tr.querySelectorAll(colSelector).map((td) => {
15+
if (!td) return { value: '', colspan: 1, rowspan: 1 }
16+
17+
const rowspan = +(td.getAttribute('rowspan') || 1) || 1
18+
const colspan = +(td.getAttribute('colspan') || 1) || 1
19+
20+
const value = (shouldBeText ? td.textContent : td.innerHTML) || ''
21+
2222
return { value: trim ? value.trim() : value, colspan, rowspan }
2323
})
24-
)
24+
})
2525
}
2626

2727
interface ParseTableOptions {
@@ -39,8 +39,8 @@ function parseTable(html: string, options: ParseTableOptions) {
3939
trim = true,
4040
} = options
4141

42-
const document = parseDocument(html)
43-
const table = selectOne(tableSelector, document)
42+
const document = new Docpa(html)
43+
const table = document.querySelector(tableSelector)
4444

4545
if (!table) throw new Error(`${tableSelector} not found in document.`)
4646

yarn.lock

+13-4
Original file line numberDiff line numberDiff line change
@@ -1247,10 +1247,10 @@ cross-spawn@^7.0.2, cross-spawn@^7.0.3:
12471247
shebang-command "^2.0.0"
12481248
which "^2.0.1"
12491249

1250-
css-select@^4.2.0:
1251-
version "4.2.0"
1252-
resolved "https://registry.yarnpkg.com/css-select/-/css-select-4.2.0.tgz#ab28276d3afb00cc05e818bd33eb030f14f57895"
1253-
integrity sha512-6YVG6hsH9yIb/si3Th/is8Pex7qnVHO6t7q7U6TIUnkQASGbS8tnUDBftnPynLNnuUl/r2+PTd0ekiiq7R0zJw==
1250+
css-select@^4.2.1:
1251+
version "4.2.1"
1252+
resolved "https://registry.yarnpkg.com/css-select/-/css-select-4.2.1.tgz#9e665d6ae4c7f9d65dbe69d0316e3221fb274cdd"
1253+
integrity sha512-/aUslKhzkTNCQUB2qTX84lVmfia9NyjP3WpDGtj/WxhwBzWBYUV3DgUpurHTme8UTPcPlAD1DJ+b0nN/t50zDQ==
12541254
dependencies:
12551255
boolbase "^1.0.0"
12561256
css-what "^5.1.0"
@@ -1364,6 +1364,15 @@ dir-glob@^3.0.1:
13641364
dependencies:
13651365
path-type "^4.0.0"
13661366

1367+
docpa@^1.0.0:
1368+
version "1.0.0"
1369+
resolved "https://registry.yarnpkg.com/docpa/-/docpa-1.0.0.tgz#98a27c64f30777ac169e494f271b12bf55b3bbde"
1370+
integrity sha512-v1aSAg6uLEAES4IIuFqQxl7+DrCpWkBf/8IJJo0vSU7FLMU6lTSzvizPvx0TSFgmsu93klxGDGlRIBKkJ4JDxA==
1371+
dependencies:
1372+
css-select "^4.2.1"
1373+
dom-serializer "^1.3.2"
1374+
htmlparser2 "^7.2.0"
1375+
13671376
doctrine@^2.1.0:
13681377
version "2.1.0"
13691378
resolved "https://registry.yarnpkg.com/doctrine/-/doctrine-2.1.0.tgz#5cd01fc101621b42c4cd7f5d1a66243716d3f39d"

0 commit comments

Comments
 (0)