Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing child elements with parse5-html-rewriting-stream #320

Open
dlong500 opened this issue Jul 20, 2020 · 1 comment
Open

Removing child elements with parse5-html-rewriting-stream #320

dlong500 opened this issue Jul 20, 2020 · 1 comment
Labels

Comments

@dlong500
Copy link

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.

@fb55
Copy link
Collaborator

fb55 commented Mar 7, 2022

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 fb55 added the question label Apr 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants