예제 #1
0
        public IEnumerable <TagHelperDescriptor> GetDescriptors([NotNull] Block root)
        {
            _directiveDescriptors = new List <TagHelperDirectiveDescriptor>();

            // This will recurse through the syntax tree.
            VisitBlock(root);

            var resolutionContext = GetTagHelperDescriptorResolutionContext(_directiveDescriptors, _errorSink);
            var descriptors       = _descriptorResolver.Resolve(resolutionContext);

            return(descriptors);
        }
        public IEnumerable <TagHelperDescriptor> GetDescriptors(Block root)
        {
            if (root == null)
            {
                throw new ArgumentNullException(nameof(root));
            }

            _directiveDescriptors = new List <TagHelperDirectiveDescriptor>();

            // This will recurse through the syntax tree.
            VisitBlock(root);

            var resolutionContext = GetTagHelperDescriptorResolutionContext(_directiveDescriptors, _errorSink);
            var descriptors       = _descriptorResolver.Resolve(resolutionContext);

            return(descriptors);
        }
        public override void VisitSpan(Span span)
        {
            // We're only interested in spans with an AddOrRemoveTagHelperCodeGenerator.

            if (span.CodeGenerator is AddOrRemoveTagHelperCodeGenerator)
            {
                var codeGenerator = (AddOrRemoveTagHelperCodeGenerator)span.CodeGenerator;

                if (_descriptorResolver == null)
                {
                    var directive = codeGenerator.RemoveTagHelperDescriptors ?
                                    SyntaxConstants.CSharp.RemoveTagHelperKeyword :
                                    SyntaxConstants.CSharp.AddTagHelperKeyword;

                    throw new InvalidOperationException(
                              RazorResources.FormatTagHelpers_CannotUseDirectiveWithNoTagHelperDescriptorResolver(
                                  directive, typeof(ITagHelperDescriptorResolver).FullName, typeof(RazorParser).FullName));
                }

                // Look up all the descriptors associated with the "LookupText".
                var descriptors = _descriptorResolver.Resolve(codeGenerator.LookupText);

                if (codeGenerator.RemoveTagHelperDescriptors)
                {
                    var evaluatedDescriptors =
                        new HashSet <TagHelperDescriptor>(descriptors, TagHelperDescriptorComparer.Default);

                    // We remove all found descriptors from the descriptor list to ignore the associated TagHelpers on the
                    // Razor page.
                    _descriptors.RemoveAll(descriptor => evaluatedDescriptors.Contains(descriptor));
                }
                else
                {
                    // Add all the found descriptors to our list.
                    _descriptors.AddRange(descriptors);
                }
            }
        }