forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicAttributeBlockCodeGenerator.cs
More file actions
142 lines (126 loc) · 5.47 KB
/
DynamicAttributeBlockCodeGenerator.cs
File metadata and controls
142 lines (126 loc) · 5.47 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// 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.Linq;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;
using Microsoft.Internal.Web.Utils;
namespace System.Web.Razor.Generator
{
public class DynamicAttributeBlockCodeGenerator : BlockCodeGenerator
{
private const string ValueWriterName = "__razor_attribute_value_writer";
private string _oldTargetWriter;
private bool _isExpression;
private ExpressionRenderingMode _oldRenderingMode;
public DynamicAttributeBlockCodeGenerator(LocationTagged<string> prefix, int offset, int line, int col)
: this(prefix, new SourceLocation(offset, line, col))
{
}
public DynamicAttributeBlockCodeGenerator(LocationTagged<string> prefix, SourceLocation valueStart)
{
Prefix = prefix;
ValueStart = valueStart;
}
public LocationTagged<string> Prefix { get; private set; }
public SourceLocation ValueStart { get; private set; }
public override void GenerateStartBlockCode(Block target, CodeGeneratorContext context)
{
if (context.Host.DesignTimeMode)
{
return; // Don't generate anything!
}
// What kind of block is nested within
string generatedCode;
Block child = target.Children.Where(n => n.IsBlock).Cast<Block>().FirstOrDefault();
if (child != null && child.Type == BlockType.Expression)
{
_isExpression = true;
generatedCode = context.BuildCodeString(cw =>
{
cw.WriteParameterSeparator();
cw.WriteStartMethodInvoke("Tuple.Create");
cw.WriteLocationTaggedString(Prefix);
cw.WriteParameterSeparator();
cw.WriteStartMethodInvoke("Tuple.Create", "System.Object", "System.Int32");
});
_oldRenderingMode = context.ExpressionRenderingMode;
context.ExpressionRenderingMode = ExpressionRenderingMode.InjectCode;
}
else
{
generatedCode = context.BuildCodeString(cw =>
{
cw.WriteParameterSeparator();
cw.WriteStartMethodInvoke("Tuple.Create");
cw.WriteLocationTaggedString(Prefix);
cw.WriteParameterSeparator();
cw.WriteStartMethodInvoke("Tuple.Create", "System.Object", "System.Int32");
cw.WriteStartConstructor(context.Host.GeneratedClassContext.TemplateTypeName);
cw.WriteStartLambdaDelegate(ValueWriterName);
});
}
context.MarkEndOfGeneratedCode();
context.BufferStatementFragment(generatedCode);
_oldTargetWriter = context.TargetWriterName;
context.TargetWriterName = ValueWriterName;
}
public override void GenerateEndBlockCode(Block target, CodeGeneratorContext context)
{
if (context.Host.DesignTimeMode)
{
return; // Don't generate anything!
}
string generatedCode;
if (_isExpression)
{
generatedCode = context.BuildCodeString(cw =>
{
cw.WriteParameterSeparator();
cw.WriteSnippet(ValueStart.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();
cw.WriteLineContinuation();
});
context.ExpressionRenderingMode = _oldRenderingMode;
}
else
{
generatedCode = context.BuildCodeString(cw =>
{
cw.WriteEndLambdaDelegate();
cw.WriteEndConstructor();
cw.WriteParameterSeparator();
cw.WriteSnippet(ValueStart.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();
cw.WriteLineContinuation();
});
}
context.AddStatement(generatedCode);
context.TargetWriterName = _oldTargetWriter;
}
public override string ToString()
{
return String.Format(CultureInfo.CurrentCulture, "DynAttr:{0:F}", Prefix);
}
public override bool Equals(object obj)
{
DynamicAttributeBlockCodeGenerator other = obj as DynamicAttributeBlockCodeGenerator;
return other != null &&
Equals(other.Prefix, Prefix);
}
public override int GetHashCode()
{
return HashCodeCombiner.Start()
.Add(Prefix)
.CombinedHash;
}
}
}