Add ability to append RenderFragment into a RenderTreeBuilder

This commit is contained in:
Steve Sanderson 2018-02-16 09:16:21 +00:00
parent 29a6175ac1
commit 41aae0b7e6
3 changed files with 47 additions and 3 deletions

View File

@ -65,6 +65,25 @@ namespace Microsoft.AspNetCore.Blazor.RenderTree
public void AddContent(int sequence, string textContent)
=> Append(RenderTreeFrame.Text(sequence, textContent ?? string.Empty));
/// <summary>
/// Appends frames representing an arbitrary fragment of content.
/// </summary>
/// <param name="sequence">An integer that represents the position of the instruction in the source code.</param>
/// <param name="fragment">Content to append.</param>
public void AddContent(int sequence, RenderFragment fragment)
{
if (fragment != null)
{
// We surround the fragment with a region delimiter to indicate that the
// sequence numbers inside the fragment are unrelated to the sequence numbers
// outside it. If we didn't do this, the diffing logic might produce inefficient
// diffs depending on how the sequence numbers compared.
OpenRegion(sequence);
fragment(this);
CloseRegion();
}
}
/// <summary>
/// Appends a frame representing text content.
/// </summary>

View File

@ -342,6 +342,33 @@ namespace Microsoft.AspNetCore.Blazor.Test
frame => AssertFrame.Text(frame, "Goodbye", 6));
}
[Fact]
public void CanAddFragments()
{
// Arrange
var builder = new RenderTreeBuilder(new TestRenderer());
RenderFragment fragment = fragmentBuilder =>
{
fragmentBuilder.AddContent(0, "Hello from the fragment");
fragmentBuilder.OpenElement(1, "Fragment element");
fragmentBuilder.AddContent(2, "Some text");
fragmentBuilder.CloseElement();
};
// Act
builder.OpenElement(10, "parent");
builder.AddContent(11, fragment);
builder.CloseElement();
// Assert
Assert.Collection(builder.GetFrames(),
frame => AssertFrame.Element(frame, "parent", 5, 10),
frame => AssertFrame.Region(frame, 4, 11),
frame => AssertFrame.Text(frame, "Hello from the fragment", 0),
frame => AssertFrame.Element(frame, "Fragment element", 2, 1),
frame => AssertFrame.Text(frame, "Some text", 2));
}
[Fact]
public void CanClear()
{

View File

@ -48,9 +48,7 @@ namespace BasicTestApp
if (_showRegion)
{
builder.OpenRegion(3);
_exampleContent(builder);
builder.CloseRegion();
builder.AddContent(3, _exampleContent);
}
builder.OpenElement(4, "button");