New tests.
[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 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
36 namespace System.Web.UI.HtmlControls 
37 {
38         // CAS
39         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         public class HtmlForm : HtmlContainerControl 
42         {
43                 bool inited;
44
45                 public HtmlForm () : base ("form")
46                 {
47                 }
48
49 #if NET_2_0
50                 string _defaultbutton;
51                 [DefaultValue ("")]
52                 public string DefaultButton
53                 {
54                         get {
55                                 return _defaultbutton ?? String.Empty;
56                         }
57                         set {
58                                 _defaultbutton = value;
59                         }
60                 }
61
62                 string _defaultfocus;
63                 [DefaultValue ("")]
64                 public string DefaultFocus
65                 {
66                         get {
67                                 return _defaultfocus ?? String.Empty;
68                         }
69                         set {
70                                 _defaultfocus = value;
71                         }
72                 }
73 #endif          
74
75                 [DefaultValue ("")]
76                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
77                 public string Enctype 
78                 {
79                         get {
80                                 string enc = Attributes["enctype"];
81
82                                 if (enc == null) {
83                                         return (String.Empty);
84                                 }
85
86                                 return (enc);
87                         }
88                         set {
89                                 if (value == null) {
90                                         Attributes.Remove ("enctype");
91                                 } else {
92                                         Attributes["enctype"] = value;
93                                 }
94                         }
95                 }
96                 
97                 [DefaultValue ("")]
98                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
99                 public string Method 
100                 {
101                         get {
102                                 string method = Attributes["method"];
103
104                                 if ((method == null) || (method.Length == 0)) {
105                                         return ("post");
106                                 }
107                                 
108                                 return (method);
109                         }
110                         set {
111                                 if (value == null) {
112                                         Attributes.Remove ("method");
113                                 } else {
114                                         Attributes["method"] = value;
115                                 }
116                         }
117                 }
118
119                 [DefaultValue ("")]
120                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
121                 public virtual string Name 
122                 {
123                         get {
124                                 return UniqueID;
125                         }
126                         set {
127                                 /* why am i here? I do nothing. */
128                         }
129                 }
130
131 #if NET_2_0
132                 bool submitdisabledcontrols = false;
133                 [DefaultValue (false)]
134                 public virtual bool SubmitDisabledControls 
135                 {
136                         get {
137                                 return submitdisabledcontrols;
138                         }
139                         set {
140                                 submitdisabledcontrols = value;
141                         }
142                 }
143 #endif
144                         
145                 [DefaultValue ("")]
146                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
147                 public string Target 
148                 {
149                         get {
150                                 string target = Attributes["target"];
151
152                                 if (target == null) {
153                                         return (String.Empty);
154                                 }
155                                 
156                                 return (target);
157                         }
158                         set {
159                                 if (value == null) {
160                                         Attributes.Remove ("target");
161                                 } else {
162                                         Attributes["target"] = value;
163                                 }
164                         }
165                 }
166
167                 public override string UniqueID {
168                         get {
169                                 return base.UniqueID;
170                         }
171                 }
172
173 #if NET_2_0             
174                 [MonoTODO ("why override?")]
175                 protected override ControlCollection CreateControlCollection ()
176                 {
177                         return base.CreateControlCollection ();
178                 }
179 #endif          
180
181 #if NET_2_0
182                 protected internal
183 #else           
184                 protected
185 #endif          
186                 override void OnInit (EventArgs e)
187                 {
188                         inited = true;
189                         Page.RegisterViewStateHandler ();
190
191 #if NET_2_0
192                         Page.RegisterForm (this);
193 #endif
194
195                         base.OnInit (e);
196                 }
197
198 #if NET_2_0
199                 bool? isUplevel;
200                 internal bool DetermineRenderUplevel ()
201                 {
202 #if TARGET_J2EE
203                         if (HttpContext.Current == null)
204                                 return false;
205
206                         return (
207                                 /* From someplace on the web: "JavaScript 1.2
208                                  * and later (also known as ECMAScript) has
209                                  * built-in support for regular
210                                  * expressions" */
211                                 ((Page.Request.Browser.EcmaScriptVersion.Major == 1
212                                   && Page.Request.Browser.EcmaScriptVersion.Minor >= 2)
213                                  || (Page.Request.Browser.EcmaScriptVersion.Major > 1))
214
215                                 /* document.getElementById, .getAttribute,
216                                  * etc, are all DOM level 1.  I don't think we
217                                  * use anything in level 2.. */
218                                 && Page.Request.Browser.W3CDomVersion.Major >= 1);
219 #else
220                         if (isUplevel != null)
221                                 return (bool) isUplevel;
222                         
223                         isUplevel = UplevelHelper.IsUplevel (
224                                 System.Web.Configuration.HttpCapabilitiesBase.GetUserAgentForDetection (HttpContext.Current.Request));
225                         return (bool) isUplevel;
226 #endif
227                 }
228
229                 protected internal override void OnPreRender (EventArgs e)
230                 {
231                         base.OnPreRender(e);
232                 }
233 #endif          
234
235                 protected override void RenderAttributes (HtmlTextWriter w)
236                 {
237                         /* Need to always render: method, action and id
238                          */
239                         /* The name attribute is rendered _only_ if we're not in
240                            2.0 mode or if the xhtml conformance mode is set to
241                            Legacy for 2.0 according to http://msdn2.microsoft.com/en-us/library/system.web.ui.htmlcontrols.htmlform.name.aspx
242                         */
243                         
244                         string action;
245 #if !TARGET_J2EE
246                         string file_path = Page.Request.FilePath;
247                         string current_path = Page.Request.CurrentExecutionFilePath;
248                         if (file_path == current_path) {
249                                 // Just the filename will do
250                                 action = UrlUtils.GetFile (file_path);
251                         } else {
252                                 // Fun. We need to make cookieless sessions work, so no
253                                 // absolute paths here.
254                                 Uri current_uri = new Uri ("http://host" + current_path);
255                                 Uri fp_uri = new Uri ("http://host" + file_path);
256                                 action = fp_uri.MakeRelative (current_uri);
257                         }
258
259                         action += Page.Request.QueryStringRaw;
260 #else
261                         // Allow the page to transform action to a portlet action url
262                         string customAction = Attributes ["action"];
263                         if (String.IsNullOrEmpty (customAction)) {
264                                 string queryString = Page.Request.QueryStringRaw;
265                                 action = CreateActionUrl (VirtualPathUtility.ToAppRelative (Page.Request.CurrentExecutionFilePath) +
266                                         (string.IsNullOrEmpty (queryString) ? string.Empty : "?" + queryString));
267                         }
268                         else
269                                 action = customAction;
270
271 #endif
272
273 #if NET_2_0
274                         XhtmlConformanceSection xhtml = WebConfigurationManager.GetSection ("system.web/xhtmlConformance") as
275                                 XhtmlConformanceSection;
276                         
277                         if (xhtml != null && xhtml.Mode == XhtmlConformanceMode.Legacy)
278 #endif
279                                 w.WriteAttribute ("name", Name);
280
281                         w.WriteAttribute ("method", Method);
282                         w.WriteAttribute ("action", action, true);
283
284                         /*
285                          * This is a hack that guarantees the ID is set properly for HtmlControl to
286                          * render it later on. As ugly as it is, we use it here because of the way
287                          * the ID, ClientID and UniqueID properties work internally in our Control
288                          * code.
289                          *
290                          * Fixes bug #82596
291                          */
292                         if (ID == null) {
293 #pragma warning disable 219
294                                 string client = ClientID;
295 #pragma warning restore 219
296                         }
297                         
298                         string submit = Page.GetSubmitStatements ();
299                         if (submit != null && submit != "") {
300                                 Attributes.Remove ("onsubmit");
301                                 w.WriteAttribute ("onsubmit", submit);
302                         }
303                         
304                         /* enctype and target should not be written if
305                          * they are empty
306                          */
307                         string enctype = Enctype;
308                         if (enctype != null && enctype != "") {
309                                 w.WriteAttribute ("enctype", enctype);
310                         }
311
312                         string target = Target;
313                         if (target != null && target != "") {
314                                 w.WriteAttribute ("target", target);
315                         }
316
317 #if NET_2_0
318                         string defaultbutton = DefaultButton;
319                         if (!String.IsNullOrEmpty (defaultbutton)) {
320                                 Control c = FindControl (defaultbutton);
321
322                                 if (c == null || !(c is IButtonControl))
323                                         throw new InvalidOperationException(String.Format ("The DefaultButton of '{0}' must be the ID of a control of type IButtonControl.",
324                                                                                            ID));
325
326                                 if (DetermineRenderUplevel ()) {
327                                         w.WriteAttribute (
328                                                 "onkeypress",
329                                                 "javascript:return " + Page.WebFormScriptReference + ".WebForm_FireDefaultButton(event, '" + c.ClientID + "')");
330                                 }
331                         }
332 #endif
333
334                         /* Now remove them from the hash so the base
335                          * RenderAttributes can do all the rest
336                          */
337                         Attributes.Remove ("method");
338                         Attributes.Remove ("enctype");
339                         Attributes.Remove ("target");
340
341                         base.RenderAttributes (w);
342                 }
343
344 #if NET_2_0
345                 protected internal
346 #else           
347                 protected
348 #endif          
349                 override void RenderChildren (HtmlTextWriter w)
350                 {
351                         if (!inited) {
352                                 Page.RegisterViewStateHandler ();
353 #if NET_2_0
354                                 Page.RegisterForm (this);
355 #endif
356                         }
357                         Page.OnFormRender (w, ClientID);
358                         base.RenderChildren (w);
359                         Page.OnFormPostRender (w, ClientID);
360                 }
361
362 #if NET_2_0
363                 /* According to corcompare */
364                 [MonoTODO ("why override?")]
365                 public override void RenderControl (HtmlTextWriter w)
366                 {
367                         base.RenderControl (w);
368                 }
369 #endif          
370
371 #if NET_2_0
372                 protected internal
373 #else           
374                 protected
375 #endif          
376                 override void Render (HtmlTextWriter w)
377                 {
378                         base.Render (w);
379                 }
380         }
381 }
382
383