parse5
parse5 copied to clipboard
Removing child elements with parse5-html-rewriting-stream
I'm experimenting with parse5-html-rewriting-stream and not seeing an easy way to remove/delete an element along with all children. Not emitting a tag removes that particular tag from output, but the children are still included. This seems counter-intuitive to me, but maybe I'm missing something simple.
This is not something that's particularly easy to do with the rewriting stream. One approach would be:
// Remove all `article` tags and their contents
let articleCount = 0;
function emitIfOutsideArticle(_token, raw) {
if (articleCount === 0) {
rewriter.emitRaw(raw);
}
}
rewriter.on('startTag', (tag, raw) => {
if (tag.tagName === 'article') {
articleCount += 1;
} else {
rewriter.emitRaw(raw);
}
});
rewriter.on('endTag', (tag, raw) => {
if (articleCount > 0 && tag.tagName === 'article') {
articleCount -= 1;
}
emitIfOutsideArticle(tag, raw)
});
for (let eventName of ['text', 'doctype', 'comment']) {
rewriter.on(eventName, emitIfOutsideArticle);
}