taoqf / node-html-parser

A very fast HTML parser, generating a simplified DOM, with basic element query support.
MIT License
1.12k stars 112 forks source link

TypeError: html.replace is not a function after change in html.ts to fix issue #200 #214

Closed sergiolindau closed 1 year ago

sergiolindau commented 2 years ago

I'm using node-html-parser to filter information from

http://www.geonames.org/export/codes.html

It's working before last modifications in file src/nodes/html.ts

After the change in src/nodes/html.ts in Commits on Aug 10, 2022 to fix issue #200 I'm experiencing error with this code fragment (typescript):

    const root = parser.parse(await download('http://www.geonames.org/export/codes.html') as string)
    const table = root.querySelector('table.restable') as unknown as HTMLTableElement

In the second line it throws:

TypeError: html.replace is not a function at Object.decode (C:\Users\sergio\metasoftmining\node_modules\he\he.js:232:15) at decode (C:\Users\sergio\metasoftmining\node_modules\node-html-parser\dist\nodes\html.js:53:51) at HTMLElement.get (C:\Users\sergio\metasoftmining\node_modules\node-html-parser\dist\nodes\html.js:681:50) at HTMLElement.getAttribute (C:\Users\sergio\metasoftmining\node_modules\node-html-parser\dist\nodes\html.js:759:21) at Object.getAttributeValue (C:\Users\sergio\metasoftmining\node_modules\node-html-parser\dist\matcher.js:11:31) at element (C:\Users\sergio\metasoftmining\node_modules\css-select\lib\attributes.js:129:32) at tag (C:\Users\sergio\metasoftmining\node_modules\css-select\lib\general.js:41:60) at findOne (C:\Users\sergio\metasoftmining\node_modules\node-html-parser\dist\matcher.js:68:13) at findOne (C:\Users\sergio\metasoftmining\node_modules\node-html-parser\dist\matcher.js:74:24) at Object.findOne (C:\Users\sergio\metasoftmining\node_modules\node-html-parser\dist\matcher.js:74:24) [ERROR] 20:18:17 TypeError: html.replace is not a function

Without much analysis I found the error point by stack and replace the line in dist/node/html.js in my node_modules folder:

681 this._attrs[key.toLowerCase()] = decode(val);

to

681 this._attrs[key.toLowerCase()] = decode(val.toString());

And the error condition disapear.

taoqf commented 1 year ago

Reopen this issue if the issue stills on your side.