public void WriteSection(CodeRenderingContext context, SectionIntermediateNode node)
        {
            // Quirk Alert!
            //
            // In 1.0.0 Razor/MVC the define section method took a parameter for a TextWriter
            // that would be used for all of the output in the section. We simplified this API for
            // 2.0.0 of MVC, but our design time codegen still needs to target 1.0.0.
            //
            // So the workaround is MVC 2.0.0 will define these methods with the TextWriter, but
            // that method is never called. We still generate the call *with* the TextWriter for
            // design time, at least until we have multi-targeting.
            var writerName = context.Options.DesignTime ? DefaultWriterName : string.Empty;

            context.CodeWriter
            .WriteStartMethodInvocation(SectionMethodName)
            .Write("\"")
            .Write(node.SectionName)
            .Write("\", ");

            using (context.CodeWriter.BuildAsyncLambda(writerName))
            {
                context.RenderChildren(node);
            }

            context.CodeWriter.WriteEndMethodInvocation(endLine: true);
        }
Exemplo n.º 2
0
        public void WriteSection(CodeRenderingContext context, SectionIntermediateNode node)
        {
            context.CodeWriter
            .WriteStartMethodInvocation(SectionMethodName)
            .Write("\"")
            .Write(node.SectionName)
            .Write("\", ");

            if (context.Options.DesignTime)
            {
                using (context.CodeWriter.BuildAsyncLambda(DefaultWriterName))
                {
                    context.RenderChildren(node);
                }
            }
            else
            {
                using (context.CodeWriter.BuildAsyncLambda())
                {
                    context.RenderChildren(node);
                }
            }

            context.CodeWriter.WriteEndMethodInvocation(endLine: true);
        }
Exemplo n.º 3
0
        public void WriteSection_WritesSectionCode_DesignTime()
        {
            // Arrange
            var node = new SectionIntermediateNode()
            {
                Children =
                {
                    new CSharpExpressionIntermediateNode(),
                },
                SectionName = "MySection"
            };

            var extension = new LegacySectionTargetExtension()
            {
                SectionMethodName = "CreateSection"
            };

            var context = TestCodeRenderingContext.CreateDesignTime();

            // Act
            extension.WriteSection(context, node);

            // Assert
            var expected = @"CreateSection(""MySection"", async(__razor_section_writer) => {
    Render Children
}
);
";

            var output = context.CodeWriter.GenerateCode();

            Assert.Equal(expected, output, ignoreLineEndingDifferences: true);
        }
        public void WriteSection_WritesSectionCode()
        {
            // Arrange
            var node = new SectionIntermediateNode()
            {
                Children =
                {
                    new CSharpExpressionIntermediateNode(),
                },
                SectionName = "MySection"
            };

            var extension = new SectionTargetExtension()
            {
                SectionMethodName = "CreateSection"
            };

            var context = TestCodeRenderingContext.CreateRuntime();

            // Act
            extension.WriteSection(context, node);

            // Assert
            var expected = @"CreateSection(""MySection"", async() => {
    Render Children
}
);
";

            var output = context.CodeWriter.GenerateCode();

            Assert.Equal(expected, output);
        }
Exemplo n.º 5
0
        protected override void ExecuteCore(RazorCodeDocument codeDocument, DocumentIntermediateNode documentNode)
        {
            var @class = documentNode.FindPrimaryClass();

            if (@class == null)
            {
                return;
            }

            foreach (var directive in documentNode.FindDirectiveReferences(SectionDirective.Directive))
            {
                var sectionName = ((DirectiveIntermediateNode)directive.Node).Tokens.FirstOrDefault()?.Content;

                var section = new SectionIntermediateNode()
                {
                    SectionName = sectionName,
                };

                var i = 0;
                for (; i < directive.Node.Children.Count; i++)
                {
                    if (!(directive.Node.Children[i] is DirectiveTokenIntermediateNode))
                    {
                        break;
                    }
                }

                while (i != directive.Node.Children.Count)
                {
                    // Move non-token children over to the section node so we don't have double references to children nodes.
                    section.Children.Add(directive.Node.Children[i]);
                    directive.Node.Children.RemoveAt(i);
                }

                directive.InsertAfter(section);
            }
        }