2 // System.Web.UI.CssStyleCollection.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Gonzalo Paniagua (gonzalo@ximian.com)
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System.Collections;
36 namespace System.Web.UI {
38 public sealed class CssStyleCollection
41 private StateBag style;
43 internal CssStyleCollection ()
45 style = new StateBag ();
48 internal CssStyleCollection (StateBag bag)
51 style = new StateBag ();
52 string st_string = bag ["style"] as string;
53 if (st_string != null)
54 FillStyle (st_string);
57 internal void FillStyle (string s)
59 int mark = s.IndexOf (':');
62 string key = s.Substring (0, mark). Trim ();
63 if (mark + 1 > s.Length)
66 string fullValue = s.Substring (mark + 1);
70 mark = fullValue.IndexOf (';');
73 value = fullValue.Trim ();
75 value = fullValue.Substring (0, mark).Trim ();
77 style.Add (key, value);
78 if (mark + 1 > fullValue.Length)
80 FillStyle (fullValue.Substring (mark + 1));
83 internal string BagToString ()
85 StringBuilder sb = new StringBuilder ();
86 foreach (string k in style.Keys)
87 sb.AppendFormat ("{0}: {1}; ", k, style [k]);
88 return sb.ToString ();
93 get { return style.Count; }
96 public string this [string key]
99 return style [key] as string;
107 public ICollection Keys {
108 get { return style.Keys; }
111 public void Add (string key, string value)
115 bag ["style"] = BagToString ();
123 void Add (HtmlTextWriterStyle key, string value)
125 Add (HtmlTextWriter.StaticGetStyleName (key), value);
131 bag.Remove ("style");
135 public void Remove (string key)
137 if (style [key] != null) {
140 bag ["style"] = BagToString ();