Pikchr

Invalid SVG for lone Ampersand
Login

Invalid SVG for lone Ampersand

(1) By Venks I (venksi) on 2021-12-24 03:56:03 [source]

I hit a problem with the generated SVG not being readable. Here's a small example:

Input:

box "A" "&" "B"

Output:

<svg xmlns='http://www.w3.org/2000/svg' class="pikchr" viewBox="0 0 112.32 76.32">
<style type="text/css">text{font-size:11pt;font-family:Helvetica}</style>
<path d="M2,74L110,74L110,2L2,2Z"  style="fill:none;stroke-width:2.16;stroke:rgb(0,0,0);" />
<text x="56" y="18" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">A</text>
<text x="56" y="38" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">&</text>
<text x="56" y="58" text-anchor="middle" fill="rgb(0,0,0)" dominant-baseline="central">B</text>
</svg>

I think the lone & in the 3rd last line should've been converted to &amp; At least one of the readers I have dies on this svg.

I have seen other entities being generated correctly, so this seems to be some corner case.

Thanks

Venks

(2) By Stephan Beal (stephan) on 2021-12-24 04:10:38 in reply to 1 [link] [source]

I think the lone & in the 3rd last line should've been converted to & At least one of the readers I have dies on this svg.

i've patched this in the current trunk and hopefully haven't introduced undesired side effects by doing so. Please try it out and let us know if it works for you. Your sample input behaves as expected for me with this patch, escaping the ampersand.

(3) By Venks I (venksi) on 2021-12-24 05:33:18 in reply to 2 [link] [source]

Thanks for the quick turnaround Stephan. Works fine for me now.

(4) By Stephan Beal (stephan) on 2022-06-14 09:17:18 in reply to 2 [link] [source]

i've patched this in the current trunk and hopefully haven't introduced undesired side effects by doing so. Please try it out and let us know if it works for you. Your sample input behaves as expected for me with this patch, escaping the ampersand

It turns out that this had side effects which have apparently gone unnoticed so far: several of the pikchr tutorial docs use &deg; and all of them show that now as &deg; instead of the degree symbol (°).

How best to resolve this is unclear. If we remove escaping of the ampersand, &deg; will work again but a standalone ampersand will generate invalid SVG. Of course, we can parse an ampersand followed by a few characters, followed by a semicolon, but that feels like the wrong approach.

(5) By Stephan Beal (stephan) on 2022-06-14 14:37:08 in reply to 4 [link] [source]

It turns out that this had side effects which have apparently gone unnoticed so far:

This is now fixed in /info/43eb7d998415bf72 and the previous checkin which changed the &deg; to ° has been reverted. The new ampersand policy is: if the construct looks like it's an HTML entity, leave it as-is under the assumption that the user is trying to use something like &deg; or &copy;, else transform & to &amp;.

(6) By Venks I (venksi) on 2022-06-14 16:20:17 in reply to 5 [link] [source]

Thanks for following up in the same thread.

Based on https://www.freeformatter.com/html-entities.html, there are entities like ² (&sup2;) and ½ (&frac12;) .., which still may not work - based on my cursory look at your code and comment.

(7) By Stephan Beal (stephan) on 2022-06-14 17:26:05 in reply to 6 [link] [source]

there are entities like ² (²) and ½ (½)

Indeed, numbers are not permitted in my patch except in &#nnn; form. Look for a patch to patch that real soon now...