Skip to content

SVGs with a loose "&" or HTML entities convert again - #125

Merged
KnifeLemon merged 1 commit into
mainfrom
fix/svg-loose-ampersand
Oct 18, 2026
Merged

KnifeLemon merged 1 commit into
mainfrom
fix/svg-loose-ampersand

Conversation

@KnifeLemon

Copy link
Copy Markdown
Owner

Closes #120.

Generated SVGs (the report's came from Gemini) often have a loose & in a <text> ("Q&A") or HTML entities like &nbsp; and &copy;. XML doesn't allow either, and Svg.Skia's parser stopped with "An error occurred while parsing EntityName".

When an SVG can't be read, SvgRenderer.Load now repairs it with SvgXmlRepair.FixAmpersands and reads it again:

  • an & that doesn't start a character reference or a known entity becomes &amp;
  • HTML named entities become character references (&nbsp; → &#160;)
  • entities declared in the DTD (Illustrator's &ns_svg;), the DOCTYPE, comments and CDATA are left alone
  • bytes that aren't UTF-8 (another declared encoding) are left alone

Valid SVGs are never changed: the repair only runs after a failed read, and it returns nothing when there was nothing to fix.

Tests: an SVG with Q&A, &nbsp;, &copy; and an unknown entity converts to PNG. Unit tests cover the repairs and the cases left untouched.

🤖 Generated with Claude Code

Generated SVGs often have "Q&A" in a <text> or "&nbsp;"/"&copy;", which
stopped Svg.Skia's XML parser ("An error occurred while parsing
EntityName"). When an SVG can't be read, Filee escapes ampersands that
don't start a reference and turns HTML entities into character
references, then reads it again. Declared DTD entities, the DOCTYPE,
comments and CDATA are left alone; valid SVGs are never changed.

Closes #120.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@KnifeLemon
KnifeLemon merged commit 131c245 into main Oct 18, 2026
5 checks passed
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

Successfully merging this pull request may close these issues.

SVG to PNG Convert Error

1 participant