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.

Make `url()` wrapping more lenient in `background-image` -- even though not officially supported

See original GitHub issue

I’ve found an issue using a data URI in the style for a node.

I’m trying to use embedded/inline SVG content. If I base64 encode it, there is no issue, e.g. style:{'background-image': 'url(data:image/svg+xml;base64, ...long base64 string of SVG...)'} This works OK

However SVG is a format that is better off not being base64 encoded (it actually ends up larger) so when I try this style:{'background-image': 'url(data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" width="50" height="50" viewBox="0 0 50 50"> ....blah rest of svg file here.... </svg>)'} This results in cytoscape.js treating it as an external URL and attempts to make HTTP GET for a very strange URL, which obviously fails.

Omitting the ;utf8 part or trying ;charset=utf-8 has no effect.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
maxkfranzcommented, Sep 1, 2016

I looked at the documentation (I should have done this from the start), and I was mistaken: Nowhere is url() wrapping mentioned, and it’s unsupported. It’s only a coincidence that some url() wrapping works, probably because it was under consideration in some very early version.

I’ve added a patch that makes the check a bit more lenient, but you really ought to use just the URL in the interest of future compatibility.

Without url() wrapping, both your SVG and my own test image work fine.

0reactions
benc-ukcommented, Sep 1, 2016

I tried encoding the SVG data and it still doesn’t work in Cytoscape, it still thinks it is an external URL and attempts to fetch it over HTTP

style:{'background-image': "url('data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20height%3D%2250%22%20width%3D%2250%22%20viewBox%3D%220%200%2050%2050%22%3E%3Cpath%20fill%3D%22%233999C6%22%20d%3D%22M49.7%2025.7c.5-.5.4-1.3%200-1.8l-2.4-2.4L36.5%2011c-.5-.5-1.2-.5-1.7%200s-.6%201.3%200%201.8l11.3%2011.1c.5.5.5%201.3%200%201.8L34.6%2037.2c-.5.5-.5%201.3%200%201.8s1.3.4%201.7%200L47%2028.4l.1-.1%202.6-2.6zM.3%2025.7c-.5-.5-.4-1.3%200-1.8l2.4-2.4L13.5%2011c.5-.5%201.2-.5%201.7%200s.6%201.3%200%201.8L4.1%2023.9c-.5.5-.5%201.3%200%201.8l11.3%2011.5c.5.5.5%201.3%200%201.8s-1.3.4-1.7%200L2.8%2028.5l-.1-.1-2.4-2.7z%22%2F%3E%3Cpath%20fill%3D%22%237FBA00%22%20d%3D%22M18.2%2024.8c0%201.9-1.6%203.3-3.3%203.3s-3.5-1.6-3.5-3.3%201.4-3.3%203.5-3.3c2%200%203.3%201.6%203.3%203.3zM28.3%2024.8c0%201.9-1.6%203.3-3.3%203.3s-3.5-1.6-3.5-3.3%201.6-3.3%203.5-3.3%203.3%201.6%203.3%203.3z%22%2F%3E%3Ccircle%20fill%3D%22%237FBA00%22%20cx%3D%2235.2%22%20cy%3D%2224.8%22%20r%3D%223.3%22%2F%3E%3C%2Fsvg%3E')"}

Both the encoded and unencoded SVG data string work when I try <img src='data:image/svg+xml,{svg_data_string}'

Read more comments on GitHub >

github_iconTop Results From Across the Web

HTML: Page background image link not working due to wrapper
I have a html page with a full background image within the body tag, followed by a wrapper: <body> <a href="#" id="bkimage">Background Image</a> ......
Read more >
Interpolation - Sass
Interpolation can be used almost anywhere in a Sass stylesheet to embed the result of a SassScript expression into a chunk of CSS....
Read more >
<color> - CSS: Cascading Style Sheets - MDN Web Docs
The CSS data type represents a color. A may also include an alpha-channel transparency value, indicating how the color should composite with ......
Read more >
How to Add an Image URL to Your Div - freeCodeCamp
It's easy to add an image using the background-image property. Just provide the path to the image in the url() value. The image...
Read more >
DOM Elements - React
To specify a CSS class, use the className attribute. This applies to all regular DOM and SVG elements like <div> , <a> ,...
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