CssStyleCollection.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. //
  2. // System.Web.UI.CssStyleCollection.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. (http://www.ximian.com)
  9. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  10. //
  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:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  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.
  29. //
  30. using System.IO;
  31. using System.Collections;
  32. using System.Security.Permissions;
  33. using System.Text;
  34. using System.Collections.Specialized;
  35. using System.Globalization;
  36. namespace System.Web.UI {
  37. // CAS - no InheritanceDemand here as the class is sealed
  38. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public sealed class CssStyleCollection
  40. {
  41. StateBag bag;
  42. HybridDictionary style;
  43. internal CssStyleCollection (StateBag bag)
  44. {
  45. this.bag = bag;
  46. if (bag != null)
  47. InitFromStyle ();
  48. }
  49. void InitFromStyle ()
  50. {
  51. #if NET_2_0
  52. style = new HybridDictionary (true);
  53. #else
  54. style = new HybridDictionary (false);
  55. #endif
  56. string att = (string) bag ["style"];
  57. if (att != null) {
  58. FillStyle (att);
  59. }
  60. }
  61. void FillStyle (string s)
  62. {
  63. int mark = s.IndexOf (':');
  64. if (mark == -1)
  65. return;
  66. string key = s.Substring (0, mark). Trim ();
  67. if (mark + 1 > s.Length)
  68. return;
  69. string fullValue = s.Substring (mark + 1);
  70. if (fullValue == "")
  71. return;
  72. mark = fullValue.IndexOf (';');
  73. string value;
  74. if (mark == -1)
  75. value = fullValue.Trim ();
  76. else
  77. value = fullValue.Substring (0, mark).Trim ();
  78. style.Add (key, value);
  79. if (mark + 1 > fullValue.Length)
  80. return;
  81. FillStyle (fullValue.Substring (mark + 1));
  82. }
  83. string BagToString ()
  84. {
  85. StringBuilder sb = new StringBuilder ();
  86. foreach (string key in style.Keys) {
  87. if (key == "background-image" && 0 != String.Compare ("url", ((string) style [key]).Substring (0, 3), true, CultureInfo.InvariantCulture))
  88. sb.AppendFormat ("{0}:url({1});", key, HttpUtility.UrlPathEncode ((string) style [key]));
  89. else
  90. sb.AppendFormat ("{0}:{1};", key, style [key]);
  91. }
  92. return sb.ToString ();
  93. }
  94. public int Count {
  95. get {
  96. return style.Count;
  97. }
  98. }
  99. public string this [string key] {
  100. get {
  101. return style [key] as string;
  102. }
  103. set {
  104. Add (key, value);
  105. }
  106. }
  107. public ICollection Keys {
  108. get {
  109. return style.Keys;
  110. }
  111. }
  112. public void Add (string key, string value)
  113. {
  114. style [key] = value;
  115. bag ["style"] = BagToString ();
  116. }
  117. #if NET_2_0
  118. public
  119. #else
  120. internal
  121. #endif
  122. void Add (HtmlTextWriterStyle key, string value)
  123. {
  124. Add (HtmlTextWriter.StaticGetStyleName (key), value);
  125. }
  126. public void Clear ()
  127. {
  128. style.Clear ();
  129. bag.Remove ("style");
  130. }
  131. public void Remove (string key)
  132. {
  133. if (style [key] == null)
  134. return;
  135. style.Remove (key);
  136. bag ["style"] = BagToString ();
  137. }
  138. #if NET_2_0
  139. public string this [HtmlTextWriterStyle key] {
  140. get {
  141. return style [HtmlTextWriter.StaticGetStyleName (key)] as string;
  142. }
  143. set {
  144. Add (HtmlTextWriter.StaticGetStyleName (key), value);
  145. }
  146. }
  147. public void Remove (HtmlTextWriterStyle key)
  148. {
  149. Remove (HtmlTextWriter.StaticGetStyleName (key));
  150. }
  151. public
  152. #else
  153. internal
  154. #endif
  155. string Value {
  156. get { return BagToString (); }
  157. set {
  158. bag ["style"] = value;
  159. InitFromStyle ();
  160. }
  161. }
  162. }
  163. }