taoqf / node-html-parser

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

comments are ignored by insertAdjacentHTML #219

Closed milahu closed 2 years ago

milahu commented 2 years ago

options should be passed to parse

https://github.com/taoqf/node-html-parser/blob/8f4cedfb0ac1b58da4f72af2f8bb01123c119df4/src/nodes/html.ts#L801-L805

https://github.com/taoqf/node-html-parser/blob/8f4cedfb0ac1b58da4f72af2f8bb01123c119df4/src/nodes/html.ts#L329-L331

https://github.com/taoqf/node-html-parser/blob/8f4cedfb0ac1b58da4f72af2f8bb01123c119df4/src/nodes/html.ts#L351-L359

const { parse: parseHtml } = require('node-html-parser')

var insrc = `\
<html>
  <div id="unwrap-me">
    <!-- keep me -->
    <div>hello</div>
  </div>
</html>
`;
var root = parseHtml(insrc, { comment: true });
console.log(root.toString()); // ok

// unwrap content: replace node with node.innerHTML
var selector = '#unwrap-me';
root.querySelectorAll(selector).forEach(node => {

  // FIXME comments are missing in innerHTML
  let innerHTML = node.innerHTML;

  // workaround 
  //let re = RegExp(`^<${node.rawTagName}[^>]*>(.*)<\/${node.rawTagName}>$`, 's')
  //let innerHTML = node.toString().replace(re, '$1');

  node.insertAdjacentHTML('afterend', innerHTML);
  node.remove();
});
console.log(root.toString()); // comments are missing

expected

<html>

    <!-- keep me -->
    <div>hello</div>

</html>

actual

<html>

    <div>hello</div>

</html>