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-2010 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;
37 using System.Web.Util;
39 namespace System.Web.UI {
41 // CAS - no InheritanceDemand here as the class is sealed
42 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43 public sealed class CssStyleCollection
47 StringBuilder _value = new StringBuilder ();
49 internal CssStyleCollection ()
51 style = new ListDictionary (StringComparer.OrdinalIgnoreCase);
54 internal CssStyleCollection (StateBag bag) : this ()
57 if (bag != null && bag [AttributeCollection.StyleAttribute] != null)
58 _value.Append (bag [AttributeCollection.StyleAttribute]);
65 if (_value.Length > 0) {
67 while (startIndex >= 0)
68 startIndex = ParseStyle (startIndex);
72 int ParseStyle (int startIndex)
75 for (int i = startIndex; i < _value.Length; i++) {
76 if (_value [i] == ':') {
81 if (colon == -1 || colon + 1 == _value.Length)
84 string key = _value.ToString (startIndex, colon - startIndex).Trim ();
87 for (int i = colon + 1; i < _value.Length; i++) {
88 if (_value [i] == ';') {
95 value = _value.ToString (colon + 1, _value.Length - colon - 1).Trim ();
97 value = _value.ToString (colon + 1, semicolon - colon - 1).Trim ();
99 style.Add (key, value);
100 if (semicolon == -1 || semicolon + 1 == _value.Length)
103 return semicolon + 1;
109 foreach (string key in style.Keys)
110 AppendStyle (_value, key, (string) style [key]);
113 static void AppendStyle (StringBuilder sb, string key, string value)
115 if (String.Compare (key, "background-image", StringComparison.OrdinalIgnoreCase) == 0 &&
116 value.Length >= 3 && String.Compare ("url", 0, value, 0, 3, StringComparison.OrdinalIgnoreCase) != 0)
117 sb.AppendFormat ("{0}:url({1});", key, HttpUtility.UrlPathEncode (value));
119 sb.AppendFormat ("{0}:{1};", key, value);
123 get { return style.Count; }
126 public string this [string key] {
127 get { return style [key] as string; }
128 set { Add (key, value); }
131 public ICollection Keys {
132 get { return style.Keys; }
135 public void Add (string key, string value)
138 throw new ArgumentNullException ("key");
145 string curr = (string) style [key];
149 AppendStyle (_value, key, value);
150 } else if (String.CompareOrdinal (curr, value) == 0) {
159 bag [AttributeCollection.StyleAttribute] = _value.ToString ();
162 public void Add (HtmlTextWriterStyle key, string value)
164 Add (HtmlTextWriter.StaticGetStyleName (key), value);
170 SetValueInternal (null);
173 public void Remove (string key)
175 if (style [key] == null)
178 if (style.Count == 0)
179 SetValueInternal (null);
184 public string this [HtmlTextWriterStyle key] {
185 get { return style [HtmlTextWriter.StaticGetStyleName (key)] as string; }
186 set { Add (HtmlTextWriter.StaticGetStyleName (key), value); }
189 public void Remove (HtmlTextWriterStyle key)
191 Remove (HtmlTextWriter.StaticGetStyleName (key));
194 public string Value {
195 get { return _value.ToString (); }
197 SetValueInternal (value);
202 void SetValueInternal (string value)
206 _value.Append (value);
209 bag.Remove (AttributeCollection.StyleAttribute);
211 bag [AttributeCollection.StyleAttribute] = value;