Issue #2214💬 AnsweredOpened August 23, 2019by Firefox20050 reactions

SVG Element is not a real SVGElement

快速解答by artf

The code returns True in Firefox but False in Safari and Chrome although I can verify in the Chrome Properties tab, that it is indeed of type SVGElement. Weird, seems like a Chromium issue...

Read full answer below ↓

Question

Behavior I use an external Javascript code that replaces some SVG elements dynamically based on their attributes. The Javascript code first checks for the type of the element to validate, that it is an SVGElement like this:

document.querySelector('#someId') instanceof SVGElement

The code returns True in Firefox but False in Safari and Chrome although I can verify in the Chrome Properties tab, that it is indeed of type SVGElement.

How I create the SVG Element I tried different things but ended up using a Plugin.

component.js

domc.addType('myType', { 
    isComponent: function(el) {
      if(el.tagName == 'svg' && el.classList && el.classList.contains('someClass'))
        return {type: 'myType'};
    },
    extend: 'svg',
    model: {
      defaults: {
        type: 'svg',
        tagName: 'svg',
        name: 'SomeName',
        traits: [...],
        attributes: {
          class:'someClass',
          ...
        },
        script: function () {
          console.log(document.querySelector('.someClass'));
          console.log(document.querySelector('.someClass') instanceof SVGElement)
          ...
        },
      }
    }
});

blocks.js

bm.add('myBlock', {
    label: 'someLabel',
    category: 'someCategory',
    name: 'blockName',
    content: '<svg class="someClass"\
                ...\
              </svg>'
});

Additional Information According to this: https://stackoverflow.com/questions/24174975/document-createelementsvg-instanceof-svgelement-is-false SVG elements are special, but from what I can see in src/dom_components/view/ComponentSvgView.js I assume, that they are created correctly.

Of course there is nothing wrong with the generated HTML-Code when using it, as browsers will then use the correct SVG - it is just not showing up when editing in GrapesJS.

Answers (1)

artfAugust 26, 2019

The code returns True in Firefox but False in Safari and Chrome although I can verify in the Chrome Properties tab, that it is indeed of type SVGElement.

Weird, seems like a Chromium issue...

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Free option

Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.

Browse free plugins →
Premium option

Premium plugins ship with support, regular updates, and production-ready features — save days of integration work.

Browse premium plugins →

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.