merged Sys.Web.Services 2.0 support in my branch:
[mono.git] / mcs / class / System.Web / System.Web.UI / CssStyleCollection.cs
index bf0084e561c89e7815dcffda218529ad1e40ec85..cb2d93e39fa996332878f3c58c63edbed90a84e7 100644 (file)
@@ -32,6 +32,8 @@ using System.IO;
 using System.Collections;
 using System.Security.Permissions;
 using System.Text;
+using System.Collections.Specialized;
+using System.Globalization;
 
 namespace System.Web.UI {
 
@@ -40,17 +42,25 @@ namespace System.Web.UI {
        public sealed class CssStyleCollection
        {
                StateBag bag;
-               StateBag style;
-               string last_string;
+               HybridDictionary style;
 
                internal CssStyleCollection (StateBag bag)
                {
                        this.bag = bag;
-                       if (bag != null) {
-                               last_string = (string) bag ["style"];
-                               style = new StateBag ();
-                               if (last_string != null)
-                                       FillStyle (last_string);
+                       if (bag != null)
+                               InitFromStyle ();
+               }
+
+               void InitFromStyle ()
+               {
+#if NET_2_0
+                       style = new HybridDictionary (true);
+#else
+                       style = new HybridDictionary (false);
+#endif
+                       string att = (string) bag ["style"];
+                       if (att != null) {
+                               FillStyle (att);
                        }
                }
 
@@ -82,22 +92,19 @@ namespace System.Web.UI {
 
                string BagToString ()
                {
-                       if (last_string != null)
-                               return last_string;
-
                        StringBuilder sb = new StringBuilder ();
-                       foreach (string key in Keys)
-                               sb.AppendFormat ("{0}: {1};", key, style [key]);
+                       foreach (string key in style.Keys) {
+                               if (key == "background-image" && 0 != String.Compare ("url", ((string) style [key]).Substring (0, 3), true, CultureInfo.InvariantCulture))
+                                       sb.AppendFormat ("{0}:url({1});", key, HttpUtility.UrlPathEncode ((string) style [key]));
+                               else
+                                       sb.AppendFormat ("{0}:{1};", key, style [key]);
+                       }
 
-                       last_string = sb.ToString ();
-                       return last_string;
+                       return sb.ToString ();
                }
 
-               public int Count
-               {
+               public int Count {
                        get {
-                               if (bag == null)
-                                       throw new NullReferenceException ();
                                return style.Count;
                        }
                }
@@ -113,15 +120,14 @@ namespace System.Web.UI {
                }
 
                public ICollection Keys {
-                       get { return style.Keys; }
+                       get {
+                               return style.Keys;
+                       }
                }
 
                public void Add (string key, string value)
                {
-                       if (style == null)
-                               style = new StateBag ();
                        style [key] = value;
-                       last_string = null;
                        bag ["style"] = BagToString ();
                }
 
@@ -137,17 +143,15 @@ namespace System.Web.UI {
 
                public void Clear ()
                {
-                       if (style != null)
-                               style.Clear ();
-                       last_string = null;
+                       style.Clear ();
                        bag.Remove ("style");
                }
 
                public void Remove (string key)
                {
-                       if (style != null)
-                               style.Remove (key);
-                       last_string = null;
+                       if (style [key] == null)
+                               return;
+                       style.Remove (key);
                        bag ["style"] = BagToString ();
                }
 #if NET_2_0
@@ -172,13 +176,8 @@ namespace System.Web.UI {
                string Value {
                        get { return BagToString (); }
                        set {
-                               if (style != null)
-                                       style = new StateBag ();
-                               style.Clear ();
-                               last_string = value;
                                bag ["style"] = value;
-                               if (value != null)
-                                       FillStyle (value);
+                               InitFromStyle ();
                        }
                }
        }