forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkupCollapser.cs
More file actions
38 lines (33 loc) · 1.31 KB
/
MarkupCollapser.cs
File metadata and controls
38 lines (33 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using System.Web.Razor.Generator;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;
namespace System.Web.Razor.Parser
{
internal class MarkupCollapser : MarkupRewriter
{
public MarkupCollapser(Action<SpanBuilder, SourceLocation, string> markupSpanFactory) : base(markupSpanFactory)
{
}
protected override bool CanRewrite(Span span)
{
return span.Kind == SpanKind.Markup && span.CodeGenerator is MarkupCodeGenerator;
}
protected override SyntaxTreeNode RewriteSpan(BlockBuilder parent, Span span)
{
// Only rewrite if we have a previous that is also markup (CanRewrite does this check for us!)
Span previous = parent.Children.LastOrDefault() as Span;
if (previous == null || !CanRewrite(previous))
{
return span;
}
// Merge spans
parent.Children.Remove(previous);
SpanBuilder merged = new SpanBuilder();
FillSpan(merged, previous.Start, previous.Content + span.Content);
return merged.Build();
}
}
}