[System.Web.Extensions] Update default profile and remove old profiles files
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlForm.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlForm.cs
3 //
4 // Author:
5 //      Dick Porter  <dick@ximian.com>
6 //
7 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System.ComponentModel;
30 using System.Collections.Specialized;
31 using System.Security.Permissions;
32 using System.Web.Util;
33 using System.Web.UI.WebControls;
34 using System.Web.Configuration;
35 using System.Web.SessionState;
36
37 namespace System.Web.UI.HtmlControls 
38 {
39         // CAS
40         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         public class HtmlForm : HtmlContainerControl 
43         {
44                 bool inited;
45                 string _defaultfocus;
46                 string _defaultbutton;
47                 bool submitdisabledcontrols = false;
48                 bool? isUplevel;
49                 
50                 public HtmlForm () : base ("form")
51                 {
52                 }
53
54                 // LAMESPEC: This is undocumented on MSDN, but apparently it does exist on MS.NET.
55                 // See https://bugzilla.novell.com/show_bug.cgi?id=442104
56                 public string Action {
57                         get {
58                                 string action = Attributes ["action"];
59                                 if (String.IsNullOrEmpty (action))
60                                         return String.Empty;
61
62                                 return action;
63                         }
64
65                         set {
66                                 if (String.IsNullOrEmpty (value))
67                                         Attributes ["action"] = null;
68                                 else
69                                         Attributes ["action"] = value;
70                         }
71                 }               
72
73                 [DefaultValue ("")]
74                 public string DefaultButton {
75                         get {
76                                 return _defaultbutton ?? String.Empty;
77                         }
78                         set {
79                                 _defaultbutton = value;
80                         }
81                 }
82
83                 [DefaultValue ("")]
84                 public string DefaultFocus {
85                         get {
86                                 return _defaultfocus ?? String.Empty;
87                         }
88                         set {
89                                 _defaultfocus = value;
90                         }
91                 }
92
93                 [DefaultValue ("")]
94                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
95                 public string Enctype {
96                         get {
97                                 string enc = Attributes["enctype"];
98
99                                 if (enc == null) {
100                                         return (String.Empty);
101                                 }
102
103                                 return (enc);
104                         }
105                         set {
106                                 if (value == null) {
107                                         Attributes.Remove ("enctype");
108                                 } else {
109                                         Attributes["enctype"] = value;
110                                 }
111                         }
112                 }
113                 
114                 [DefaultValue ("")]
115                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
116                 public string Method {
117                         get {
118                                 string method = Attributes["method"];
119
120                                 if ((method == null) || (method.Length == 0)) {
121                                         return ("post");
122                                 }
123                                 
124                                 return (method);
125                         }
126                         set {
127                                 if (value == null) {
128                                         Attributes.Remove ("method");
129                                 } else {
130                                         Attributes["method"] = value;
131                                 }
132                         }
133                 }
134
135                 [DefaultValue ("")]
136                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
137                 public virtual string Name {
138                         get {
139                                 return UniqueID;
140                         }
141                         set {
142                                 /* why am i here? I do nothing. */
143                         }
144                 }
145
146                 [DefaultValue (false)]
147                 public virtual bool SubmitDisabledControls {
148                         get {
149                                 return submitdisabledcontrols;
150                         }
151                         set {
152                                 submitdisabledcontrols = value;
153                         }
154                 }
155                         
156                 [DefaultValue ("")]
157                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
158                 public string Target {
159                         get {
160                                 string target = Attributes["target"];
161
162                                 if (target == null) {
163                                         return (String.Empty);
164                                 }
165                                 
166                                 return (target);
167                         }
168                         set {
169                                 if (value == null) {
170                                         Attributes.Remove ("target");
171                                 } else {
172                                         Attributes["target"] = value;
173                                 }
174                         }
175                 }
176
177                 public override string UniqueID {
178                         get {
179                                 Control container = NamingContainer;
180                                 if (container == Page)
181                                         return ID;
182                                 return "aspnetForm";
183                         }
184                 }
185
186                 [MonoTODO ("why override?")]
187                 protected override ControlCollection CreateControlCollection ()
188                 {
189                         return base.CreateControlCollection ();
190                 }
191
192                 protected internal override void OnInit (EventArgs e)
193                 {
194                         inited = true;
195                         Page page = Page;
196                         if (page != null) {
197                                 page.RegisterViewStateHandler ();
198                                 page.RegisterForm (this);
199                         }
200                         
201                         base.OnInit (e);
202                 }
203
204                 internal bool DetermineRenderUplevel ()
205                 {
206                         if (isUplevel != null)
207                                 return (bool) isUplevel;
208                         
209                         isUplevel = UplevelHelper.IsUplevel (
210                                 System.Web.Configuration.HttpCapabilitiesBase.GetUserAgentForDetection (HttpContext.Current.Request));
211                         return (bool) isUplevel;
212                 }
213
214                 protected internal override void OnPreRender (EventArgs e)
215                 {
216                         base.OnPreRender(e);
217                 }
218
219                 protected override void RenderAttributes (HtmlTextWriter w)
220                 {
221                         /* Need to always render: method, action and id
222                          */
223                         
224                         string action;
225                         string customAction = Attributes ["action"];
226                         Page page = Page;
227                         HttpRequest req = page != null ? page.RequestInternal : null;
228                         if (String.IsNullOrEmpty (customAction)) {
229                                 string file_path = req != null ? req.ClientFilePath : null;
230                                 string current_path = req != null ? req.CurrentExecutionFilePath : null;
231
232                                 if (file_path == null)
233                                         action = Action;
234                                 else if (file_path == current_path) {
235                                         // Just the filename will do
236                                         action = UrlUtils.GetFile (file_path);
237                                 } else {
238                                         // Fun. We need to make cookieless sessions work, so no
239                                         // absolute paths here.
240                                         bool cookieless;
241                                         SessionStateSection sec = WebConfigurationManager.GetSection ("system.web/sessionState") as SessionStateSection;
242                                         cookieless = sec != null ? sec.Cookieless == HttpCookieMode.UseUri: false;
243                                         string appVPath = HttpRuntime.AppDomainAppVirtualPath;
244                                         int appVPathLen = appVPath.Length;
245                                                 
246                                         if (appVPathLen > 1) {
247                                                 if (cookieless) {
248                                                         if (StrUtils.StartsWith (file_path, appVPath, true))
249                                                                 file_path = file_path.Substring (appVPathLen + 1);
250                                                 } else if (StrUtils.StartsWith (current_path, appVPath, true))
251                                                         current_path = current_path.Substring (appVPathLen + 1);
252                                         }
253                                         
254                                         if (cookieless) {
255                                                 Uri current_uri = new Uri ("http://host" + current_path);
256                                                 Uri fp_uri = new Uri ("http://host" + file_path);
257                                                 action = fp_uri.MakeRelative (current_uri);
258                                         } else
259                                                 action = current_path;
260                                 }
261                         } else
262                                 action = customAction;
263                         if (req != null)
264                                 action += req.QueryStringRaw;
265                         if (req != null) {
266                                 XhtmlConformanceSection xhtml = WebConfigurationManager.GetSection ("system.web/xhtmlConformance") as XhtmlConformanceSection;
267                                 if (xhtml == null || xhtml.Mode != XhtmlConformanceMode.Strict)
268                                         if (RenderingCompatibilityLessThan40)
269                                                 // LAMESPEC: MSDN says the 'name' attribute is rendered only in
270                                                 // Legacy mode, this is not true.
271                                                 w.WriteAttribute ("name", Name);
272                         }
273                         
274                         w.WriteAttribute ("method", Method);
275                         if (String.IsNullOrEmpty (customAction))
276                                 w.WriteAttribute ("action", action, true);
277
278                         /*
279                          * This is a hack that guarantees the ID is set properly for HtmlControl to
280                          * render it later on. As ugly as it is, we use it here because of the way
281                          * the ID, ClientID and UniqueID properties work internally in our Control
282                          * code.
283                          *
284                          * Fixes bug #82596
285                          */
286                         if (ID == null) {
287 #pragma warning disable 219
288                                 string client = ClientID;
289 #pragma warning restore 219
290                         }
291                         
292                         string submit = page != null ? page.GetSubmitStatements () : null;
293                         if (!String.IsNullOrEmpty (submit)) {
294                                 Attributes.Remove ("onsubmit");
295                                 w.WriteAttribute ("onsubmit", submit);
296                         }
297                         
298                         /* enctype and target should not be written if
299                          * they are empty
300                          */
301                         string enctype = Enctype;
302                         if (!String.IsNullOrEmpty (enctype))
303                                 w.WriteAttribute ("enctype", enctype);
304
305                         string target = Target;
306                         if (!String.IsNullOrEmpty (target))
307                                 w.WriteAttribute ("target", target);
308
309                         string defaultbutton = DefaultButton;
310                         if (!String.IsNullOrEmpty (defaultbutton)) {
311                                 Control c = FindControl (defaultbutton);
312
313                                 if (c == null || !(c is IButtonControl))
314                                         throw new InvalidOperationException(String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.",
315                                                                                            ID));
316
317                                 if (page != null && DetermineRenderUplevel ()) {
318                                         w.WriteAttribute (
319                                                 "onkeypress",
320                                                 "javascript:return " + page.WebFormScriptReference + ".WebForm_FireDefaultButton(event, '" + c.ClientID + "')");
321                                 }
322                         }
323
324                         /* Now remove them from the hash so the base
325                          * RenderAttributes can do all the rest
326                          */
327                         Attributes.Remove ("method");
328                         Attributes.Remove ("enctype");
329                         Attributes.Remove ("target");
330
331                         base.RenderAttributes (w);
332                 }
333
334                 protected internal override void RenderChildren (HtmlTextWriter w)
335                 {
336                         Page page = Page;
337                         
338                         if (!inited && page != null) {
339                                 page.RegisterViewStateHandler ();
340                                 page.RegisterForm (this);
341                         }
342                         if (page != null)
343                                 page.OnFormRender (w, ClientID);
344                         base.RenderChildren (w);
345                         if (page != null)
346                                 page.OnFormPostRender (w, ClientID);
347                 }
348
349                 /* According to corcompare */
350                 [MonoTODO ("why override?")]
351                 public override void RenderControl (HtmlTextWriter w)
352                 {
353                         base.RenderControl (w);
354                 }
355
356                 protected internal override void Render (HtmlTextWriter w)
357                 {
358                         base.Render (w);
359                 }
360         }
361 }