public async Task ProcessAsync_WithContentAndForAttributeGeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-label",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-label",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                tagHelperContent.SetContent("Label content");
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            var tagHelper = new LabelTagHelper(new DefaultGovUkHtmlGenerator(), new DefaultModelHelper())
            {
                For = "some-input-id"
            };

            // Act
            await tagHelper.ProcessAsync(context, output);

            // Assert
            var html = output.AsString();

            Assert.Equal("<label class=\"govuk-label\" for=\"some-input-id\">Label content</label>", html);
        }
 /// <summary>
 /// Renders the label.
 /// </summary>
 /// <param name="tagHelper">The tag helper.</param>
 /// <param name="output">The tag helper output.</param>
 internal static void RenderLabel(this BoconsFormTagHelperBase tagHelper, TagHelperOutput output)
 {
     // Label
     if (!string.IsNullOrEmpty(tagHelper.Label))
     {
         output.PreElement.Prepend(LabelTagHelper.Build(tagHelper.Label, tagHelper.Id, tagHelper.IsRequired, tagHelper.FormContext));
     }
 }
        protected virtual string GetLabelAsHtmlUsingTagHelper(TagHelperContext context, TagHelperOutput output)
        {
            var labelTagHelper = new LabelTagHelper(_generator)
            {
                For         = TagHelper.AspFor,
                ViewContext = TagHelper.ViewContext
            };

            return(RenderTagHelper(new TagHelperAttributeList(), context, labelTagHelper, _encoder, "label", TagMode.StartTagAndEndTag, true));
        }
        protected virtual async Task <string> GetLabelAsHtmlUsingTagHelperAsync(TagHelperContext context, TagHelperOutput output)
        {
            var labelTagHelper = new LabelTagHelper(_generator)
            {
                For         = TagHelper.AspFor,
                ViewContext = TagHelper.ViewContext
            };

            return(await labelTagHelper.RenderAsync(new TagHelperAttributeList(), context, _encoder, "label", TagMode.StartTagAndEndTag));
        }
        public async Task ProcessAsync_WithNoContentAndAspForAttributeGeneratesExpectedOutput()
        {
            // Arrange
            var context = new TagHelperContext(
                tagName: "govuk-label",
                allAttributes: new TagHelperAttributeList(),
                items: new Dictionary <object, object>(),
                uniqueId: "test");

            var output = new TagHelperOutput(
                "govuk-label",
                attributes: new TagHelperAttributeList(),
                getChildContentAsync: (useCachedResult, encoder) =>
            {
                var tagHelperContent = new DefaultTagHelperContent();
                return(Task.FromResult <TagHelperContent>(tagHelperContent));
            });

            output.TagMode = TagMode.SelfClosing;

            var htmlGenerator = new Mock <DefaultGovUkHtmlGenerator>()
            {
                CallBase = true
            };

            htmlGenerator
            .Setup(mock => mock.GetFullHtmlFieldName(
                       /*viewContext: */ It.IsAny <ViewContext>(),
                       /*expression: */ It.IsAny <string>()))
            .Returns("Foo");

            htmlGenerator
            .Setup(mock => mock.GetDisplayName(
                       /*viewContext: */ It.IsAny <ViewContext>(),
                       /*modelExplorer: */ It.IsAny <ModelExplorer>(),
                       /*expression: */ It.IsAny <string>()))
            .Returns("Generated label");

            var modelExplorer = new EmptyModelMetadataProvider().GetModelExplorerForType(typeof(Model), "Foo");

            var tagHelper = new LabelTagHelper(htmlGenerator.Object)
            {
                AspFor      = new ModelExpression("Foo", modelExplorer),
                ViewContext = new ViewContext()
            };

            // Act
            await tagHelper.ProcessAsync(context, output);

            // Assert
            var html = output.AsString();

            Assert.Equal("<label class=\"govuk-label\" for=\"Foo\">Generated label</label>", html);
        }
Exemple #6
0
        public async Task WhenLabelTagHelperCalled_ThenCorrectClassCalled()
        {
            var tagHelper = Substitute.For <IMockViewComponentHelper>();

            var componentTag = new LabelTagHelper(tagHelper)
            {
                Text = "LabelText", For = "ForText", AdditionalClass = "AdditionalClass"
            };

            await ViewComponentTestHelper.CallTagHelper("Label", tagHelper, componentTag);
        }
Exemple #7
0
        private async Task <TagHelperOutput> CreateLabelElement(TagHelperContext context)
        {
            LabelTagHelper labelTagHelper = new LabelTagHelper(Generator);

            labelTagHelper.ViewContext = ViewContext;
            labelTagHelper.For         = For;
            var tagHelperOutput = TagHelperExtension.CreateTagHelperOutput("label");
            await labelTagHelper.ProcessAsync(context, tagHelperOutput);

            return(tagHelperOutput);
        }
Exemple #8
0
    protected virtual async Task <string> GetLabelAsHtmlUsingTagHelperAsync(TagHelperContext context, TagHelperOutput output, bool isCheckbox)
    {
        var labelTagHelper = new LabelTagHelper(_generator)
        {
            For         = TagHelper.AspFor,
            ViewContext = TagHelper.ViewContext
        };

        var attributeList = new TagHelperAttributeList();

        attributeList.AddClass(isCheckbox ? "form-check-label" : "form-label");

        return(await labelTagHelper.RenderAsync(attributeList, context, _encoder, "label", TagMode.StartTagAndEndTag));
    }
Exemple #9
0
        private async Task <TagHelperOutput> GenerateLabel(TagHelperContext context)
        {
            var labelTagHelper =
                new LabelTagHelper(_htmlGenerator)
            {
                For         = this.For,
                ViewContext = this.ViewContext,
            };

            var tagHelperOutput = CreateTagHelperOutput("label");

            await labelTagHelper.ProcessAsync(context, tagHelperOutput);

            return(tagHelperOutput);
        }
Exemple #10
0
        private async Task <TagHelperOutput> GenerateLabel(TagHelperContext context)
        {
            var labelTagHelper =
                new LabelTagHelper(_htmlGenerator)
            {
                For         = this.For,
                ViewContext = this.ViewContext,
            };

            var tagHelperOutput = CreateTagHelperOutput("label");

            tagHelperOutput.Attributes.Add(new TagHelperAttribute("class", _defaultLabelClass));

            await labelTagHelper.ProcessAsync(context, tagHelperOutput);

            return(tagHelperOutput);
        }
Exemple #11
0
        protected virtual string GetLabelAsHtmlUsingTagHelper(TagHelperContext context, TagHelperOutput output, bool isCheckbox)
        {
            var labelTagHelper = new LabelTagHelper(_generator)
            {
                For         = TagHelper.AspFor,
                ViewContext = TagHelper.ViewContext
            };

            var attributeList = new TagHelperAttributeList();

            if (isCheckbox)
            {
                attributeList.AddClass("form-check-label");
            }

            return(RenderTagHelper(attributeList, context, labelTagHelper, _encoder, "label", TagMode.StartTagAndEndTag, true));
        }
Exemple #12
0
        protected virtual async Task <string> GetLabelAsHtmlUsingTagHelperAsync(TagHelperContext context, TagHelperOutput output, bool isCheckbox)
        {
            var labelTagHelper = new LabelTagHelper(_generator)
            {
                For         = TagHelper.AspFor,
                ViewContext = TagHelper.ViewContext
            };

            var attributeList = new TagHelperAttributeList();

            if (isCheckbox)
            {
                attributeList.AddClass("custom-control-label");
            }

            return(await labelTagHelper.RenderAsync(attributeList, context, _encoder, "label", TagMode.StartTagAndEndTag).ConfigureAwait(false));
        }
Exemple #13
0
        [Test] public async Task Can_set_label_content()
        {
            // arrange
            var command = new Command();
            var tag     = new LabelTagHelper
            {
                For             = MakeExpression(command, m => m.Name),
                RequestServices = ServiceProvider
            };

            // act
            var output = await ProcessTagAsync(tag, "miru-label", "Customer Name");

            // assert
            output.TagName.ShouldBeNull();
            output.PreElement.GetContent().ShouldBe("<label for=\"Name\">Customer Name</label>");
        }
Exemple #14
0
        private async Task <string> buildLabelHtml(ModelExplorer property, TweakingConfiguration tweakingConfig)
        {
            TagHelper label = new LabelTagHelper(_htmlGenerator)
            {
                For         = new ModelExpression(property.GetFullName(), property),
                ViewContext = _viewContext
            };

            PropertyTweakingConfiguration propertyConfig = tweakingConfig
                                                           .GetByPropertyFullName(property.GetFullName());

            return(await GetGeneratedContentFromTagHelper(
                       "label",
                       TagMode.StartTagAndEndTag, label,
                       new TagHelperAttributeList()
            {
                new TagHelperAttribute("class", propertyConfig?.LabelClasses)
            }));
        }
Exemple #15
0
        private async Task <TagHelperOutput> CreateLabelElement(TagHelperContext context)
        {
            LabelTagHelper labelTagHelper =
                new LabelTagHelper(Generator)
            {
                For         = this.For,
                ViewContext = this.ViewContext
            };

            TagHelperOutput labelOutput = CreateTagHelperOutput("label");

            await labelTagHelper.ProcessAsync(context, labelOutput);


            labelOutput.Attributes.Add(
                new TagHelperAttribute("class", _defaultLabelClass));

            return(labelOutput);
        }
Exemple #16
0
        public override async Task ProcessAsync(TagHelperContext context, TagHelperOutput output)
        {
            // Get input tag
            var inputTag = new InputTagHelper(_htmlGenerator)
            {
                For         = this.For,
                ViewContext = this.ViewContext
            };
            // Get label tag
            var labelTag = new LabelTagHelper(_htmlGenerator)
            {
                For         = this.For,
                ViewContext = this.ViewContext
            };
            // Get validation tag
            var validationTag = new ValidationMessageTagHelper(_htmlGenerator)
            {
                For         = this.For,
                ViewContext = this.ViewContext
            };

            output.Content.AppendHtml(await labelTag.GetGeneratedContent(_htmlEncoder, "label", TagMode.StartTagAndEndTag, new TagHelperAttributeList {
                new TagHelperAttribute("class", "col-md-2 control-label")
            }));
            // Field and validation within the same div
            output.Content.AppendHtml("<div class=\"col-md-10\">");
            output.Content.AppendHtml(await inputTag.GetGeneratedContent(_htmlEncoder, "input", TagMode.StartTagAndEndTag, new TagHelperAttributeList {
                new TagHelperAttribute("class", "form-control")
            }));
            output.Content.AppendHtml(await validationTag.GetGeneratedContent(_htmlEncoder, "span", TagMode.StartTagAndEndTag, new TagHelperAttributeList {
                new TagHelperAttribute("class", "text-danger")
            }));
            output.Content.AppendHtml("</div>");

            string classValue = "form-group";

            if (output.Attributes.ContainsName("class"))
            {
                classValue += " " + output.Attributes["class"].Value;
            }
            output.Attributes.SetAttribute("class", classValue);
        }
Exemple #17
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            if (!NotEdit)
            {
                var inputContext = CrateTagHelperContext();
                var inputOutput  = CreateTagHelperOutput("input");

                inputOutput.Attributes.Add("class", "form-control");

                var input = new InputTagHelper(_htmlGenerator)
                {
                    For         = For,
                    ViewContext = ViewContext
                };

                input.Process(inputContext, inputOutput);
                output.Content.AppendHtml(inputOutput);
            }
            else
            {
                var labelContext = CrateTagHelperContext();
                var labelOutput  = CreateTagHelperOutput("label");

                //labelOutput.Content.Append(LabelContent);

                if (For != null)
                {
                    labelOutput.Attributes.Add("for", For.Name);
                }

                var label = new LabelTagHelper(_htmlGenerator)
                {
                    ViewContext = ViewContext
                };

                label.Process(labelContext, labelOutput);
                output.Content.AppendHtml(labelOutput);
            }
        }
Exemple #18
0
 /// <summary>
 /// 测试初始化
 /// </summary>
 public LabelTagHelperTest(ITestOutputHelper output)
 {
     _output    = output;
     _component = new LabelTagHelper();
 }
    public async Task ProcessAsync_GeneratesExpectedOutput_WithDisplayName(
        string displayName,
        string originalChildContent,
        string htmlFieldPrefix,
        string expectedContent,
        string expectedId)
    {
        // Arrange
        var expectedAttributes = new TagHelperAttributeList
        {
            { "for", expectedId }
        };

        var name             = nameof(NestedModel.Text);
        var metadataProvider = new TestModelMetadataProvider();

        metadataProvider
        .ForProperty <NestedModel>(name)
        .DisplayDetails(metadata => metadata.DisplayName = () => displayName);

        var htmlGenerator = new TestableHtmlGenerator(metadataProvider);
        var viewContext   = TestableHtmlGenerator.GetViewContext(
            model: null,
            htmlGenerator: htmlGenerator,
            metadataProvider: metadataProvider);

        var viewData = new ViewDataDictionary <NestedModel>(metadataProvider, viewContext.ModelState);

        viewData.TemplateInfo.HtmlFieldPrefix = htmlFieldPrefix;
        viewContext.ViewData = viewData;

        var containerExplorer = metadataProvider.GetModelExplorerForType(typeof(NestedModel), model: null);
        var modelExplorer     = containerExplorer.GetExplorerForProperty(name);
        var modelExpression   = new ModelExpression(name, modelExplorer);
        var tagHelper         = new LabelTagHelper(htmlGenerator)
        {
            For         = modelExpression,
            ViewContext = viewContext,
        };

        var tagHelperContext = new TagHelperContext(
            tagName: "label",
            allAttributes: new TagHelperAttributeList(),
            items: new Dictionary <object, object>(),
            uniqueId: "test");
        var output = new TagHelperOutput(
            "label",
            new TagHelperAttributeList(),
            getChildContentAsync: (useCachedResult, encoder) =>
        {
            var tagHelperContent = new DefaultTagHelperContent();
            tagHelperContent.AppendHtml(originalChildContent);
            return(Task.FromResult <TagHelperContent>(tagHelperContent));
        });

        // Act
        await tagHelper.ProcessAsync(tagHelperContext, output);

        // Assert
        Assert.Equal(expectedAttributes, output.Attributes);
        Assert.Equal(expectedContent, HtmlContentUtilities.HtmlContentToString(output.Content));
    }
    public async Task ProcessAsync_GeneratesExpectedOutput(
        object model,
        Type containerType,
        Func <object> modelAccessor,
        string propertyPath,
        TagHelperOutputContent tagHelperOutputContent)
    {
        // Arrange
        var expectedTagName    = "not-label";
        var expectedAttributes = new TagHelperAttributeList
        {
            { "class", "form-control" },
            { "for", tagHelperOutputContent.ExpectedId }
        };
        var metadataProvider = new TestModelMetadataProvider();

        var containerMetadata = metadataProvider.GetMetadataForType(containerType);
        var containerExplorer = metadataProvider.GetModelExplorerForType(containerType, model);

        var propertyMetadata = metadataProvider.GetMetadataForProperty(containerType, "Text");
        var modelExplorer    = containerExplorer.GetExplorerForExpression(propertyMetadata, modelAccessor());
        var htmlGenerator    = new TestableHtmlGenerator(metadataProvider);

        var modelExpression = new ModelExpression(propertyPath, modelExplorer);
        var tagHelper       = new LabelTagHelper(htmlGenerator)
        {
            For = modelExpression,
        };
        var expectedPreContent  = "original pre-content";
        var expectedPostContent = "original post-content";

        var tagHelperContext = new TagHelperContext(
            tagName: "not-label",
            allAttributes: new TagHelperAttributeList(),
            items: new Dictionary <object, object>(),
            uniqueId: "test");
        var htmlAttributes = new TagHelperAttributeList
        {
            { "class", "form-control" },
        };
        var output = new TagHelperOutput(
            expectedTagName,
            htmlAttributes,
            getChildContentAsync: (useCachedResult, encoder) =>
        {
            var tagHelperContent = new DefaultTagHelperContent();
            tagHelperContent.AppendHtml(tagHelperOutputContent.OriginalChildContent);
            return(Task.FromResult <TagHelperContent>(tagHelperContent));
        });

        output.PreContent.AppendHtml(expectedPreContent);
        output.PostContent.AppendHtml(expectedPostContent);

        // LabelTagHelper checks IsContentModified so we don't want to forcibly set it if
        // tagHelperOutputContent.OriginalContent is going to be null or empty.
        if (!string.IsNullOrEmpty(tagHelperOutputContent.OriginalContent))
        {
            output.Content.AppendHtml(tagHelperOutputContent.OriginalContent);
        }

        var viewContext = TestableHtmlGenerator.GetViewContext(model, htmlGenerator, metadataProvider);

        tagHelper.ViewContext = viewContext;

        // Act
        await tagHelper.ProcessAsync(tagHelperContext, output);

        // Assert
        Assert.Equal(expectedAttributes, output.Attributes);
        Assert.Equal(expectedPreContent, output.PreContent.GetContent());
        Assert.Equal(
            tagHelperOutputContent.ExpectedContent,
            HtmlContentUtilities.HtmlContentToString(output.Content));
        Assert.Equal(expectedPostContent, output.PostContent.GetContent());
        Assert.Equal(TagMode.StartTagAndEndTag, output.TagMode);
        Assert.Equal(expectedTagName, output.TagName);
    }
 public static void SetLabelContext(this TagHelperContext context, LabelTagHelper tagHelper) {
     if (context.Items.ContainsKey(LabelContext))
         context.Items[LabelContext] = tagHelper;
     else
         context.Items.Add(LabelContext, tagHelper);
 }