forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConditionalAttributeCollapser.cs
More file actions
55 lines (47 loc) · 2.04 KB
/
Copy pathConditionalAttributeCollapser.cs
File metadata and controls
55 lines (47 loc) · 2.04 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// 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.Diagnostics;
using System.Linq;
using System.Web.Razor.Editor;
using System.Web.Razor.Generator;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;
using System.Web.Razor.Tokenizer;
namespace System.Web.Razor.Parser
{
internal class ConditionalAttributeCollapser : MarkupRewriter
{
public ConditionalAttributeCollapser(Action<SpanBuilder, SourceLocation, string> markupSpanFactory) : base(markupSpanFactory)
{
}
protected override bool CanRewrite(Block block)
{
AttributeBlockCodeGenerator gen = block.CodeGenerator as AttributeBlockCodeGenerator;
return gen != null && block.Children.Any() && block.Children.All(IsLiteralAttributeValue);
}
protected override SyntaxTreeNode RewriteBlock(BlockBuilder parent, Block block)
{
// Collect the content of this node
string content = String.Concat(block.Children.Cast<Span>().Select(s => s.Content));
// Create a new span containing this content
SpanBuilder span = new SpanBuilder();
span.EditHandler = new SpanEditHandler(HtmlTokenizer.Tokenize);
FillSpan(span, block.Children.Cast<Span>().First().Start, content);
return span.Build();
}
private bool IsLiteralAttributeValue(SyntaxTreeNode node)
{
if (node.IsBlock)
{
return false;
}
Span span = node as Span;
Debug.Assert(span != null);
LiteralAttributeCodeGenerator litGen = span.CodeGenerator as LiteralAttributeCodeGenerator;
return span != null &&
((litGen != null && litGen.ValueGenerator == null) ||
span.CodeGenerator == SpanCodeGenerator.Null ||
span.CodeGenerator is MarkupCodeGenerator);
}
}
}