hyperapp: Namespacing wrong when adding children to SVG tag
I 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>
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 19 (10 by maintainers)
@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: