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

Bug in RE_COMMENT results in removal of uncommented content #313

Open
mvastola opened this issue Jul 23, 2023 · 1 comment
Open

Bug in RE_COMMENT results in removal of uncommented content #313

mvastola opened this issue Jul 23, 2023 · 1 comment

Comments

@mvastola
Copy link

I just ran into an issue with this library where (what I think is) valid, uncommented HTML is being removed at

const html = context.html.replace(RE_COMMENT, '');

due to the RE_COMMENT regex here:

const RE_COMMENT = /(<!--[^[i][\S\s]+?--\s?>)/gm;

A minimal example is this HTML fragment.

<!--
--><img src="images/foo.svg"><!-- comment -->

This regex will match the entire fragment (including the uncommented <img src="..."> tag) and causes inline-source to ignore the image.

I'm having trouble groking the regex personally (as I'm not sure what the i is for, and I can't make sense of the mismatched brackets), but the problem seems to be due to the use of a newline to separate the open/close tags for the comment, without any other characters.

If I change the + into a *, the regex works for me, but -- as I'm not clear on exactly how this regex works -- I can't speak to if that creates any false positives or what the best solution is.

Unfortunately my use case ingests HTML generated by a third party library, meaning I can't easily generate the HTML without the useless comment.

Thanks for your help!

@mvastola
Copy link
Author

mvastola commented Jul 23, 2023

Oh wait. I just realized the second [ appears to be recognized in JS as a literal [. If that's the case, while I'm still not sure why the i is in the pattern, the following regex seems likely to do the trick:

/(<!--(?!=[\[i])[\S\s]+?--\s?>)/

Happy to create a PR if you'd like.

Note: I escaped the literal [ for ease of understanding above, but I imagine that can be removed if desired.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant