New tests.
[mono.git] / mcs / class / System.Web / System.Web.UI.HtmlControls / HtmlControl.cs
1 //
2 // System.Web.UI.HtmlControls.HtmlControl.cs
3 //
4 // Author
5 //   Bob Smith <bob@thestuff.net>
6 //
7 //
8 // (C) Bob Smith
9 // Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.ComponentModel;
32 using System.ComponentModel.Design;
33 using System.Globalization;
34 using System.Security.Permissions;
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         // attributes
42         [ToolboxItem(false)]
43         [Designer ("System.Web.UI.Design.HtmlIntrinsicControlDesigner, " + Consts.AssemblySystem_Design,
44                         "System.ComponentModel.Design.IDesigner")]
45         public abstract class HtmlControl : Control, IAttributeAccessor
46         {
47                 internal string _tagName;
48                 AttributeCollection _attributes;
49
50                 protected HtmlControl() : this ("span") {}
51                 
52                 protected HtmlControl(string tag)
53                 {
54                         _tagName = tag;
55                 }
56                 
57                 protected override ControlCollection CreateControlCollection ()
58                 {
59                         return new EmptyControlCollection (this);
60                 }
61
62                 internal static string AttributeToString(int n){
63                         if (n != -1)return n.ToString(NumberFormatInfo.InvariantInfo);
64                         return null;
65                 }
66                 
67                 internal static string AttributeToString(string s){
68                         if (s != null && s.Length != 0) return s;
69                         return null;
70                 }
71                 
72                 internal void PreProcessRelativeReference(HtmlTextWriter writer, string attribName){
73                         string attr = Attributes[attribName];
74                         if (attr != null){
75                                 if (attr.Length != 0){
76                                         try{
77                                                 attr = ResolveClientUrl(attr);
78                                         }
79                                         catch (Exception) {
80                                                 throw new HttpException(attribName + " property had malformed url");
81                                         }
82                                         writer.WriteAttribute(attribName, attr, true);
83                                         Attributes.Remove(attribName);
84                                 }
85                         }
86                 }
87
88                 /* keep these two methods in sync with the
89                  * IAttributeAccessor iface methods below */
90                 protected virtual string GetAttribute (string name)
91                 {
92                         return Attributes[name];
93                 }
94
95                 protected virtual void SetAttribute (string name, string value)
96                 {
97                         Attributes[name] = value;
98                 }
99                 
100                 string System.Web.UI.IAttributeAccessor.GetAttribute(string name){
101                         return Attributes[name];
102                 }
103                 
104                 void System.Web.UI.IAttributeAccessor.SetAttribute(string name, string value){
105                         Attributes[name] = value;
106                 }
107                 
108                 protected virtual void RenderBeginTag (HtmlTextWriter writer)
109                 {
110                         writer.WriteBeginTag (TagName);
111                         RenderAttributes (writer);
112                         writer.Write ('>');
113                 }
114
115                 protected internal override void Render (HtmlTextWriter writer)
116                 {
117                         RenderBeginTag (writer);
118                 }
119                 
120                 protected virtual void RenderAttributes(HtmlTextWriter writer){
121                         if (ID != null){
122                                 writer.WriteAttribute("id",ClientID);
123                         }
124                         Attributes.Render(writer);
125                 }
126                 
127                 [Browsable(false)]
128                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
129                 public AttributeCollection Attributes {
130                         get { 
131                                 if (_attributes == null)
132                                         _attributes = new AttributeCollection (ViewState);
133                                 return _attributes;
134                         }
135                 }
136
137                 [DefaultValue(false)]
138                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
139                 [WebSysDescription("")]
140                 [WebCategory("Behavior")]
141                 [TypeConverter (typeof(MinimizableAttributeTypeConverter))]
142                 public bool Disabled {
143                         get {
144                                 string disableAttr = Attributes["disabled"] as string;
145                                 return (disableAttr != null);
146                         }
147                         set {
148                                 if (!value)
149                                         Attributes.Remove ("disabled");
150                                 else
151                                         Attributes["disabled"] = "disabled";
152                         }
153                 }
154
155                 [Browsable(false)]
156                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
157                 public CssStyleCollection Style {
158                         get { return Attributes.CssStyle; }
159                 }
160
161                 [DefaultValue("")]
162                 [DesignerSerializationVisibility (DesignerSerializationVisibility.Hidden)]
163                 [WebSysDescription("")]
164                 [WebCategory("Appearance")]
165                 public virtual string TagName {
166                         get { return _tagName; }
167                 }
168
169                 protected override bool ViewStateIgnoresCase {
170                         get {
171                                 return true;
172                         }
173                 }
174         }
175 }