Skip to content

Commit

Permalink
Added SVG support (#366) (dotnet#435)
Browse files Browse the repository at this point in the history
* Added SVG support (#366)

* Added E2E tests for SVG
  • Loading branch information
sjroe authored and SteveSandersonMS committed Mar 30, 2018
1 parent 5b658c8 commit cda3692
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,9 @@ export class BrowserRenderer {

insertElement(componentId: number, parent: Element, childIndex: number, frames: System_Array<RenderTreeFramePointer>, 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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,29 @@ public void CanUseComponentAndStaticContentFromExternalNuGetPackage()
externalComponentButton.Click();
Assert.Equal("It works", externalComponentButton.Text);
}

[Fact]
public void CanRenderSvgWithCorrectNamespace()
{
var appElement = MountTestComponent<SvgComponent>();

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<SvgWithChildComponent>();

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);
}
}
}
1 change: 1 addition & 0 deletions test/testapps/BasicTestApp/SvgCircleComponent.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<circle cx="125" cy="125" r="100" fill="red" stroke="black" stroke-width="3" />
3 changes: 3 additions & 0 deletions test/testapps/BasicTestApp/SvgComponent.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<svg height="250" width="250">
<circle cx="125" cy="125" r="100" fill="red" stroke="black" stroke-width="3" />
</svg>
5 changes: 5 additions & 0 deletions test/testapps/BasicTestApp/SvgWithChildComponent.cshtml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<h1>SVG with Child Component</h1>

<svg height="250" width="250">
<SvgCircleComponent />
</svg>
2 changes: 2 additions & 0 deletions test/testapps/BasicTestApp/wwwroot/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
<option value="BasicTestApp.HttpClientTest.HttpRequestsComponent">HttpClient tester</option>
<option value="BasicTestApp.BindCasesComponent">@bind cases</option>
<option value="BasicTestApp.ExternalContentPackage">External content package</option>
<option value="BasicTestApp.SvgComponent">SVG</option>
<option value="BasicTestApp.SvgWithChildComponent">SVG with child component</option>
<!--<option value="BasicTestApp.RouterTest.Default">Router</option> Excluded because it requires additional setup to work correctly when loaded manually -->
</select>
&nbsp;
Expand Down

0 comments on commit cda3692

Please sign in to comment.