html2json icon indicating copy to clipboard operation
html2json copied to clipboard

error when html contain any mark code

Open witwave opened this issue 7 years ago • 2 comments

when the html contain some code

<!-- this is a test mark -->

node_modules/html2json/src/html2json.js:116 if (parent.child === undefined) { ^

TypeError: Cannot read property 'child' of undefined at Object.HTMLParser.comment (/mnt/d/workspace/peach2/wevue/node_modules/html2json/src/html2json.js:116:19) at HTMLParser.HTMLParser (/mnt/d/workspace/peach2/wevue/node_modules/html2json/lib/Pure-JavaScript-HTML5-Parser/htmlparser.js:126:16)

witwave avatar Mar 30 '17 06:03 witwave

i got the same issue, any help?

vishal847 avatar Nov 20 '18 08:11 vishal847

you should remove DOCTYPE and all the Comments, then continue to convert it to JSON, at the end if you desire to write it locally

const resp = response.replace(/\<(\?xml|(\!DOCTYPE[^\>\[]+(\[[^\]]+)?))+[^>]+\>/g, '');
const rs = resp.replace(/<\!--.*?-->/g, "");
const json = html2json(rs);
const data = JSON.stringify(json, null, 2)
var filename = `output-${Date.now()}.json`;
fs.writeFileSync(filename, data);

Enigman2017 avatar Jun 28 '20 17:06 Enigman2017