CssStyleCollection.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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. //
  10. using System;
  11. using System.Collections;
  12. using System.Text;
  13. namespace System.Web.UI {
  14. public sealed class CssStyleCollection
  15. {
  16. private StateBag bag;
  17. private StateBag style;
  18. internal CssStyleCollection (StateBag bag)
  19. {
  20. this.bag = bag;
  21. style = new StateBag ();
  22. string st_string = bag ["style"] as string;
  23. if (st_string != null)
  24. FillStyle (st_string);
  25. }
  26. internal void FillStyle (string s)
  27. {
  28. int mark = s.IndexOf (':');
  29. if (mark == -1)
  30. return;
  31. string key = s.Substring (0, mark). Trim ();
  32. if (mark + 1 > s.Length)
  33. return;
  34. string fullValue = s.Substring (mark + 1);
  35. if (fullValue == "")
  36. return;
  37. mark = fullValue.IndexOf (';');
  38. string value;
  39. if (mark == -1)
  40. value = fullValue.Trim ();
  41. else
  42. value = fullValue.Substring (0, mark).Trim ();
  43. style.Add (key, value);
  44. if (mark + 1 > fullValue.Length)
  45. return;
  46. FillStyle (fullValue.Substring (mark + 1));
  47. }
  48. private string BagToString ()
  49. {
  50. StringBuilder sb = new StringBuilder ();
  51. foreach (string k in style.Keys)
  52. sb.AppendFormat ("{0}: {1}; ", k, style [k]);
  53. return sb.ToString ();
  54. }
  55. public int Count
  56. {
  57. get { return style.Count; }
  58. }
  59. public string this [string key]
  60. {
  61. get {
  62. return style [key] as string;
  63. }
  64. set {
  65. Add (key, value);
  66. }
  67. }
  68. public ICollection Keys {
  69. get { return style.Keys; }
  70. }
  71. public void Add (string key, string value)
  72. {
  73. style [key] = value;
  74. bag ["style"] = BagToString ();
  75. }
  76. public void Clear ()
  77. {
  78. bag.Remove ("style");
  79. style.Clear ();
  80. }
  81. public void Remove (string key)
  82. {
  83. if (style [key] != null) {
  84. style.Remove (key);
  85. bag ["style"] = BagToString ();
  86. }
  87. }
  88. }
  89. }