polymer icon indicating copy to clipboard operation
polymer copied to clipboard

template doesn't work inside svg elements

Open bendavis78 opened this issue 11 years ago • 41 comments

For example:

    <dom-module id="test-element">
      <template>
        <svg class="clock" version="1.1" xmlns="http://www.w3.org/2000/svg">
          <template is="dom-repeat" items="{{positions}}" as="cx">
            <ellipse r="25" cx$="{{cx}}" cy="25" stroke="black"></ellipse> 
          </template>
        </svg>
      </template>
      <script>
        Polymer({
          is: 'test-element',
          ready: function() {
            this.positions = [0, 100, 200, 300];
          },
        });
      </script>
    </dom-module>

This produces the following JS error:

Uncaught TypeError: Cannot read property 'nodeType' of undefined

The problem seems to be that the dom template object doesn't have a content property. This worked fine in Polymer 0.5.

Demo here: http://jsbin.com/wiqowo/31/edit?html,output

bendavis78 avatar Jun 28 '15 01:06 bendavis78

It looks like some code didn't make it over from 0.5 that explicitly addressed this issue. Template elements with a <svg> parent have the same namespaceURI (http://www.w3.org/2000/svg), so the browser doesn't implement HTMLTemplateElement. The bootstrap code in 0.5 basically transplanted all child nodes of any svg-namespaced template into a new html-namespaced template, and then inserted that node back in its place.

If anyone else is looking for a workaround, you can put this snippet at the top of your element's <script> tag (it must be evaluated before call to Polymer()). It basically performs the same operation as it was done it 0.5. note: this snippet only works in chrome atm -- not sure yet how to get firefox working

bendavis78 avatar Jun 29 '15 00:06 bendavis78

We also hit this issue, thanks for the workaround @bendavis78 .

Will it be fixed in Polymer?

nomego avatar Jun 29 '15 13:06 nomego

@bendavis78 your snippet was a day saviour for me but it still has some problem. If you have more than one polymer element on the page and this svg element is not the first one the snippet breaks. This line:

var root = doc.querySelector('dom-module > template').content;

should be changed so dom-module becomes the actual #id-of-svg-element which is put in the <dom-module id="id-of-svg-element">

canufeel avatar Jul 09 '15 06:07 canufeel

+1

kevin-smets avatar Nov 12 '15 07:11 kevin-smets

+1

shmendo avatar Dec 04 '15 04:12 shmendo

Same error in Chrome 49, even with the tweaked version of svg-template-support.js, and also: svg-template-support.js:4 Uncaught TypeError: Cannot read property 'content' of null

ocampesato avatar Dec 25 '15 11:12 ocampesato

+1 same error in latest chrome

raghav135 avatar Dec 30 '15 20:12 raghav135

+1

brian-cruickshank avatar Jan 05 '16 18:01 brian-cruickshank

+1

RossRothenstine avatar Jan 06 '16 23:01 RossRothenstine

Thanks for the workaround - would be really nice to have templates within SVG work natively !

christophe-g avatar Jan 18 '16 10:01 christophe-g

Does anyone know of a way to implement the workaround while still vulcanizing elements?

joshuahiggins avatar Jan 29 '16 23:01 joshuahiggins

https://github.com/Polymer/polymer/pull/3372 C.

christophe-g avatar Feb 04 '16 16:02 christophe-g

Note that the June 28, 2015 workaround does not work in Firefox 45.0.2 - it will actually prevent any of the svg from being rendered.

brian-cruickshank avatar Apr 19 '16 20:04 brian-cruickshank

Ran into this issue today. Any word on Polymer 1.0 support?

boazhachlili avatar Apr 26 '16 08:04 boazhachlili

+1 same error

andrewillard avatar May 15 '16 02:05 andrewillard

Although there is already a pull-request #3372 that could fix the namespace issue, here would be a work-around, that would build up the svg-node recursivly node by node.

For that there could be a container-element:

<dom-module id="svg-container">
  <template>
    <svg id="svg" version="1.1" xmlns="http://www.w3.org/2000/svg"></svg>
    <content select="svg-component"></content>
  </template>

  <script>
    Polymer({
      is: 'svg-container',

      attached: function() {
        for (var i = 0; i < this.attributes.length; i++) {
          this.$.svg.setAttribute(Polymer.CaseMap.dashToCamelCase(this.attributes[i].name), this.attributes[i].value);
        }
      }
    });
  </script>
</dom-module>

and a element as svg-component, that creates the kind of svg-element, to be attached:

<dom-module id="svg-component">
  <template>
    <content select="svg-component"></content>
  </template>
  <script>
    SvgComponent = Polymer({
      is: 'svg-component',

      _node: {},
      _namespace: 'http://www.w3.org/2000/svg',

      get rootElement () {
        if (this.parentNode.nodeName === 'SVG-CONTAINER')
          return this.parentNode.$.svg;
        else
          return this.parentNode._node;
      },

      factoryImpl: function(attributes) {
        for (var key in attributes)
          this.setAttribute(key, attributes[key]);
      },

      attached: function() {
        var is = this.attributes.is.value;

        if (is !== undefined) {
          this._node = document.createElementNS(this._namespace, is);
          for (var i = 0; i < this.attributes.length; i++) {
            if (this.attributes[i].name !== 'is')
              this._node.setAttribute(this.attributes[i].name, this.attributes[i].value);
          }
          this.rootElement.appendChild(this._node);
        }
      }
    });
  </script>
</dom-module>

Property binding would have to be formally and also event-binding, but you could use that to do it automatically. It would definitely be better, that SVG namespace is implemented in Polymer as an option for registration of new elements .

And in a test: plunker

fooloomanzoo avatar Jun 12 '16 19:06 fooloomanzoo

@fooloomanzii Thanks a lot for the workaround. You saved me a lot of time !

+1 for native implementation.

jonadeline avatar Jun 27 '16 10:06 jonadeline

+1 for native implementation

shahraship avatar Jul 08 '16 13:07 shahraship

+1

garryyao avatar Sep 13 '16 07:09 garryyao

+1

FlorianFe avatar Oct 23 '16 11:10 FlorianFe