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

Not rendering multiple SVG elements #130

Closed
antstanley opened this issue Dec 6, 2016 · 3 comments · Fixed by #131
Closed

Not rendering multiple SVG elements #130

antstanley opened this issue Dec 6, 2016 · 3 comments · Fixed by #131

Comments

@antstanley
Copy link

When I have multiple svg elements in a component it is only rendering the first element correctly, but subsequent elements are not rendered. Looked into the compiled code, and it uses createElementNS as shown below to create the first svg element

var svg = document.createElementNS( 'http://www.w3.org/2000/svg', 'svg' );

but subsequent svg's don't use the createElementNS and just use createElement without specifying the svg schema like below.

var svg2 = document.createElement( 'svg' );

All nested elements below the svg elements will follow the convention of the parent element.

@antstanley
Copy link
Author

Ok... found the problem! It's in https://github.com/sveltejs/svelte/blob/master/compiler/generate/visitors/Element.js

The piece of code that evaluates whether the element is an svg evaluates on the exact match on the string 'svg'. The first svg element passed to this function will have the name 'svg', but subsequent svg elements will be numbered. ie 'svg2', 'svg3'. So the subsequent elements fail the test as they do not match 'svg' exactly.

The following change is needed to fix it.

Original

const local = { name, namespace: name === 'svg' ? 'http://www.w3.org/2000/svg' : generator.current.namespace,

new code

const local = { name, namespace: name.substring(0,3) === 'svg' ? 'http://www.w3.org/2000/svg' : generator.current.namespace,

Essentially changing the evaluation to the first three letters of the element name, and not the whole name.

Rich-Harris added a commit that referenced this issue Dec 6, 2016
Rich-Harris added a commit that referenced this issue Dec 6, 2016
@Rich-Harris
Copy link
Member

Thanks! Fixed in 1.1.1

@antstanley
Copy link
Author

Ha! Just submitted a pull request to fix this.. will cancel it! You're too quick! Thank you!

@Ryuno-Ki
Copy link

Ryuno-Ki commented Dec 6, 2016

Bear in mind, that this should affect other SVG elements like rect or g as well. Those need to be created with createElementNS as well (learned that at this year's js13kgames :-))

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

Successfully merging a pull request may close this issue.

3 participants