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

How to replace `HTMLElement`s using `exchangeChild()` #244

Closed skizzo closed 1 year ago

skizzo commented 1 year ago

Hi,

given a HTML string, I need to replace all occurences of <figure> ...

<figure><img src="https://my.image.url" /><figcaption>Some image description</figcaption></figure>

... with an actual <img>:

<img src="https://my.image.url" alt="Some image description"/>

I honestly have no idea how to do this, the docs are not very useful for anything beyond fetching HTML tags and their attributes. Would be really useful to have a working example of how to use e.g. the exchangeChild() function.

Thank you for any hints!

taoqf commented 1 year ago

You can replace img like this:

const html = `<figure><img src="https://my.image.url" /><figcaption>Some image description</figcaption></figure>`;
const root = parse(html);
const divs = root.querySelectorAll('figure');
for (const div of divs) {
    const img = div.querySelector('img');
    img.replaceWith(parse('<img src="https://my.image.url" alt="Some image description"/>'));
}