CssStyleCollection.cs 3.8 KB

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