Wed Feb 24 15:47:16 CET 2010 Paolo Molaro <lupus@ximian.com>
[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-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.IO;
32 using System.Collections;
33 using System.Security.Permissions;
34 using System.Text;
35 using System.Collections.Specialized;
36 using System.Globalization;
37 using System.Web.Util;
38
39 namespace System.Web.UI {
40
41         // CAS - no InheritanceDemand here as the class is sealed
42         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         public sealed class CssStyleCollection
44         {
45                 StateBag bag;
46                 ListDictionary style;
47                 StringBuilder _value = new StringBuilder ();
48                 
49                 internal CssStyleCollection ()
50                 {
51                         style = new ListDictionary (StringComparer.OrdinalIgnoreCase);
52                 }
53
54                 internal CssStyleCollection (StateBag bag) : this ()
55                 {
56                         this.bag = bag;
57                         if (bag != null && bag [AttributeCollection.StyleAttribute] != null)
58                                 _value.Append (bag [AttributeCollection.StyleAttribute]);
59                         InitFromStyle ();
60                 }
61
62                 void InitFromStyle ()
63                 {
64                         style.Clear ();
65                         if (_value.Length > 0) {
66                                 int startIndex = 0;
67                                 while (startIndex >= 0)
68                                         startIndex = ParseStyle (startIndex);
69                         }
70                 }
71
72                 int ParseStyle (int startIndex)
73                 {
74                         int colon = -1;
75                         for (int i = startIndex; i < _value.Length; i++) {
76                                 if (_value [i] == ':') {
77                                         colon = i;
78                                         break;
79                                 }
80                         }
81                         if (colon == -1 || colon + 1 == _value.Length)
82                                 return -1;
83
84                         string key = _value.ToString (startIndex, colon - startIndex).Trim ();
85
86                         int semicolon = -1;
87                         for (int i = colon + 1; i < _value.Length; i++) {
88                                 if (_value [i] == ';') {
89                                         semicolon = i;
90                                         break;
91                                 }
92                         }
93                         string value;
94                         if (semicolon == -1)
95                                 value = _value.ToString (colon + 1, _value.Length - colon - 1).Trim ();
96                         else
97                                 value = _value.ToString (colon + 1, semicolon - colon - 1).Trim ();
98
99                         style.Add (key, value);
100                         if (semicolon == -1 || semicolon + 1 == _value.Length)
101                                 return -1;
102
103                         return semicolon + 1;
104                 }
105
106                 void BagToValue ()
107                 {
108                         _value.Length = 0;
109                         foreach (string key in style.Keys)
110                                 AppendStyle (_value, key, (string) style [key]);
111                 }
112
113                 static void AppendStyle (StringBuilder sb, string key, string value)
114                 {
115                         if (String.Compare (key, "background-image", StringComparison.OrdinalIgnoreCase) == 0 &&
116                             value.Length >= 3 && String.Compare ("url", 0, value, 0, 3, StringComparison.OrdinalIgnoreCase) != 0)
117                                 sb.AppendFormat ("{0}:url({1});", key, HttpUtility.UrlPathEncode (value));
118                         else
119                                 sb.AppendFormat ("{0}:{1};", key, value);
120                 }
121
122                 public int Count {
123                         get { return style.Count; }
124                 }
125
126                 public string this [string key] {
127                         get { return style [key] as string; }
128                         set { Add (key, value); }
129                 }
130
131                 public ICollection Keys {
132                         get { return style.Keys; }
133                 }
134
135                 public void Add (string key, string value)
136                 {
137                         if (key == null)
138                                 throw new ArgumentNullException ("key");
139
140                         if (value == null) {
141                                 Remove (key);
142                                 return;
143                         }
144
145                         string curr = (string) style [key];
146                         if (curr == null) {
147                                 // just append
148                                 style [key] = value;
149                                 AppendStyle (_value, key, value);
150                         } else if (String.CompareOrdinal (curr, value) == 0) {
151                                 // do nothing
152                                 return;
153                         } else {
154                                 style [key] = value;
155                                 BagToValue ();
156                         }
157
158                         if (bag != null)
159                                 bag [AttributeCollection.StyleAttribute] = _value.ToString ();
160                 }
161
162                 public void Add (HtmlTextWriterStyle key, string value)
163                 {
164                         Add (HtmlTextWriter.StaticGetStyleName (key), value);
165                 }
166
167                 public void Clear ()
168                 {
169                         style.Clear ();
170                         SetValueInternal (null);
171                 }
172
173                 public void Remove (string key)
174                 {
175                         if (style [key] == null)
176                                 return;
177                         style.Remove (key);
178                         if (style.Count == 0)
179                                 SetValueInternal (null);
180                         else
181                                 BagToValue ();
182                 }
183
184                 public string this [HtmlTextWriterStyle key] {
185                         get { return style [HtmlTextWriter.StaticGetStyleName (key)] as string; }
186                         set { Add (HtmlTextWriter.StaticGetStyleName (key), value); }
187                 }
188
189                 public void Remove (HtmlTextWriterStyle key)
190                 {
191                         Remove (HtmlTextWriter.StaticGetStyleName (key));
192                 }
193
194                 public string Value {
195                         get { return _value.ToString (); }
196                         set {
197                                 SetValueInternal (value);
198                                 InitFromStyle ();
199                         }
200                 }
201
202                 void SetValueInternal (string value)
203                 {
204                         _value.Length = 0;
205                         if (value != null)
206                                 _value.Append (value);
207                         if (bag != null) {
208                                 if (value == null)
209                                         bag.Remove (AttributeCollection.StyleAttribute);
210                                 else
211                                         bag [AttributeCollection.StyleAttribute] = value;
212                         }
213                 }
214         }
215 }
216