Add ability to append RenderFragment into a RenderTreeBuilder
This commit is contained in:
parent
29a6175ac1
commit
41aae0b7e6
|
|
@ -65,6 +65,25 @@ namespace Microsoft.AspNetCore.Blazor.RenderTree
|
||||||
public void AddContent(int sequence, string textContent)
|
public void AddContent(int sequence, string textContent)
|
||||||
=> Append(RenderTreeFrame.Text(sequence, textContent ?? string.Empty));
|
=> 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>
|
/// <summary>
|
||||||
/// Appends a frame representing text content.
|
/// Appends a frame representing text content.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|
|
||||||
|
|
@ -342,6 +342,33 @@ namespace Microsoft.AspNetCore.Blazor.Test
|
||||||
frame => AssertFrame.Text(frame, "Goodbye", 6));
|
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]
|
[Fact]
|
||||||
public void CanClear()
|
public void CanClear()
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -48,9 +48,7 @@ namespace BasicTestApp
|
||||||
|
|
||||||
if (_showRegion)
|
if (_showRegion)
|
||||||
{
|
{
|
||||||
builder.OpenRegion(3);
|
builder.AddContent(3, _exampleContent);
|
||||||
_exampleContent(builder);
|
|
||||||
builder.CloseRegion();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
builder.OpenElement(4, "button");
|
builder.OpenElement(4, "button");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue