Skip to content

Usage of .Text() in custom builder always gets overridden #99

Closed

Description

@mycroes

This is related to #95 (and a regression since HtmlTags 6.x). When creating a custom builder as following:

public class DateTimeBuilder : IElementBuilder
{
    public static string Token = "date-time-builder";

    public HtmlTag Build(ElementRequest request)
    {
        return new HtmlTag("span").Text(Token);
    }
}

the test below fails:

[Fact]
public void ShouldAllowOverridingOfConventionTextUsingCustomBuilder()
{
    var offset = new DateTimeOffset(2018, 1, 1, 12, 00, 00, TimeSpan.FromHours(-6));
    var subject = new Subject {DateValue = offset};
    var helper = GetHtmlHelper(subject);

    var display = helper.Display(m => m.DateValue);

    display.Text().ShouldBe(DateTimeBuilder.Token);
}

I'd love to know if you intend to fix this or if you'll accept this as expected behavior (I guess a modifier can still be used to override the text).

Activity

  1. jbogard commented on Aug 13, 2018

    @jbogard
    Contributor

    Do you have a larger example? What does your registration code look like?

  2. mycroes commented on Aug 13, 2018

    @mycroes
    Author

    Provided as pull request. I guess the test can't be more to the point (and for this issue I don't see any use for additional configuration), but the code is part of mycroes/SupportManager (...Conventions.cs).

  3. mycroes commented on Aug 13, 2018

    @mycroes
    Author

    Well actually, the issue is part of SupportManager, where I use a builder to properly format DateTime(Offset)s, the code is just a contrived test.

  4. jbogard commented on Dec 31, 2020

    @jbogard
    Contributor

    Finally coming back to this. This is because the ModelMetadata extensions (that make these behave like ASP.NET Core) are modifiers, not builders. Builders go first, then modifiers. You've replaced the builder, not the modifier.

    Adding your modifier makes this work:

    reg.Displays.IfPropertyIs<DateTimeOffset>().ModifyWith(m => m.CurrentTag.Text("date-time-builder"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions