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 serviceProvider = _httpContextAccessor.HttpContext.RequestServices;
            var shapeTableManager = (IShapeTableManager)serviceProvider.GetService(typeof(IShapeTableManager));

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

            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<string>(),
                    });
            }
            
            return descriptors.Concat(shapeTagDescriptors);
        }
示例#2
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>();
            }
        /// <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;
        }
        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;
        }
        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;
        }
示例#6
0
            public IEnumerable <TagHelperDescriptor> Resolve(TagHelperDescriptorResolutionContext resolutionContext)
            {
                DirectiveDescriptors.AddRange(resolutionContext.DirectiveDescriptors);

                return(Enumerable.Empty <TagHelperDescriptor>());
            }