Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Internal Attributes #111 #112

Merged
merged 8 commits into from
Apr 30, 2020
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/bunit.web.tests/Rendering/Internal/HtmlizerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
namespace Bunit.Rendering.Internal
{
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Web;
using Xunit;

public class HtmlizerTests : ComponentTestFixture
{
[Theory(DisplayName =
"The component is rendered without internal Blazor attributes, " +
"regardless of the PreventDefault and StopPropagation settings.")]
[InlineData(false, false)]
[InlineData(false, true)]
[InlineData(true, false)]
[InlineData(true, true)]
public void Test001(bool stopPropagation, bool preventDefault)
{
//Arrange
var component = RenderComponent<Test001Component>(
EventCallback<MouseEventArgs>(nameof(Test001Component.OnClick), OnClickCallback),
Parameter(nameof(Test001Component.OnClickStopPropagation), stopPropagation),
Parameter(nameof(Test001Component.OnClickPreventDefault), preventDefault));

//Act

//Assert
component.MarkupMatches("<button type=\"button\">Click me!</button>");
void OnClickCallback(MouseEventArgs e)
{
// NOTE: This line is only for the completeness of the EventCallback
Assert.NotNull(e);
}
}

private class Test001Component : ComponentBase
{
[Parameter]
public EventCallback<MouseEventArgs> OnClick { get; set; }

[Parameter]
public bool OnClickStopPropagation { get; set; }

[Parameter]
public bool OnClickPreventDefault { get; set; }

/// <inheritdoc />
protected override void BuildRenderTree(RenderTreeBuilder builder)
{
base.BuildRenderTree(builder);

builder.OpenElement(0, "button");
builder.AddAttribute(1, "type", "button");

if (OnClick.HasDelegate)
{
builder.AddAttribute(2, "onclick", Microsoft.AspNetCore.Components.EventCallback.Factory.Create(this, OnClick));
builder.AddEventStopPropagationAttribute(3, "onclick", OnClickStopPropagation);
builder.AddEventPreventDefaultAttribute(4, "onclick", OnClickPreventDefault);
}

builder.AddContent(5, "Click me!");

builder.CloseElement();
}
}
}
}
10 changes: 10 additions & 0 deletions src/bunit.web/Rendering/Internal/Htmlizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ internal class Htmlizer
"area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr"
};

private const string BLAZOR_INTERNAL_ATTR_PREFIX = "__internal_";

public const string BLAZOR_ATTR_PREFIX = "blazor:";
public const string ELEMENT_REFERENCE_ATTR_NAME = BLAZOR_ATTR_PREFIX + "elementreference";

Expand Down Expand Up @@ -220,6 +222,14 @@ private static int RenderAttributes(

switch (frame.AttributeValue)
{
case bool flag when flag && frame.AttributeName.StartsWith(BLAZOR_INTERNAL_ATTR_PREFIX, StringComparison.Ordinal):
// NOTE: This was added to make it more obvious
// that this is a generated/special blazor attribute
// for internal usage
result.Add(" ");
result.Add(BLAZOR_ATTR_PREFIX);
result.Add(frame.AttributeName);
break;
case bool flag when flag:
result.Add(" ");
result.Add(frame.AttributeName);
Expand Down