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 all 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
99 changes: 99 additions & 0 deletions src/bunit.web.tests/Rendering/Internal/HtmlizerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
namespace Bunit.Rendering.Internal
{
using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Rendering;
using Microsoft.AspNetCore.Components.Web;
using Xunit;

public class HtmlizerTests : ComponentTestFixture
{
public static TheoryData GetOnClickData()
{
return new TheoryData<bool, bool>
{
{ false, true },
{ true, false },
{ true, true }
};
}

[Theory(DisplayName =
"The component is rendered without internal Blazor attributes, " +
"regardless of the PreventDefault and StopPropagation settings.")]
[MemberData(nameof(GetOnClickData))]
public void Test001(bool stopPropagation, bool preventDefault)
{
//Arrange
var component = RenderComponent<Htmlizer01Component>(
EventCallback<MouseEventArgs>(nameof(Htmlizer01Component.OnClick), OnClickCallback),
Parameter(nameof(Htmlizer01Component.OnClickStopPropagation), stopPropagation),
Parameter(nameof(Htmlizer01Component.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);
}
}

[Theory(DisplayName = "The component contains correctly prefixed internal attributes.")]
[MemberData(nameof(GetOnClickData))]
public void Test002(bool stopPropagation, bool preventDefault)
{
//Arrange
var component = RenderComponent<Htmlizer01Component>(
EventCallback<MouseEventArgs>(nameof(Htmlizer01Component.OnClick), OnClickCallback),
Parameter(nameof(Htmlizer01Component.OnClickStopPropagation), stopPropagation),
Parameter(nameof(Htmlizer01Component.OnClickPreventDefault), preventDefault));

//Act
var button = component.Find("button");

//Assert
Assert.Equal(stopPropagation, button.HasAttribute("blazor:__internal_stopPropagation_onclick"));
Assert.Equal(preventDefault, button.HasAttribute("blazor:__internal_preventDefault_onclick"));

void OnClickCallback(MouseEventArgs e)
{
// NOTE: This line is only for the completeness of the EventCallback
Assert.NotNull(e);
}
}

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

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

[Parameter]
public bool OnClickStopPropagation { 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