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

HTMLRewriter doesn't remove all comments #17033

Open
Atulin opened this issue Feb 4, 2025 · 0 comments
Open

HTMLRewriter doesn't remove all comments #17033

Atulin opened this issue Feb 4, 2025 · 0 comments
Labels
bug Something isn't working HTMLRewriter cloudflare workers API

Comments

@Atulin
Copy link

Atulin commented Feb 4, 2025

What version of Bun is running?

1.2.2+c1708ea6a

What platform is your computer?

Microsoft Windows NT 10.0.26100.0 x64

What steps can reproduce the bug?

  1. Take an SVG string generated with lit-html that inserted a bunch of comments:
<!--lit-part +ZGedneDRl0=-->
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" id="spritesheet">
  <!--lit-part-->
  <!--lit-part Dn/eF33xwto=-->
  <!--lit-node 0-->
  <symbol id="lucide:accessibility" viewBox="0 0 24 24">
    <!--lit-part ZDR9ln7dX7E=-->
    <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24">
      <g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2">
        <circle cx="16" cy="4" r="1" />
        <path d="m18 19l1-7l-6 1M5 8l3-3l5.5 3l-2.36 3.5m-6.9 3a5 5 0 0 0 6.88 6" />
        <path d="M13.76 17.5a5 5 0 0 0-6.88-6" />
      </g>
    </svg>
    <!--/lit-part-->
  </symbol>
  <!--/lit-part-->
  <!--lit-part Dn/eF33xwto=-->
  <!--lit-node 0-->
  <symbol id="lucide:ban" viewBox="0 0 24 24">
    <!--lit-part XFAHU9+Up4g=-->
    <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24">
      <g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2">
        <circle cx="12" cy="12" r="10" />
        <path d="m4.9 4.9l14.2 14.2" />
      </g>
    </svg>
    <!--/lit-part-->
  </symbol>
  <!--/lit-part-->
  <!--lit-part Dn/eF33xwto=-->
  <!--lit-node 0-->
  <symbol id="lucide:fish-symbol" viewBox="0 0 24 24">
    <!--lit-part v/heyEDXlfI=-->
    <svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24">
      <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2 16s9-15 20-4C11 23 2 8 2 8" />
    </svg>
    <!--/lit-part-->
  </symbol>
  <!--/lit-part-->
  <!--/lit-part-->
</svg>
<!--/lit-part-->
  1. Process it with a rewriter to unwrap inner SVGs and remove comments:
const rewriter = new HTMLRewriter().on("*", {
	comments(comment: HTMLRewriterTypes.Comment): void | Promise<void> {
		comment.remove();
	},
	element(element: HTMLRewriterTypes.Element): void | Promise<void> {
		if (element.tagName !== "svg" || element.getAttribute("id") === "spritesheet") {
			return;
		}
		element.removeAndKeepContent();
	},
});

const sheet = rewriter.transform(generatedString);

What is the expected behavior?

No comments whatsoever:

<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" id="spritesheet">
  <symbol id="lucide:accessibility" viewBox="0 0 24 24">
      <g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2">
        <circle cx="16" cy="4" r="1" />
        <path d="m18 19l1-7l-6 1M5 8l3-3l5.5 3l-2.36 3.5m-6.9 3a5 5 0 0 0 6.88 6" />
        <path d="M13.76 17.5a5 5 0 0 0-6.88-6" />
      </g>
  </symbol>
  <symbol id="lucide:ban" viewBox="0 0 24 24">
      <g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2">
        <circle cx="12" cy="12" r="10" />
        <path d="m4.9 4.9l14.2 14.2" />
      </g>
  </symbol>
  <symbol id="lucide:fish-symbol" viewBox="0 0 24 24">
      <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2 16s9-15 20-4C11 23 2 8 2 8" />
  </symbol>
</svg>

What do you see instead?

Some comments left:

<!--lit-part +ZGedneDRl0=-->
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" id="spritesheet">
  <symbol id="lucide:accessibility" viewBox="0 0 24 24">
      <g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2">
        <circle cx="16" cy="4" r="1" />
        <path d="m18 19l1-7l-6 1M5 8l3-3l5.5 3l-2.36 3.5m-6.9 3a5 5 0 0 0 6.88 6" />
        <path d="M13.76 17.5a5 5 0 0 0-6.88-6" />
      </g>
  </symbol>
  <symbol id="lucide:ban" viewBox="0 0 24 24">
      <g fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2">
        <circle cx="12" cy="12" r="10" />
        <path d="m4.9 4.9l14.2 14.2" />
      </g>
  </symbol>
  <symbol id="lucide:fish-symbol" viewBox="0 0 24 24">
      <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M2 16s9-15 20-4C11 23 2 8 2 8" />
  </symbol>
</svg>
<!--/lit-part-->

Additional information

No response

@Atulin Atulin added bug Something isn't working needs triage labels Feb 4, 2025
@RiskyMH RiskyMH added HTMLRewriter cloudflare workers API and removed needs triage labels Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working HTMLRewriter cloudflare workers API
Projects
None yet
Development

No branches or pull requests

2 participants