Пример #1
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");

            // Assert
            Assert.Null(tagHelperBinding);
        }
Пример #2
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);
        }
Пример #3
0
        private static bool MatchesDirective(TagHelperDescriptor descriptor, string typePattern, string assemblyName)
        {
            if (!string.Equals(descriptor.AssemblyName, assemblyName, StringComparison.Ordinal))
            {
                return(false);
            }

            if (typePattern.EndsWith("*", StringComparison.Ordinal))
            {
                if (typePattern.Length == 1)
                {
                    // TypePattern is "*".
                    return(true);
                }

                var lookupTypeName = typePattern.Substring(0, typePattern.Length - 1);

                return(descriptor.Name.StartsWith(lookupTypeName, StringComparison.Ordinal));
            }

            return(string.Equals(descriptor.Name, typePattern, StringComparison.Ordinal));
        }
Пример #4
0
        private void Register(TagHelperDescriptor descriptor)
        {
            var count = descriptor.TagMatchingRules.Count;

            for (var i = 0; i < count; i++)
            {
                var rule            = descriptor.TagMatchingRules[i];
                var registrationKey =
                    string.Equals(rule.TagName, TagHelperMatchingConventions.ElementCatchAllName, StringComparison.Ordinal) ?
                    TagHelperMatchingConventions.ElementCatchAllName :
                    _tagHelperPrefix + rule.TagName;

                // Ensure there's a HashSet to add the descriptor to.
                if (!_registrations.TryGetValue(registrationKey, out HashSet <TagHelperDescriptor> descriptorSet))
                {
                    descriptorSet = new HashSet <TagHelperDescriptor>(TagHelperDescriptorComparer.Default);
                    _registrations[registrationKey] = descriptorSet;
                }

                descriptorSet.Add(descriptor);
            }
        }
        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);
        }
 internal static bool TrySplitNamespaceAndType(TagHelperDescriptor tagHelperDescriptor, out StringSegment @namespace)
 => TrySplitNamespaceAndType(tagHelperDescriptor, out @namespace, out _);
Пример #7
0
 public IReadOnlyList <TagMatchingRuleDescriptor> GetBoundRules(TagHelperDescriptor descriptor)
 {
     return(_mappings[descriptor]);
 }