Exemplo n.º 1
0
    public void GetBinding_ReturnsBindingResultCatchAllDescriptorsForPrefixedTags()
    {
        // Arrange
        var catchAllDescriptor = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly")
                                 .TagMatchingRuleDescriptor(rule => rule.RequireTagName(TagHelperMatchingConventions.ElementCatchAllName))
                                 .Build();
        var descriptors     = new[] { catchAllDescriptor };
        var tagHelperBinder = new TagHelperBinder("th:", descriptors);

        // Act
        var bindingResultDiv = tagHelperBinder.GetBinding(
            tagName: "th:div",
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: "p",
            parentIsTagHelper: false);
        var bindingResultSpan = tagHelperBinder.GetBinding(
            tagName: "th:span",
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: "p",
            parentIsTagHelper: false);

        // Assert
        var descriptor = Assert.Single(bindingResultDiv.Descriptors);

        Assert.Same(catchAllDescriptor, descriptor);
        descriptor = Assert.Single(bindingResultSpan.Descriptors);
        Assert.Same(catchAllDescriptor, descriptor);
    }
Exemplo n.º 2
0
        public override TagHelperBinding GetTagHelperBinding(
            TagHelperDocumentContext documentContext,
            string tagName,
            IEnumerable <KeyValuePair <string, string> > attributes,
            string parentTag,
            bool parentIsTagHelper)
        {
            if (documentContext == null)
            {
                throw new ArgumentNullException(nameof(documentContext));
            }

            if (tagName == null)
            {
                throw new ArgumentNullException(nameof(tagName));
            }

            if (attributes == null)
            {
                throw new ArgumentNullException(nameof(attributes));
            }

            var descriptors = documentContext.TagHelpers;

            if (descriptors == null || descriptors.Count == 0)
            {
                return(null);
            }

            var prefix          = documentContext.Prefix;
            var tagHelperBinder = new TagHelperBinder(prefix, descriptors);
            var binding         = tagHelperBinder.GetBinding(tagName, attributes.ToList(), parentTag, parentIsTagHelper);

            return(binding);
        }
Exemplo n.º 3
0
    public void GetBinding_CaseSensitiveRequiredAttribute_CaseMismatch_ReturnsNull()
    {
        // Arrange
        var divTagHelper = TagHelperDescriptorBuilder.Create("DivTagHelper", "SomeAssembly")
                           .TagMatchingRuleDescriptor(rule => rule
                                                      .RequireTagName("div")
                                                      .RequireAttributeDescriptor(attribute => attribute.Name("class")))
                           .SetCaseSensitive()
                           .Build();
        var expectedDescriptors = new[] { divTagHelper };
        var expectedAttributes  = new[]
        {
            new KeyValuePair <string, string>("CLASS", "something")
        };
        var tagHelperBinder = new TagHelperBinder(null, expectedDescriptors);

        // Act
        var bindingResult = tagHelperBinder.GetBinding(
            tagName: "div",
            attributes: expectedAttributes,
            parentTagName: "body",
            parentIsTagHelper: false);

        // Assert
        Assert.Null(bindingResult);
    }
Exemplo n.º 4
0
    public void GetBinding_IsAttributeMatch_MixedAttributeMatches()
    {
        // Arrange
        var divDescriptor1 = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly")
                             .TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
                             .AddMetadata(TagHelperMetadata.Common.ClassifyAttributesOnly, bool.TrueString)
                             .Build();

        var divDescriptor2 = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly")
                             .TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
                             .Build();

        var descriptors     = new[] { divDescriptor1, divDescriptor2, };
        var tagHelperBinder = new TagHelperBinder("", descriptors);

        // Act
        var bindingResult = tagHelperBinder.GetBinding(
            tagName: "div",
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: "p",
            parentIsTagHelper: false);

        // Assert
        Assert.False(bindingResult.IsAttributeMatch);
    }
Exemplo n.º 5
0
    public void GetBinding_PrefixedParent_ReturnsBinding()
    {
        // Arrange
        var divDescriptor = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly")
                            .TagMatchingRuleDescriptor(rule => rule.RequireTagName("div").RequireParentTag("p"))
                            .Build();
        var pDescriptor = TagHelperDescriptorBuilder.Create("foo2", "SomeAssembly")
                          .TagMatchingRuleDescriptor(rule => rule.RequireTagName("p"))
                          .Build();
        var descriptors     = new[] { divDescriptor, pDescriptor };
        var tagHelperBinder = new TagHelperBinder("th:", descriptors);

        // Act
        var bindingResult = tagHelperBinder.GetBinding(
            tagName: "th:div",
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: "th:p",
            parentIsTagHelper: true);

        // Assert
        var boundDescriptor = Assert.Single(bindingResult.Descriptors);

        Assert.Same(divDescriptor, boundDescriptor);
        var boundRules = bindingResult.Mappings[boundDescriptor];
        var boundRule  = Assert.Single(boundRules);

        Assert.Equal("div", boundRule.TagName);
        Assert.Equal("p", boundRule.ParentTag);
    }
Exemplo n.º 6
0
    public void GetBinding_DescriptorWithMultipleRules_CorrectlySelectsMatchingRules()
    {
        // Arrange
        var multiRuleDescriptor = TagHelperDescriptorBuilder.Create("foo", "SomeAssembly")
                                  .TagMatchingRuleDescriptor(rule => rule
                                                             .RequireTagName(TagHelperMatchingConventions.ElementCatchAllName)
                                                             .RequireParentTag("body"))
                                  .TagMatchingRuleDescriptor(rule => rule
                                                             .RequireTagName("div"))
                                  .TagMatchingRuleDescriptor(rule => rule
                                                             .RequireTagName("span"))
                                  .Build();
        var descriptors     = new TagHelperDescriptor[] { multiRuleDescriptor };
        var tagHelperBinder = new TagHelperBinder(null, descriptors);

        // Act
        var binding = tagHelperBinder.GetBinding(
            tagName: "div",
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: "p",
            parentIsTagHelper: false);

        // Assert
        var boundDescriptor = Assert.Single(binding.Descriptors);

        Assert.Same(multiRuleDescriptor, boundDescriptor);
        var boundRules = binding.Mappings[boundDescriptor];
        var boundRule  = Assert.Single(boundRules);

        Assert.Equal("div", boundRule.TagName);
    }
Exemplo n.º 7
0
    public void GetBinding_ReturnsBindingWithInformation()
    {
        // Arrange
        var divTagHelper = TagHelperDescriptorBuilder.Create("DivTagHelper", "SomeAssembly")
                           .TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
                           .Build();
        var expectedDescriptors = new[] { divTagHelper };
        var expectedAttributes  = new[]
        {
            new KeyValuePair <string, string>("class", "something")
        };
        var tagHelperBinder = new TagHelperBinder("th:", expectedDescriptors);

        // Act
        var bindingResult = tagHelperBinder.GetBinding(
            tagName: "th:div",
            attributes: expectedAttributes,
            parentTagName: "body",
            parentIsTagHelper: false);

        // Assert
        Assert.Equal(expectedDescriptors, bindingResult.Descriptors, TagHelperDescriptorComparer.Default);
        Assert.Equal("th:div", bindingResult.TagName);
        Assert.Equal("body", bindingResult.ParentTagName);
        Assert.Equal(expectedAttributes, bindingResult.Attributes);
        Assert.Equal("th:", bindingResult.TagHelperPrefix);
        Assert.Equal(divTagHelper.TagMatchingRules, bindingResult.Mappings[divTagHelper], TagMatchingRuleDescriptorComparer.Default);
    }
Exemplo n.º 8
0
    public void GetDescriptors_ReturnsCatchAllsWithEveryTagName()
    {
        // Arrange
        var divDescriptor = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly")
                            .TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
                            .Build();
        var spanDescriptor = TagHelperDescriptorBuilder.Create("foo2", "SomeAssembly")
                             .TagMatchingRuleDescriptor(rule => rule.RequireTagName("span"))
                             .Build();
        var catchAllDescriptor = TagHelperDescriptorBuilder.Create("foo3", "SomeAssembly")
                                 .TagMatchingRuleDescriptor(rule => rule.RequireTagName(TagHelperMatchingConventions.ElementCatchAllName))
                                 .Build();
        var descriptors     = new TagHelperDescriptor[] { divDescriptor, spanDescriptor, catchAllDescriptor };
        var tagHelperBinder = new TagHelperBinder(null, descriptors);

        // Act
        var divBinding = tagHelperBinder.GetBinding(
            tagName: "div",
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: "p",
            parentIsTagHelper: false);
        var spanBinding = tagHelperBinder.GetBinding(
            tagName: "span",
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: "p",
            parentIsTagHelper: false);

        // Assert
        // For divs
        Assert.Equal(2, divBinding.Descriptors.Count());
        Assert.Contains(divDescriptor, divBinding.Descriptors);
        Assert.Contains(catchAllDescriptor, divBinding.Descriptors);

        // For spans
        Assert.Equal(2, spanBinding.Descriptors.Count());
        Assert.Contains(spanDescriptor, spanBinding.Descriptors);
        Assert.Contains(catchAllDescriptor, spanBinding.Descriptors);
    }
Exemplo n.º 9
0
    public void GetBinding_ReturnsBindingResultDescriptorsWithRequiredAttributes(
        string tagName,
        IReadOnlyList <KeyValuePair <string, string> > providedAttributes,
        object availableDescriptors,
        object expectedDescriptors)
    {
        // Arrange
        var tagHelperBinder = new TagHelperBinder(null, (IReadOnlyList <TagHelperDescriptor>)availableDescriptors);

        // Act
        var bindingResult = tagHelperBinder.GetBinding(tagName, providedAttributes, parentTagName: "p", parentIsTagHelper: false);

        // Assert
        Assert.Equal((IEnumerable <TagHelperDescriptor>)expectedDescriptors, bindingResult?.Descriptors, TagHelperDescriptorComparer.Default);
    }
Exemplo n.º 10
0
    public void GetBinding_ReturnsBindingResultWithDescriptorsParentTags(
        string tagName,
        string parentTagName,
        object availableDescriptors,
        object expectedDescriptors)
    {
        // Arrange
        var tagHelperBinder = new TagHelperBinder(null, (IEnumerable <TagHelperDescriptor>)availableDescriptors);

        // Act
        var bindingResult = tagHelperBinder.GetBinding(
            tagName,
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: parentTagName,
            parentIsTagHelper: false);

        // Assert
        Assert.Equal((IEnumerable <TagHelperDescriptor>)expectedDescriptors, bindingResult.Descriptors, TagHelperDescriptorComparer.Default);
    }
Exemplo n.º 11
0
    public void GetBinding_ReturnsNullForUnprefixedTags(string tagName)
    {
        // Arrange
        var divDescriptor = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly")
                            .TagMatchingRuleDescriptor(rule => rule.RequireTagName(tagName))
                            .Build();
        var descriptors     = new[] { divDescriptor };
        var tagHelperBinder = new TagHelperBinder("th:", descriptors);

        // Act
        var bindingResult = tagHelperBinder.GetBinding(
            tagName: "div",
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: "p",
            parentIsTagHelper: false);

        // Assert
        Assert.Null(bindingResult);
    }
Exemplo n.º 12
0
    public void GetBinding_ReturnsNullBindingResultPrefixAsTagName()
    {
        // Arrange
        var catchAllDescriptor = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly")
                                 .TagMatchingRuleDescriptor(rule => rule.RequireTagName(TagHelperMatchingConventions.ElementCatchAllName))
                                 .Build();
        var descriptors     = new[] { catchAllDescriptor };
        var tagHelperBinder = new TagHelperBinder("th", descriptors);

        // Act
        var bindingResult = tagHelperBinder.GetBinding(
            tagName: "th",
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: "p",
            parentIsTagHelper: false);

        // Assert
        Assert.Null(bindingResult);
    }
Exemplo n.º 13
0
    public void GetDescriptors_DuplicateDescriptorsAreNotPartOfTagHelperDescriptorPool()
    {
        // Arrange
        var divDescriptor = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly")
                            .TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
                            .Build();
        var descriptors     = new TagHelperDescriptor[] { divDescriptor, divDescriptor };
        var tagHelperBinder = new TagHelperBinder(null, descriptors);

        // Act
        var bindingResult = tagHelperBinder.GetBinding(
            tagName: "div",
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: "p",
            parentIsTagHelper: false);

        // Assert
        var descriptor = Assert.Single(bindingResult.Descriptors);

        Assert.Same(divDescriptor, descriptor);
    }
Exemplo n.º 14
0
    public void GetDescriptors_ReturnsNothingForUnregisteredTags()
    {
        // Arrange
        var divDescriptor = TagHelperDescriptorBuilder.Create("foo1", "SomeAssembly")
                            .TagMatchingRuleDescriptor(rule => rule.RequireTagName("div"))
                            .Build();
        var spanDescriptor = TagHelperDescriptorBuilder.Create("foo2", "SomeAssembly")
                             .TagMatchingRuleDescriptor(rule => rule.RequireTagName("span"))
                             .Build();
        var descriptors     = new TagHelperDescriptor[] { divDescriptor, spanDescriptor };
        var tagHelperBinder = new TagHelperBinder(null, descriptors);

        // Act
        var tagHelperBinding = tagHelperBinder.GetBinding(
            tagName: "foo",
            attributes: Array.Empty <KeyValuePair <string, string> >(),
            parentTagName: "p",
            parentIsTagHelper: false);

        // Assert
        Assert.Null(tagHelperBinding);
    }