Mark tests as not working under TARGET_JVM
[mono.git] / mcs / class / System.Web / System.Web.UI / CssStyleCollection.cs
1 //
2 // System.Web.UI.CssStyleCollection.cs
3 //
4 // Authors:
5 //      Duncan Mak  (duncan@ximian.com)
6 //      Gonzalo Paniagua (gonzalo@ximian.com)
7 //
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
9 // Copyright (C) 2005 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.IO;
32 using System.Collections;
33 using System.Security.Permissions;
34 using System.Text;
35 using System.Collections.Specialized;
36 using System.Globalization;
37
38 namespace System.Web.UI {
39
40         // CAS - no InheritanceDemand here as the class is sealed
41         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         public sealed class CssStyleCollection
43         {
44                 StateBag bag;
45                 HybridDictionary style;
46                 string _value;
47                 
48                 string ValueInternal {
49                         get { return _value; }
50                         set {
51                                 _value = value;
52                                 if (bag != null) {
53                                         if (_value == null) {
54                                                 bag.Remove (AttributeCollection.StyleAttribute);
55                                         }
56                                         else {
57                                                 bag [AttributeCollection.StyleAttribute] = _value;
58                                         }
59                                 }
60                         }
61                 }
62                 
63                 internal CssStyleCollection ()
64                 {
65 #if NET_2_0
66                         style = new HybridDictionary (true);
67 #else
68                         style = new HybridDictionary (false);
69 #endif
70                 }
71
72                 internal CssStyleCollection (StateBag bag)
73                         : this ()
74                 {
75                         this.bag = bag;
76                         if (bag != null)
77                                 _value = (string) bag [AttributeCollection.StyleAttribute];
78                         InitFromStyle ();
79                 }
80
81                 void InitFromStyle ()
82                 {
83                         style.Clear ();
84                         if (_value != null) {
85                                 FillStyle (_value);
86                         }
87                 }
88
89                 void FillStyle (string s)
90                 {
91                         int mark = s.IndexOf (':');
92                         if (mark == -1)
93                                 return;
94                         string key = s.Substring (0, mark). Trim ();
95                         if (mark + 1 > s.Length)
96                                 return;
97
98                         string fullValue = s.Substring (mark + 1);
99                         if (fullValue == "")
100                                 return;
101
102                         mark = fullValue.IndexOf (';');
103                         string value;
104                         if (mark == -1)
105                                 value = fullValue.Trim ();
106                         else
107                                 value = fullValue.Substring (0, mark).Trim ();
108
109                         style.Add (key, value);
110                         if (mark + 1 > fullValue.Length)
111                                 return;
112                         FillStyle (fullValue.Substring (mark + 1));
113                 }
114
115                 string BagToString ()
116                 {
117                         StringBuilder sb = new StringBuilder ();
118                         foreach (string key in style.Keys) {
119                                 if (key == "background-image" && 0 != String.Compare ("url", ((string) style [key]).Substring (0, 3), true, CultureInfo.InvariantCulture))
120                                         sb.AppendFormat ("{0}:url({1});", key, HttpUtility.UrlPathEncode ((string) style [key]));
121                                 else
122                                         sb.AppendFormat ("{0}:{1};", key, style [key]);
123                         }
124
125                         return sb.ToString ();
126                 }
127
128                 public int Count {
129                         get {
130                                 return style.Count;
131                         }
132                 }
133
134                 public string this [string key] {
135                         get {
136                                 return style [key] as string;
137                         }
138
139                         set {
140                                 Add (key, value);
141                         }
142                 }
143
144                 public ICollection Keys {
145                         get {
146                                 return style.Keys;
147                         }
148                 }
149
150                 public void Add (string key, string value)
151                 {
152                         style [key] = value;
153                         ValueInternal = BagToString ();
154                 }
155
156 #if NET_2_0
157                 public
158 #else
159                 internal
160 #endif
161                 void Add (HtmlTextWriterStyle key, string value)
162                 {
163                         Add (HtmlTextWriter.StaticGetStyleName (key), value);
164                 }
165
166                 public void Clear ()
167                 {
168                         style.Clear ();
169                         ValueInternal = null;
170                 }
171
172                 public void Remove (string key)
173                 {
174                         if (style [key] == null)
175                                 return;
176                         style.Remove (key);
177                         if (style.Count == 0)
178                                 ValueInternal = null;
179                         else
180                                 ValueInternal = BagToString ();
181                 }
182 #if NET_2_0
183                 public string this [HtmlTextWriterStyle key] {
184                         get {
185                                 return style [HtmlTextWriter.StaticGetStyleName (key)] as string;
186                         }
187                         set {
188                                 Add (HtmlTextWriter.StaticGetStyleName (key), value);
189                         }
190                 }
191
192                 public void Remove (HtmlTextWriterStyle key)
193                 {
194                         Remove (HtmlTextWriter.StaticGetStyleName (key));
195                 }
196
197                 public
198 #else
199                 internal
200 #endif
201                 string Value {
202                         get { return ValueInternal; }
203                         set {
204                                 ValueInternal = value;
205                                 InitFromStyle ();
206                         }
207                 }
208         }
209 }
210