Skip to content
This repository was archived by the owner on Feb 25, 2021. It is now read-only.

Commit cdeb36d

Browse files
sjroeSteveSandersonMS
authored andcommitted
Added SVG support (#366) (#435)
* Added SVG support (#366) * Added E2E tests for SVG
1 parent 4407de1 commit cdeb36d

File tree

6 files changed

+41
-2
lines changed

6 files changed

+41
-2
lines changed

src/Microsoft.AspNetCore.Blazor.Browser.JS/src/Rendering/BrowserRenderer.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,9 @@ export class BrowserRenderer {
115115

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

121123
// Apply attributes
@@ -149,7 +151,9 @@ export class BrowserRenderer {
149151
// (counting child components as a single item), so N will rarely if ever be large.
150152
// We could even keep track of whether all the child components happen to have exactly 1
151153
// top level frames, and in that case, there's no need to sum as we can do direct lookups.
152-
const containerElement = document.createElement('blazor-component');
154+
const containerElement = parent.namespaceURI === 'http://www.w3.org/2000/svg' ?
155+
document.createElementNS('http://www.w3.org/2000/svg', 'g') :
156+
document.createElement('blazor-component');
153157
insertNodeIntoDOM(containerElement, parent, childIndex);
154158

155159
// All we have to do is associate the child component ID with its location. We don't actually

test/Microsoft.AspNetCore.Blazor.E2ETest/Tests/ComponentRenderingTest.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,5 +228,29 @@ public void CanUseComponentAndStaticContentFromExternalNuGetPackage()
228228
externalComponentButton.Click();
229229
Assert.Equal("It works", externalComponentButton.Text);
230230
}
231+
232+
[Fact]
233+
public void CanRenderSvgWithCorrectNamespace()
234+
{
235+
var appElement = MountTestComponent<SvgComponent>();
236+
237+
var svgElement = appElement.FindElement(By.XPath("//*[local-name()='svg' and namespace-uri()='http://www.w3.org/2000/svg']"));
238+
Assert.NotNull(svgElement);
239+
240+
var svgCircleElement = appElement.FindElement(By.XPath("//*[local-name()='circle' and namespace-uri()='http://www.w3.org/2000/svg']"));
241+
Assert.NotNull(svgCircleElement);
242+
}
243+
244+
[Fact]
245+
public void CanRenderSvgChildComponentWithCorrectNamespace()
246+
{
247+
var appElement = MountTestComponent<SvgWithChildComponent>();
248+
249+
var svgElement = appElement.FindElement(By.XPath("//*[local-name()='svg' and namespace-uri()='http://www.w3.org/2000/svg']"));
250+
Assert.NotNull(svgElement);
251+
252+
var svgCircleElement = appElement.FindElement(By.XPath("//*[local-name()='circle' and namespace-uri()='http://www.w3.org/2000/svg']"));
253+
Assert.NotNull(svgCircleElement);
254+
}
231255
}
232256
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<circle cx="125" cy="125" r="100" fill="red" stroke="black" stroke-width="3" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<svg height="250" width="250">
2+
<circle cx="125" cy="125" r="100" fill="red" stroke="black" stroke-width="3" />
3+
</svg>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<h1>SVG with Child Component</h1>
2+
3+
<svg height="250" width="250">
4+
<SvgCircleComponent />
5+
</svg>

test/testapps/BasicTestApp/wwwroot/index.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
<option value="BasicTestApp.HttpClientTest.HttpRequestsComponent">HttpClient tester</option>
2525
<option value="BasicTestApp.BindCasesComponent">@bind cases</option>
2626
<option value="BasicTestApp.ExternalContentPackage">External content package</option>
27+
<option value="BasicTestApp.SvgComponent">SVG</option>
28+
<option value="BasicTestApp.SvgWithChildComponent">SVG with child component</option>
2729
<!--<option value="BasicTestApp.RouterTest.Default">Router</option> Excluded because it requires additional setup to work correctly when loaded manually -->
2830
</select>
2931
&nbsp;

0 commit comments

Comments
 (0)