parse5 icon indicating copy to clipboard operation
parse5 copied to clipboard

Removing child elements with parse5-html-rewriting-stream

Open dlong500 opened this issue 4 years ago • 1 comments

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.

dlong500 avatar Jul 20 '20 23:07 dlong500

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);
}

fb55 avatar Mar 07 '22 15:03 fb55