question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to specify encoding for SVG background rendering?

See original GitHub issue

Rendering SVG node background images does work in principle (the white background and the text “Node” are rendered as SVG <rect> and <text>respectively):

screen shot 2017-07-03 at 16 57 58

https://jsfiddle.net/u7asnwdp/6/

But if a SVG <text> element contains an Umlaut (e.g. ä ö ü) SVG rendering breaks completely (here the red background is a result of Cytoscape style 'background-color': 'red'):

screen shot 2017-07-03 at 17 00 09

https://jsfiddle.net/jri_/hrxp965e/

Apparently this is an error in the Cytoscape SVG rendering code.

That pure SVG (without Cytoscape involved) has no problem with rendering Umlaute is shown here:

screen shot 2017-07-03 at 17 07 18

https://jsfiddle.net/jri_/26cr7q1e/1/

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
maxkfranzcommented, Jul 4, 2017

If it’s working in some browsers and not others, then it’s a bug in the way that the particular browser draws an svg image to canvas. Browsers seem to have differences in how they render svg to canvas as compared to how they render svg in the html dom.

A few years ago, even basic svg in the dom had many bugs and differences between the browsers. Maybe it’s a bit better now, but unreliable browser support for svg is one of the reasons cyjs uses canvas.

There’s not much that can be done on the cyjs end to remedy that. So it’s probably best to file bugs in the issue trackers for the offending browsers.

This is the most reliable way of using svg data uri images that we’ve found:

'background-image': 'data:image/svg+xml;charset=utf-8,' + encodeURIComponent(svg)

This way you can explicitly specify the encoding — in both the url and the svg itself just in case the browser ignores one — and encodeURIComponent() handles non-ascii characters. Maybe that will work better for you. Even if it does, I think it still makes sense to report bugs in browser issue trackers. The browsers shouldn’t treat proper support for European languages as an afterthought.

1reaction
jricommented, Jul 4, 2017

‘background-image’: ‘data:image/svg+xml;charset=utf-8,’ + encodeURIComponent(svg)

Indeed, not using Base64 encoding and encode with encodeURIComponent instead solves the problem!

This way it works in all my browsers (Safari, Firefox, Chrome) even without specifying charset in the data URL and without the XML declaration (<?xml version="1.0" encoding="..."?>), provided the source file contains a proper <meta charset="..."> header (in case of ISO-8859-1 even that header is not necessary).

This was a very helpful hint! Thank you very much! 😃

This issue can be closed.

You’re right, we should file browser bug reports.

Read more comments on GitHub >

github_iconTop Results From Across the Web

09: SVG with Data URIs - CSS-Tricks
If I generate an SVG icon, save the base64 encoded string and use that string in a thousand img tags, does the browser...
Read more >
SVG background in CSS without base64 encoding - Possible?
In the past, I always base64 encode the source of the SVG image, which is not needed.
Read more >
CSS: Using raw svg in the URL parameter of a background ...
... URL encoded form. i.e. I wanted to render the ▾ character ( U+25BE ). ... background-image: url("data:image/svg+xml;charset=utf8,%3Csvg ...
Read more >
Render SVG on webpage | remarkablemark
Encoding §. The one catch with data URI is that certain characters need to be encoded. For example, the following image will fail...
Read more >
Getting started - SVG: Scalable Vector Graphics | MDN
Copy the code and paste it in a file demo1.svg. Then open the file in Firefox. It will render as shown in the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found