Skip to content

Inline SVGs are not rendered #16341

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

Closed
Suchiman opened this issue Mar 25, 2018 · 6 comments
Closed

Inline SVGs are not rendered #16341

Suchiman opened this issue Mar 25, 2018 · 6 comments
Labels
area-blazor Includes: Blazor, Razor Components

Comments

@Suchiman
Copy link
Contributor

Inline SVGs are rendered to into the DOM but are not visually rendered by the browser.
It appears this issue is due to the way how the DOM is updated in BrowserRenderer.ts.
I did some research and it appears this is the reason https://stackoverflow.com/questions/41161094/dynamically-created-svg-elements-are-not-rendered-by-the-browser

Minimal repro steps

  1. Create a Blazor application
  2. Paste the following SVG / HTML snippet into the Index page
<svg xmlns="http://www.w3.org/2000/svg" height="140" width="500">
    <ellipse cx="200" cy="80" rx="100" ry="50" style="fill:yellow;stroke:purple;stroke-width:2" />
    Sorry, your browser does not support inline SVG.
</svg>

Expected result

The browser treats this as normal html with inline svg and renders it properly

Actual result

The browser displays Sorry, your browser does not support inline SVG. due to the way how the DOM is updated

@danroth27
Copy link
Member

Thanks for bringing this to our attention!

@AdrienTorris
Copy link

I have the same issue. I think it's because the attributes are not computed.

@sjroe
Copy link
Contributor

sjroe commented Mar 27, 2018

I have encountered the same issue and a quick play with the JS via Chrome dev tools suggests that the small below modification in Rendering/BrowserRenderer.ts resolves the issue:

insertElement(componentId: number, parent: Element, childIndex: number, frames: System_Array<RenderTreeFramePointer>, frame: RenderTreeFramePointer, frameIndex: number) {
  const tagName = renderTreeFrame.elementName(frame)!;
  const newDomElement = tagName === 'svg' || parent.namespaceURI === 'http://www.w3.org/2000/svg' ?
    document.createElementNS('http://www.w3.org/2000/svg', tagName) :
    document.createElement(tagName);
  insertNodeIntoDOM(newDomElement, parent, childIndex);
...

@sjroe
Copy link
Contributor

sjroe commented Mar 27, 2018

Additionally, SVG based components do not render due to the blazor-component wrappers. A quick fix for this is to modify the insertComponent function in Rendering/BrowserRenderer.ts to utilize a g tag when we're in an SVG tree:

const containerElement = parent.namespaceURI === 'http://www.w3.org/2000/svg' ?
    document.createElementNS('http://www.w3.org/2000/svg', 'g') :
    document.createElement('blazor-component');

The comments suggest that the blazor-component wrappers are a temporary solution and will one day be removed.

@AdrienTorris
Copy link

@sjroe Yes I also saw that the blazor-component will disappear, I didn't see when however. I will test your workarounds, thank's.

sjroe referenced this issue in sjroe/Blazor Mar 29, 2018
SteveSandersonMS referenced this issue in dotnet/blazor Mar 30, 2018
* Added SVG support (#366)

* Added E2E tests for SVG
@SteveSandersonMS
Copy link
Member

Resolved in dotnet/blazor#435

SteveSandersonMS referenced this issue in SteveSandersonMS/BlazorMigration Nov 27, 2018
* Added SVG support (#366)

* Added E2E tests for SVG
@mkArtakMSFT mkArtakMSFT transferred this issue from dotnet/blazor Oct 27, 2019
@mkArtakMSFT mkArtakMSFT added the area-blazor Includes: Blazor, Razor Components label Oct 27, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

No branches or pull requests

6 participants