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

[Feature] Add a .clone() method #188

Closed chardin1 closed 2 years ago

chardin1 commented 2 years ago

It would be very helpful to be able to clone a node. I've tried using outerHTML and then reparsing it but my particular string seems to be throwing the parser off.

Says this only has 1 child: <tr><td translate="no">outline-white</td><td translate="no">outline-color: {{color.white}};</td></tr>

nonara commented 2 years ago

Thanks for the request! I agree that clone() would be a good feature. (If anyone is interested, PRs are welcome!)

Says this only has 1 child: outline-whiteoutline-color: {{color.white}};

All HTML Input is returned in a wrapper node, which means your first node will be the first child.

let res = parse('<tr><td translate="no">outline-white</td><td translate="no">outline-color: {{color.white}};</td></tr>');
res.firstChild // this is your tr
res.firstChild.children // these are your tds

Hope that helps!

taoqf commented 2 years ago

try v5.2.2