Implement MachineKey.Protect and MachineKey.Unprotect
[mono.git] / mcs / class / System.Web.Mvc / System.Web.Mvc / Html / TextAreaExtensions.cs
1 /* ****************************************************************************\r
2  *\r
3  * Copyright (c) Microsoft Corporation. All rights reserved.\r
4  *\r
5  * This software is subject to the Microsoft Public License (Ms-PL). \r
6  * A copy of the license can be found in the license.htm file included \r
7  * in this distribution.\r
8  *\r
9  * You must not remove this notice, or any other, from this software.\r
10  *\r
11  * ***************************************************************************/\r
12 \r
13 namespace System.Web.Mvc.Html {\r
14     using System;\r
15     using System.Collections.Generic;\r
16     using System.Globalization;\r
17     using System.Web.Mvc.Resources;\r
18     using System.Web.Routing;\r
19 \r
20     public static class TextAreaExtensions {\r
21         // These values are similar to the defaults used by WebForms\r
22         // when using <asp:TextBox TextMode="MultiLine"> without specifying\r
23         // the Rows and Columns attributes.\r
24         private const int TextAreaRows = 2;\r
25         private const int TextAreaColumns = 20;\r
26 \r
27         public static string TextArea(this HtmlHelper htmlHelper, string name) {\r
28             return TextArea(htmlHelper, name, (object)null /* htmlAttributes */);\r
29         }\r
30 \r
31         public static string TextArea(this HtmlHelper htmlHelper, string name, object htmlAttributes) {\r
32             return TextArea(htmlHelper, name, new RouteValueDictionary(htmlAttributes));\r
33         }\r
34 \r
35         public static string TextArea(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes) {\r
36             // Add implicit parameters\r
37             Dictionary<string, object> implicitAttributes = new Dictionary<string, object>();\r
38             implicitAttributes.Add("rows", TextAreaRows.ToString(CultureInfo.InvariantCulture));\r
39             implicitAttributes.Add("cols", TextAreaColumns.ToString(CultureInfo.InvariantCulture));\r
40             return TextAreaHelper(htmlHelper, name, true /* useViewData */, null /* value */, implicitAttributes, null /* explicitParameters */, htmlAttributes);\r
41         }\r
42 \r
43         public static string TextArea(this HtmlHelper htmlHelper, string name, string value) {\r
44             return TextArea(htmlHelper, name, value, (object) null /* htmlAttributes */);\r
45         }\r
46 \r
47         public static string TextArea(this HtmlHelper htmlHelper, string name, string value, object htmlAttributes) {\r
48             return TextArea(htmlHelper, name, value, new RouteValueDictionary(htmlAttributes));\r
49         }\r
50 \r
51         public static string TextArea(this HtmlHelper htmlHelper, string name, string value, IDictionary<string, object> htmlAttributes) {\r
52             // Add implicit parameters\r
53             Dictionary<string, object> implicitAttributes = new Dictionary<string, object>();\r
54             implicitAttributes.Add("rows", TextAreaRows.ToString(CultureInfo.InvariantCulture));\r
55             implicitAttributes.Add("cols", TextAreaColumns.ToString(CultureInfo.InvariantCulture));\r
56             return TextAreaHelper(htmlHelper, name, (value == null) /* useViewData */, value, implicitAttributes, null /* explicitParameters */, htmlAttributes);\r
57         }\r
58 \r
59         public static string TextArea(this HtmlHelper htmlHelper, string name, string value, int rows, int columns, object htmlAttributes) {\r
60             return TextArea(htmlHelper, name, value, rows, columns, new RouteValueDictionary(htmlAttributes));\r
61         }\r
62 \r
63         public static string TextArea(this HtmlHelper htmlHelper, string name, string value, int rows, int columns, IDictionary<string, object> htmlAttributes) {\r
64             if (rows <= 0) {\r
65                 throw new ArgumentOutOfRangeException("rows", MvcResources.HtmlHelper_TextAreaParameterOutOfRange);\r
66             }\r
67             if (columns <= 0) {\r
68                 throw new ArgumentOutOfRangeException("columns", MvcResources.HtmlHelper_TextAreaParameterOutOfRange);\r
69             }\r
70 \r
71             Dictionary<string, object> explicitParameters = new Dictionary<string, object>();\r
72             explicitParameters.Add("rows", rows.ToString(CultureInfo.InvariantCulture));\r
73             explicitParameters.Add("cols", columns.ToString(CultureInfo.InvariantCulture));\r
74             return TextAreaHelper(htmlHelper, name, (value == null) /* useViewData */, value, null /* implictAttributes */, explicitParameters, htmlAttributes);\r
75         }\r
76 \r
77         private static string TextAreaHelper(this HtmlHelper htmlHelper, string name, bool useViewData, string value, IDictionary<string, object> implicitAttributes, IDictionary<string, object> explicitParameters, IDictionary<string, object> htmlAttributes) {\r
78             if (String.IsNullOrEmpty(name)) {\r
79                 throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");\r
80             }\r
81 \r
82             TagBuilder tagBuilder = new TagBuilder("textarea");\r
83             // Add implicit attributes.\r
84             tagBuilder.MergeAttributes(implicitAttributes);\r
85             tagBuilder.GenerateId(name);\r
86             // Merge htmlAttributes.\r
87             tagBuilder.MergeAttributes(htmlAttributes, true);\r
88             // Override all the attributes with explicit parameters.\r
89             tagBuilder.MergeAttributes(explicitParameters, true);\r
90             tagBuilder.MergeAttribute("name", name, true);\r
91 \r
92             // If there are any errors for a named field, we add the css attribute.\r
93             ModelState modelState;\r
94             if (htmlHelper.ViewData.ModelState.TryGetValue(name, out modelState)) {\r
95                 if (modelState.Errors.Count > 0) {\r
96                     tagBuilder.AddCssClass(HtmlHelper.ValidationInputCssClassName);\r
97                 }\r
98             }\r
99 \r
100             // The first newline is always trimmed when a TextArea is rendered, so we add an extra one\r
101             // in case the value being rendered is something like "\r\nHello".\r
102             // The attempted value receives precedence over the explicitly supplied value parameter.\r
103             string attemptedValue = (string)htmlHelper.GetModelStateValue(name, typeof(string));\r
104             tagBuilder.SetInnerText(Environment.NewLine + (attemptedValue ?? ((useViewData) ? htmlHelper.EvalString(name) : value)));\r
105             return tagBuilder.ToString(TagRenderMode.Normal);\r
106         }\r
107     }\r
108 }\r