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