Ejemplo n.º 1
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);
    }
Ejemplo n.º 2
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);
    }
Ejemplo n.º 4
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);
        }
    }