CssStyleCollection.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. namespace System.Web.UI {
  35. // CAS - no InheritanceDemand here as the class is sealed
  36. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. public sealed class CssStyleCollection
  38. {
  39. private StateBag bag;
  40. private StateBag style;
  41. internal CssStyleCollection ()
  42. {
  43. style = new StateBag ();
  44. }
  45. internal CssStyleCollection (StateBag bag)
  46. {
  47. this.bag = bag;
  48. style = new StateBag ();
  49. string st_string = bag ["style"] as string;
  50. if (st_string != null)
  51. FillStyle (st_string);
  52. }
  53. internal void FillStyle (string s)
  54. {
  55. int mark = s.IndexOf (':');
  56. if (mark == -1)
  57. return;
  58. string key = s.Substring (0, mark). Trim ();
  59. if (mark + 1 > s.Length)
  60. return;
  61. string fullValue = s.Substring (mark + 1);
  62. if (fullValue == "")
  63. return;
  64. mark = fullValue.IndexOf (';');
  65. string value;
  66. if (mark == -1)
  67. value = fullValue.Trim ();
  68. else
  69. value = fullValue.Substring (0, mark).Trim ();
  70. style.Add (key, value);
  71. if (mark + 1 > fullValue.Length)
  72. return;
  73. FillStyle (fullValue.Substring (mark + 1));
  74. }
  75. internal string BagToString ()
  76. {
  77. HtmlTextWriter writer = new HtmlTextWriter (new StringWriter ());
  78. foreach (string k in style.Keys)
  79. writer.WriteStyleAttribute ((k as string), (style [k] as string));
  80. return writer.InnerWriter.ToString ();
  81. }
  82. public int Count
  83. {
  84. get { return style.Count; }
  85. }
  86. public string this [string key]
  87. {
  88. get {
  89. return style [key] as string;
  90. }
  91. set {
  92. Add (key, value);
  93. }
  94. }
  95. public ICollection Keys {
  96. get { return style.Keys; }
  97. }
  98. public void Add (string key, string value)
  99. {
  100. style [key] = value;
  101. if (bag != null)
  102. bag ["style"] = BagToString ();
  103. }
  104. #if NET_2_0
  105. public
  106. #else
  107. internal
  108. #endif
  109. void Add (HtmlTextWriterStyle key, string value)
  110. {
  111. Add (HtmlTextWriter.StaticGetStyleName (key), value);
  112. }
  113. public void Clear ()
  114. {
  115. if (bag != null)
  116. bag.Remove ("style");
  117. style.Clear ();
  118. }
  119. public void Remove (string key)
  120. {
  121. if (style [key] != null) {
  122. style.Remove (key);
  123. if (bag != null)
  124. bag ["style"] = BagToString ();
  125. }
  126. }
  127. #if NET_2_0
  128. public string this [HtmlTextWriterStyle key] {
  129. get {
  130. return style [HtmlTextWriter.StaticGetStyleName (key)] as string;
  131. }
  132. set {
  133. Add (HtmlTextWriter.StaticGetStyleName (key), value);
  134. }
  135. }
  136. public string Value {
  137. get { return BagToString (); }
  138. set {
  139. Clear ();
  140. FillStyle (value);
  141. }
  142. }
  143. public void Remove (HtmlTextWriterStyle key)
  144. {
  145. Remove (HtmlTextWriter.StaticGetStyleName (key));
  146. }
  147. #endif
  148. }
  149. }