tinymce icon indicating copy to clipboard operation
tinymce copied to clipboard

Unable to show a SVG correctly

Open nawrasg opened this issue 3 years ago • 5 comments

Hello,

I am using tinymce v6 with an Angular application. We would like to add SVG figures, generated by mermaidjs, to the documents written using tinymce.

The SVG generation using mermaidjs is working perfectly. The figure is shown correctly in the Angular app. However, when we try to inject the SVG in the editor, the text is not shown.

We tried to add the different tags of the SVG to extended_valid_elements. However, the tag foreignObject is removed once added to tinymce.

We also tried allowing all tags using the valid_elements attribute but it does nothing. The tag ForeignObject keeps been removed.

Any idea how to solve this issue? Thank you,

nawrasg avatar Nov 22 '23 23:11 nawrasg

This happens because TinyMCE strips empty and invalid tags. You can solve it by

Adding '&nbsp' to each empty element:

svg.find('*').each(function() {
  if (!$(this).html()) {
    $(this).text(' ');
  }
});

above, svg is your jQuery wrapped svg element.

Extending the valid elements according to the svg element reference*

extended_valid_elements:"svg[*],defs[*],pattern[*],desc[*],metadata[*],g[*],mask[*],path[*],line[*],marker[*],rect[*],circle[*],ellipse[*],polygon[*],polyline[*],linearGradient[*],radialGradient[*],stop[*],image[*],view[*],text[*],textPath[*],title[*],tspan[*],glyph[*],symbol[*],switch[*],use[*]"

*Note I added only the elements relevant for my case.

Are you adding it as svg[*]?

mrinasugosh avatar Dec 07 '23 14:12 mrinasugosh

In TinyMCE 6.8 we made some changes so you should only require extended_valid_elements: 'svg[viewBox|width|height]'. Can you try with the new release?

TheSpyder avatar Dec 07 '23 22:12 TheSpyder

Using svg tags that include use will not work since the use tag is stripped away. I have tested both with and without   as part of the use tag. Tested with 6.8.2, 6.8.3 and 7.0.1 with only code plugin added. Pasted the html in code plugin and opening it again and the use tag will be removed.

I configured the tiny with:

tinymce.init({
            selector: 'textarea',
            plugins: 'code',
            toolbar: 'code',
            verify_html: false,
            extended_valid_elements:"svg[*],defs[*],pattern[*],desc[*],metadata[*],g[*],mask[*],path[*],line[*],marker[*],rect[*],circle[*],ellipse[*],polygon[*],polyline[*],linearGradient[*],radialGradient[*],stop[*],image[*],view[*],text[*],textPath[*],title[*],tspan[*],glyph[*],symbol[*],switch[*],use[*]"
          });

I used this svg and use example from Mozilla (https://developer.mozilla.org/en-US/docs/Web/SVG/Element/use)

<svg viewBox="0 0 30 10" xmlns="http://www.w3.org/2000/svg">
  <circle id="myCircle" cx="5" cy="5" r="4" stroke="blue" />
  <use href="#myCircle" x="10" fill="blue" />
  <use href="#myCircle" x="20" fill="white" stroke="red" />
</svg>

Is this expected behaviour?

DeepRed avatar Apr 17 '24 14:04 DeepRed

Yes it's correct TinyMCE filters out some of the SVG to limit XSS attacks. Please consult our security guide for more information and potential options.

lostkeys avatar Apr 17 '24 19:04 lostkeys

Thank you @lostkeys! Tested and verified on our bundling of Tiny.

DeepRed avatar Apr 18 '24 05:04 DeepRed