forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLiteralAttributeCodeGenerator.cs
More file actions
114 lines (104 loc) · 4.27 KB
/
Copy pathLiteralAttributeCodeGenerator.cs
File metadata and controls
114 lines (104 loc) · 4.27 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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.Globalization;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;
using Microsoft.Internal.Web.Utils;
namespace System.Web.Razor.Generator
{
public class LiteralAttributeCodeGenerator : SpanCodeGenerator
{
public LiteralAttributeCodeGenerator(LocationTagged<string> prefix, LocationTagged<SpanCodeGenerator> valueGenerator)
{
Prefix = prefix;
ValueGenerator = valueGenerator;
}
public LiteralAttributeCodeGenerator(LocationTagged<string> prefix, LocationTagged<string> value)
{
Prefix = prefix;
Value = value;
}
public LocationTagged<string> Prefix { get; private set; }
public LocationTagged<string> Value { get; private set; }
public LocationTagged<SpanCodeGenerator> ValueGenerator { get; private set; }
public override void GenerateCode(Span target, CodeGeneratorContext context)
{
if (context.Host.DesignTimeMode)
{
return;
}
ExpressionRenderingMode oldMode = context.ExpressionRenderingMode;
context.BufferStatementFragment(context.BuildCodeString(cw =>
{
cw.WriteParameterSeparator();
cw.WriteStartMethodInvoke("Tuple.Create");
cw.WriteLocationTaggedString(Prefix);
cw.WriteParameterSeparator();
if (ValueGenerator != null)
{
cw.WriteStartMethodInvoke("Tuple.Create", "System.Object", "System.Int32");
context.ExpressionRenderingMode = ExpressionRenderingMode.InjectCode;
}
else
{
cw.WriteLocationTaggedString(Value);
cw.WriteParameterSeparator();
// literal: true - This attribute value is a literal value
cw.WriteBooleanLiteral(true);
cw.WriteEndMethodInvoke();
// In VB, we need a line continuation
cw.WriteLineContinuation();
}
}));
if (ValueGenerator != null)
{
ValueGenerator.Value.GenerateCode(target, context);
context.FlushBufferedStatement();
context.ExpressionRenderingMode = oldMode;
context.AddStatement(context.BuildCodeString(cw =>
{
cw.WriteParameterSeparator();
cw.WriteSnippet(ValueGenerator.Location.AbsoluteIndex.ToString(CultureInfo.CurrentCulture));
cw.WriteEndMethodInvoke();
cw.WriteParameterSeparator();
// literal: false - This attribute value is not a literal value, it is dynamically generated
cw.WriteBooleanLiteral(false);
cw.WriteEndMethodInvoke();
// In VB, we need a line continuation
cw.WriteLineContinuation();
}));
}
else
{
context.FlushBufferedStatement();
}
}
public override string ToString()
{
if (ValueGenerator == null)
{
return String.Format(CultureInfo.CurrentCulture, "LitAttr:{0:F},{1:F}", Prefix, Value);
}
else
{
return String.Format(CultureInfo.CurrentCulture, "LitAttr:{0:F},<Sub:{1:F}>", Prefix, ValueGenerator);
}
}
public override bool Equals(object obj)
{
LiteralAttributeCodeGenerator other = obj as LiteralAttributeCodeGenerator;
return other != null &&
Equals(other.Prefix, Prefix) &&
Equals(other.Value, Value) &&
Equals(other.ValueGenerator, ValueGenerator);
}
public override int GetHashCode()
{
return HashCodeCombiner.Start()
.Add(Prefix)
.Add(Value)
.Add(ValueGenerator)
.CombinedHash;
}
}
}