-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Error when using x-for
to generate SVG elements
#637
Comments
FWIW I suspect this is not an Alpine issue - if you are using HTML tags (like Edit: Sounds like Polymer ran into something similar and there is now a WHATWG feature request open to provide support: whatwg/html#3563 - not much help in the short term though. |
Yeah, it's a DOM issue. templates inside an svg tag are not implemented and they miss the content property (you can see it using the inspector without alpine). You need to include a polyfil like the one below (adapted from one mentioned on the polymer issue)
|
@SimoTod That polyfill seems to be working (tested in Firefox and Chromium). Thanks a lot 🙂 |
No worries. Make sure you test it on all browsers you support (IE11, old edge, Safari, etc). It should work but I did not check. 👍 |
This closes alpinejs#637. Co-authored-by: Simone Todaro <[email protected]>
This closes alpinejs#637. Co-authored-by: Simone Todaro <[email protected]>
This closes alpinejs#637. Co-authored-by: Simone Todaro <[email protected]>
I'm curious why |
Yes, Alpine.js doesn't use a virtual DOM which means you can only use |
Hmmm I don't think it's strictly VDOM vs not, I think it's a trade-off (admittedly partly due to no VDOM). You probably could have an |
Edit: sorry, you can ignore this comment. I was applying the polyfill too early. Here's an updated example that works: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rectangle Editor</title>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/alpine.js"></script>
</head>
<body>
<div x-data="rectangleEditor()">
<svg width="1024" height="1024">
<!-- Error -->
<template x-for="rectangle in rectangles" :key="rectangle">
<rect :x="rectangle.x" :y="rectangle.y" :width="rectangle.width" :height="rectangle.height" />
</template>
</svg>
<!-- No error -->
<template x-for="rectangle in rectangles" :key="rectangle">
<div :x="rectangle.x" :y="rectangle.y" :width="rectangle.width" :height="rectangle.height" />
</template>
</div>
<script>
function rectangleEditor() {
return {
rectangles: [
{ x: 20, y: 40, width: 200, height: 400 },
{ x: 60, y: 100, width: 500, height: 500 },
{ x: 200, y: 40, width: 100, height: 400 },
{ x: 300, y: 40, width: 100, height: 400 },
],
}
};
(function(){
var templates = document.querySelectorAll('svg template');
var el, template, attribs, attrib, count, child, content;
for (var i=0; i<templates.length; i++) {
el = templates[i];
template = el.ownerDocument.createElement('template');
el.parentNode.insertBefore(template, el);
attribs = el.attributes;
count = attribs.length;
while (count-- > 0) {
attrib = attribs[count];
template.setAttribute(attrib.name, attrib.value);
el.removeAttribute(attrib.name);
}
el.parentNode.removeChild(el);
content = template.content;
while ((child = el.firstChild)) {
content.appendChild(child);
}
}
})();
</script>
</body>
</html> For posterity, here's my original comment:
Can anyone supply a small example page using `x-for` with `svg` and the latest Alpine? With the polyfill above, I still get errors.
This is what I'm trying at the moment: <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Rectangle Editor</title>
<script defer src="https://cdn.jsdelivr.net/npm/[email protected]/dist/alpine.js"></script>
<script>
(function(){
var templates = document.querySelectorAll('svg template');
var el, template, attribs, attrib, count, child, content;
for (var i=0; i<templates.length; i++) {
el = templates[i];
template = el.ownerDocument.createElement('template');
el.parentNode.insertBefore(template, el);
attribs = el.attributes;
count = attribs.length;
while (count-- > 0) {
attrib = attribs[count];
template.setAttribute(attrib.name, attrib.value);
el.removeAttribute(attrib.name);
}
el.parentNode.removeChild(el);
content = template.content;
while ((child = el.firstChild)) {
content.appendChild(child);
}
}
})();
</script>
</head>
<body>
<div x-data="rectangleEditor()">
<svg width="1024" height="1024">
<!-- Error -->
<template x-for="rectangle in rectangles" :key="rectangle">
<rect :x="rectangle.x" :y="rectangle.y" :width="rectangle.width" :height="rectangle.height" />
</template>
</svg>
<!-- No error -->
<template x-for="rectangle in rectangles" :key="rectangle">
<div :x="rectangle.x" :y="rectangle.y" :width="rectangle.width" :height="rectangle.height" />
</template>
</div>
<script>
function rectangleEditor() {
return {
rectangles: [
{ x: 20, y: 40, width: 200, height: 400 },
{ x: 60, y: 100, width: 500, height: 500 },
{ x: 200, y: 40, width: 100, height: 400 },
{ x: 300, y: 40, width: 100, height: 400 },
],
}
};
</script>
</body>
</html> |
Alpine version: 2.4.1
I'm getting an error when using
x-for
on atemplate
tag to generate SVG elements. With the following HTML:I don't get such an error when using
x-for
outside asvg
element. But as soon as I add elements in asvg
element usingx-for
(even standard HTML elements such asdiv
), I get the following stack trace:Chromium 81:
Firefox 78:
If this isn't supported, then this should be documented in the README. (I'm willing to open a PR for that if this is the case.)
The text was updated successfully, but these errors were encountered: