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)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
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:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
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.
32 using System.Collections;
33 using System.Security.Permissions;
35 using System.Collections.Specialized;
36 using System.Globalization;
38 namespace System.Web.UI {
40 // CAS - no InheritanceDemand here as the class is sealed
41 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 public sealed class CssStyleCollection
45 HybridDictionary style;
46 StringBuilder _value = new StringBuilder ();
48 internal CssStyleCollection ()
51 style = new HybridDictionary (true);
53 style = new HybridDictionary (false);
57 internal CssStyleCollection (StateBag bag) : this ()
60 if (bag != null && bag [AttributeCollection.StyleAttribute] != null)
61 _value.Append (bag [AttributeCollection.StyleAttribute]);
68 if (_value.Length > 0) {
70 while (startIndex >= 0)
71 startIndex = ParseStyle (startIndex);
75 int ParseStyle (int startIndex)
78 for (int i = startIndex; i < _value.Length; i++) {
79 if (_value [i] == ':') {
84 if (colon == -1 || colon + 1 == _value.Length)
87 string key = _value.ToString (startIndex, colon - startIndex).Trim ();
90 for (int i = colon + 1; i < _value.Length; i++) {
91 if (_value [i] == ';') {
98 value = _value.ToString (colon + 1, _value.Length - colon - 1).Trim ();
100 value = _value.ToString (colon + 1, semicolon - colon - 1).Trim ();
102 style.Add (key, value);
103 if (semicolon == -1 || semicolon + 1 == _value.Length)
106 return semicolon + 1;
112 foreach (string key in style.Keys)
113 AppendStyle (_value, key, (string) style [key]);
116 static void AppendStyle (StringBuilder sb, string key, string value)
119 if (String.Compare (key, "background-image", StringComparison.OrdinalIgnoreCase) == 0 &&
120 value.Length >= 3 && String.Compare ("url", 0, value, 0, 3, StringComparison.OrdinalIgnoreCase) != 0)
122 if (key == "background-image" && 0 != String.Compare ("url", value.Substring (0, 3), true,
123 CultureInfo.InvariantCulture))
125 sb.AppendFormat ("{0}:url({1});", key, HttpUtility.UrlPathEncode (value));
127 sb.AppendFormat ("{0}:{1};", key, value);
131 get { return style.Count; }
134 public string this [string key] {
135 get { return style [key] as string; }
136 set { Add (key, value); }
139 public ICollection Keys {
140 get { return style.Keys; }
143 public void Add (string key, string value)
146 throw new ArgumentNullException ("key");
153 string curr = (string) style [key];
157 AppendStyle (_value, key, value);
158 } else if (String.CompareOrdinal (curr, value) == 0) {
167 bag [AttributeCollection.StyleAttribute] = _value.ToString ();
175 void Add (HtmlTextWriterStyle key, string value)
177 Add (HtmlTextWriter.StaticGetStyleName (key), value);
183 SetValueInternal (null);
186 public void Remove (string key)
188 if (style [key] == null)
191 if (style.Count == 0)
192 SetValueInternal (null);
197 public string this [HtmlTextWriterStyle key] {
198 get { return style [HtmlTextWriter.StaticGetStyleName (key)] as string; }
199 set { Add (HtmlTextWriter.StaticGetStyleName (key), value); }
202 public void Remove (HtmlTextWriterStyle key)
204 Remove (HtmlTextWriter.StaticGetStyleName (key));
212 get { return _value.ToString (); }
214 SetValueInternal (value);
219 void SetValueInternal (string value)
223 _value.Append (value);
226 bag.Remove (AttributeCollection.StyleAttribute);
228 bag [AttributeCollection.StyleAttribute] = value;