Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash on svg with <use> tags referencing external svg files #425

Open
strindhaug opened this issue Aug 28, 2023 · 4 comments
Open

Crash on svg with <use> tags referencing external svg files #425

strindhaug opened this issue Aug 28, 2023 · 4 comments
Labels

Comments

@strindhaug
Copy link

If the html contains svg with an external svg-file in the use tag then the html-to-image functions crash.

Expected Behavior

I'd expect it to work without issue.

Current Behavior

The javascript crashes, with a message saying something like

Document.querySelector: '/gfx/sprites.svg?v=v123#some-icon' is not a valid selector

Possible Solution

It looks like the code in ensureSVGSymbols blindly assumes the xlink:href attribute is a valid queryselector to an svg element elsewhere in the document.
I'm pretty sure the xlink:href and href attribute is not required to be a valid queryselector since the spec says it's an URL: https://www.w3.org/TR/SVG2/struct.html#UseElementHrefAttribute
and it does work on our website.

The fact that it works when the use-tag refers to an embedded element is mostly an accident.

I'm not quite sure how to fix it so that ensureSVGSymbols would work with both files and local references;
but at least if you wrapped these two querySelector's in try/catch or verify that the "id" variable is a valid css selector before using it,
it would at least not crash, and you'll still get an image possibly missing a few icons rather than nothing.

Steps To Reproduce

Try to use any of toPng, toJpeg, toBlob, toPixelData, toSvg on html containing an svg defined something like this:

<svg viewBox="0 0 135 57"><use xlink:href="/gfx/sprites.svg?v=v123#some-icon" href="/gfx/sprites.svg?v=v123#some-icon"></use></svg>
@strindhaug strindhaug added the bug label Aug 28, 2023
@vivcat
Copy link
Contributor

vivcat bot commented Aug 28, 2023

👋 @strindhaug

Thanks for opening your first issue here! If you're reporting a 🐞 bug, please make sure you include steps to reproduce it.
To help make it easier for us to investigate your issue, please follow the contributing guidelines.

We get a lot of issues on this repo, so please be patient and we will get back to you as soon as we can.

@Tilesto
Copy link

Tilesto commented Oct 27, 2023

Absolutely the same issue. Not working with sprites
+1

@MatthiasGrandl
Copy link

+1

1 similar comment
@cgmonte
Copy link

cgmonte commented Nov 3, 2023

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants