using System.IO;
using System.Collections;
+using System.Security.Permissions;
using System.Text;
+using System.Collections.Specialized;
+using System.Globalization;
namespace System.Web.UI {
+ // CAS - no InheritanceDemand here as the class is sealed
+ [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
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);
}
}
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;
}
}
}
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 ();
}
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
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 ();
}
}
}