Namespacing wrong when adding children to SVG tag
See original GitHub issueI have an app which makes random svg circles (test case for a bigger project). Initially there are three circles, but a button lets you add more.
The new circle elements are added, but with the wrong namespace: xhtml rather than svg (tested in Chrome 59.0.3071.115). This can be seen by opening the developer tools, going to elements, and then clicking “Add Circle”. A new circle tag is added but the namespace URI is wrong, so svg ignores it and it is not rendered.
The “Move” button is not part of the bug report; it was added to see if altering attributes (position) works - it does.
The code is below:
[Note also usage of Object.assign in h( ) call - if I just pass this.attr, the change in attributes isn’t picked up]
<script src="https://unpkg.com/hyperapp"></script>
<body></body>
<script>
var h = hyperapp.h;
var app = hyperapp.app;
function randInt(a,b) { // random integer in [a,b]
return Math.floor(Math.random()*(b+1-a))+a;
}
// random circle object
function Circle() {
this.attr = {
cx: randInt(1,400),
cy: randInt(1,400),
r: randInt(10,30)
}
}
Circle.prototype = {
h: function() {
// a change in attributes isn't picked up unless Object.assign is used!
return h("circle", Object.assign({}, this.attr))
},
move: function() {
this.attr.cx = randInt(1,400);
this.attr.cy = randInt(1,400);
return this;
}
}
// the app
app({
state: { // just a list of objects
objects: [],
},
view: function v(state, actions) {
return h("div",{},[
// buttons
h("button", {onclick: actions.addObj}, 'Add circle'),
h("button", {onclick: actions.move}, 'Move'),
h("hr"),
// svg area
h("svg", {width:400, height:400, style:{backgroundColor: 'lavender'}},
state.objects.map(obj => obj.h())
)
])
},
actions: {
addObj: function(state) {
// add a new object
return {
objects: state.objects.concat(new Circle())
}
},
move: function(state) {
// moves all the objects
return {
objects: state.objects.map(obj => obj.move())
}
}
},
events: {
init: function(state, actions) {
// put 3 random objects in the state
actions.addObj(state)
actions.addObj(state)
actions.addObj(state)
}
}
})
</script>
Issue Analytics
- State:
- Created 6 years ago
- Comments:19 (10 by maintainers)
Top Results From Across the Web
Namespaces crash course - SVG: Scalable Vector Graphics
This parameter says that the <svg> element and its child elements belong to whichever XML dialect has the namespace name http://www.w3.org/2000/ ...
Read more >Document Structure — SVG 2
An SVG document fragment can consist of a stand-alone SVG document, or a fragment of a parent document enclosed by an 'svg' element....
Read more >HTML 5, inline SVG, and namespace awareness for SVG DOM
XHTML is namespace aware and XHTML documents apply namespaces according to the rules of XML, so all namespaces must be assigned to each...
Read more >Webstorm not recognizing namespaced svg elements in inline ...
One solution is to wrap all templates in 'svg:g' elements and leave children without namespaces, but that is not ideal. I found other...
Read more >Docs • Svelte
Readonly props can be accessed as properties on the element, tied to the ... This works by adding a class to affected elements,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@w-mcilhagga Thanks! I’ll refactor this patch a bit and create a commit soon with other small changes I am currently working on (unless you really want to send me a PR 😏).
Anyway, I’ll make sure to thoroughly thank you in the commit message!
Good job! Also got the SVG fractal example to finally work too! 😅 🎉
OK, patched the patch function. This works: