private static string ResolveTagHelperPrefix(TagHelperDescriptorResolutionContext context)
        {
            var prefixDirectiveDescriptors = context.DirectiveDescriptors.Where(
                descriptor => descriptor.DirectiveType == TagHelperDirectiveType.TagHelperPrefix);

            TagHelperDirectiveDescriptor prefixDirective = null;

            foreach (var directive in prefixDirectiveDescriptors)
            {
                if (prefixDirective == null)
                {
                    prefixDirective = directive;
                }
                else
                {
                    // For each invalid @tagHelperPrefix we need to create an error.
                    context.ErrorSink.OnError(
                        directive.Location,
                        Resources.FormatTagHelperDescriptorResolver_InvalidTagHelperDirective(
                            SyntaxConstants.CSharp.TagHelperPrefixKeyword),
                        GetErrorLength(directive.DirectiveText));
                }
            }

            var prefix = prefixDirective?.DirectiveText;

            if (prefix != null && !EnsureValidPrefix(prefix, prefixDirective.Location, context.ErrorSink))
            {
                prefix = null;
            }

            return(prefix);
        }
Пример #2
0
        public void DescriptorResolver_UnderstandsUnexpectedExceptions_DoesNotThrow()
        {
            // Arrange
            var expectedErrorMessage = "Encountered an unexpected error when attempting to resolve tag helper " +
                                       "directive '@addtaghelper' with value 'A custom lookup text'. Error: A " +
                                       "custom exception";
            var documentLocation            = new SourceLocation(1, 2, 3);
            var directiveType               = TagHelperDirectiveType.AddTagHelper;
            var errorSink                   = new ParserErrorSink();
            var expectedError               = new Exception("A custom exception");
            var tagHelperDescriptorResolver = new ThrowingTagHelperDescriptorResolver(expectedError);
            var resolutionContext           = new TagHelperDescriptorResolutionContext(
                new[] { new TagHelperDirectiveDescriptor("A custom lookup text", documentLocation, directiveType) },
                errorSink);


            // Act
            tagHelperDescriptorResolver.Resolve(resolutionContext);

            // Assert
            var error = Assert.Single(errorSink.Errors);

            Assert.Equal(1, error.Length);
            Assert.Equal(documentLocation, error.Location);
            Assert.Equal(expectedErrorMessage, error.Message);
        }
        private static IEnumerable <TagHelperDescriptor> PrefixDescriptors(
            TagHelperDescriptorResolutionContext context,
            IEnumerable <TagHelperDescriptor> descriptors)
        {
            var tagHelperPrefix = ResolveTagHelperPrefix(context);

            if (!string.IsNullOrEmpty(tagHelperPrefix))
            {
                return(descriptors.Select(descriptor =>
                                          new TagHelperDescriptor
                {
                    Prefix = tagHelperPrefix,
                    TagName = descriptor.TagName,
                    TypeName = descriptor.TypeName,
                    AssemblyName = descriptor.AssemblyName,
                    Attributes = descriptor.Attributes,
                    RequiredAttributes = descriptor.RequiredAttributes,
                    AllowedChildren = descriptor.AllowedChildren,
                    RequiredParent = descriptor.RequiredParent,
                    TagStructure = descriptor.TagStructure,
                    DesignTimeDescriptor = descriptor.DesignTimeDescriptor
                }));
            }

            return(descriptors);
        }
Пример #4
0
        public void DescriptorResolver_CreatesErrorIfNullOrEmptyLookupText_DoesNotThrow(string lookupText)
        {
            // Arrange
            var errorSink = new ParserErrorSink();
            var tagHelperDescriptorResolver =
                new TestTagHelperDescriptorResolver(
                    new TestTagHelperTypeResolver(InvalidTestableTagHelpers));
            var documentLocation     = new SourceLocation(1, 2, 3);
            var directiveType        = TagHelperDirectiveType.AddTagHelper;
            var expectedErrorMessage =
                Resources.FormatTagHelperDescriptorResolver_InvalidTagHelperLookupText(lookupText);
            var resolutionContext = new TagHelperDescriptorResolutionContext(
                new [] { new TagHelperDirectiveDescriptor(lookupText, documentLocation, directiveType) },
                errorSink);

            // Act
            tagHelperDescriptorResolver.Resolve(resolutionContext);

            // Assert
            var error = Assert.Single(errorSink.Errors);

            Assert.Equal(1, error.Length);
            Assert.Equal(documentLocation, error.Location);
            Assert.Equal(expectedErrorMessage, error.Message);
        }
        public void Resolve_CalculatesAssemblyLocationInLookupText(string lookupText, int assemblyLocation)
        {
            // Arrange
            var errorSink = new ErrorSink();
            var tagHelperDescriptorResolver = new InspectableTagHelperDescriptorResolver();
            var directiveType = TagHelperDirectiveType.AddTagHelper;
            var resolutionContext = new TagHelperDescriptorResolutionContext(
                new[]
                {
                    new TagHelperDirectiveDescriptor
                    {
                        DirectiveText = lookupText,
                        Location = SourceLocation.Zero,
                        DirectiveType = directiveType
                    }
                },
                errorSink);
            var expectedSourceLocation = new SourceLocation(assemblyLocation, 0, assemblyLocation);

            // Act
            tagHelperDescriptorResolver.Resolve(resolutionContext);

            // Assert
            Assert.Empty(errorSink.Errors);
            Assert.Equal(expectedSourceLocation, tagHelperDescriptorResolver.DocumentLocation);
        }
Пример #6
0
        /// <inheritdoc />
        public IEnumerable <TagHelperDescriptor> Resolve([NotNull] TagHelperDescriptorResolutionContext context)
        {
            var resolvedDescriptors = new HashSet <TagHelperDescriptor>(TagHelperDescriptorComparer.Default);

            // tagHelperPrefix directives do not affect which TagHelperDescriptors are added or removed from the final
            // list, need to remove them.
            var actionableDirectiveDescriptors = context.DirectiveDescriptors.Where(
                directive => directive.DirectiveType != TagHelperDirectiveType.TagHelperPrefix);

            foreach (var directiveDescriptor in actionableDirectiveDescriptors)
            {
                try
                {
                    var lookupInfo = GetLookupInfo(directiveDescriptor, context.ErrorSink);

                    // Could not resolve the lookup info.
                    if (lookupInfo == null)
                    {
                        return(Enumerable.Empty <TagHelperDescriptor>());
                    }

                    if (directiveDescriptor.DirectiveType == TagHelperDirectiveType.RemoveTagHelper)
                    {
                        resolvedDescriptors.RemoveWhere(descriptor => MatchesLookupInfo(descriptor, lookupInfo));
                    }
                    else if (directiveDescriptor.DirectiveType == TagHelperDirectiveType.AddTagHelper)
                    {
                        var descriptors = ResolveDescriptorsInAssembly(
                            lookupInfo.AssemblyName,
                            directiveDescriptor.Location,
                            context.ErrorSink);

                        // Only use descriptors that match our lookup info
                        descriptors = descriptors.Where(descriptor => MatchesLookupInfo(descriptor, lookupInfo));

                        resolvedDescriptors.UnionWith(descriptors);
                    }
                }
                catch (Exception ex)
                {
                    string directiveName;
                    _directiveNames.TryGetValue(directiveDescriptor.DirectiveType, out directiveName);
                    Debug.Assert(!string.IsNullOrEmpty(directiveName));

                    context.ErrorSink.OnError(
                        directiveDescriptor.Location,
                        Resources.FormatTagHelperDescriptorResolver_EncounteredUnexpectedError(
                            "@" + directiveName,
                            directiveDescriptor.DirectiveText,
                            ex.Message));
                }
            }

            var prefixedDescriptors = PrefixDescriptors(context, resolvedDescriptors);

            return(prefixedDescriptors);
        }
Пример #7
0
        public IEnumerable <TagHelperDescriptor> Resolve(TagHelperDescriptorResolutionContext resolutionContext)
        {
            var descriptors = new List <TagHelperDescriptor>();

            descriptors.AddRange(_tagHelperDescriptorResolver.Resolve(resolutionContext));
            descriptors.AddRange(_viewComponentTagHelperDescriptorResolver.Resolve(resolutionContext));

            return(descriptors);
        }
Пример #8
0
        public void Resolve_AllowsOverridenResolveDescriptorsInAssembly(string lookupText, string expectedAssemblyName)
        {
            // Arrange
            var tagHelperDescriptorResolver = new AssemblyCheckingTagHelperDescriptorResolver();
            var context = new TagHelperDescriptorResolutionContext(
                new[] { new TagHelperDirectiveDescriptor(lookupText, TagHelperDirectiveType.AddTagHelper) },
                new ParserErrorSink());

            // Act
            tagHelperDescriptorResolver.Resolve(context);

            // Assert
            Assert.Equal(expectedAssemblyName, tagHelperDescriptorResolver.CalledWithAssemblyName);
        }
        IEnumerable<TagHelperDescriptor> ITagHelperDescriptorResolver.Resolve(TagHelperDescriptorResolutionContext resolutionContext)
        {
            var descriptors = base.Resolve(resolutionContext);

            var shapeTagDescriptors = ResolveViewComponentTagHelperDescriptors(descriptors.FirstOrDefault()?.Prefix ?? string.Empty);

            // Remove the descriptors that were hravested from tooling classes
            var allShapeTags = shapeTagDescriptors
                .Select(x => x.TagName.ToLowerInvariant())
                .ToArray();

            descriptors = descriptors
                .Where(x => !allShapeTags.Contains(x.TagName.ToLowerInvariant()));

            return descriptors.Concat(shapeTagDescriptors);
        }
        private static IEnumerable <TagHelperDescriptor> PrefixDescriptors(
            TagHelperDescriptorResolutionContext context,
            IEnumerable <TagHelperDescriptor> descriptors)
        {
            var tagHelperPrefix = ResolveTagHelperPrefix(context);

            if (!string.IsNullOrEmpty(tagHelperPrefix))
            {
                return(descriptors.Select(descriptor => new TagHelperDescriptor(descriptor)
                {
                    Prefix = tagHelperPrefix
                }));
            }

            return(descriptors);
        }
Пример #11
0
        IEnumerable <TagHelperDescriptor> ITagHelperDescriptorResolver.Resolve(TagHelperDescriptorResolutionContext resolutionContext)
        {
            var descriptors = base.Resolve(resolutionContext);

            var shapeTagDescriptors = ResolveViewComponentTagHelperDescriptors(descriptors.FirstOrDefault()?.Prefix ?? string.Empty);

            // Remove the descriptors that were hravested from tooling classes
            var allShapeTags = shapeTagDescriptors
                               .Select(x => x.TagName.ToLowerInvariant())
                               .ToArray();

            descriptors = descriptors
                          .Where(x => !allShapeTags.Contains(x.TagName.ToLowerInvariant()));

            return(descriptors.Concat(shapeTagDescriptors));
        }
Пример #12
0
        /// <inheritdoc />
        public IEnumerable <TagHelperDescriptor> Resolve([NotNull] TagHelperDescriptorResolutionContext context)
        {
            var resolvedDescriptors = new HashSet <TagHelperDescriptor>(TagHelperDescriptorComparer.Default);

            foreach (var directiveDescriptor in context.DirectiveDescriptors)
            {
                try
                {
                    var lookupInfo = GetLookupInfo(directiveDescriptor, context.ErrorSink);

                    // Could not resolve the lookup info.
                    if (lookupInfo == null)
                    {
                        return(Enumerable.Empty <TagHelperDescriptor>());
                    }

                    if (directiveDescriptor.DirectiveType == TagHelperDirectiveType.RemoveTagHelper)
                    {
                        resolvedDescriptors.RemoveWhere(descriptor => MatchesLookupInfo(descriptor, lookupInfo));
                    }
                    else if (directiveDescriptor.DirectiveType == TagHelperDirectiveType.AddTagHelper)
                    {
                        var descriptors = ResolveDescriptorsInAssembly(lookupInfo.AssemblyName,
                                                                       directiveDescriptor.Location,
                                                                       context.ErrorSink);

                        // Only use descriptors that match our lookup info
                        descriptors = descriptors.Where(descriptor => MatchesLookupInfo(descriptor, lookupInfo));

                        resolvedDescriptors.UnionWith(descriptors);
                    }
                }
                catch (Exception ex)
                {
                    var directiveName = "@" + directiveDescriptor.DirectiveType.ToString().ToLowerInvariant();

                    context.ErrorSink.OnError(
                        directiveDescriptor.Location,
                        Resources.FormatTagHelperDescriptorResolver_EncounteredUnexpectedError(
                            directiveName,
                            directiveDescriptor.LookupText,
                            ex.Message));
                }
            }

            return(resolvedDescriptors);
        }
Пример #13
0
        public void Resolve_CanReturnEmptyDescriptorsBasedOnDirectiveDescriptors(
            Dictionary <string, IEnumerable <Type> > descriptorAssemblyLookups,
            IEnumerable <TagHelperDirectiveDescriptor> directiveDescriptors)
        {
            // Arrange
            var tagHelperDescriptorResolver =
                new TestTagHelperDescriptorResolver(
                    new LookupBasedTagHelperTypeResolver(descriptorAssemblyLookups));
            var resolutionContext = new TagHelperDescriptorResolutionContext(
                directiveDescriptors,
                new ParserErrorSink());

            // Act
            var descriptors = tagHelperDescriptorResolver.Resolve(resolutionContext);

            // Assert
            Assert.Empty(descriptors);
        }
        IEnumerable <TagHelperDescriptor> ITagHelperDescriptorResolver.Resolve(TagHelperDescriptorResolutionContext resolutionContext)
        {
            // This method is called for every compiled view that contains @tagHelpers. We are caching the shape
            // ones are they are the same for all the views. Furthermore the shapes are discovered using a null
            // theme as we need to grab all the potential shapes

            var descriptors = base.Resolve(resolutionContext);
            var prefix      = descriptors.FirstOrDefault()?.Prefix ?? string.Empty;

            var serviceProvider   = _httpContextAccessor.HttpContext.RequestServices;
            var shapeTableManager = serviceProvider.GetService <IShapeTableManager>();

            // During Setup, shapeTableManager is null
            if (shapeTableManager == null)
            {
                return(descriptors);
            }

            var shapeTagDescriptors = new List <TagHelperDescriptor>();

            foreach (var shape in shapeTableManager.GetShapeTable(null).Descriptors)
            {
                // Don't add the shape tag if another provider already described it
                if (descriptors.Any(x => string.Equals(x.TagName, shape.Key, StringComparison.OrdinalIgnoreCase)))
                {
                    continue;
                }

                shapeTagDescriptors.Add(
                    new TagHelperDescriptor
                {
                    Prefix             = prefix, // The prefix might be different for each call, even if the same shape is rendered
                    TagName            = shape.Key,
                    TypeName           = ShapeTagHelperType.FullName,
                    AssemblyName       = ShapeTagHelperType.GetTypeInfo().Assembly.GetName().Name,
                    Attributes         = Enumerable.Empty <TagHelperAttributeDescriptor>(),
                    RequiredAttributes = Enumerable.Empty <TagHelperRequiredAttributeDescriptor>()
                });
            }

            return(descriptors.Concat(shapeTagDescriptors));
        }
        private static IEnumerable <TagHelperDescriptor> PrefixDescriptors(
            TagHelperDescriptorResolutionContext context,
            IEnumerable <TagHelperDescriptor> descriptors)
        {
            var tagHelperPrefix = ResolveTagHelperPrefix(context);

            if (!string.IsNullOrEmpty(tagHelperPrefix))
            {
                return(descriptors.Select(descriptor =>
                                          new TagHelperDescriptor(
                                              tagHelperPrefix,
                                              descriptor.TagName,
                                              descriptor.TypeName,
                                              descriptor.AssemblyName,
                                              descriptor.Attributes,
                                              descriptor.RequiredAttributes)));
            }

            return(descriptors);
        }
        IEnumerable<TagHelperDescriptor> ITagHelperDescriptorResolver.Resolve(TagHelperDescriptorResolutionContext resolutionContext)
        {
            // This method is called for every compiled view that contains @tagHelpers. We are caching the shape
            // ones are they are the same for all the views. Furthermore the shapes are discovered using a null
            // theme as we need to grab all the potential shapes

            var descriptors = base.Resolve(resolutionContext);
            var prefix = descriptors.FirstOrDefault()?.Prefix ?? string.Empty;

            var serviceProvider = _httpContextAccessor.HttpContext.RequestServices;
            var shapeTableManager = serviceProvider.GetService<IShapeTableManager>();

            // During Setup, shapeTableManager is null
            if (shapeTableManager == null)
            {
                return descriptors;
            }

            var shapeTagDescriptors = new List<TagHelperDescriptor>();
            foreach (var shape in shapeTableManager.GetShapeTable(null).Descriptors)
            {
                // Don't add the shape tag if another provider already described it
                if (descriptors.Any(x => string.Equals(x.TagName, shape.Key, StringComparison.OrdinalIgnoreCase)))
                {
                    continue;
                }

                shapeTagDescriptors.Add(
                    new TagHelperDescriptor
                    {
                        Prefix = prefix, // The prefix might be different for each call, even if the same shape is rendered
                        TagName = shape.Key,
                        TypeName = ShapeTagHelperType.FullName,
                        AssemblyName = ShapeTagHelperType.GetTypeInfo().Assembly.GetName().Name,
                        Attributes = Enumerable.Empty<TagHelperAttributeDescriptor>(),
                        RequiredAttributes = Enumerable.Empty<TagHelperRequiredAttributeDescriptor>()
                    });
            }

            return descriptors.Concat(shapeTagDescriptors);
        }
Пример #17
0
            public IEnumerable <TagHelperDescriptor> Resolve(TagHelperDescriptorResolutionContext resolutionContext)
            {
                IEnumerable <TagHelperDescriptor> descriptors = null;

                foreach (var directiveDescriptor in resolutionContext.DirectiveDescriptors)
                {
                    if (directiveDescriptor.DirectiveType == TagHelperDirectiveType.RemoveTagHelper)
                    {
                        // We don't yet support "typeName, assemblyName" for @removeTagHelper in this test class. Will
                        // add that ability and add the corresponding end-to-end test verification in:
                        // https://github.com/aspnet/Razor/issues/222
                        descriptors = null;
                    }
                    else if (directiveDescriptor.DirectiveType == TagHelperDirectiveType.AddTagHelper)
                    {
                        descriptors = _tagHelperDescriptors;
                    }
                }

                return(descriptors ?? Enumerable.Empty <TagHelperDescriptor>());
            }
Пример #18
0
        public void TagHelperResolution(bool designTime)
        {
            var descriptorResolver    = new TagHelperDescriptorResolver(designTime);
            var errorSink             = new ErrorSink();
            var addTagHelperDirective = new TagHelperDirectiveDescriptor
            {
                DirectiveText = "*, Microsoft.AspNetCore.Mvc.TagHelpers",
                DirectiveType = TagHelperDirectiveType.AddTagHelper,
                Location      = SourceLocation.Zero
            };
            var resolutionContext = new TagHelperDescriptorResolutionContext(
                new[] { addTagHelperDirective },
                errorSink);
            IEnumerable <TagHelperDescriptor> descriptors;

            using (Collector.StartCollection())
            {
                descriptors = descriptorResolver.Resolve(resolutionContext);
            }

            Assert.NotEmpty(descriptors);
            Assert.Empty(errorSink.Errors);
        }
Пример #19
0
        public void Resolve_ReturnsDescriptorsBasedOnDirectiveDescriptors(
            Dictionary <string, IEnumerable <Type> > descriptorAssemblyLookups,
            IEnumerable <TagHelperDirectiveDescriptor> directiveDescriptors,
            IEnumerable <TagHelperDescriptor> expectedDescriptors)
        {
            // Arrange
            var tagHelperDescriptorResolver =
                new TestTagHelperDescriptorResolver(
                    new LookupBasedTagHelperTypeResolver(descriptorAssemblyLookups));
            var resolutionContext = new TagHelperDescriptorResolutionContext(
                directiveDescriptors,
                new ParserErrorSink());

            // Act
            var descriptors = tagHelperDescriptorResolver.Resolve(resolutionContext);

            // Assert
            Assert.Equal(expectedDescriptors.Count(), descriptors.Count());

            foreach (var expectedDescriptor in expectedDescriptors)
            {
                Assert.Contains(expectedDescriptor, descriptors, TagHelperDescriptorComparer.Default);
            }
        }
        public void DescriptorResolver_CreatesErrorIfInvalidLookupText_DoesNotThrow(string lookupText, int errorLength)
        {
            // Arrange
            var errorSink = new ErrorSink();
            var tagHelperDescriptorResolver =
                new TestTagHelperDescriptorResolver(
                    new TestTagHelperTypeResolver(InvalidTestableTagHelpers));
            var documentLocation = new SourceLocation(1, 2, 3);
            var directiveType = TagHelperDirectiveType.AddTagHelper;
            var expectedErrorMessage = string.Format(
                "Invalid tag helper directive look up text '{0}'. The correct look up text " +
                "format is: \"typeName, assemblyName\".",
                lookupText);
            var resolutionContext = new TagHelperDescriptorResolutionContext(
                new[]
                {
                    new TagHelperDirectiveDescriptor
                    {
                        DirectiveText = lookupText,
                        Location = documentLocation,
                        DirectiveType = directiveType
                    }
                },
                errorSink);

            // Act
            tagHelperDescriptorResolver.Resolve(resolutionContext);

            // Assert
            var error = Assert.Single(errorSink.Errors);
            Assert.Equal(errorLength, error.Length);
            Assert.Equal(documentLocation, error.Location);
            Assert.Equal(expectedErrorMessage, error.Message);
        }
Пример #21
0
        /// <inheritdoc />
        public IEnumerable<TagHelperDescriptor> Resolve(TagHelperDescriptorResolutionContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var resolvedDescriptors = new HashSet<TagHelperDescriptor>(TagHelperDescriptorComparer.Default);

            // tagHelperPrefix directives do not affect which TagHelperDescriptors are added or removed from the final
            // list, need to remove them.
            var actionableDirectiveDescriptors = context.DirectiveDescriptors.Where(
                directive => directive.DirectiveType != TagHelperDirectiveType.TagHelperPrefix);

            foreach (var directiveDescriptor in actionableDirectiveDescriptors)
            {
                try
                {
                    var lookupInfo = GetLookupInfo(directiveDescriptor, context.ErrorSink);

                    // Could not resolve the lookup info.
                    if (lookupInfo == null)
                    {
                        return Enumerable.Empty<TagHelperDescriptor>();
                    }

                    if (directiveDescriptor.DirectiveType == TagHelperDirectiveType.RemoveTagHelper)
                    {
                        resolvedDescriptors.RemoveWhere(descriptor => MatchesLookupInfo(descriptor, lookupInfo));
                    }
                    else if (directiveDescriptor.DirectiveType == TagHelperDirectiveType.AddTagHelper)
                    {
                        var descriptors = ResolveDescriptorsInAssembly(
                            lookupInfo.AssemblyName,
                            lookupInfo.AssemblyNameLocation,
                            context.ErrorSink);

                        // Only use descriptors that match our lookup info
                        descriptors = descriptors.Where(descriptor => MatchesLookupInfo(descriptor, lookupInfo));

                        resolvedDescriptors.UnionWith(descriptors);
                    }
                }
                catch (Exception ex)
                {
                    string directiveName;
                    _directiveNames.TryGetValue(directiveDescriptor.DirectiveType, out directiveName);
                    Debug.Assert(!string.IsNullOrEmpty(directiveName));

                    context.ErrorSink.OnError(
                        directiveDescriptor.Location,
                        Resources.FormatTagHelperDescriptorResolver_EncounteredUnexpectedError(
                            "@" + directiveName,
                            directiveDescriptor.DirectiveText,
                            ex.Message),
                        GetErrorLength(directiveDescriptor.DirectiveText));
                }
            }

            var prefixedDescriptors = PrefixDescriptors(context, resolvedDescriptors);

            return prefixedDescriptors;
        }
Пример #22
0
 public IEnumerable <TagHelperDescriptor> Resolve(TagHelperDescriptorResolutionContext resolutionContext)
 {
     return(Enumerable.Empty <TagHelperDescriptor>());
 }
Пример #23
0
        private static IEnumerable<TagHelperDescriptor> PrefixDescriptors(
            TagHelperDescriptorResolutionContext context,
            IEnumerable<TagHelperDescriptor> descriptors)
        {
            var tagHelperPrefix = ResolveTagHelperPrefix(context);

            if (!string.IsNullOrEmpty(tagHelperPrefix))
            {
                return descriptors.Select(descriptor =>
                    new TagHelperDescriptor
                    {
                        Prefix = tagHelperPrefix,
                        TagName = descriptor.TagName,
                        TypeName = descriptor.TypeName,
                        AssemblyName = descriptor.AssemblyName,
                        Attributes = descriptor.Attributes,
                        RequiredAttributes = descriptor.RequiredAttributes,
                        AllowedChildren = descriptor.AllowedChildren,
                        RequiredParent = descriptor.RequiredParent,
                        TagStructure = descriptor.TagStructure,
                        DesignTimeDescriptor = descriptor.DesignTimeDescriptor
                    });
            }

            return descriptors;
        }
Пример #24
0
        private static string ResolveTagHelperPrefix(TagHelperDescriptorResolutionContext context)
        {
            var prefixDirectiveDescriptors = context.DirectiveDescriptors.Where(
                descriptor => descriptor.DirectiveType == TagHelperDirectiveType.TagHelperPrefix);

            TagHelperDirectiveDescriptor prefixDirective = null;

            foreach (var directive in prefixDirectiveDescriptors)
            {
                if (prefixDirective == null)
                {
                    prefixDirective = directive;
                }
                else
                {
                    // For each invalid @tagHelperPrefix we need to create an error.
                    context.ErrorSink.OnError(
                        directive.Location,
                        Resources.FormatTagHelperDescriptorResolver_InvalidTagHelperDirective(
                            SyntaxConstants.CSharp.TagHelperPrefixKeyword),
                        GetErrorLength(directive.DirectiveText));
                }
            }

            var prefix = prefixDirective?.DirectiveText;

            if (prefix != null && !EnsureValidPrefix(prefix, prefixDirective.Location, context.ErrorSink))
            {
                prefix = null;
            }

            return prefix;
        }
Пример #25
0
            public IEnumerable<TagHelperDescriptor> Resolve(TagHelperDescriptorResolutionContext resolutionContext)
            {
                IEnumerable<TagHelperDescriptor> descriptors = null;

                foreach (var directiveDescriptor in resolutionContext.DirectiveDescriptors)
                {
                    if (directiveDescriptor.DirectiveType == TagHelperDirectiveType.RemoveTagHelper)
                    {
                        // We don't yet support "typeName, assemblyName" for @removeTagHelper in this test class. Will
                        // add that ability and add the corresponding end-to-end test verification in:
                        // https://github.com/aspnet/Razor/issues/222
                        descriptors = null;
                    }
                    else if (directiveDescriptor.DirectiveType == TagHelperDirectiveType.AddTagHelper)
                    {
                        descriptors = _tagHelperDescriptors;
                    }
                }

                return descriptors ?? Enumerable.Empty<TagHelperDescriptor>();
            }
        public void Resolve_CanReturnEmptyDescriptorsBasedOnDirectiveDescriptors(
            Dictionary<string, IEnumerable<Type>> descriptorAssemblyLookups,
            IEnumerable<TagHelperDirectiveDescriptor> directiveDescriptors)
        {
            // Arrange
            var tagHelperDescriptorResolver =
                new TestTagHelperDescriptorResolver(
                    new LookupBasedTagHelperTypeResolver(descriptorAssemblyLookups));
            var resolutionContext = new TagHelperDescriptorResolutionContext(
                directiveDescriptors,
                new ErrorSink());

            // Act
            var descriptors = tagHelperDescriptorResolver.Resolve(resolutionContext);

            // Assert
            Assert.Empty(descriptors);
        }
        public void DescriptorResolver_UnderstandsUnexpectedExceptions_DoesNotThrow()
        {
            // Arrange
            var expectedErrorMessage = "Encountered an unexpected error when attempting to resolve tag helper " +
                                       "directive '@addTagHelper' with value 'A custom, lookup text'. Error: A " +
                                       "custom exception";
            var documentLocation = new SourceLocation(1, 2, 3);
            var directiveType = TagHelperDirectiveType.AddTagHelper;
            var errorSink = new ErrorSink();
            var expectedError = new Exception("A custom exception");
            var tagHelperDescriptorResolver = new ThrowingTagHelperDescriptorResolver(expectedError);
            var resolutionContext = new TagHelperDescriptorResolutionContext(
                new[]
                {
                    new TagHelperDirectiveDescriptor
                    {
                        DirectiveText = "A custom, lookup text",
                        Location = documentLocation,
                        DirectiveType = directiveType
                    }
                },
                errorSink);


            // Act
            tagHelperDescriptorResolver.Resolve(resolutionContext);

            // Assert
            var error = Assert.Single(errorSink.Errors);
            Assert.Equal(21, error.Length);
            Assert.Equal(documentLocation, error.Location);
            Assert.Equal(expectedErrorMessage, error.Message);
        }
        public void Resolve_AllowsOverridenResolveDescriptorsInAssembly(string lookupText, string expectedAssemblyName)
        {
            // Arrange
            var tagHelperDescriptorResolver = new AssemblyCheckingTagHelperDescriptorResolver();
            var context = new TagHelperDescriptorResolutionContext(
                new[] { CreateTagHelperDirectiveDescriptor(lookupText, TagHelperDirectiveType.AddTagHelper) },
                new ErrorSink());

            // Act
            tagHelperDescriptorResolver.Resolve(context);

            // Assert
            Assert.Equal(expectedAssemblyName, tagHelperDescriptorResolver.CalledWithAssemblyName);
        }
        public void Resolve_ReturnsDescriptorsBasedOnDirectiveDescriptors(
            Dictionary<string, IEnumerable<Type>> descriptorAssemblyLookups,
            IEnumerable<TagHelperDirectiveDescriptor> directiveDescriptors,
            IEnumerable<TagHelperDescriptor> expectedDescriptors)
        {
            // Arrange
            var tagHelperDescriptorResolver =
                new TestTagHelperDescriptorResolver(
                    new LookupBasedTagHelperTypeResolver(descriptorAssemblyLookups));
            var resolutionContext = new TagHelperDescriptorResolutionContext(
                directiveDescriptors,
                new ErrorSink());

            // Act
            var descriptors = tagHelperDescriptorResolver.Resolve(resolutionContext);

            // Assert
            Assert.Equal(expectedDescriptors.Count(), descriptors.Count());

            foreach (var expectedDescriptor in expectedDescriptors)
            {
                Assert.Contains(expectedDescriptor, descriptors, TagHelperDescriptorComparer.Default);
            }
        }