StyleBlock.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // Authors:
  3. // Marek Habersack <[email protected]>
  4. //
  5. // (C) 2010 Novell, Inc (http://novell.com)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections.Generic;
  29. using System.Web.UI;
  30. namespace System.Web.UI.WebControls
  31. {
  32. sealed class StyleBlock : Control
  33. {
  34. List <NamedCssStyleCollection> cssStyles;
  35. Dictionary <string, NamedCssStyleCollection> cssStyleIndex;
  36. string stylePrefix;
  37. List <NamedCssStyleCollection> CssStyles {
  38. get {
  39. if (cssStyles == null) {
  40. cssStyles = new List <NamedCssStyleCollection> ();
  41. cssStyleIndex = new Dictionary <string, NamedCssStyleCollection> (StringComparer.Ordinal);
  42. }
  43. return cssStyles;
  44. }
  45. }
  46. public StyleBlock (string stylePrefix)
  47. {
  48. if (String.IsNullOrEmpty (stylePrefix))
  49. throw new ArgumentNullException ("stylePrefix");
  50. this.stylePrefix = stylePrefix;
  51. }
  52. public NamedCssStyleCollection RegisterStyle (string name = null)
  53. {
  54. if (name == null)
  55. name = String.Empty;
  56. return GetStyle (name);
  57. }
  58. public NamedCssStyleCollection RegisterStyle (Style style, string name = null)
  59. {
  60. if (style == null)
  61. throw new ArgumentNullException ("style");
  62. if (name == null)
  63. name = String.Empty;
  64. NamedCssStyleCollection cssStyle = GetStyle (name);
  65. cssStyle.CopyFrom (style.GetStyleAttributes (null));
  66. return cssStyle;
  67. }
  68. public NamedCssStyleCollection RegisterStyle (HtmlTextWriterStyle key, string value, string styleName = null)
  69. {
  70. if (styleName == null)
  71. styleName = String.Empty;
  72. NamedCssStyleCollection style = GetStyle (styleName);
  73. style.Add (key, value);
  74. return style;
  75. }
  76. NamedCssStyleCollection GetStyle (string name)
  77. {
  78. List <NamedCssStyleCollection> cssStyles = CssStyles;
  79. NamedCssStyleCollection style;
  80. if (!cssStyleIndex.TryGetValue (name, out style)) {
  81. style = new NamedCssStyleCollection (name);
  82. cssStyleIndex.Add (name, style);
  83. cssStyles.Add (style);
  84. }
  85. if (style == null)
  86. throw new InvalidOperationException (String.Format ("Internal error. Stylesheet for style {0} is null.", name));
  87. return style;
  88. }
  89. protected internal override void Render (HtmlTextWriter writer)
  90. {
  91. if (cssStyles == null || cssStyles.Count == 0)
  92. return;
  93. writer.AddAttribute (HtmlTextWriterAttribute.Type, "text/css");
  94. writer.RenderBeginTag (HtmlTextWriterTag.Style);
  95. writer.WriteLine ("/* <![CDATA[ */");
  96. string name, value;
  97. foreach (var css in cssStyles) {
  98. value = css.Collection.Value;
  99. if (String.IsNullOrEmpty (value))
  100. continue;
  101. name = css.Name;
  102. if (name != String.Empty)
  103. name = name + " ";
  104. writer.WriteLine ("#{0} {1}{{ {2} }}", stylePrefix, name, value);
  105. }
  106. writer.WriteLine ("/* ]]> */");
  107. writer.RenderEndTag (); // </style>
  108. }
  109. }
  110. }