fixed tests
[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
47                 internal CssStyleCollection (StateBag bag)
48                 {
49                         this.bag = bag;
50                         if (bag != null)
51                                 InitFromStyle ();
52                 }
53
54                 void InitFromStyle ()
55                 {
56 #if NET_2_0
57                         style = new HybridDictionary (true);
58 #else
59                         style = new HybridDictionary (false);
60 #endif
61                         string att = (string) bag ["style"];
62                         if (att != null) {
63                                 FillStyle (att);
64                         }
65                 }
66
67                 void FillStyle (string s)
68                 {
69                         int mark = s.IndexOf (':');
70                         if (mark == -1)
71                                 return;
72                         string key = s.Substring (0, mark). Trim ();
73                         if (mark + 1 > s.Length)
74                                 return;
75
76                         string fullValue = s.Substring (mark + 1);
77                         if (fullValue == "")
78                                 return;
79
80                         mark = fullValue.IndexOf (';');
81                         string value;
82                         if (mark == -1)
83                                 value = fullValue.Trim ();
84                         else
85                                 value = fullValue.Substring (0, mark).Trim ();
86
87                         style.Add (key, value);
88                         if (mark + 1 > fullValue.Length)
89                                 return;
90                         FillStyle (fullValue.Substring (mark + 1));
91                 }
92
93                 string BagToString ()
94                 {
95                         StringBuilder sb = new StringBuilder ();
96                         foreach (string key in style.Keys) {
97                                 if (key == "background-image" && 0 != String.Compare ("url", ((string) style [key]).Substring (0, 3), true, CultureInfo.InvariantCulture))
98                                         sb.AppendFormat ("{0}:url({1});", key, HttpUtility.UrlPathEncode ((string) style [key]));
99                                 else
100                                         sb.AppendFormat ("{0}:{1};", key, style [key]);
101                         }
102
103                         return sb.ToString ();
104                 }
105
106                 public int Count {
107                         get {
108                                 return style.Count;
109                         }
110                 }
111
112                 public string this [string key] {
113                         get {
114                                 return style [key] as string;
115                         }
116
117                         set {
118                                 Add (key, value);
119                         }
120                 }
121
122                 public ICollection Keys {
123                         get {
124                                 return style.Keys;
125                         }
126                 }
127
128                 public void Add (string key, string value)
129                 {
130                         style [key] = value;
131                         bag ["style"] = BagToString ();
132                 }
133
134 #if NET_2_0
135                 public
136 #else
137                 internal
138 #endif
139                 void Add (HtmlTextWriterStyle key, string value)
140                 {
141                         Add (HtmlTextWriter.StaticGetStyleName (key), value);
142                 }
143
144                 public void Clear ()
145                 {
146                         style.Clear ();
147                         bag.Remove ("style");
148                 }
149
150                 public void Remove (string key)
151                 {
152                         if (style [key] == null)
153                                 return;
154                         style.Remove (key);
155                         bag ["style"] = BagToString ();
156                 }
157 #if NET_2_0
158                 public string this [HtmlTextWriterStyle key] {
159                         get {
160                                 return style [HtmlTextWriter.StaticGetStyleName (key)] as string;
161                         }
162                         set {
163                                 Add (HtmlTextWriter.StaticGetStyleName (key), value);
164                         }
165                 }
166
167                 public void Remove (HtmlTextWriterStyle key)
168                 {
169                         Remove (HtmlTextWriter.StaticGetStyleName (key));
170                 }
171
172                 public
173 #else
174                 internal
175 #endif
176                 string Value {
177                         get { return BagToString (); }
178                         set {
179                                 bag ["style"] = value;
180                                 InitFromStyle ();
181                         }
182                 }
183         }
184 }
185