forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHtmlHelper.cs
More file actions
284 lines (244 loc) · 7.55 KB
/
Copy pathHtmlHelper.cs
File metadata and controls
284 lines (244 loc) · 7.55 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* This software is subject to the Microsoft Public License (Ms-PL).
* A copy of the license can be found in the license.htm file included
* in this distribution.
*
* You must not remove this notice, or any other, from this software.
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Web;
using ServiceStack.Common.Web;
using ServiceStack.Markdown;
using ServiceStack.ServiceHost;
using ServiceStack.Text;
using ServiceStack.WebHost.Endpoints.Support.Markdown;
namespace ServiceStack.Html
{
[Flags]
public enum HttpVerbs
{
Get = 1 << 0,
Post = 1 << 1,
Put = 1 << 2,
Delete = 1 << 3,
Head = 1 << 4
}
public enum InputType
{
CheckBox,
Hidden,
Password,
Radio,
Text
}
public class HtmlHelper<TModel> : HtmlHelper
{
private ViewDataDictionary<TModel> viewData;
public new ViewDataDictionary<TModel> ViewData
{
get
{
return viewData ??
(viewData = base.ViewData as ViewDataDictionary<TModel>
?? new ViewDataDictionary<TModel>((TModel)base.ViewData.Model));
}
}
}
public class HtmlHelper
{
public static List<Type> HtmlExtensions = new List<Type>
{
typeof(DisplayTextExtensions),
typeof(InputExtensions),
typeof(LabelExtensions),
typeof(TextAreaExtensions),
};
public static MethodInfo GetMethod(string methodName)
{
foreach (var htmlExtension in HtmlExtensions)
{
var mi = htmlExtension.GetMethods().ToList()
.FirstOrDefault(x => x.Name == methodName);
if (mi != null) return mi;
}
return null;
}
private delegate string HtmlEncoder(object value);
private static readonly HtmlEncoder htmlEncoder = GetHtmlEncoder();
public bool RenderHtml { get; protected set; }
public IHttpRequest HttpRequest { get; set; }
public IViewEngine ViewEngine { get; set; }
public MarkdownPage MarkdownPage { get; protected set; }
public Dictionary<string, object> ScopeArgs { get; protected set; }
public virtual ViewDataDictionary ViewData { get; protected set; }
private static int counter = 0;
private int id = 0;
public HtmlHelper()
{
this.RenderHtml = true;
id = counter++;
}
public void Init(MarkdownPage markdownPage, Dictionary<string, object> scopeArgs,
bool renderHtml, ViewDataDictionary viewData)
{
Init(null, markdownPage.Markdown, viewData);
this.RenderHtml = renderHtml;
this.MarkdownPage = markdownPage;
this.ScopeArgs = scopeArgs;
}
public void Init(IHttpRequest httpReq, IViewEngine viewEngine, ViewDataDictionary viewData)
{
this.RenderHtml = true;
this.HttpRequest = httpReq;
this.ViewEngine = viewEngine;
this.ViewData = viewData;
this.ViewData.PopulateModelState();
}
public MvcHtmlString Partial(string viewName)
{
return Partial(viewName, null);
}
public MvcHtmlString Partial(string viewName, object model)
{
var result = ViewEngine.RenderPartial(viewName, model, this.RenderHtml, this.HttpRequest);
return MvcHtmlString.Create(result);
}
public string Debug(object model)
{
if (model != null)
{
model.PrintDump();
}
return null;
}
public string Raw(object content)
{
if (content == null) return null;
var strContent = content as string;
return strContent ?? content.ToString(); //MvcHtmlString
}
public static RouteValueDictionary AnonymousObjectToHtmlAttributes(object htmlAttributes)
{
var result = new RouteValueDictionary();
if (htmlAttributes != null)
{
foreach (PropertyDescriptor property in TypeDescriptor.GetProperties(htmlAttributes))
{
result.Add(property.Name.Replace('_', '-'), property.GetValue(htmlAttributes));
}
}
return result;
}
public string AttributeEncode(string value)
{
return !string.IsNullOrEmpty(value) ? HttpUtility.HtmlAttributeEncode(value) : String.Empty;
}
public string AttributeEncode(object value)
{
return AttributeEncode(Convert.ToString(value, CultureInfo.InvariantCulture));
}
public string Encode(string value)
{
return !string.IsNullOrEmpty(value) ? HttpUtility.HtmlEncode(value) : String.Empty;
}
public string Encode(object value)
{
return htmlEncoder(value);
}
internal object GetModelStateValue(string key, Type destinationType)
{
ModelState modelState;
if (ViewData.ModelState.TryGetValue(key, out modelState))
{
if (modelState.Value != null)
{
return modelState.Value.ConvertTo(destinationType, null /* culture */);
}
}
return null;
}
internal string EvalString(string key)
{
return Convert.ToString(ViewData.Eval(key), CultureInfo.CurrentCulture);
}
internal bool EvalBoolean(string key)
{
return Convert.ToBoolean(ViewData.Eval(key), CultureInfo.InvariantCulture);
}
// method used if HttpUtility.HtmlEncode(object) method does not exist
private static string EncodeLegacy(object value)
{
var stringVal = Convert.ToString(value, CultureInfo.CurrentCulture);
return !string.IsNullOrEmpty(stringVal) ? HttpUtility.HtmlEncode(stringVal) : String.Empty;
}
// selects the v3.5 (legacy) or v4 HTML encoder
private static HtmlEncoder GetHtmlEncoder()
{
return TypeHelpers.CreateDelegate<HtmlEncoder>(TypeHelpers.SystemWebAssembly, "System.Web.HttpUtility", "HtmlEncode", null)
?? EncodeLegacy;
}
public static string GetInputTypeString(InputType inputType)
{
switch (inputType)
{
case InputType.CheckBox:
return "checkbox";
case InputType.Hidden:
return "hidden";
case InputType.Password:
return "password";
case InputType.Radio:
return "radio";
case InputType.Text:
return "text";
default:
return "text";
}
}
public MvcHtmlString HttpMethodOverride(HttpVerbs httpVerb)
{
string httpMethod;
switch (httpVerb)
{
case HttpVerbs.Delete:
httpMethod = "DELETE";
break;
case HttpVerbs.Head:
httpMethod = "HEAD";
break;
case HttpVerbs.Put:
httpMethod = "PUT";
break;
default:
throw new ArgumentException(MvcResources.HtmlHelper_InvalidHttpVerb, "httpVerb");
}
return HttpMethodOverride(httpMethod);
}
public MvcHtmlString HttpMethodOverride(string httpMethod)
{
if (String.IsNullOrEmpty(httpMethod))
{
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "httpMethod");
}
if (String.Equals(httpMethod, "GET", StringComparison.OrdinalIgnoreCase) ||
String.Equals(httpMethod, "POST", StringComparison.OrdinalIgnoreCase))
{
throw new ArgumentException(MvcResources.HtmlHelper_InvalidHttpMethod, "httpMethod");
}
var tagBuilder = new TagBuilder("input");
tagBuilder.Attributes["type"] = "hidden";
tagBuilder.Attributes["name"] = HttpHeaders.XHttpMethodOverride;
tagBuilder.Attributes["value"] = httpMethod;
return tagBuilder.ToMvcHtmlString(TagRenderMode.SelfClosing);
}
}
}