I experimented with HTML5 and SVG; and I am very new to JavaScript and web development, so something is probably missing me. I'm trying to create reusable web components using some of the new features like HTML Imports, Shadow DOM, and an extension of existing web elements. I have two html files:
test.html
<html> <head> <title>Test HTML5</title> </head> <body> <link rel="import" href="Elements/tank.html"> <svg width="100%" viewBox="0 0 500 500" style="border: solid; border-width: thin; stroke-width: 2;"> <polyline points="0,300 100,300 100,100 250,100 250,150 500,150" style="fill:none;stroke:black;stroke-width:3" onclick="window.alert('you clicked line')" /> <svg is="my-tank" x="200" y="350" width="50" height="50"></svg> </svg> </body> </html>
tank.html
<html> <body> <template id="tank"> <rect fill="red" width="50" height="50"></rect> </template> <script type="text/javascript"> var tankElement = document.currentScript.ownerDocument.querySelector('#tank'); document.registerElement('my-tank', { prototype: Object.create(SVGSVGElement.prototype, { createdCallback: { value: function () { var root = this.createShadowRoot(); </script> </body> </html>
The code under the comment //Works draws a red rectangle that I expect in the allocated SVG space. The code under //Doesn't work does not draw anything or does not give any error.
Screenshot //Works : 
Screenshot //Doesn't work : 
I tested Opera 41.0.
javascript html5 svg web-component shadow-dom
Justin huskic
source share