/// <summary>
        /// Starts the generation of a <see cref="TagHelperChunk"/>.
        /// </summary>
        /// <param name="target">
        /// The <see cref="Block"/> responsible for this <see cref="TagHelperCodeGenerator"/>.
        /// </param>
        /// <param name="context">A <see cref="CodeGeneratorContext"/> instance that contains information about
        /// the current code generation process.</param>
        public override void GenerateStartBlockCode(Block target, CodeGeneratorContext context)
        {
            var tagHelperBlock = target as TagHelperBlock;

            if (tagHelperBlock == null)
            {
                throw new ArgumentException(
                    RazorResources.TagHelpers_TagHelperCodeGeneartorMustBeAssociatedWithATagHelperBlock);
            }

            var attributes = new List<KeyValuePair<string, Chunk>>();

            // We need to create a code generator to create chunks for each of the attributes.
            var codeGenerator = context.Host.CreateCodeGenerator(
                context.ClassName,
                context.RootNamespace,
                context.SourceFile);

            foreach (var attribute in tagHelperBlock.Attributes)
            {
                ChunkBlock attributeChunkValue = null;

                if (attribute.Value != null)
                {
                    // Populates the code tree with chunks associated with attributes
                    attribute.Value.Accept(codeGenerator);

                    var chunks = codeGenerator.Context.CodeTreeBuilder.CodeTree.Chunks;
                    var first = chunks.FirstOrDefault();

                    attributeChunkValue = new ChunkBlock
                    {
                        Association = first?.Association,
                        Children = chunks,
                        Start = first == null ? SourceLocation.Zero : first.Start
                    };
                }

                attributes.Add(new KeyValuePair<string, Chunk>(attribute.Key, attributeChunkValue));

                // Reset the code tree builder so we can build a new one for the next attribute
                codeGenerator.Context.CodeTreeBuilder = new CodeTreeBuilder();
            }

            var unprefixedTagName = tagHelperBlock.TagName.Substring(_tagHelperDescriptors.First().Prefix.Length);

            context.CodeTreeBuilder.StartChunkBlock(
                new TagHelperChunk(
                    unprefixedTagName,
                    tagHelperBlock.SelfClosing,
                    attributes,
                    _tagHelperDescriptors),
                target,
                topLevel: false);
        }
Exemple #2
0
 protected abstract void Visit(ChunkBlock chunk);