/
HtmlElementFactory.cs
357 lines (292 loc) · 12.4 KB
/
HtmlElementFactory.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
namespace ZenCoding
{
public static class HtmlElementFactory
{
private static readonly Regex _valueCounterRegex = new Regex(@"(\$+)", RegexOptions.Compiled);
public static HtmlControl CloneElement(this HtmlControl element, int count)
{
if (element == null)
return null;
using (HtmlControl control = HtmlElementFactory.Create(element.TagName.Increment(count), element.GetType(), true))
{
foreach (var attribute in element.Attributes.Keys)
{
string attr = (string)attribute;
control.Attributes[attr] = element.Attributes[attr].Increment(count);
if (attr == "class")
{
control.Attributes[attr] = control.Attributes[attr].Increment(count);
}
}
if (!string.IsNullOrEmpty(element.ID))
{
control.ID = element.ID.Increment(count);
}
var lorem = element as LoremControl;
if (lorem != null)
{
(control as LoremControl).InnerText = lorem.Generate(count);
}
else if (element.Controls.Count == 1)
{
LiteralControl literal = element.Controls[0] as LiteralControl;
if (literal != null)
control.Controls.Add(new LiteralControl(literal.Text.Increment(count)));
}
return control;
}
}
public static string Increment(this string text, int count)
{
MatchCollection matches = _valueCounterRegex.Matches(text);
checked
{
foreach (Match match in matches)
{
text = text.Replace(match.Value, (count + 1).ToString(CultureInfo.CurrentCulture).PadLeft(match.Value.Length, '0'));
}
}
return text;
}
public static HtmlControl Create(string tagName)
{
return Create(tagName, null, false);
}
public static HtmlControl Create(string tagName, Type type)
{
return Create(tagName, type, false);
}
public static HtmlControl Create(string tagName, Type type, bool isClone)
{
if (tagName == null)
return null;
HtmlControl control = TryCreateSepcialControl(tagName, type, isClone);
if (control != null)
return control;
return new BlockHtmlControl(tagName);
}
private static HtmlControl TryCreateSepcialControl(string tagName, Type type, bool isClone)
{
if (tagName.StartsWith("lorem", System.StringComparison.Ordinal) ||
(type == typeof(LoremControl) && isClone))
{
return new LoremControl(isClone ? "lorem0" : tagName);
}
else if (tagName.StartsWith("pix", System.StringComparison.Ordinal))
{
return new LoremPixel(tagName);
}
else if (tagName.StartsWith("place", System.StringComparison.Ordinal))
{
return new PlaceHold(tagName);
}
return TryCreateFromStateMachine(tagName);
}
private static HtmlControl TryCreateFromStateMachine(string tagName)
{
switch (tagName)
{
case "":
return new EmptyHtmlControl();
case "a":
using (var a = new CustomHtmlAnchor())
{
a.Attributes["href"] = "";
return a;
}
case "button":
case "btn":
return new HtmlButton();
case "input":
using (CustomHtmlInput input = new CustomHtmlInput())
{
input.Attributes["value"] = "";
input.Attributes["type"] = "";
return input;
}
case "img":
using (var img = new CustomHtmlImage())
{
img.Attributes["src"] = string.Empty;
img.Attributes["alt"] = string.Empty;
return img;
}
case "source":
case "src":
return new HtmlGenericSelfClosing("source");
case "meta":
return new CustomHtmlMeta();
case "link":
return new CustomHtmlLink();
case "abbr":
case "acronym":
using (HtmlGenericControl abbr = new HtmlGenericControl(tagName))
{
abbr.Attributes["title"] = string.Empty;
return abbr;
}
case "area":
using (BlockHtmlControl area = new BlockHtmlControl("area"))
{
area.Attributes["shape"] = string.Empty;
area.Attributes["coords"] = string.Empty;
area.Attributes["href"] = string.Empty;
area.Attributes["alt"] = string.Empty;
return area;
}
case "iframe":
case "ifr":
using (HtmlGenericControl iframe = new HtmlGenericControl("iframe"))
{
iframe.Attributes["src"] = string.Empty;
iframe.Attributes["frameborder"] = "0";
return iframe;
}
case "param":
using (HtmlGenericSelfClosing param = new HtmlGenericSelfClosing("param"))
{
param.Attributes["name"] = string.Empty;
param.Attributes["value"] = string.Empty;
return param;
}
case "section":
case "sect":
return new BlockHtmlControl("section");
case "article":
case "art":
return new BlockHtmlControl("article");
case "hgroup":
case "hgr":
return new BlockHtmlControl("hgroup");
case "header":
case "hdr":
return new BlockHtmlControl("header");
case "footer":
case "ftr":
return new BlockHtmlControl("footer");
case "adr":
return new BlockHtmlControl("address");
case "dlg":
return new BlockHtmlControl("dialog");
case "bq":
return new HtmlGenericControl("blockquote");
case "acr":
return new HtmlGenericControl("acronym");
case "prog":
return new HtmlGenericControl("progress");
case "figure":
case "fig":
return new BlockHtmlControl("figure");
case "emb":
return new HtmlGenericControl("embed");
case "object":
case "obj":
return new BlockHtmlControl("object");
case "cap":
return new HtmlGenericControl("caption");
case "colg":
return new HtmlGenericControl("colgroup");
case "fset":
return new BlockHtmlControl("fieldset");
case "leg":
return new BlockHtmlControl("legend");
case "optg":
return new HtmlGenericControl("optgroup");
case "opt":
return new HtmlGenericControl("option");
case "datag":
return new HtmlGenericControl("datagrid");
case "datal":
return new HtmlGenericControl("datalist");
case "textarea":
case "tarea":
using (var textarea = new CustomHtmlTextArea())
{
textarea.ID = string.Empty;
textarea.Attributes["cols"] = string.Empty;
textarea.Attributes["rows"] = string.Empty;
return textarea;
}
case "kg":
return new HtmlGenericControl("keygen");
case "out":
return new HtmlGenericControl("output");
case "html":
case "head":
case "body":
case "div":
case "table":
case "tr":
case "p":
return new BlockHtmlControl(tagName);
case "br":
case "hr":
return new HtmlGenericSelfClosing(tagName);
}
return null;
}
public static Control CreateDoctypes(string part, ref IEnumerable<Control> current)
{
if (part == null)
return null;
using (ContentPlaceHolder root = new ContentPlaceHolder())
{
string second = part.Substring(part.IndexOf(':') + 1);
root.Controls.Add(new LiteralControl(GetDoctype(second)));
using (HtmlControl html = HtmlElementFactory.Create("html"))
{
if (!second.StartsWith("x", StringComparison.Ordinal))
{
html.Attributes["lang"] = "en";
}
else
{
html.Attributes["xmlns"] = "http://www.w3.org/1999/xhtml";
html.Attributes["xml:lang"] = "en";
}
root.Controls.Add(html);
using (HtmlControl head = HtmlElementFactory.Create("head"))
using (HtmlControl title = HtmlElementFactory.Create("title"))
using (HtmlControl meta = ShortcutHelper.Parse("meta:utf"))
{
head.Controls.Add(title);
head.Controls.Add(meta);
html.Controls.Add(head);
}
using (HtmlControl body = HtmlElementFactory.Create("body"))
{
html.Controls.Add(body);
current = new Control[] { body };
}
return root;
}
}
}
private static string GetDoctype(string key)
{
switch (key)
{
case "4t":
return "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">";
case "4s":
return "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">";
case "xt":
return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
case "xs":
return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">";
case "xxs":
return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.1//EN\" \"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">";
case "5":
return "<!DOCTYPE html>";
}
return null;
}
}
}