T
T
thehighhomie2020-04-26 17:42:13
JavaScript
thehighhomie, 2020-04-26 17:42:13

SVG string to createObjectURL?

When trying to create a blobUrl from a string, an error appears that swears at ampersands in the href of the image.

This is how I want to pass the svg string to the createObjectURL method:

const svgString = `
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="1920" height="1080" viewBox="0 0 1920 1080" xml:space="preserve">
<desc>Created with Fabric.js 3.6.2</desc>
<defs>
</defs>
<rect x="0" y="0" width="100%" height="100%" fill="#151616"></rect>
<g transform="matrix(1 0 0 1 960 540)"  >
<rect style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(255,255,255); fill-rule: nonzero; opacity: 1;" vector-effect="non-scaling-stroke"  x="-960" y="-540" rx="0" ry="0" width="1920" height="1080" />
</g>
<g transform="matrix(0.54 0 0 0.54 894.3 440)"  >
  <image style="stroke: none; stroke-width: 0; stroke-dasharray: none; stroke-linecap: butt; stroke-dashoffset: 0; stroke-linejoin: miter; stroke-miterlimit: 4; fill: rgb(0,0,0); fill-rule: nonzero; opacity: 1;" vector-effect="non-scaling-stroke"  xlink:href="https://images.unsplash.com/photo-1587839443822-98fc3e351c22?ixlib=rb-1.2.1&q=80&fm=jpg&crop=entropy&cs=tinysrgb&w=1080&fit=max&ixid=eyJhcHBfaWQiOjIzOTk3fQ" x="-540" y="-810" width="1080" height="1620"></image>
</g>
</svg>
`

const blob = new Blob([svgString], { type: 'image/svg+xml' })
const url = URL.createObjectURL(blob)


when such a url is set in the src of the img tag, then the icon of the broken image appears in the browser near the img tag, and when I open the blobUrl, it can be seen that the parser swears and swears at the ampersands in the xlink: href of the image.

ok, i change everything & to '&' when i open the blobUrl, there is no error in the image itself, but the image in which the link with ampersands already has a broken image icon, and other elements are displayed normally in the image. Moreover, if I remove all the get parameters, the picture will be displayed normally, but the get parameters are needed, because I took unsplash as an example, I get additional image settings for the get parameters,

in general, replacing ampersands & with '&' parsing errors disappear but blubUrl still not working...

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question