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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
namespace System.Web.Mvc.Html {
	public static class HtmlTagExtensions {
		/// <summary>
		/// Create an <see cref="System.Web.Mvc.Html.HtmlTag"/> for a given model property.
		/// </summary>
		/// <typeparam name="TModel"></typeparam>
		/// <typeparam name="TProperty"></typeparam>
		/// <param name="htmlHelper"></param>
		/// <param name="model"></param>
		/// <returns></returns>
		public static HtmlTag<TModel, TProperty> TagFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> model) {
			return new HtmlTag<TModel, TProperty>(htmlHelper, model);
		}

		public static HtmlTag Tag(this HtmlHelper htmlHelper, string name) {
			return new HtmlTag(name);
		}

		public static MarkdownEditor<TModel, TProperty> MarkdownEditorFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> model) {
			return new MarkdownEditor<TModel, TProperty>(htmlHelper, model);
		}

		/// <summary>
		/// Splits a list of tags apart based on a common delimiter.
		/// </summary>
		/// <param name="content"></param>
		/// <param name="delimiter"></param>
		/// <returns></returns>
		public static string[] ToDelimitedArray(this string content, params char[] delimiter) {
			return content.Split(delimiter).Select(x => x.Trim()).ToArray();
		}

		public static string Join(this IEnumerable<string> strings, string separator) {
			return string.Join(separator, strings);
		}

		public static MvcHtmlString ToHtml(this HtmlTag tag) {
			return MvcHtmlString.Create(tag.ToString());
		}
	}

	public class HtmlText {

		/// <summary>
		/// Constructs a new HtmlText
		/// </summary>
		public HtmlText() {
			Encode = true;
		}

		/// <summary>
		/// Constructs a new HtmlText with the given value.
		/// </summary>
		/// <param name="text"></param>
		public HtmlText(string text) : this() {
			Text = text;
		}

		/// <summary>
		/// Construct a new HtmlText with the given encoding and value.
		/// </summary>
		/// <param name="text"></param>
		/// <param name="encode"></param>
		public HtmlText(string text, bool encode) : this(text) {
			Encode = encode;
		}

		/// <summary>
		/// The actual text to write.
		/// </summary>
		public string Text { get; set; }

		/// <summary>
		/// Whether or not to encode the text.
		/// </summary>
		public bool Encode { get; set; }
	}
	/// <summary>
	/// Represents a typesafe html tag that can be used to build complicated html models with
	/// intellisense and encoding, including validation attributes.
	/// </summary>
	public class HtmlTag {
		/// <summary>
		/// The name of the html tag being created
		/// </summary>
		public string Tag { get; set; }

		/// <summary>
		/// The inner text of the Html Tag
		/// </summary>
		public HtmlText HtmlText { get; set; }

		/// <summary>
		/// Specifies whether or not there is a closing tag
		/// </summary>
		public Boolean HasClosingTag { get; set; }

		/// <summary>
		/// Construct a new Html Tag.
		/// </summary>
		public HtmlTag() {
			// default the closing tag value
			HasClosingTag = true;
			// construct the list of children tags
			Children = new List<HtmlTag>();
			// construct the styles list
			Styles = new Dictionary<string, string>();
			// construct the classes list
			Classes = new List<string>();
			// construct the attributes 
			Attributes = new Dictionary<string, string>();
			// construct the meta data
			MetaData = new Dictionary<string, string>();
			// construct the data binding collection
			DataBind = new Dictionary<string, string>();
		}

		/// <summary>
		/// Construct a new Html Tag of the given type.
		/// </summary>
		/// <param name="tag">The html tag to create.</param>
		public HtmlTag(string tag)
			: this() {
			// set the html tag property, make sure to always
			// use the lowercase variant just for consistency.
			this.Tag = tag.ToLower();
		}

		/// <summary>
		/// Construct a new Html Tag as a child of the given tag.
		/// </summary>
		/// <param name="tag"></param>
		/// <param name="parent"></param>
		public HtmlTag(string tag, HtmlTag parent)
			: this(tag) {
			if (parent != null) parent.Append(this);
		}

		/// <summary>
		/// Construct a new Html Tag with the given validation attributes.
		/// </summary>
		/// <param name="validationAttributes">
		/// The validation attributes to include.
		/// </param>
		public HtmlTag(IDictionary<string, object> validationAttributes)
			: this() {
			this.ValidationAttributes = validationAttributes;
		}

		/// <summary>
		/// Construct a new Html Tag with the given validation attributes and model metadata.
		/// </summary>
		/// <param name="validationAttributes">
		/// The validation attributes to include.
		/// </param>
		/// <param name="metadata">
		/// The model metadata to include
		/// </param>
		public HtmlTag(ModelMetadata metadata, IDictionary<string, object> validationAttributes)
			: this(validationAttributes) {
			this.ModelMetadata = metadata;
		}

		/// <summary>
		/// Set the html tag type.
		/// </summary>
		/// <param name="tag"></param>
		/// <returns></returns>
		public HtmlTag Of(string tag) {
			Tag = tag;
			return this;
		}

		/// <summary>
		/// Sets the 'type' Html Attribute.
		/// </summary>
		/// <param name="type"></param>
		/// <returns></returns>
		public HtmlTag Type(string type) {
			return Attribute("type", type);
		}

		/// <summary>
		/// Sets the 'Id' Html Attribute.
		/// </summary>
		/// <param name="id">
		/// The value of the id
		/// </param>
		/// <returns></returns>
		public HtmlTag Id(string id) {
			return Attribute("id", id);
		}

		/// <summary>
		/// Sets the 'Name' Html Attribute.
		/// </summary>
		/// <param name="name">
		/// The value of the name.
		/// </param>
		/// <returns></returns>
		public HtmlTag Name(string name) {
			return Attribute("name", name);
		}

		/// <summary>
		/// Specify the inner Html text
		/// </summary>
		/// <param name="text"></param>
		/// <returns></returns>
		public HtmlTag Text(string text) {
			this.HtmlText = new HtmlText(text); return this;
		}

		/// <summary>
		/// Specify the inner html text and encoding.
		/// </summary>
		/// <param name="text"></param>
		/// <param name="encode"></param>
		/// <returns></returns>
		public HtmlTag Text(string text, bool encode) {
			this.HtmlText = new HtmlText(text, encode); return this;
		}

		/// <summary>
		/// Specifies whether or not the tag closes itself.
		/// </summary>
		/// <param name="value"></param>
		/// <returns></returns>
		public HtmlTag Closes(bool value) {
			this.HasClosingTag = value; return this;
		}

		/// <summary>
		/// Set a specific cascading stylesheet style value.
		/// </summary>
		/// <param name="key">The style to set.</param>
		/// <param name="value">The value of the style.</param>
		/// <returns></returns>
		public HtmlTag Style(string key, string value) {
			Styles[key] = value;
			return this;
		}

		/// <summary>
		/// Set a specific html attribute for the tag.
		/// </summary>
		/// <param name="key"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public HtmlTag Attribute(string key, string value) {
			Attributes[key] = value;
			return this;
		}

		/// <summary>
		/// Adds a cascading stylesheet class to the tag.
		/// </summary>
		/// <param name="css"></param>
		/// <returns></returns>
		public HtmlTag Class(string css) {
			if (!Classes.Contains(css))
				Classes.Add(css);
			return this;
		}

		/// <summary>
		/// Adds a data-binding attribute.
		/// </summary>
		/// <param name="key"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public HtmlTag Bind(string key, string value) {
			DataBind[key] = value; return this;
		}

		/// <summary>
		/// Inserts a sibling tag immediately after the current tag. Any existing sibling will follow the inserted tag.
		/// </summary>
		/// <param name="tag">The tag to add as a sibling</param>
		/// <returns>The original tag</returns>
		public HtmlTag After(HtmlTag tag) {
			Next = tag; return this;
		}

		/// <summary>
		/// Inserts a sibling tag immediately after the current tag. Any existing sibling will follow the inserted tag.
		/// </summary>
		/// <param name="configuration"></param>
		/// <returns></returns>
		public HtmlTag After(Action<HtmlTag> configuration) {
			Next = new HtmlTag(); configuration(Next); return this;
		}

		/// <summary>
		/// Inserts a parent tag immediately before the current tag.
		/// </summary>
		/// <param name="configuration"></param>
		/// <returns></returns>
		public HtmlTag Before(Action<HtmlTag> configuration) {
			Previous = new HtmlTag(); configuration(Previous); return this;
		}

		/// <summary>
		/// Creates nested child tags and returns the innermost tag. Use <see cref="Append(string)"/> if you want to return the parent tag.
		/// </summary>
		/// <param name="tagNames">One or more HTML element names separated by a <c>/</c> or <c>></c></param>
		/// <returns>The innermost tag that was newly added</returns>
		public HtmlTag Add(string tags) {
			return tags
				.ToDelimitedArray('/', '>')
				.Aggregate(this, (parent, tag) => new HtmlTag(tag, parent));
		}

		/// <summary>
		/// Creates nested child tags and returns the innermost tag after running <paramref name="configuration"/> on it. Use <see cref="Append(string, Action{HtmlTag})"/> if you want to return the parent tag.
		/// </summary>
		/// <param name="tags">One or more HTML element names separated by a <c>/</c> or <c>></c></param>
		/// <param name="configuration">Modifications to perform on the newly added innermost tag</param>
		/// <returns>The innermost tag that was newly added</returns>
		public HtmlTag Add(string tags, Action<HtmlTag> configuration) {
			var element = Add(tags); configuration(element); return this;
		}

		/// <summary>
		/// Creates a tag of <typeparamref name="T"/> and adds it as a child. Returns the created child tag.
		/// </summary>
		/// <typeparam name="T">The type of <see cref="HtmlTag"/> to create</typeparam>
		/// <returns>The created child tag</returns>
		public T Add<T>() where T : HtmlTag, new() {
			var child = new T(); Children.Add(child); return child;
		}

		/// <summary>
		/// Adds the given tag as the last child of the parent, and returns the parent.
		/// </summary>
		/// <param name="child">The tag to add as a child of the parent.</param>
		/// <returns>The parent tag</returns>
		public HtmlTag Append(HtmlTag child) {
			Children.Add(child); return this;
		}

		/// <summary>
		/// Creates nested child tags and returns the tag on which the method was called. Use <see cref="Add(string)"/> if you want to return the innermost tag.
		/// </summary>
		/// <param name="tags">One or more HTML element names separated by a <c>/</c> or <c>></c></param>
		/// <returns>The instance on which the method was called (the parent of the new tags)</returns>
		public HtmlTag Append(string tags) {
			Add(tags); return this;
		}

		/// <summary>
		/// Creates nested child tags, runs <paramref name="configuration"/> on the innermost tag, and returns the tag on which the method was called. Use <see cref="Add(string, Action{HtmlTag})"/> if you want to return the innermost tag.
		/// </summary>
		/// <param name="tags"></param>
		/// <param name="configuration"></param>
		/// <returns>The parent tag</returns>
		public HtmlTag Append(string tags, Action<HtmlTag> configuration) {
			Add(tags, configuration); return this;
		}

		/// <summary>
		/// Adds a sequence of tags as children of the current tag. Returns the parent tag.
		/// </summary>
		/// <param name="tags">A sequence of tags to add as children.</param>
		/// <returns>The parent tag</returns>
		public HtmlTag Append(IEnumerable<HtmlTag> tags) {
			Children.AddRange(tags); return this;
		}

		public override string ToString() {
			return ToString(new System.Web.UI.HtmlTextWriter(new System.IO.StringWriter(), string.Empty) { NewLine = string.Empty });
		}

		public string ToString(System.Web.UI.HtmlTextWriter html) {
			WriteHtml(html); return html.InnerWriter.ToString();
		}

		internal virtual void WriteHtml(System.Web.UI.HtmlTextWriter html) {
			if (Previous != null)
				Previous.WriteHtml(html);

			// first, construct a tag builder
			var htmlTag = new TagBuilder(Tag);

			// assemble data bindings if neccessary.
			if (!DataBind.IsEmpty()) {
				// build the data-binding
				var bindings = new System.Text.StringBuilder();
				// build a full string of all data bindings
				DataBind.Each(data => {
					// insert each binding, separated by a comma, parsed by a colon
					bool last = data.Equals(DataBind.Last()) ? true : false;
					bindings.AppendFormat("{0}:{1}{2}", data.Key, data.Value, last ? null : ",");
				});

				// add the data binding attribute with all of the appropriate bindings
				Attribute("data-bind", bindings.ToString());
			}

			// build the html attributes for the tag
			htmlTag.MergeAttributes(Attributes);
			// build the data attributes for the tag
			htmlTag.MergeAttributes(ValidationAttributes);
			// build the stylesheet data
			htmlTag.MergeAttributes(Styles);

			// we have used the tag builder to do our merging and formatting, 
			// now we can use it to begin drawing actual html.
			htmlTag.Attributes.Each(attribute => {
				html.AddAttribute(attribute.Key, attribute.Value);
			});

			// add all of the css classes to the html writer.
			if (Classes.Count > 0) {
				var classValue = Classes.Join(" ");
				html.AddAttribute("class", classValue);
			}

			// Draw the Beginning Tag
			html.RenderBeginTag(Tag);

			// if there is any inner text, write it now
			WriteInnerText(html);

			// Draw each Child Tag
			Children.Each(child => child.WriteHtml(html));

			// if there is a closing tag to be written, draw it
			// now
			if (HasClosingTag)
				html.RenderEndTag();

			if (Next != null)
				Next.WriteHtml(html);
		}

		private void WriteInnerText(System.Web.UI.HtmlTextWriter html) {
			// if there is no inner text, just exit.
			if (HtmlText == null)
				return;

			if (String.IsNullOrEmpty(HtmlText.Text))
				return;

			if (HtmlText.Encode)
				html.WriteEncodedText(HtmlText.Text);
			else
				html.Write(HtmlText.Text);
		}
		/// <summary>
		/// The unobtrusive validation attributes that apply to this html tag, from the
		/// model metadata.
		/// </summary>
		protected IDictionary<string, object> ValidationAttributes { get; set; }
		/// <summary>
		/// The various cascading stylesheet styles to include.
		/// </summary>
		protected IDictionary<string, string> Styles { get; set; }
		/// <summary>
		/// The various cascading stylesheet classes to include.
		/// </summary>
		protected List<string> Classes { get; set; }
		/// <summary>
		/// The html attributes that belong to the tag.
		/// </summary>
		protected IDictionary<string, string> Attributes { get; set; }
		/// <summary>
		/// The html5 meta data that is attached to the tag.
		/// </summary>
		protected IDictionary<string, string> MetaData { get; set; }
		/// <summary>
		/// The html5 data-binding
		/// </summary>
		protected IDictionary<string, string> DataBind { get; set; }

		/// <summary>
		/// The html children of this tag that should be rendered with it.
		/// </summary>
		protected List<HtmlTag> Children { get; set; }

		/// <summary>
		/// The Mvc Model Meta Data.
		/// </summary>
		protected ModelMetadata ModelMetadata { get; set; }

		/// <summary>
		/// The next Html Tag in the sequence of rendering, if applicable.
		/// </summary>
		protected HtmlTag Next { get; set; }

		/// <summary>
		/// The tag to render before this one.
		/// </summary>
		protected HtmlTag Previous { get; set; }
	}
	/// <summary>
	/// Represents a typesafe html tag that can be used to build complicated html models with
	/// intellisense and encoding, including validation attributes.
	/// </summary>
	public class HtmlTag<TModel, TProperty> : HtmlTag {
		/// <summary>
		/// Construct a new Html Tag of the given type.
		/// </summary>
		/// <param name="tag">The html tag to create.</param>
		public HtmlTag(string tag): base(tag) {
		}
		/// <summary>
		/// Construct a new Html Tag with the given HtmlHelper.
		/// </summary>
		/// <param name="helper">The html helper</param>
		public HtmlTag(HtmlHelper<TModel> helper)
			: base() {
			this.Helper = helper;
		}

		/// <summary>
		/// Construct a new Html Tag with the given HtmlHelper from a Model property.
		/// </summary>
		/// <param name="helper">The html helper</param>
		/// <param name="model">The html model</param>
		public HtmlTag(HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> model)
			: this(helper) {
			this.Model = model;
		}

		/// <summary>
		/// Construct a new Html Tag with the given validation attributes and model metadata and view data
		/// </summary>
		/// <param name="validationAttributes">
		/// The validation attributes to include.
		/// </param>
		/// <param name="metadata">
		/// The model metadata to include
		/// </param>
		public HtmlTag(ModelMetadata metadata, IDictionary<string, object> validationAttributes, ViewDataDictionary<TModel> viewData)
			: base(metadata, validationAttributes) {
			this.ViewData = viewData;
		}

		/// <summary>
		/// Construct a new Html Tag with the given validation attributes and model metadata and view data
		/// </summary>
		/// <param name="validationAttributes">
		/// The validation attributes to include.
		/// </param>
		/// <param name="metadata">
		/// The model metadata to include
		/// </param>
		public HtmlTag(ModelMetadata metadata, IDictionary<string, object> validationAttributes, HtmlHelper<TModel> helper)
			: base(metadata, validationAttributes) {
			this.Helper = helper;
			this.ViewData = helper.ViewData;
		}

		/// <summary>
		/// Set the html tag type.
		/// </summary>
		/// <param name="tag"></param>
		/// <returns></returns>
		public HtmlTag<TModel, TProperty> Of(string tag) {
			Tag = tag; return this;
		}

		/// <summary>
		/// Sets the 'type' Html Attribute.
		/// </summary>
		/// <param name="type"></param>
		/// <returns></returns>
		public HtmlTag<TModel, TProperty> Type(string type) {
			return Attribute("type", type);
		}

		/// <summary>
		/// Sets the 'Id' Html Attribute.
		/// </summary>
		/// <param name="id">
		/// The value of the id
		/// </param>
		/// <returns></returns>
		public HtmlTag<TModel, TProperty> Id(string id) {
			return Attribute("id", id);
		}

		/// <summary>
		/// Sets the 'Name' Html Attribute.
		/// </summary>
		/// <param name="name">
		/// The value of the name.
		/// </param>
		/// <returns></returns>
		public HtmlTag<TModel, TProperty> Name(string name) {
			return Attribute("name", name);
		}

		/// <summary>
		/// Set a specific cascading stylesheet style value.
		/// </summary>
		/// <param name="key">The style to set.</param>
		/// <param name="value">The value of the style.</param>
		/// <returns></returns>
		public HtmlTag<TModel, TProperty> Style(string key, string value) {
			Styles[key] = value;
			return this;
		}

		/// <summary>
		/// Set a specific html attribute for the tag.
		/// </summary>
		/// <param name="key"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public HtmlTag<TModel, TProperty> Attribute(string key, string value) {
			Attributes[key] = value;
			return this;
		}

		/// <summary>
		/// Adds a cascading stylesheet class to the tag.
		/// </summary>
		/// <param name="css"></param>
		/// <returns></returns>
		public HtmlTag<TModel, TProperty> Class(string css) {
			if (!Classes.Contains(css))
				Classes.Add(css);
			return this;
		}

		/// <summary>
		/// Adds a data-binding attribute.
		/// </summary>
		/// <param name="key"></param>
		/// <param name="value"></param>
		/// <returns></returns>
		public HtmlTag<TModel, TProperty> Bind(string key, string value) {
			DataBind[key] = value;
			return this;
		}

		public HtmlTag<TModel, TProperty> Validation() {
			// get the field name of the model property
			string fieldName = ExpressionHelper.GetExpressionText(Model);
			// decompile the model metadata so that we can have access to the attributes.
			ModelMetadata metadata = ModelMetadata.FromLambdaExpression(Model, Helper.ViewData);
			// retrieve the unobtrusive validation attributes
			this.ValidationAttributes = Helper.GetUnobtrusiveValidationAttributes(fieldName, metadata);
			// return the html tag
			return this;
		}

		public HtmlTag<TModel, TProperty> ValidationFor(Expression<Func<TModel, TProperty>> model) {
			// get the field name of the model property
			string fieldName = ExpressionHelper.GetExpressionText(model);
			// decompile the model metadata so that we can have access to the attributes.
			ModelMetadata metadata = ModelMetadata.FromLambdaExpression(model, Helper.ViewData);
			// retrieve the unobtrusive validation attributes
			this.ValidationAttributes = Helper.GetUnobtrusiveValidationAttributes(fieldName, metadata);
			// return the html tag
			return this;
		}

		/// <summary>Gets the strongly typed view data dictionary.</summary>
		/// <returns>The strongly typed view data dictionary.</returns>
		protected ViewDataDictionary<TModel> ViewData { get; set; }

		/// <summary>
		/// The HtmlHelper that created this tag.
		/// </summary>
		protected HtmlHelper<TModel> Helper { get; set; }

		/// <summary>
		/// The Html Model used to Associate this Tag.
		/// </summary>
		protected Expression<Func<TModel, TProperty>> Model { get; set; }
	}

	public class MarkdownEditor<TModel, TProperty> : HtmlTag<TModel, TProperty> {

		public MarkdownEditor(ModelMetadata metadata, IDictionary<string, object> validationAttributes, HtmlHelper<TModel> helper)
			: base(metadata, validationAttributes, helper) {
		}

		/// <summary>
		/// Sets the <strong>Id</strong> of the Markdown preview pane.
		/// </summary>
		/// <param name="name">The html element id of the preview pane to create.</param>
		/// <returns>
		/// The preview pane will be accessed using the <em>#(selector)</em> behavior by <em>jQuery</em>.
		/// </returns>
		public HtmlTag<TModel, TProperty> Preview(string name)  {
			this.PreviewPane = name; return this;
		}

		/// <summary>
		/// Construct a new Html Tag with the given HtmlHelper from a Model property.
		/// </summary>
		/// <param name="helper">The html helper</param>
		/// <param name="model">The html model</param>
		public MarkdownEditor(HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> model): base(helper, model) {
		}

		internal override void WriteHtml(System.Web.UI.HtmlTextWriter html) {
			// ensure that this textarea will be an editor
			Of("textarea");
			Class("mdd_editor");
			Attribute("data-mdd-preview", String.Format("#{0}", this.PreviewPane));

			// add the extra data that has to enclose the editor
			Before(tag => tag.Of("div").Class("mdd_toolbar"))
				.After(tag => tag.Of("div").Class("mdd_resizer"))
				.After(tag => tag.Of("div").Id(PreviewPane));

			base.WriteHtml(html);
		}

		public string PreviewPane { get; set; }
	}

	public class HtmlSelectTag<TModel, TProperty> : HtmlTag<TModel, TProperty> {
		public HtmlSelectTag():base("select") {
		}
		/// <summary>
		/// Construct a new Html Tag with the given HtmlHelper from a Model property.
		/// </summary>
		/// <param name="helper">The html helper</param>
		/// <param name="model">The html model</param>
		public HtmlSelectTag(HtmlHelper<TModel> helper, Expression<Func<TModel, TProperty>> model): base(helper, model) {
			Of("select");
		}

		public HtmlSelectTag<TModel, TProperty> Options(IEnumerable<SelectListItem> options) {
			options.Each(option => {
							 Option(option.Text, option.Value);
						 });
			return this;
		}
		public HtmlSelectTag<TModel, TProperty> Option(string display, string value) {
			Append(MakeOption(display, value)); return this;
		}

		private static HtmlTag MakeOption(string display, string value) {
			return new HtmlTag("option")
				.Text(display)
				.Attribute("value", value);
		}
	}
}