/
Literal.cs
220 lines (189 loc) · 8.3 KB
/
Literal.cs
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
using System;
using System.Linq;
using System.Net;
using System.Runtime.CompilerServices;
using DotVVM.Framework.Binding;
using DotVVM.Framework.Binding.Expressions;
using DotVVM.Framework.Binding.Properties;
using DotVVM.Framework.Hosting;
using DotVVM.Framework.Runtime;
using DotVVM.Framework.Utils;
using Newtonsoft.Json;
namespace DotVVM.Framework.Controls
{
/// <summary>
/// Renders a text into the page.
/// </summary>
[ControlMarkupOptions(AllowContent = false)]
public sealed class Literal : HtmlGenericControl
{
/// <summary>
/// Gets or sets the text displayed in the control.
/// </summary>
public string Text
{
get { return GetValue(TextProperty)?.ToString() ?? ""; }
set { SetValue(TextProperty, value); }
}
public static readonly DotvvmProperty TextProperty =
DotvvmProperty.Register<object, Literal>(t => t.Text, "");
/// <summary>
/// Gets or sets the format string that will be applied to numeric or date-time values.
/// </summary>
[MarkupOptions(AllowBinding = false)]
public string? FormatString
{
get { return (string?)GetValue(FormatStringProperty); }
set { SetValue(FormatStringProperty, value); }
}
public static readonly DotvvmProperty FormatStringProperty =
DotvvmProperty.Register<string, Literal>(c => c.FormatString, "");
/// <summary>
/// Gets or sets whether the literal should render the wrapper span HTML element.
/// </summary>
[MarkupOptions(AllowBinding = false)]
public bool RenderSpanElement
{
get { return (bool)GetValue(RenderSpanElementProperty)!; }
set { SetValue(RenderSpanElementProperty, value); }
}
public static readonly DotvvmProperty RenderSpanElementProperty =
DotvvmProperty.Register<bool, Literal>(t => t.RenderSpanElement, true);
/// <summary>
/// Initializes a new instance of the <see cref="Literal"/> class.
/// </summary>
public Literal() : base("span", false)
{
LifecycleRequirements = ControlLifecycleRequirements.None;
}
/// <summary>
/// Initializes a new instance of the <see cref="Literal"/> class.
/// </summary>
public Literal(string text, bool renderSpan = false) : base("span", false)
{
Text = text;
RenderSpanElement = renderSpan;
LifecycleRequirements = ControlLifecycleRequirements.None;
}
public Literal(ValueOrBinding<string> text, bool renderSpan = false): this()
{
SetValue(TextProperty, text);
RenderSpanElement = renderSpan;
}
public Literal(ValueOrBinding text, bool renderSpan = false): this()
{
SetValue(TextProperty, text);
RenderSpanElement = renderSpan;
}
public Literal(IStaticValueBinding text, bool renderSpan = false): this()
{
SetBinding(TextProperty, text);
RenderSpanElement = renderSpan;
}
public static bool NeedsFormatting(IValueBinding? binding)
{
bool isFormattedType(Type? type) =>
type != null && (type == typeof(float) || type == typeof(double) || type == typeof(decimal) ||
type == typeof(DateTime) || type == typeof(DateOnly) || type == typeof(TimeOnly) || isFormattedType(Nullable.GetUnderlyingType(type)));
bool isFormattedTypeOrObj(Type? type) => type == typeof(object) || isFormattedType(type);
return isFormattedType(binding?.ResultType) && isFormattedTypeOrObj(binding?.GetProperty<ExpectedTypeBindingProperty>(ErrorHandlingMode.ReturnNull)?.Type);
}
protected override bool RendersHtmlTag => RenderSpanElement;
public bool IsFormattingRequired =>
!string.IsNullOrEmpty(FormatString) ||
NeedsFormatting(GetValueBinding(TextProperty));
private new struct RenderState
{
public object? Text;
public bool RenderSpanElement;
public bool HasFormattingStuff;
public DotvvmControl.RenderState BaseState;
public HtmlGenericControl.RenderState HtmlState;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private bool TouchProperty(DotvvmProperty prop, object? value, ref RenderState r)
{
if (prop == TextProperty)
r.Text = value;
else if (prop == RenderSpanElementProperty)
r.RenderSpanElement = (bool)EvalPropertyValue(RenderSpanElementProperty, value)!;
else if (prop == FormatStringProperty)
r.HasFormattingStuff = true;
else if (base.TouchProperty(prop, value, ref r.HtmlState)) { }
else if (DotvvmControl.TouchProperty(prop, value, ref r.BaseState)) { }
else return false;
return true;
}
protected override void RenderControl(IHtmlWriter writer, IDotvvmRequestContext context)
{
RenderState r = default;
r.RenderSpanElement = true;
foreach (var (prop, value) in properties)
TouchProperty(prop, value, ref r);
r.HtmlState.RendersHtmlTag = r.RenderSpanElement;
if (base.RenderBeforeControl(in r.BaseState, writer, context))
return;
base.AddAttributesCore(writer, ref r.HtmlState);
var textBinding = r.Text as IValueBinding;
var isFormattingRequired = (r.HasFormattingStuff || textBinding != null) && this.IsFormattingRequired;
// render Knockout data-bind
string? expression = null;
if (textBinding != null)
{
expression = textBinding.GetKnockoutBindingExpression(this);
if (isFormattingRequired)
{
// almost always the Literal will be rendered before script resources are, so requesting the resource in render should be safe. In case it's not, user can always add it manually (the error message should be quite clear).
context.ResourceManager.AddCurrentCultureGlobalizationResource();
// get value binding type and unwrap if it is nullable
var valueBindingType = GetValueBinding(TextProperty)?.ResultType;
if (valueBindingType != null && valueBindingType.IsNullable())
valueBindingType = valueBindingType.UnwrapNullableType();
var formattedType = $"\"{valueBindingType?.Name.ToLowerInvariant()}\"";
expression = "dotvvm.globalize.formatString(" + JsonConvert.ToString(FormatString) + ", " + expression + ", " + formattedType + ")";
}
if (r.RenderSpanElement)
{
writer.AddKnockoutDataBind("text", expression);
}
}
// render start tag
if (r.RenderSpanElement)
{
writer.RenderBeginTag(TagName!);
}
else if (expression != null)
{
writer.WriteKnockoutDataBindComment("text", expression);
}
if (expression == null || r.HtmlState.RenderOnServer(this))
{
string textToDisplay;
if (isFormattingRequired)
{
var formatString = FormatString;
if (string.IsNullOrEmpty(formatString))
{
formatString = "G";
}
textToDisplay = string.Format("{0:" + formatString + "}", EvalPropertyValue(TextProperty, r.Text));
}
else
{
textToDisplay = EvalPropertyValue(TextProperty, r.Text)?.ToString() ?? "";
}
writer.WriteText(textToDisplay);
}
// render end tag
if (r.RenderSpanElement)
{
writer.RenderEndTag();
}
else if (expression != null)
{
writer.WriteKnockoutDataBindEndComment();
}
base.RenderAfterControl(in r.BaseState, writer);
}
}
}