diff --git a/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Rendering/BrowserRenderer.ts b/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Rendering/BrowserRenderer.ts index 1d16c5e489..d0da6c1965 100644 --- a/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Rendering/BrowserRenderer.ts +++ b/src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Rendering/BrowserRenderer.ts @@ -115,7 +115,9 @@ export class BrowserRenderer { insertElement(componentId: number, parent: Element, childIndex: number, frames: System_Array, frame: RenderTreeFramePointer, frameIndex: number) { const tagName = renderTreeFrame.elementName(frame)!; - const newDomElement = document.createElement(tagName); + 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); // Apply attributes @@ -149,7 +151,9 @@ export class BrowserRenderer { // (counting child components as a single item), so N will rarely if ever be large. // We could even keep track of whether all the child components happen to have exactly 1 // top level frames, and in that case, there's no need to sum as we can do direct lookups. - const containerElement = document.createElement('blazor-component'); + const containerElement = parent.namespaceURI === 'http://www.w3.org/2000/svg' ? + document.createElementNS('http://www.w3.org/2000/svg', 'g') : + document.createElement('blazor-component'); insertNodeIntoDOM(containerElement, parent, childIndex); // All we have to do is associate the child component ID with its location. We don't actually diff --git a/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs b/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs index a3512557b9..2bd952dd37 100644 --- a/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs +++ b/test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs @@ -228,5 +228,29 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests externalComponentButton.Click(); Assert.Equal("It works", externalComponentButton.Text); } + + [Fact] + public void CanRenderSvgWithCorrectNamespace() + { + var appElement = MountTestComponent(); + + var svgElement = appElement.FindElement(By.XPath("//*[local-name()='svg' and namespace-uri()='http://www.w3.org/2000/svg']")); + Assert.NotNull(svgElement); + + var svgCircleElement = appElement.FindElement(By.XPath("//*[local-name()='circle' and namespace-uri()='http://www.w3.org/2000/svg']")); + Assert.NotNull(svgCircleElement); + } + + [Fact] + public void CanRenderSvgChildComponentWithCorrectNamespace() + { + var appElement = MountTestComponent(); + + var svgElement = appElement.FindElement(By.XPath("//*[local-name()='svg' and namespace-uri()='http://www.w3.org/2000/svg']")); + Assert.NotNull(svgElement); + + var svgCircleElement = appElement.FindElement(By.XPath("//*[local-name()='circle' and namespace-uri()='http://www.w3.org/2000/svg']")); + Assert.NotNull(svgCircleElement); + } } } diff --git a/test/testapps/BasicTestApp/SvgCircleComponent.cshtml b/test/testapps/BasicTestApp/SvgCircleComponent.cshtml new file mode 100644 index 0000000000..1c2849b7c7 --- /dev/null +++ b/test/testapps/BasicTestApp/SvgCircleComponent.cshtml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/test/testapps/BasicTestApp/SvgComponent.cshtml b/test/testapps/BasicTestApp/SvgComponent.cshtml new file mode 100644 index 0000000000..ef745967f2 --- /dev/null +++ b/test/testapps/BasicTestApp/SvgComponent.cshtml @@ -0,0 +1,3 @@ + + + \ No newline at end of file diff --git a/test/testapps/BasicTestApp/SvgWithChildComponent.cshtml b/test/testapps/BasicTestApp/SvgWithChildComponent.cshtml new file mode 100644 index 0000000000..a21d49b0aa --- /dev/null +++ b/test/testapps/BasicTestApp/SvgWithChildComponent.cshtml @@ -0,0 +1,5 @@ +

SVG with Child Component

+ + + + \ No newline at end of file diff --git a/test/testapps/BasicTestApp/wwwroot/index.html b/test/testapps/BasicTestApp/wwwroot/index.html index 1b7342f7ec..1709d80eff 100644 --- a/test/testapps/BasicTestApp/wwwroot/index.html +++ b/test/testapps/BasicTestApp/wwwroot/index.html @@ -24,6 +24,8 @@ + +