style and ^Ms in last patch from Alon
[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 //
10
11 using System;
12 using System.Collections;
13 using System.Text;
14
15 namespace System.Web.UI {
16
17         public sealed class CssStyleCollection
18         {
19                 private StateBag bag;
20                 private StateBag style;
21
22                 internal CssStyleCollection (StateBag bag)
23                 {
24                         this.bag = bag;
25                         style = new StateBag ();
26                         string st_string = bag ["style"] as string;
27                         if (st_string != null)
28                                 FillStyle (st_string);
29                 }
30
31                 internal void FillStyle (string s)
32                 {
33                         int mark = s.IndexOf (':');
34                         if (mark == -1)
35                                 return;
36                         string key = s.Substring (0, mark). Trim ();
37                         if (mark + 1 > s.Length)
38                                 return;
39
40                         string fullValue = s.Substring (mark + 1);
41                         if (fullValue == "")
42                                 return;
43
44                         mark = fullValue.IndexOf (';');
45                         string value;
46                         if (mark == -1)
47                                 value = fullValue.Trim ();
48                         else
49                                 value = fullValue.Substring (0, mark).Trim ();
50
51                         style.Add (key, value);
52                         if (mark + 1 > fullValue.Length)
53                                 return;
54                         FillStyle (fullValue.Substring (mark + 1));
55                 }
56
57                 private string BagToString ()
58                 {
59                         StringBuilder sb = new StringBuilder ();
60                         foreach (string k in style.Keys)
61                                 sb.AppendFormat ("{0}: {1}; ", k, style [k]);
62                         return sb.ToString ();
63                 }
64                 
65                 public int Count
66                 {
67                         get { return style.Count; }
68                 }
69
70                 public string this [string key]
71                 {
72                         get {
73                                 return style [key] as string;
74                         }
75
76                         set {
77                                 Add (key, value);
78                         }
79                 }
80
81                 public ICollection Keys {
82                         get { return style.Keys; }
83                 }
84
85                 public void Add (string key, string value)
86                 {
87                         style [key] = value;
88                         bag ["style"] = BagToString ();
89                 }
90
91                 public void Clear ()
92                 {
93                         bag.Remove ("style");
94                         style.Clear ();
95                 }
96
97                 public void Remove (string key)
98                 {
99                         if (style [key] != null) {
100                                 style.Remove (key);
101                                 bag ["style"] = BagToString ();
102                         }
103                 }
104         }
105 }
106